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 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 162 | /* Same for linebreaks */ |
| 163 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 164 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 165 | /* 0x000A, * LINE FEED */ |
| 166 | /* 0x000D, * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 167 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 169 | /* 0x001C, * FILE SEPARATOR */ |
| 170 | /* 0x001D, * GROUP SEPARATOR */ |
| 171 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 172 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 177 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 184 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 185 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 189 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 190 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 191 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 192 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 193 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 194 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 195 | /* This is actually an illegal character, so it should |
| 196 | not be passed to unichr. */ |
| 197 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 198 | #endif |
| 199 | } |
| 200 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 201 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 202 | |
| 203 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 204 | to keep things simple, we use a single bitmask, using the least 5 |
| 205 | bits from each unicode characters as the bit index. */ |
| 206 | |
| 207 | /* the linebreak mask is set up by Unicode_Init below */ |
| 208 | |
| 209 | #define BLOOM_MASK unsigned long |
| 210 | |
| 211 | static BLOOM_MASK bloom_linebreak; |
| 212 | |
| 213 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 214 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 215 | #define BLOOM_LINEBREAK(ch) \ |
| 216 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 217 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 218 | |
| 219 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 220 | { |
| 221 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 222 | |
| 223 | long mask; |
| 224 | Py_ssize_t i; |
| 225 | |
| 226 | mask = 0; |
| 227 | for (i = 0; i < len; i++) |
| 228 | mask |= (1 << (ptr[i] & 0x1F)); |
| 229 | |
| 230 | return mask; |
| 231 | } |
| 232 | |
| 233 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 234 | { |
| 235 | Py_ssize_t i; |
| 236 | |
| 237 | for (i = 0; i < setlen; i++) |
| 238 | if (set[i] == chr) |
| 239 | return 1; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 244 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 245 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | /* --- Unicode Object ----------------------------------------------------- */ |
| 248 | |
| 249 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 250 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 251 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 252 | { |
| 253 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 255 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 256 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 257 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 258 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 259 | /* Resizing shared object (unicode_empty or single character |
| 260 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 261 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 262 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 263 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 264 | (unicode->length == 1 && |
| 265 | unicode->str[0] < 256U && |
| 266 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 267 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 268 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 269 | return -1; |
| 270 | } |
| 271 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 272 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 273 | The overallocation is also used by fastsearch, which assumes that it's |
| 274 | safe to look at str[length] (without making any assumptions about what |
| 275 | it contains). */ |
| 276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 278 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 279 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 280 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 281 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 282 | PyErr_NoMemory(); |
| 283 | return -1; |
| 284 | } |
| 285 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 286 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 287 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 288 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 289 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 290 | if (unicode->defenc) { |
| 291 | Py_DECREF(unicode->defenc); |
| 292 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 293 | } |
| 294 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /* 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] | 300 | Ux0000 terminated; some code (e.g. new_identifier) |
| 301 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 302 | |
| 303 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 304 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | |
| 306 | */ |
| 307 | |
| 308 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 309 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 310 | { |
| 311 | register PyUnicodeObject *unicode; |
| 312 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 313 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 314 | if (length == 0 && unicode_empty != NULL) { |
| 315 | Py_INCREF(unicode_empty); |
| 316 | return unicode_empty; |
| 317 | } |
| 318 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 319 | /* Ensure we won't overflow the size. */ |
| 320 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 321 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 322 | } |
| 323 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 324 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 325 | if (free_list) { |
| 326 | unicode = free_list; |
| 327 | free_list = *(PyUnicodeObject **)unicode; |
| 328 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 329 | if (unicode->str) { |
| 330 | /* Keep-Alive optimization: we only upsize the buffer, |
| 331 | never downsize it. */ |
| 332 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 333 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 334 | PyObject_DEL(unicode->str); |
| 335 | unicode->str = NULL; |
| 336 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 337 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 338 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 339 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 340 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 341 | } |
| 342 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 343 | } |
| 344 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 345 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 346 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 347 | if (unicode == NULL) |
| 348 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 349 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 350 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 353 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 354 | PyErr_NoMemory(); |
| 355 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 356 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 357 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 358 | * the caller fails before initializing str -- unicode_resize() |
| 359 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 360 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 361 | * We don't want unicode_resize to read uninitialized memory in |
| 362 | * that case. |
| 363 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 364 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 365 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 366 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 367 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 368 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 369 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 370 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 371 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 372 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 373 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 374 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 375 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 376 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 377 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 381 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 382 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 383 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 384 | case SSTATE_NOT_INTERNED: |
| 385 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 386 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 387 | case SSTATE_INTERNED_MORTAL: |
| 388 | /* revive dead object temporarily for DelItem */ |
| 389 | Py_REFCNT(unicode) = 3; |
| 390 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 391 | Py_FatalError( |
| 392 | "deletion of interned string failed"); |
| 393 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 394 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 395 | case SSTATE_INTERNED_IMMORTAL: |
| 396 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 397 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 398 | default: |
| 399 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 402 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 403 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 404 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 405 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 406 | PyObject_DEL(unicode->str); |
| 407 | unicode->str = NULL; |
| 408 | unicode->length = 0; |
| 409 | } |
| 410 | if (unicode->defenc) { |
| 411 | Py_DECREF(unicode->defenc); |
| 412 | unicode->defenc = NULL; |
| 413 | } |
| 414 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 415 | *(PyUnicodeObject **)unicode = free_list; |
| 416 | free_list = unicode; |
| 417 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 418 | } |
| 419 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 420 | PyObject_DEL(unicode->str); |
| 421 | Py_XDECREF(unicode->defenc); |
| 422 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 426 | static |
| 427 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 428 | { |
| 429 | register PyUnicodeObject *v; |
| 430 | |
| 431 | /* Argument checks */ |
| 432 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 433 | PyErr_BadInternalCall(); |
| 434 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 435 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 436 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 437 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 438 | PyErr_BadInternalCall(); |
| 439 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* Resizing unicode_empty and single character objects is not |
| 443 | possible since these are being shared. We simply return a fresh |
| 444 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 445 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 446 | (v == unicode_empty || v->length == 1)) { |
| 447 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 448 | if (w == NULL) |
| 449 | return -1; |
| 450 | Py_UNICODE_COPY(w->str, v->str, |
| 451 | length < v->length ? length : v->length); |
| 452 | Py_DECREF(*unicode); |
| 453 | *unicode = w; |
| 454 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 458 | objects, since we can modify them in-place. */ |
| 459 | return unicode_resize(v, length); |
| 460 | } |
| 461 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 462 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 463 | { |
| 464 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 465 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 467 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 468 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 469 | { |
| 470 | PyUnicodeObject *unicode; |
| 471 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 472 | /* If the Unicode data is known at construction time, we can apply |
| 473 | some optimizations which share commonly used objects. */ |
| 474 | if (u != NULL) { |
| 475 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 476 | /* Optimization for empty strings */ |
| 477 | if (size == 0 && unicode_empty != NULL) { |
| 478 | Py_INCREF(unicode_empty); |
| 479 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 480 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 481 | |
| 482 | /* Single character Unicode objects in the Latin-1 range are |
| 483 | shared when using this constructor */ |
| 484 | if (size == 1 && *u < 256) { |
| 485 | unicode = unicode_latin1[*u]; |
| 486 | if (!unicode) { |
| 487 | unicode = _PyUnicode_New(1); |
| 488 | if (!unicode) |
| 489 | return NULL; |
| 490 | unicode->str[0] = *u; |
| 491 | unicode_latin1[*u] = unicode; |
| 492 | } |
| 493 | Py_INCREF(unicode); |
| 494 | return (PyObject *)unicode; |
| 495 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 496 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 497 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 498 | unicode = _PyUnicode_New(size); |
| 499 | if (!unicode) |
| 500 | return NULL; |
| 501 | |
| 502 | /* Copy the Unicode data into the new object */ |
| 503 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 504 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 505 | |
| 506 | return (PyObject *)unicode; |
| 507 | } |
| 508 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 509 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 510 | { |
| 511 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 512 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 513 | if (size < 0) { |
| 514 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 515 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 516 | return NULL; |
| 517 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 518 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 519 | /* 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] | 520 | some optimizations which share commonly used objects. |
| 521 | Also, this means the input must be UTF-8, so fall back to the |
| 522 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 523 | if (u != NULL) { |
| 524 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 525 | /* Optimization for empty strings */ |
| 526 | if (size == 0 && unicode_empty != NULL) { |
| 527 | Py_INCREF(unicode_empty); |
| 528 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 529 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 530 | |
| 531 | /* Single characters are shared when using this constructor. |
| 532 | Restrict to ASCII, since the input must be UTF-8. */ |
| 533 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 534 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 535 | if (!unicode) { |
| 536 | unicode = _PyUnicode_New(1); |
| 537 | if (!unicode) |
| 538 | return NULL; |
| 539 | unicode->str[0] = Py_CHARMASK(*u); |
| 540 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 541 | } |
| 542 | Py_INCREF(unicode); |
| 543 | return (PyObject *)unicode; |
| 544 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 545 | |
| 546 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 549 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 550 | if (!unicode) |
| 551 | return NULL; |
| 552 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 553 | return (PyObject *)unicode; |
| 554 | } |
| 555 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 556 | PyObject *PyUnicode_FromString(const char *u) |
| 557 | { |
| 558 | size_t size = strlen(u); |
| 559 | if (size > PY_SSIZE_T_MAX) { |
| 560 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | return PyUnicode_FromStringAndSize(u, size); |
| 565 | } |
| 566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 567 | #ifdef HAVE_WCHAR_H |
| 568 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 569 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 570 | # define CONVERT_WCHAR_TO_SURROGATES |
| 571 | #endif |
| 572 | |
| 573 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 574 | |
| 575 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 576 | to convert from UTF32 to UTF16. */ |
| 577 | |
| 578 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 579 | Py_ssize_t size) |
| 580 | { |
| 581 | PyUnicodeObject *unicode; |
| 582 | register Py_ssize_t i; |
| 583 | Py_ssize_t alloc; |
| 584 | const wchar_t *orig_w; |
| 585 | |
| 586 | if (w == NULL) { |
| 587 | if (size == 0) |
| 588 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 589 | PyErr_BadInternalCall(); |
| 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | if (size == -1) { |
| 594 | size = wcslen(w); |
| 595 | } |
| 596 | |
| 597 | alloc = size; |
| 598 | orig_w = w; |
| 599 | for (i = size; i > 0; i--) { |
| 600 | if (*w > 0xFFFF) |
| 601 | alloc++; |
| 602 | w++; |
| 603 | } |
| 604 | w = orig_w; |
| 605 | unicode = _PyUnicode_New(alloc); |
| 606 | if (!unicode) |
| 607 | return NULL; |
| 608 | |
| 609 | /* Copy the wchar_t data into the new object */ |
| 610 | { |
| 611 | register Py_UNICODE *u; |
| 612 | u = PyUnicode_AS_UNICODE(unicode); |
| 613 | for (i = size; i > 0; i--) { |
| 614 | if (*w > 0xFFFF) { |
| 615 | wchar_t ordinal = *w++; |
| 616 | ordinal -= 0x10000; |
| 617 | *u++ = 0xD800 | (ordinal >> 10); |
| 618 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 619 | } |
| 620 | else |
| 621 | *u++ = *w++; |
| 622 | } |
| 623 | } |
| 624 | return (PyObject *)unicode; |
| 625 | } |
| 626 | |
| 627 | #else |
| 628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 629 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 630 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 631 | { |
| 632 | PyUnicodeObject *unicode; |
| 633 | |
| 634 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 635 | if (size == 0) |
| 636 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 637 | PyErr_BadInternalCall(); |
| 638 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 641 | if (size == -1) { |
| 642 | size = wcslen(w); |
| 643 | } |
| 644 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 645 | unicode = _PyUnicode_New(size); |
| 646 | if (!unicode) |
| 647 | return NULL; |
| 648 | |
| 649 | /* Copy the wchar_t data into the new object */ |
| 650 | #ifdef HAVE_USABLE_WCHAR_T |
| 651 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 652 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 653 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 654 | register Py_UNICODE *u; |
| 655 | register Py_ssize_t i; |
| 656 | u = PyUnicode_AS_UNICODE(unicode); |
| 657 | for (i = size; i > 0; i--) |
| 658 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 659 | } |
| 660 | #endif |
| 661 | |
| 662 | return (PyObject *)unicode; |
| 663 | } |
| 664 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 665 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 666 | |
| 667 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 668 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 669 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 670 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 671 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 672 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 673 | *fmt++ = '%'; |
| 674 | if (width) { |
| 675 | if (zeropad) |
| 676 | *fmt++ = '0'; |
| 677 | fmt += sprintf(fmt, "%d", width); |
| 678 | } |
| 679 | if (precision) |
| 680 | fmt += sprintf(fmt, ".%d", precision); |
| 681 | if (longflag) |
| 682 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 683 | else if (longlongflag) { |
| 684 | /* longlongflag should only ever be nonzero on machines with |
| 685 | HAVE_LONG_LONG defined */ |
| 686 | #ifdef HAVE_LONG_LONG |
| 687 | char *f = PY_FORMAT_LONG_LONG; |
| 688 | while (*f) |
| 689 | *fmt++ = *f++; |
| 690 | #else |
| 691 | /* we shouldn't ever get here */ |
| 692 | assert(0); |
| 693 | *fmt++ = 'l'; |
| 694 | #endif |
| 695 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 696 | else if (size_tflag) { |
| 697 | char *f = PY_FORMAT_SIZE_T; |
| 698 | while (*f) |
| 699 | *fmt++ = *f++; |
| 700 | } |
| 701 | *fmt++ = c; |
| 702 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 705 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 706 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 707 | /* size of fixed-size buffer for formatting single arguments */ |
| 708 | #define ITEM_BUFFER_LEN 21 |
| 709 | /* maximum number of characters required for output of %ld. 21 characters |
| 710 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 711 | #define MAX_LONG_CHARS 21 |
| 712 | /* maximum number of characters required for output of %lld. |
| 713 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 714 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 715 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 716 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 717 | PyObject * |
| 718 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 719 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 720 | va_list count; |
| 721 | Py_ssize_t callcount = 0; |
| 722 | PyObject **callresults = NULL; |
| 723 | PyObject **callresult = NULL; |
| 724 | Py_ssize_t n = 0; |
| 725 | int width = 0; |
| 726 | int precision = 0; |
| 727 | int zeropad; |
| 728 | const char* f; |
| 729 | Py_UNICODE *s; |
| 730 | PyObject *string; |
| 731 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 732 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 733 | /* use abuffer instead of buffer, if we need more space |
| 734 | * (which can happen if there's a format specifier with width). */ |
| 735 | char *abuffer = NULL; |
| 736 | char *realbuffer; |
| 737 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 738 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 739 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 740 | |
| 741 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 742 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 743 | #else |
| 744 | #ifdef __va_copy |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 745 | __va_copy(count, vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 746 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 747 | count = vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 748 | #endif |
| 749 | #endif |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 750 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 751 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 752 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 753 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 754 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 755 | if (*f == '%') { |
| 756 | if (*(f+1)=='%') |
| 757 | continue; |
| 758 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 759 | ++callcount; |
| 760 | while (ISDIGIT((unsigned)*f)) |
| 761 | width = (width*10) + *f++ - '0'; |
| 762 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 763 | ; |
| 764 | if (*f == 's') |
| 765 | ++callcount; |
| 766 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 767 | } |
| 768 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 769 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 770 | if (callcount) { |
| 771 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 772 | if (!callresults) { |
| 773 | PyErr_NoMemory(); |
| 774 | return NULL; |
| 775 | } |
| 776 | callresult = callresults; |
| 777 | } |
| 778 | /* step 3: figure out how large a buffer we need */ |
| 779 | for (f = format; *f; f++) { |
| 780 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 781 | #ifdef HAVE_LONG_LONG |
| 782 | int longlongflag = 0; |
| 783 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 784 | const char* p = f; |
| 785 | width = 0; |
| 786 | while (ISDIGIT((unsigned)*f)) |
| 787 | width = (width*10) + *f++ - '0'; |
| 788 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 789 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 790 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 791 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 792 | * they don't affect the amount of space we reserve. |
| 793 | */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 794 | if (*f == 'l') { |
| 795 | if (f[1] == 'd' || f[1] == 'u') { |
| 796 | ++f; |
| 797 | } |
| 798 | #ifdef HAVE_LONG_LONG |
| 799 | else if (f[1] == 'l' && |
| 800 | (f[2] == 'd' || f[2] == 'u')) { |
| 801 | longlongflag = 1; |
| 802 | f += 2; |
| 803 | } |
| 804 | #endif |
| 805 | } |
| 806 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 807 | ++f; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 808 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 809 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 810 | switch (*f) { |
| 811 | case 'c': |
| 812 | (void)va_arg(count, int); |
| 813 | /* fall through... */ |
| 814 | case '%': |
| 815 | n++; |
| 816 | break; |
| 817 | case 'd': case 'u': case 'i': case 'x': |
| 818 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 819 | #ifdef HAVE_LONG_LONG |
| 820 | if (longlongflag) { |
| 821 | if (width < MAX_LONG_LONG_CHARS) |
| 822 | width = MAX_LONG_LONG_CHARS; |
| 823 | } |
| 824 | else |
| 825 | #endif |
| 826 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 827 | including sign. Decimal takes the most space. This |
| 828 | isn't enough for octal. If a width is specified we |
| 829 | need more (which we allocate later). */ |
| 830 | if (width < MAX_LONG_CHARS) |
| 831 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 832 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 833 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 834 | if (abuffersize < width) |
| 835 | abuffersize = width; |
| 836 | break; |
| 837 | case 's': |
| 838 | { |
| 839 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 840 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 841 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 842 | if (!str) |
| 843 | goto fail; |
| 844 | n += PyUnicode_GET_SIZE(str); |
| 845 | /* Remember the str and switch to the next slot */ |
| 846 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 847 | break; |
| 848 | } |
| 849 | case 'U': |
| 850 | { |
| 851 | PyObject *obj = va_arg(count, PyObject *); |
| 852 | assert(obj && PyUnicode_Check(obj)); |
| 853 | n += PyUnicode_GET_SIZE(obj); |
| 854 | break; |
| 855 | } |
| 856 | case 'V': |
| 857 | { |
| 858 | PyObject *obj = va_arg(count, PyObject *); |
| 859 | const char *str = va_arg(count, const char *); |
| 860 | assert(obj || str); |
| 861 | assert(!obj || PyUnicode_Check(obj)); |
| 862 | if (obj) |
| 863 | n += PyUnicode_GET_SIZE(obj); |
| 864 | else |
| 865 | n += strlen(str); |
| 866 | break; |
| 867 | } |
| 868 | case 'S': |
| 869 | { |
| 870 | PyObject *obj = va_arg(count, PyObject *); |
| 871 | PyObject *str; |
| 872 | assert(obj); |
| 873 | str = PyObject_Str(obj); |
| 874 | if (!str) |
| 875 | goto fail; |
| 876 | n += PyUnicode_GET_SIZE(str); |
| 877 | /* Remember the str and switch to the next slot */ |
| 878 | *callresult++ = str; |
| 879 | break; |
| 880 | } |
| 881 | case 'R': |
| 882 | { |
| 883 | PyObject *obj = va_arg(count, PyObject *); |
| 884 | PyObject *repr; |
| 885 | assert(obj); |
| 886 | repr = PyObject_Repr(obj); |
| 887 | if (!repr) |
| 888 | goto fail; |
| 889 | n += PyUnicode_GET_SIZE(repr); |
| 890 | /* Remember the repr and switch to the next slot */ |
| 891 | *callresult++ = repr; |
| 892 | break; |
| 893 | } |
| 894 | case 'A': |
| 895 | { |
| 896 | PyObject *obj = va_arg(count, PyObject *); |
| 897 | PyObject *ascii; |
| 898 | assert(obj); |
| 899 | ascii = PyObject_ASCII(obj); |
| 900 | if (!ascii) |
| 901 | goto fail; |
| 902 | n += PyUnicode_GET_SIZE(ascii); |
| 903 | /* Remember the repr and switch to the next slot */ |
| 904 | *callresult++ = ascii; |
| 905 | break; |
| 906 | } |
| 907 | case 'p': |
| 908 | (void) va_arg(count, int); |
| 909 | /* maximum 64-bit pointer representation: |
| 910 | * 0xffffffffffffffff |
| 911 | * so 19 characters is enough. |
| 912 | * XXX I count 18 -- what's the extra for? |
| 913 | */ |
| 914 | n += 19; |
| 915 | break; |
| 916 | default: |
| 917 | /* if we stumble upon an unknown |
| 918 | formatting code, copy the rest of |
| 919 | the format string to the output |
| 920 | string. (we cannot just skip the |
| 921 | code, since there's no way to know |
| 922 | what's in the argument list) */ |
| 923 | n += strlen(p); |
| 924 | goto expand; |
| 925 | } |
| 926 | } else |
| 927 | n++; |
| 928 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 929 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 930 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 931 | /* add 1 for sprintf's trailing null byte */ |
| 932 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 933 | if (!abuffer) { |
| 934 | PyErr_NoMemory(); |
| 935 | goto fail; |
| 936 | } |
| 937 | realbuffer = abuffer; |
| 938 | } |
| 939 | else |
| 940 | realbuffer = buffer; |
| 941 | /* step 4: fill the buffer */ |
| 942 | /* Since we've analyzed how much space we need for the worst case, |
| 943 | we don't have to resize the string. |
| 944 | There can be no errors beyond this point. */ |
| 945 | string = PyUnicode_FromUnicode(NULL, n); |
| 946 | if (!string) |
| 947 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 948 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 949 | s = PyUnicode_AS_UNICODE(string); |
| 950 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 951 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 952 | for (f = format; *f; f++) { |
| 953 | if (*f == '%') { |
| 954 | const char* p = f++; |
| 955 | int longflag = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 956 | int longlongflag = 0; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 957 | int size_tflag = 0; |
| 958 | zeropad = (*f == '0'); |
| 959 | /* parse the width.precision part */ |
| 960 | width = 0; |
| 961 | while (ISDIGIT((unsigned)*f)) |
| 962 | width = (width*10) + *f++ - '0'; |
| 963 | precision = 0; |
| 964 | if (*f == '.') { |
| 965 | f++; |
| 966 | while (ISDIGIT((unsigned)*f)) |
| 967 | precision = (precision*10) + *f++ - '0'; |
| 968 | } |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 969 | /* Handle %ld, %lu, %lld and %llu. */ |
| 970 | if (*f == 'l') { |
| 971 | if (f[1] == 'd' || f[1] == 'u') { |
| 972 | longflag = 1; |
| 973 | ++f; |
| 974 | } |
| 975 | #ifdef HAVE_LONG_LONG |
| 976 | else if (f[1] == 'l' && |
| 977 | (f[2] == 'd' || f[2] == 'u')) { |
| 978 | longlongflag = 1; |
| 979 | f += 2; |
| 980 | } |
| 981 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 982 | } |
| 983 | /* handle the size_t flag. */ |
| 984 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 985 | size_tflag = 1; |
| 986 | ++f; |
| 987 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 988 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 989 | switch (*f) { |
| 990 | case 'c': |
| 991 | *s++ = va_arg(vargs, int); |
| 992 | break; |
| 993 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 994 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 995 | width, precision, 'd'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 996 | if (longflag) |
| 997 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 998 | #ifdef HAVE_LONG_LONG |
| 999 | else if (longlongflag) |
| 1000 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1001 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1002 | else if (size_tflag) |
| 1003 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1004 | else |
| 1005 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1006 | appendstring(realbuffer); |
| 1007 | break; |
| 1008 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1009 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1010 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1011 | if (longflag) |
| 1012 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1013 | #ifdef HAVE_LONG_LONG |
| 1014 | else if (longlongflag) |
| 1015 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1016 | unsigned PY_LONG_LONG)); |
| 1017 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1018 | else if (size_tflag) |
| 1019 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1020 | else |
| 1021 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1022 | appendstring(realbuffer); |
| 1023 | break; |
| 1024 | case 'i': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1025 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1026 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1027 | appendstring(realbuffer); |
| 1028 | break; |
| 1029 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1030 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1031 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1032 | appendstring(realbuffer); |
| 1033 | break; |
| 1034 | case 's': |
| 1035 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1036 | /* unused, since we already have the result */ |
| 1037 | (void) va_arg(vargs, char *); |
| 1038 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1039 | PyUnicode_GET_SIZE(*callresult)); |
| 1040 | s += PyUnicode_GET_SIZE(*callresult); |
| 1041 | /* We're done with the unicode()/repr() => forget it */ |
| 1042 | Py_DECREF(*callresult); |
| 1043 | /* switch to next unicode()/repr() result */ |
| 1044 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1045 | break; |
| 1046 | } |
| 1047 | case 'U': |
| 1048 | { |
| 1049 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1050 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1051 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1052 | s += size; |
| 1053 | break; |
| 1054 | } |
| 1055 | case 'V': |
| 1056 | { |
| 1057 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1058 | const char *str = va_arg(vargs, const char *); |
| 1059 | if (obj) { |
| 1060 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1061 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1062 | s += size; |
| 1063 | } else { |
| 1064 | appendstring(str); |
| 1065 | } |
| 1066 | break; |
| 1067 | } |
| 1068 | case 'S': |
| 1069 | case 'R': |
| 1070 | { |
| 1071 | Py_UNICODE *ucopy; |
| 1072 | Py_ssize_t usize; |
| 1073 | Py_ssize_t upos; |
| 1074 | /* unused, since we already have the result */ |
| 1075 | (void) va_arg(vargs, PyObject *); |
| 1076 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1077 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1078 | for (upos = 0; upos<usize;) |
| 1079 | *s++ = ucopy[upos++]; |
| 1080 | /* We're done with the unicode()/repr() => forget it */ |
| 1081 | Py_DECREF(*callresult); |
| 1082 | /* switch to next unicode()/repr() result */ |
| 1083 | ++callresult; |
| 1084 | break; |
| 1085 | } |
| 1086 | case 'p': |
| 1087 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1088 | /* %p is ill-defined: ensure leading 0x. */ |
| 1089 | if (buffer[1] == 'X') |
| 1090 | buffer[1] = 'x'; |
| 1091 | else if (buffer[1] != 'x') { |
| 1092 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1093 | buffer[0] = '0'; |
| 1094 | buffer[1] = 'x'; |
| 1095 | } |
| 1096 | appendstring(buffer); |
| 1097 | break; |
| 1098 | case '%': |
| 1099 | *s++ = '%'; |
| 1100 | break; |
| 1101 | default: |
| 1102 | appendstring(p); |
| 1103 | goto end; |
| 1104 | } |
| 1105 | } else |
| 1106 | *s++ = *f; |
| 1107 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1108 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1109 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1110 | if (callresults) |
| 1111 | PyObject_Free(callresults); |
| 1112 | if (abuffer) |
| 1113 | PyObject_Free(abuffer); |
| 1114 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1115 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1116 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1117 | if (callresults) { |
| 1118 | PyObject **callresult2 = callresults; |
| 1119 | while (callresult2 < callresult) { |
| 1120 | Py_DECREF(*callresult2); |
| 1121 | ++callresult2; |
| 1122 | } |
| 1123 | PyObject_Free(callresults); |
| 1124 | } |
| 1125 | if (abuffer) |
| 1126 | PyObject_Free(abuffer); |
| 1127 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | #undef appendstring |
| 1131 | |
| 1132 | PyObject * |
| 1133 | PyUnicode_FromFormat(const char *format, ...) |
| 1134 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1135 | PyObject* ret; |
| 1136 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1137 | |
| 1138 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1139 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1140 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1141 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1142 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1143 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1144 | va_end(vargs); |
| 1145 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1148 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1149 | wchar_t *w, |
| 1150 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1151 | { |
| 1152 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1153 | PyErr_BadInternalCall(); |
| 1154 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1155 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1156 | |
| 1157 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1158 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1159 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1161 | #ifdef HAVE_USABLE_WCHAR_T |
| 1162 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1163 | #else |
| 1164 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1165 | register Py_UNICODE *u; |
| 1166 | register Py_ssize_t i; |
| 1167 | u = PyUnicode_AS_UNICODE(unicode); |
| 1168 | for (i = size; i > 0; i--) |
| 1169 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1170 | } |
| 1171 | #endif |
| 1172 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1173 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1174 | return PyUnicode_GET_SIZE(unicode); |
| 1175 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1176 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | #endif |
| 1180 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1181 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1182 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1183 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1184 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1185 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1186 | PyErr_SetString(PyExc_ValueError, |
| 1187 | "chr() arg not in range(0x110000)"); |
| 1188 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1189 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1190 | |
| 1191 | #ifndef Py_UNICODE_WIDE |
| 1192 | if (ordinal > 0xffff) { |
| 1193 | ordinal -= 0x10000; |
| 1194 | s[0] = 0xD800 | (ordinal >> 10); |
| 1195 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1196 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1197 | } |
| 1198 | #endif |
| 1199 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1200 | s[0] = (Py_UNICODE)ordinal; |
| 1201 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1204 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1205 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1206 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1207 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1208 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1209 | Py_INCREF(obj); |
| 1210 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1211 | } |
| 1212 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1213 | /* For a Unicode subtype that's not a Unicode object, |
| 1214 | return a true Unicode object with the same data. */ |
| 1215 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1216 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1217 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1218 | PyErr_Format(PyExc_TypeError, |
| 1219 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1220 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1221 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1225 | const char *encoding, |
| 1226 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1227 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1228 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1229 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1230 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1232 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1233 | PyErr_BadInternalCall(); |
| 1234 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1235 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1236 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1237 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1238 | PyErr_SetString(PyExc_TypeError, |
| 1239 | "decoding str is not supported"); |
| 1240 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1241 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1242 | |
| 1243 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1244 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1245 | s = PyBytes_AS_STRING(obj); |
| 1246 | len = PyBytes_GET_SIZE(obj); |
| 1247 | } |
| 1248 | else if (PyByteArray_Check(obj)) { |
| 1249 | s = PyByteArray_AS_STRING(obj); |
| 1250 | len = PyByteArray_GET_SIZE(obj); |
| 1251 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1252 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1253 | /* Overwrite the error message with something more useful in |
| 1254 | case of a TypeError. */ |
| 1255 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1256 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1257 | "coercing to str: need string or buffer, " |
| 1258 | "%.80s found", |
| 1259 | Py_TYPE(obj)->tp_name); |
| 1260 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1261 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1262 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1263 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1264 | if (len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1265 | Py_INCREF(unicode_empty); |
| 1266 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1267 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1268 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1269 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1270 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1271 | return v; |
| 1272 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1273 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1274 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1278 | Py_ssize_t size, |
| 1279 | const char *encoding, |
| 1280 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1281 | { |
| 1282 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1283 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1284 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1285 | char *l; |
| 1286 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1287 | |
| 1288 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1289 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1290 | |
| 1291 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1292 | catch e.g. UTF_8 */ |
| 1293 | e = encoding; |
| 1294 | l = lower; |
| 1295 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1296 | if (ISUPPER(*e)) { |
| 1297 | *l++ = TOLOWER(*e++); |
| 1298 | } |
| 1299 | else if (*e == '_') { |
| 1300 | *l++ = '-'; |
| 1301 | e++; |
| 1302 | } |
| 1303 | else { |
| 1304 | *l++ = *e++; |
| 1305 | } |
| 1306 | } |
| 1307 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1308 | |
| 1309 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1310 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1311 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1312 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1313 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1314 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1315 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1316 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1317 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1318 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1319 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1320 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1321 | else if (strcmp(lower, "utf-16") == 0) |
| 1322 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1323 | else if (strcmp(lower, "utf-32") == 0) |
| 1324 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1325 | |
| 1326 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1327 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1328 | 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] | 1329 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1330 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1331 | if (buffer == NULL) |
| 1332 | goto onError; |
| 1333 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1334 | if (unicode == NULL) |
| 1335 | goto onError; |
| 1336 | if (!PyUnicode_Check(unicode)) { |
| 1337 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1338 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1339 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1340 | Py_DECREF(unicode); |
| 1341 | goto onError; |
| 1342 | } |
| 1343 | Py_DECREF(buffer); |
| 1344 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1345 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1346 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1347 | Py_XDECREF(buffer); |
| 1348 | return NULL; |
| 1349 | } |
| 1350 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1351 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1352 | const char *encoding, |
| 1353 | const char *errors) |
| 1354 | { |
| 1355 | PyObject *v; |
| 1356 | |
| 1357 | if (!PyUnicode_Check(unicode)) { |
| 1358 | PyErr_BadArgument(); |
| 1359 | goto onError; |
| 1360 | } |
| 1361 | |
| 1362 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1363 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1364 | |
| 1365 | /* Decode via the codec registry */ |
| 1366 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1367 | if (v == NULL) |
| 1368 | goto onError; |
| 1369 | return v; |
| 1370 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1371 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1372 | return NULL; |
| 1373 | } |
| 1374 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1375 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1376 | const char *encoding, |
| 1377 | const char *errors) |
| 1378 | { |
| 1379 | PyObject *v; |
| 1380 | |
| 1381 | if (!PyUnicode_Check(unicode)) { |
| 1382 | PyErr_BadArgument(); |
| 1383 | goto onError; |
| 1384 | } |
| 1385 | |
| 1386 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1387 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1388 | |
| 1389 | /* Decode via the codec registry */ |
| 1390 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1391 | if (v == NULL) |
| 1392 | goto onError; |
| 1393 | if (!PyUnicode_Check(v)) { |
| 1394 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1395 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1396 | Py_TYPE(v)->tp_name); |
| 1397 | Py_DECREF(v); |
| 1398 | goto onError; |
| 1399 | } |
| 1400 | return v; |
| 1401 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1402 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1403 | return NULL; |
| 1404 | } |
| 1405 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1406 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1407 | Py_ssize_t size, |
| 1408 | const char *encoding, |
| 1409 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1410 | { |
| 1411 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1412 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1413 | unicode = PyUnicode_FromUnicode(s, size); |
| 1414 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1415 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1416 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1417 | Py_DECREF(unicode); |
| 1418 | return v; |
| 1419 | } |
| 1420 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1421 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1422 | const char *encoding, |
| 1423 | const char *errors) |
| 1424 | { |
| 1425 | PyObject *v; |
| 1426 | |
| 1427 | if (!PyUnicode_Check(unicode)) { |
| 1428 | PyErr_BadArgument(); |
| 1429 | goto onError; |
| 1430 | } |
| 1431 | |
| 1432 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1433 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1434 | |
| 1435 | /* Encode via the codec registry */ |
| 1436 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1437 | if (v == NULL) |
| 1438 | goto onError; |
| 1439 | return v; |
| 1440 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1441 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1442 | return NULL; |
| 1443 | } |
| 1444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1445 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1446 | const char *encoding, |
| 1447 | const char *errors) |
| 1448 | { |
| 1449 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1451 | if (!PyUnicode_Check(unicode)) { |
| 1452 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1453 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1454 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1455 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1456 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1457 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1458 | |
| 1459 | /* Shortcuts for common default encodings */ |
| 1460 | if (errors == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1461 | if (strcmp(encoding, "utf-8") == 0) |
| 1462 | return PyUnicode_AsUTF8String(unicode); |
| 1463 | else if (strcmp(encoding, "latin-1") == 0) |
| 1464 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1465 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1466 | else if (strcmp(encoding, "mbcs") == 0) |
| 1467 | return PyUnicode_AsMBCSString(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1468 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1469 | else if (strcmp(encoding, "ascii") == 0) |
| 1470 | return PyUnicode_AsASCIIString(unicode); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1471 | /* During bootstrap, we may need to find the encodings |
| 1472 | package, to load the file system encoding, and require the |
| 1473 | file system encoding in order to load the encodings |
| 1474 | package. |
| 1475 | |
| 1476 | Break out of this dependency by assuming that the path to |
| 1477 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1478 | instead, if the file system encoding is the locale's |
| 1479 | encoding. */ |
| 1480 | else if (Py_FileSystemDefaultEncoding && |
| 1481 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1482 | !PyThreadState_GET()->interp->codecs_initialized) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1483 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1484 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* Encode via the codec registry */ |
| 1487 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1488 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1489 | return NULL; |
| 1490 | |
| 1491 | /* The normal path */ |
| 1492 | if (PyBytes_Check(v)) |
| 1493 | return v; |
| 1494 | |
| 1495 | /* 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] | 1496 | if (PyByteArray_Check(v)) { |
| 1497 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1498 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1499 | PyOS_snprintf(msg, sizeof(msg), |
| 1500 | "encoder %s returned buffer instead of bytes", |
| 1501 | encoding); |
| 1502 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1503 | Py_DECREF(v); |
| 1504 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1505 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1506 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1507 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1508 | Py_DECREF(v); |
| 1509 | return b; |
| 1510 | } |
| 1511 | |
| 1512 | PyErr_Format(PyExc_TypeError, |
| 1513 | "encoder did not return a bytes object (type=%.400s)", |
| 1514 | Py_TYPE(v)->tp_name); |
| 1515 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1516 | return NULL; |
| 1517 | } |
| 1518 | |
| 1519 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1520 | const char *encoding, |
| 1521 | const char *errors) |
| 1522 | { |
| 1523 | PyObject *v; |
| 1524 | |
| 1525 | if (!PyUnicode_Check(unicode)) { |
| 1526 | PyErr_BadArgument(); |
| 1527 | goto onError; |
| 1528 | } |
| 1529 | |
| 1530 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1531 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1532 | |
| 1533 | /* Encode via the codec registry */ |
| 1534 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1535 | if (v == NULL) |
| 1536 | goto onError; |
| 1537 | if (!PyUnicode_Check(v)) { |
| 1538 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1539 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1540 | Py_TYPE(v)->tp_name); |
| 1541 | Py_DECREF(v); |
| 1542 | goto onError; |
| 1543 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1544 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1545 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1546 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1547 | return NULL; |
| 1548 | } |
| 1549 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1550 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1551 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1552 | { |
| 1553 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1554 | if (v) |
| 1555 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1556 | if (errors != NULL) |
| 1557 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1558 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1559 | PyUnicode_GET_SIZE(unicode), |
| 1560 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1561 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1562 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1563 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1564 | return v; |
| 1565 | } |
| 1566 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1567 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1568 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1569 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1570 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1571 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1572 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1573 | PyObject* |
| 1574 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1575 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1576 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1577 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1578 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1579 | bootstrapping process where the codecs aren't ready yet. |
| 1580 | */ |
| 1581 | if (Py_FileSystemDefaultEncoding) { |
| 1582 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1583 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1584 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1585 | } |
| 1586 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1587 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1588 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1589 | } |
| 1590 | #endif |
| 1591 | return PyUnicode_Decode(s, size, |
| 1592 | Py_FileSystemDefaultEncoding, |
| 1593 | "replace"); |
| 1594 | } |
| 1595 | else { |
| 1596 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1597 | } |
| 1598 | } |
| 1599 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1600 | /* Convert the argument to a bytes object, according to the file |
| 1601 | system encoding */ |
| 1602 | |
| 1603 | int |
| 1604 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1605 | { |
| 1606 | PyObject *output = NULL; |
| 1607 | Py_ssize_t size; |
| 1608 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1609 | if (arg == NULL) { |
| 1610 | Py_DECREF(*(PyObject**)addr); |
| 1611 | return 1; |
| 1612 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1613 | if (PyBytes_Check(arg) || PyByteArray_Check(arg)) { |
| 1614 | output = arg; |
| 1615 | Py_INCREF(output); |
| 1616 | } |
| 1617 | else { |
| 1618 | arg = PyUnicode_FromObject(arg); |
| 1619 | if (!arg) |
| 1620 | return 0; |
| 1621 | output = PyUnicode_AsEncodedObject(arg, |
| 1622 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1623 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1624 | Py_DECREF(arg); |
| 1625 | if (!output) |
| 1626 | return 0; |
| 1627 | if (!PyBytes_Check(output)) { |
| 1628 | Py_DECREF(output); |
| 1629 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1630 | return 0; |
| 1631 | } |
| 1632 | } |
| 1633 | if (PyBytes_Check(output)) { |
| 1634 | size = PyBytes_GET_SIZE(output); |
| 1635 | data = PyBytes_AS_STRING(output); |
| 1636 | } |
| 1637 | else { |
| 1638 | size = PyByteArray_GET_SIZE(output); |
| 1639 | data = PyByteArray_AS_STRING(output); |
| 1640 | } |
| 1641 | if (size != strlen(data)) { |
| 1642 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1643 | Py_DECREF(output); |
| 1644 | return 0; |
| 1645 | } |
| 1646 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1647 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1651 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1652 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1653 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1654 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1655 | if (!PyUnicode_Check(unicode)) { |
| 1656 | PyErr_BadArgument(); |
| 1657 | return NULL; |
| 1658 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1659 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1660 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1661 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1662 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1663 | *psize = PyBytes_GET_SIZE(bytes); |
| 1664 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1668 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1669 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1670 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1673 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1674 | { |
| 1675 | if (!PyUnicode_Check(unicode)) { |
| 1676 | PyErr_BadArgument(); |
| 1677 | goto onError; |
| 1678 | } |
| 1679 | return PyUnicode_AS_UNICODE(unicode); |
| 1680 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1681 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1682 | return NULL; |
| 1683 | } |
| 1684 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1685 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1686 | { |
| 1687 | if (!PyUnicode_Check(unicode)) { |
| 1688 | PyErr_BadArgument(); |
| 1689 | goto onError; |
| 1690 | } |
| 1691 | return PyUnicode_GET_SIZE(unicode); |
| 1692 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1693 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1694 | return -1; |
| 1695 | } |
| 1696 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1697 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1698 | { |
| 1699 | return unicode_default_encoding; |
| 1700 | } |
| 1701 | |
| 1702 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1703 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1704 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1705 | PyErr_Format(PyExc_ValueError, |
| 1706 | "Can only set default encoding to %s", |
| 1707 | unicode_default_encoding); |
| 1708 | return -1; |
| 1709 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1710 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1713 | /* error handling callback helper: |
| 1714 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1715 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1716 | and adjust various state variables. |
| 1717 | return 0 on success, -1 on error |
| 1718 | */ |
| 1719 | |
| 1720 | static |
| 1721 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1722 | const char *encoding, const char *reason, |
| 1723 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1724 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1725 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1726 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1727 | 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] | 1728 | |
| 1729 | PyObject *restuple = NULL; |
| 1730 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1731 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1732 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1733 | Py_ssize_t requiredsize; |
| 1734 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1735 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1736 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1737 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1738 | int res = -1; |
| 1739 | |
| 1740 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1741 | *errorHandler = PyCodec_LookupError(errors); |
| 1742 | if (*errorHandler == NULL) |
| 1743 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1747 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1748 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
| 1749 | if (*exceptionObject == NULL) |
| 1750 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1751 | } |
| 1752 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1753 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1754 | goto onError; |
| 1755 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1756 | goto onError; |
| 1757 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1758 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1762 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1763 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1764 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1765 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1766 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1767 | } |
| 1768 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1769 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1770 | |
| 1771 | /* Copy back the bytes variables, which might have been modified by the |
| 1772 | callback */ |
| 1773 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1774 | if (!inputobj) |
| 1775 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1776 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1777 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1778 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1779 | *input = PyBytes_AS_STRING(inputobj); |
| 1780 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1781 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1782 | /* we can DECREF safely, as the exception has another reference, |
| 1783 | so the object won't go away. */ |
| 1784 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1785 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1786 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1787 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1788 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1789 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1790 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1791 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1792 | |
| 1793 | /* need more space? (at least enough for what we |
| 1794 | have+the replacement+the rest of the string (starting |
| 1795 | at the new input position), so we won't have to check space |
| 1796 | when there are no errors in the rest of the string) */ |
| 1797 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1798 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1799 | requiredsize = *outpos + repsize + insize-newpos; |
| 1800 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1801 | if (requiredsize<2*outsize) |
| 1802 | requiredsize = 2*outsize; |
| 1803 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1804 | goto onError; |
| 1805 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1806 | } |
| 1807 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1808 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1809 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1810 | *outptr += repsize; |
| 1811 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1812 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1813 | /* we made it! */ |
| 1814 | res = 0; |
| 1815 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1816 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1817 | Py_XDECREF(restuple); |
| 1818 | return res; |
| 1819 | } |
| 1820 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1821 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1822 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1823 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1824 | |
| 1825 | /* Three simple macros defining base-64. */ |
| 1826 | |
| 1827 | /* Is c a base-64 character? */ |
| 1828 | |
| 1829 | #define IS_BASE64(c) \ |
| 1830 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1831 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1832 | ((c) >= '0' && (c) <= '9') || \ |
| 1833 | (c) == '+' || (c) == '/') |
| 1834 | |
| 1835 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1836 | |
| 1837 | #define FROM_BASE64(c) \ |
| 1838 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1839 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1840 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1841 | (c) == '+' ? 62 : 63) |
| 1842 | |
| 1843 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1844 | |
| 1845 | #define TO_BASE64(n) \ |
| 1846 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1847 | |
| 1848 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1849 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1850 | * byte not decoding to itself is the + which begins a base64 |
| 1851 | * string. */ |
| 1852 | |
| 1853 | #define DECODE_DIRECT(c) \ |
| 1854 | ((c) <= 127 && (c) != '+') |
| 1855 | |
| 1856 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1857 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1858 | * the above). See RFC2152. This array identifies these different |
| 1859 | * sets: |
| 1860 | * 0 : "Set D" |
| 1861 | * alphanumeric and '(),-./:? |
| 1862 | * 1 : "Set O" |
| 1863 | * !"#$%&*;<=>@[]^_`{|} |
| 1864 | * 2 : "whitespace" |
| 1865 | * ht nl cr sp |
| 1866 | * 3 : special (must be base64 encoded) |
| 1867 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1868 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1869 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1870 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1871 | char utf7_category[128] = { |
| 1872 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 1873 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 1874 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 1875 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1876 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 1877 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 1878 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 1879 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 1880 | /* @ A B C D E F G H I J K L M N O */ |
| 1881 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1882 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 1883 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 1884 | /* ` a b c d e f g h i j k l m n o */ |
| 1885 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1886 | /* p q r s t u v w x y z { | } ~ del */ |
| 1887 | 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] | 1888 | }; |
| 1889 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1890 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 1891 | * answer depends on whether we are encoding set O as itself, and also |
| 1892 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 1893 | * clear that the answers to these questions vary between |
| 1894 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1895 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1896 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 1897 | ((c) < 128 && (c) > 0 && \ |
| 1898 | ((utf7_category[(c)] == 0) || \ |
| 1899 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 1900 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1901 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1902 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1903 | Py_ssize_t size, |
| 1904 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1905 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1906 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1907 | } |
| 1908 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1909 | /* The decoder. The only state we preserve is our read position, |
| 1910 | * i.e. how many characters we have consumed. So if we end in the |
| 1911 | * middle of a shift sequence we have to back off the read position |
| 1912 | * and the output to the beginning of the sequence, otherwise we lose |
| 1913 | * all the shift state (seen bits, number of bits seen, high |
| 1914 | * surrogate). */ |
| 1915 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1916 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1917 | Py_ssize_t size, |
| 1918 | const char *errors, |
| 1919 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1920 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1921 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1922 | Py_ssize_t startinpos; |
| 1923 | Py_ssize_t endinpos; |
| 1924 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1925 | const char *e; |
| 1926 | PyUnicodeObject *unicode; |
| 1927 | Py_UNICODE *p; |
| 1928 | const char *errmsg = ""; |
| 1929 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1930 | Py_UNICODE *shiftOutStart; |
| 1931 | unsigned int base64bits = 0; |
| 1932 | unsigned long base64buffer = 0; |
| 1933 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1934 | PyObject *errorHandler = NULL; |
| 1935 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1936 | |
| 1937 | unicode = _PyUnicode_New(size); |
| 1938 | if (!unicode) |
| 1939 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1940 | if (size == 0) { |
| 1941 | if (consumed) |
| 1942 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1943 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1944 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1945 | |
| 1946 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1947 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1948 | e = s + size; |
| 1949 | |
| 1950 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1951 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1952 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1953 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1954 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1955 | if (inShift) { /* in a base-64 section */ |
| 1956 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 1957 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 1958 | base64bits += 6; |
| 1959 | s++; |
| 1960 | if (base64bits >= 16) { |
| 1961 | /* we have enough bits for a UTF-16 value */ |
| 1962 | Py_UNICODE outCh = (Py_UNICODE) |
| 1963 | (base64buffer >> (base64bits-16)); |
| 1964 | base64bits -= 16; |
| 1965 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 1966 | if (surrogate) { |
| 1967 | /* expecting a second surrogate */ |
| 1968 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 1969 | #ifdef Py_UNICODE_WIDE |
| 1970 | *p++ = (((surrogate & 0x3FF)<<10) |
| 1971 | | (outCh & 0x3FF)) + 0x10000; |
| 1972 | #else |
| 1973 | *p++ = surrogate; |
| 1974 | *p++ = outCh; |
| 1975 | #endif |
| 1976 | surrogate = 0; |
| 1977 | } |
| 1978 | else { |
| 1979 | surrogate = 0; |
| 1980 | errmsg = "second surrogate missing"; |
| 1981 | goto utf7Error; |
| 1982 | } |
| 1983 | } |
| 1984 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 1985 | /* first surrogate */ |
| 1986 | surrogate = outCh; |
| 1987 | } |
| 1988 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 1989 | errmsg = "unexpected second surrogate"; |
| 1990 | goto utf7Error; |
| 1991 | } |
| 1992 | else { |
| 1993 | *p++ = outCh; |
| 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1998 | inShift = 0; |
| 1999 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2000 | if (surrogate) { |
| 2001 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2002 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2003 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2004 | if (base64bits > 0) { /* left-over bits */ |
| 2005 | if (base64bits >= 6) { |
| 2006 | /* We've seen at least one base-64 character */ |
| 2007 | errmsg = "partial character in shift sequence"; |
| 2008 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2009 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2010 | else { |
| 2011 | /* Some bits remain; they should be zero */ |
| 2012 | if (base64buffer != 0) { |
| 2013 | errmsg = "non-zero padding bits in shift sequence"; |
| 2014 | goto utf7Error; |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | if (ch != '-') { |
| 2019 | /* '-' is absorbed; other terminating |
| 2020 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2021 | *p++ = ch; |
| 2022 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2023 | } |
| 2024 | } |
| 2025 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2026 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2027 | s++; /* consume '+' */ |
| 2028 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2029 | s++; |
| 2030 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2031 | } |
| 2032 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2033 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2034 | shiftOutStart = p; |
| 2035 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2036 | } |
| 2037 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2038 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2039 | *p++ = ch; |
| 2040 | s++; |
| 2041 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2042 | else { |
| 2043 | startinpos = s-starts; |
| 2044 | s++; |
| 2045 | errmsg = "unexpected special character"; |
| 2046 | goto utf7Error; |
| 2047 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2048 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2049 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2050 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2051 | endinpos = s-starts; |
| 2052 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2053 | errors, &errorHandler, |
| 2054 | "utf7", errmsg, |
| 2055 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2056 | &unicode, &outpos, &p)) |
| 2057 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2060 | /* end of string */ |
| 2061 | |
| 2062 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2063 | /* if we're in an inconsistent state, that's an error */ |
| 2064 | if (surrogate || |
| 2065 | (base64bits >= 6) || |
| 2066 | (base64bits > 0 && base64buffer != 0)) { |
| 2067 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2068 | endinpos = size; |
| 2069 | if (unicode_decode_call_errorhandler( |
| 2070 | errors, &errorHandler, |
| 2071 | "utf7", "unterminated shift sequence", |
| 2072 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2073 | &unicode, &outpos, &p)) |
| 2074 | goto onError; |
| 2075 | if (s < e) |
| 2076 | goto restart; |
| 2077 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2078 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2079 | |
| 2080 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2081 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2082 | if (inShift) { |
| 2083 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2084 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2085 | } |
| 2086 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2087 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2088 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2089 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2090 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2091 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2092 | goto onError; |
| 2093 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2094 | Py_XDECREF(errorHandler); |
| 2095 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2096 | return (PyObject *)unicode; |
| 2097 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2098 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2099 | Py_XDECREF(errorHandler); |
| 2100 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2101 | Py_DECREF(unicode); |
| 2102 | return NULL; |
| 2103 | } |
| 2104 | |
| 2105 | |
| 2106 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2107 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2108 | int base64SetO, |
| 2109 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2110 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2111 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2112 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2113 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2114 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2115 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2116 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2117 | unsigned int base64bits = 0; |
| 2118 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2119 | char * out; |
| 2120 | char * start; |
| 2121 | |
| 2122 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2123 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2124 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2125 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2126 | return PyErr_NoMemory(); |
| 2127 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2128 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2129 | if (v == NULL) |
| 2130 | return NULL; |
| 2131 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2132 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2133 | for (;i < size; ++i) { |
| 2134 | Py_UNICODE ch = s[i]; |
| 2135 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2136 | if (inShift) { |
| 2137 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2138 | /* shifting out */ |
| 2139 | if (base64bits) { /* output remaining bits */ |
| 2140 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2141 | base64buffer = 0; |
| 2142 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2143 | } |
| 2144 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2145 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2146 | so no '-' is required, except if the character is itself a '-' */ |
| 2147 | if (IS_BASE64(ch) || ch == '-') { |
| 2148 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2149 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2150 | *out++ = (char) ch; |
| 2151 | } |
| 2152 | else { |
| 2153 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2154 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2155 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2156 | else { /* not in a shift sequence */ |
| 2157 | if (ch == '+') { |
| 2158 | *out++ = '+'; |
| 2159 | *out++ = '-'; |
| 2160 | } |
| 2161 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2162 | *out++ = (char) ch; |
| 2163 | } |
| 2164 | else { |
| 2165 | *out++ = '+'; |
| 2166 | inShift = 1; |
| 2167 | goto encode_char; |
| 2168 | } |
| 2169 | } |
| 2170 | continue; |
| 2171 | encode_char: |
| 2172 | #ifdef Py_UNICODE_WIDE |
| 2173 | if (ch >= 0x10000) { |
| 2174 | /* code first surrogate */ |
| 2175 | base64bits += 16; |
| 2176 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2177 | while (base64bits >= 6) { |
| 2178 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2179 | base64bits -= 6; |
| 2180 | } |
| 2181 | /* prepare second surrogate */ |
| 2182 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2183 | } |
| 2184 | #endif |
| 2185 | base64bits += 16; |
| 2186 | base64buffer = (base64buffer << 16) | ch; |
| 2187 | while (base64bits >= 6) { |
| 2188 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2189 | base64bits -= 6; |
| 2190 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2191 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2192 | if (base64bits) |
| 2193 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2194 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2195 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2196 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2197 | return NULL; |
| 2198 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2201 | #undef IS_BASE64 |
| 2202 | #undef FROM_BASE64 |
| 2203 | #undef TO_BASE64 |
| 2204 | #undef DECODE_DIRECT |
| 2205 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2206 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2207 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2208 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2209 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2210 | char utf8_code_length[256] = { |
| 2211 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 2212 | illegal prefix. see RFC 2279 for details */ |
| 2213 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2214 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2215 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2216 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2217 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2218 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2219 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2220 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2221 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2222 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2223 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2224 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2225 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2226 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2227 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2228 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 2229 | }; |
| 2230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2231 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2232 | Py_ssize_t size, |
| 2233 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2234 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2235 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2236 | } |
| 2237 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2238 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2239 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2240 | |
| 2241 | /* Mask to quickly check whether a C 'long' contains a |
| 2242 | non-ASCII, UTF8-encoded char. */ |
| 2243 | #if (SIZEOF_LONG == 8) |
| 2244 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2245 | #elif (SIZEOF_LONG == 4) |
| 2246 | # define ASCII_CHAR_MASK 0x80808080L |
| 2247 | #else |
| 2248 | # error C 'long' size should be either 4 or 8! |
| 2249 | #endif |
| 2250 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2251 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2252 | Py_ssize_t size, |
| 2253 | const char *errors, |
| 2254 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2255 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2256 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2257 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2258 | Py_ssize_t startinpos; |
| 2259 | Py_ssize_t endinpos; |
| 2260 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2261 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2262 | PyUnicodeObject *unicode; |
| 2263 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2264 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2265 | PyObject *errorHandler = NULL; |
| 2266 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2267 | |
| 2268 | /* Note: size will always be longer than the resulting Unicode |
| 2269 | character count */ |
| 2270 | unicode = _PyUnicode_New(size); |
| 2271 | if (!unicode) |
| 2272 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2273 | if (size == 0) { |
| 2274 | if (consumed) |
| 2275 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2276 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2277 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2278 | |
| 2279 | /* Unpack UTF-8 encoded data */ |
| 2280 | p = unicode->str; |
| 2281 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2282 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2283 | |
| 2284 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2285 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2286 | |
| 2287 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2288 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2289 | input will consist of an overwhelming majority of ASCII |
| 2290 | characters, we try to optimize for this case by checking |
| 2291 | as many characters as a C 'long' can contain. |
| 2292 | First, check if we can do an aligned read, as most CPUs have |
| 2293 | a penalty for unaligned reads. |
| 2294 | */ |
| 2295 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2296 | /* Help register allocation */ |
| 2297 | register const char *_s = s; |
| 2298 | register Py_UNICODE *_p = p; |
| 2299 | while (_s < aligned_end) { |
| 2300 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2301 | and do a fast unrolled copy if it only contains ASCII |
| 2302 | characters. */ |
| 2303 | unsigned long data = *(unsigned long *) _s; |
| 2304 | if (data & ASCII_CHAR_MASK) |
| 2305 | break; |
| 2306 | _p[0] = (unsigned char) _s[0]; |
| 2307 | _p[1] = (unsigned char) _s[1]; |
| 2308 | _p[2] = (unsigned char) _s[2]; |
| 2309 | _p[3] = (unsigned char) _s[3]; |
| 2310 | #if (SIZEOF_LONG == 8) |
| 2311 | _p[4] = (unsigned char) _s[4]; |
| 2312 | _p[5] = (unsigned char) _s[5]; |
| 2313 | _p[6] = (unsigned char) _s[6]; |
| 2314 | _p[7] = (unsigned char) _s[7]; |
| 2315 | #endif |
| 2316 | _s += SIZEOF_LONG; |
| 2317 | _p += SIZEOF_LONG; |
| 2318 | } |
| 2319 | s = _s; |
| 2320 | p = _p; |
| 2321 | if (s == e) |
| 2322 | break; |
| 2323 | ch = (unsigned char)*s; |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2328 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2329 | s++; |
| 2330 | continue; |
| 2331 | } |
| 2332 | |
| 2333 | n = utf8_code_length[ch]; |
| 2334 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2335 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2336 | if (consumed) |
| 2337 | break; |
| 2338 | else { |
| 2339 | errmsg = "unexpected end of data"; |
| 2340 | startinpos = s-starts; |
| 2341 | endinpos = size; |
| 2342 | goto utf8Error; |
| 2343 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2344 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2345 | |
| 2346 | switch (n) { |
| 2347 | |
| 2348 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2349 | errmsg = "unexpected code byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2350 | startinpos = s-starts; |
| 2351 | endinpos = startinpos+1; |
| 2352 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2353 | |
| 2354 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2355 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2356 | startinpos = s-starts; |
| 2357 | endinpos = startinpos+1; |
| 2358 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2359 | |
| 2360 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2361 | if ((s[1] & 0xc0) != 0x80) { |
| 2362 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2363 | startinpos = s-starts; |
| 2364 | endinpos = startinpos+2; |
| 2365 | goto utf8Error; |
| 2366 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2367 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2368 | if (ch < 0x80) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2369 | startinpos = s-starts; |
| 2370 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2371 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2372 | goto utf8Error; |
| 2373 | } |
| 2374 | else |
| 2375 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2376 | break; |
| 2377 | |
| 2378 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2379 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2380 | (s[2] & 0xc0) != 0x80) { |
| 2381 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2382 | startinpos = s-starts; |
| 2383 | endinpos = startinpos+3; |
| 2384 | goto utf8Error; |
| 2385 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2386 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2387 | if (ch < 0x0800 || (ch >= 0xd800 && ch <= 0xDFFF)) { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2388 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2389 | startinpos = s-starts; |
| 2390 | endinpos = startinpos+3; |
| 2391 | goto utf8Error; |
| 2392 | } |
| 2393 | else |
| 2394 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2395 | break; |
| 2396 | |
| 2397 | case 4: |
| 2398 | if ((s[1] & 0xc0) != 0x80 || |
| 2399 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2400 | (s[3] & 0xc0) != 0x80) { |
| 2401 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2402 | startinpos = s-starts; |
| 2403 | endinpos = startinpos+4; |
| 2404 | goto utf8Error; |
| 2405 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2406 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2407 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2408 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2409 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2410 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2411 | || (ch > 0x10ffff)) /* maximum value allowed for |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2412 | UTF-16 */ |
| 2413 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2414 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2415 | startinpos = s-starts; |
| 2416 | endinpos = startinpos+4; |
| 2417 | goto utf8Error; |
| 2418 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2419 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2420 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2421 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2422 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2423 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2424 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2425 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2426 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2427 | /* high surrogate = top 10 bits added to D800 */ |
| 2428 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2429 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2430 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2431 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2432 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2433 | break; |
| 2434 | |
| 2435 | default: |
| 2436 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2437 | errmsg = "unsupported Unicode code range"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2438 | startinpos = s-starts; |
| 2439 | endinpos = startinpos+n; |
| 2440 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2441 | } |
| 2442 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2443 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2444 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2445 | utf8Error: |
| 2446 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2447 | if (unicode_decode_call_errorhandler( |
| 2448 | errors, &errorHandler, |
| 2449 | "utf8", errmsg, |
| 2450 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2451 | &unicode, &outpos, &p)) |
| 2452 | goto onError; |
| 2453 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2454 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2455 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2456 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2457 | |
| 2458 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2459 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2460 | goto onError; |
| 2461 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2462 | Py_XDECREF(errorHandler); |
| 2463 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2464 | return (PyObject *)unicode; |
| 2465 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2466 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2467 | Py_XDECREF(errorHandler); |
| 2468 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2469 | Py_DECREF(unicode); |
| 2470 | return NULL; |
| 2471 | } |
| 2472 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2473 | #undef ASCII_CHAR_MASK |
| 2474 | |
| 2475 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2476 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2477 | and allocate exactly as much space needed at the end. Else allocate the |
| 2478 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2479 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2480 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2481 | PyObject * |
| 2482 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2483 | Py_ssize_t size, |
| 2484 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2485 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2486 | #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] | 2487 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2488 | Py_ssize_t i; /* index into s of next input byte */ |
| 2489 | PyObject *result; /* result string object */ |
| 2490 | char *p; /* next free byte in output buffer */ |
| 2491 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2492 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2493 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2494 | PyObject *errorHandler = NULL; |
| 2495 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2496 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2497 | assert(s != NULL); |
| 2498 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2499 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2500 | if (size <= MAX_SHORT_UNICHARS) { |
| 2501 | /* Write into the stack buffer; nallocated can't overflow. |
| 2502 | * At the end, we'll allocate exactly as much heap space as it |
| 2503 | * turns out we need. |
| 2504 | */ |
| 2505 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2506 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2507 | p = stackbuf; |
| 2508 | } |
| 2509 | else { |
| 2510 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2511 | nallocated = size * 4; |
| 2512 | if (nallocated / 4 != size) /* overflow! */ |
| 2513 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2514 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2515 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2516 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2517 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2518 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2519 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2520 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2521 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2522 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2523 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2524 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2525 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2526 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2527 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2528 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2529 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2530 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2531 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2532 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2533 | /* Encode UCS2 Unicode ordinals */ |
| 2534 | if (ch < 0x10000) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2535 | #ifndef Py_UNICODE_WIDE |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2536 | /* Special case: check for high surrogate */ |
| 2537 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2538 | Py_UCS4 ch2 = s[i]; |
| 2539 | /* Check for low surrogate and combine the two to |
| 2540 | form a UCS4 value */ |
| 2541 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2542 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2543 | i++; |
| 2544 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2545 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2546 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2547 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2548 | #endif |
| 2549 | if (ch >= 0xd800 && ch <= 0xdfff) { |
| 2550 | Py_ssize_t newpos; |
| 2551 | PyObject *rep; |
| 2552 | char *prep; |
| 2553 | int k; |
| 2554 | rep = unicode_encode_call_errorhandler |
| 2555 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2556 | s, size, &exc, i-1, i, &newpos); |
| 2557 | if (!rep) |
| 2558 | goto error; |
| 2559 | /* Implementation limitations: only support error handler that return |
| 2560 | bytes, and only support up to four replacement bytes. */ |
| 2561 | if (!PyBytes_Check(rep)) { |
| 2562 | PyErr_SetString(PyExc_TypeError, "error handler should have returned bytes"); |
| 2563 | Py_DECREF(rep); |
| 2564 | goto error; |
| 2565 | } |
| 2566 | if (PyBytes_Size(rep) > 4) { |
| 2567 | PyErr_SetString(PyExc_TypeError, "error handler returned too many bytes"); |
| 2568 | Py_DECREF(rep); |
| 2569 | goto error; |
| 2570 | } |
| 2571 | prep = PyBytes_AsString(rep); |
| 2572 | for(k = PyBytes_Size(rep); k > 0; k--) |
| 2573 | *p++ = *prep++; |
| 2574 | Py_DECREF(rep); |
| 2575 | continue; |
| 2576 | |
| 2577 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2578 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2579 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2580 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2581 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2582 | } |
Benjamin Peterson | adf6a6c | 2009-11-10 21:23:15 +0000 | [diff] [blame] | 2583 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2584 | encodeUCS4: |
Benjamin Peterson | adf6a6c | 2009-11-10 21:23:15 +0000 | [diff] [blame] | 2585 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2586 | /* Encode UCS4 Unicode ordinals */ |
| 2587 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2588 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2589 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2590 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2591 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2592 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2593 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2594 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2595 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2596 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2597 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2598 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2599 | } |
| 2600 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2601 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2602 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2603 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2604 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2605 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2606 | Py_XDECREF(errorHandler); |
| 2607 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2608 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2609 | error: |
| 2610 | Py_XDECREF(errorHandler); |
| 2611 | Py_XDECREF(exc); |
| 2612 | Py_XDECREF(result); |
| 2613 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2614 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2615 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2618 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2619 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2620 | if (!PyUnicode_Check(unicode)) { |
| 2621 | PyErr_BadArgument(); |
| 2622 | return NULL; |
| 2623 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2624 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2625 | PyUnicode_GET_SIZE(unicode), |
| 2626 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2629 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2630 | |
| 2631 | PyObject * |
| 2632 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2633 | Py_ssize_t size, |
| 2634 | const char *errors, |
| 2635 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2636 | { |
| 2637 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2638 | } |
| 2639 | |
| 2640 | PyObject * |
| 2641 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2642 | Py_ssize_t size, |
| 2643 | const char *errors, |
| 2644 | int *byteorder, |
| 2645 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2646 | { |
| 2647 | const char *starts = s; |
| 2648 | Py_ssize_t startinpos; |
| 2649 | Py_ssize_t endinpos; |
| 2650 | Py_ssize_t outpos; |
| 2651 | PyUnicodeObject *unicode; |
| 2652 | Py_UNICODE *p; |
| 2653 | #ifndef Py_UNICODE_WIDE |
| 2654 | int i, pairs; |
| 2655 | #else |
| 2656 | const int pairs = 0; |
| 2657 | #endif |
| 2658 | const unsigned char *q, *e; |
| 2659 | int bo = 0; /* assume native ordering by default */ |
| 2660 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2661 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2662 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2663 | int iorder[] = {0, 1, 2, 3}; |
| 2664 | #else |
| 2665 | int iorder[] = {3, 2, 1, 0}; |
| 2666 | #endif |
| 2667 | PyObject *errorHandler = NULL; |
| 2668 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2669 | /* On narrow builds we split characters outside the BMP into two |
| 2670 | codepoints => count how much extra space we need. */ |
| 2671 | #ifndef Py_UNICODE_WIDE |
| 2672 | for (i = pairs = 0; i < size/4; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2673 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2674 | pairs++; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2675 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2676 | |
| 2677 | /* This might be one to much, because of a BOM */ |
| 2678 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2679 | if (!unicode) |
| 2680 | return NULL; |
| 2681 | if (size == 0) |
| 2682 | return (PyObject *)unicode; |
| 2683 | |
| 2684 | /* Unpack UTF-32 encoded data */ |
| 2685 | p = unicode->str; |
| 2686 | q = (unsigned char *)s; |
| 2687 | e = q + size; |
| 2688 | |
| 2689 | if (byteorder) |
| 2690 | bo = *byteorder; |
| 2691 | |
| 2692 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2693 | byte order setting accordingly. In native mode, the leading BOM |
| 2694 | mark is skipped, in all other modes, it is copied to the output |
| 2695 | stream as-is (giving a ZWNBSP character). */ |
| 2696 | if (bo == 0) { |
| 2697 | if (size >= 4) { |
| 2698 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2699 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2700 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2701 | if (bom == 0x0000FEFF) { |
| 2702 | q += 4; |
| 2703 | bo = -1; |
| 2704 | } |
| 2705 | else if (bom == 0xFFFE0000) { |
| 2706 | q += 4; |
| 2707 | bo = 1; |
| 2708 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2709 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2710 | if (bom == 0x0000FEFF) { |
| 2711 | q += 4; |
| 2712 | bo = 1; |
| 2713 | } |
| 2714 | else if (bom == 0xFFFE0000) { |
| 2715 | q += 4; |
| 2716 | bo = -1; |
| 2717 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2718 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2719 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | if (bo == -1) { |
| 2723 | /* force LE */ |
| 2724 | iorder[0] = 0; |
| 2725 | iorder[1] = 1; |
| 2726 | iorder[2] = 2; |
| 2727 | iorder[3] = 3; |
| 2728 | } |
| 2729 | else if (bo == 1) { |
| 2730 | /* force BE */ |
| 2731 | iorder[0] = 3; |
| 2732 | iorder[1] = 2; |
| 2733 | iorder[2] = 1; |
| 2734 | iorder[3] = 0; |
| 2735 | } |
| 2736 | |
| 2737 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2738 | Py_UCS4 ch; |
| 2739 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2740 | if (e-q<4) { |
| 2741 | if (consumed) |
| 2742 | break; |
| 2743 | errmsg = "truncated data"; |
| 2744 | startinpos = ((const char *)q)-starts; |
| 2745 | endinpos = ((const char *)e)-starts; |
| 2746 | goto utf32Error; |
| 2747 | /* The remaining input chars are ignored if the callback |
| 2748 | chooses to skip the input */ |
| 2749 | } |
| 2750 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2751 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2752 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2753 | if (ch >= 0x110000) |
| 2754 | { |
| 2755 | errmsg = "codepoint not in range(0x110000)"; |
| 2756 | startinpos = ((const char *)q)-starts; |
| 2757 | endinpos = startinpos+4; |
| 2758 | goto utf32Error; |
| 2759 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2760 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2761 | if (ch >= 0x10000) |
| 2762 | { |
| 2763 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2764 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2765 | } |
| 2766 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2767 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2768 | *p++ = ch; |
| 2769 | q += 4; |
| 2770 | continue; |
| 2771 | utf32Error: |
| 2772 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2773 | if (unicode_decode_call_errorhandler( |
| 2774 | errors, &errorHandler, |
| 2775 | "utf32", errmsg, |
| 2776 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2777 | &unicode, &outpos, &p)) |
| 2778 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | if (byteorder) |
| 2782 | *byteorder = bo; |
| 2783 | |
| 2784 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2785 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2786 | |
| 2787 | /* Adjust length */ |
| 2788 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2789 | goto onError; |
| 2790 | |
| 2791 | Py_XDECREF(errorHandler); |
| 2792 | Py_XDECREF(exc); |
| 2793 | return (PyObject *)unicode; |
| 2794 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2795 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2796 | Py_DECREF(unicode); |
| 2797 | Py_XDECREF(errorHandler); |
| 2798 | Py_XDECREF(exc); |
| 2799 | return NULL; |
| 2800 | } |
| 2801 | |
| 2802 | PyObject * |
| 2803 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2804 | Py_ssize_t size, |
| 2805 | const char *errors, |
| 2806 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2807 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2808 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2809 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2810 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2811 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2812 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2813 | #else |
| 2814 | const int pairs = 0; |
| 2815 | #endif |
| 2816 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2817 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2818 | int iorder[] = {0, 1, 2, 3}; |
| 2819 | #else |
| 2820 | int iorder[] = {3, 2, 1, 0}; |
| 2821 | #endif |
| 2822 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2823 | #define STORECHAR(CH) \ |
| 2824 | do { \ |
| 2825 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2826 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2827 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2828 | p[iorder[0]] = (CH) & 0xff; \ |
| 2829 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2830 | } while(0) |
| 2831 | |
| 2832 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2833 | so we need less space. */ |
| 2834 | #ifndef Py_UNICODE_WIDE |
| 2835 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2836 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2837 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2838 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2839 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2840 | nsize = (size - pairs + (byteorder == 0)); |
| 2841 | bytesize = nsize * 4; |
| 2842 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2843 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2844 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2845 | if (v == NULL) |
| 2846 | return NULL; |
| 2847 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2848 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2849 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2850 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2851 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2852 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2853 | |
| 2854 | if (byteorder == -1) { |
| 2855 | /* force LE */ |
| 2856 | iorder[0] = 0; |
| 2857 | iorder[1] = 1; |
| 2858 | iorder[2] = 2; |
| 2859 | iorder[3] = 3; |
| 2860 | } |
| 2861 | else if (byteorder == 1) { |
| 2862 | /* force BE */ |
| 2863 | iorder[0] = 3; |
| 2864 | iorder[1] = 2; |
| 2865 | iorder[2] = 1; |
| 2866 | iorder[3] = 0; |
| 2867 | } |
| 2868 | |
| 2869 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2870 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2871 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2872 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2873 | Py_UCS4 ch2 = *s; |
| 2874 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2875 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2876 | s++; |
| 2877 | size--; |
| 2878 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2879 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2880 | #endif |
| 2881 | STORECHAR(ch); |
| 2882 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2883 | |
| 2884 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2885 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2886 | #undef STORECHAR |
| 2887 | } |
| 2888 | |
| 2889 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2890 | { |
| 2891 | if (!PyUnicode_Check(unicode)) { |
| 2892 | PyErr_BadArgument(); |
| 2893 | return NULL; |
| 2894 | } |
| 2895 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2896 | PyUnicode_GET_SIZE(unicode), |
| 2897 | NULL, |
| 2898 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2901 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2902 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2903 | PyObject * |
| 2904 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2905 | Py_ssize_t size, |
| 2906 | const char *errors, |
| 2907 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2908 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2909 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2910 | } |
| 2911 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2912 | /* Two masks for fast checking of whether a C 'long' may contain |
| 2913 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 2914 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 2915 | rare in most input. |
| 2916 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 2917 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2918 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2919 | #if (SIZEOF_LONG == 8) |
| 2920 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 2921 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 2922 | #elif (SIZEOF_LONG == 4) |
| 2923 | # define FAST_CHAR_MASK 0x80008000L |
| 2924 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 2925 | #else |
| 2926 | # error C 'long' size should be either 4 or 8! |
| 2927 | #endif |
| 2928 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2929 | PyObject * |
| 2930 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2931 | Py_ssize_t size, |
| 2932 | const char *errors, |
| 2933 | int *byteorder, |
| 2934 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2935 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2936 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2937 | Py_ssize_t startinpos; |
| 2938 | Py_ssize_t endinpos; |
| 2939 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2940 | PyUnicodeObject *unicode; |
| 2941 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2942 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2943 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2944 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2945 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2946 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2947 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2948 | int ihi = 1, ilo = 0; |
| 2949 | #else |
| 2950 | int ihi = 0, ilo = 1; |
| 2951 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2952 | PyObject *errorHandler = NULL; |
| 2953 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2954 | |
| 2955 | /* Note: size will always be longer than the resulting Unicode |
| 2956 | character count */ |
| 2957 | unicode = _PyUnicode_New(size); |
| 2958 | if (!unicode) |
| 2959 | return NULL; |
| 2960 | if (size == 0) |
| 2961 | return (PyObject *)unicode; |
| 2962 | |
| 2963 | /* Unpack UTF-16 encoded data */ |
| 2964 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2965 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2966 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | |
| 2968 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2969 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2970 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2971 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2972 | byte order setting accordingly. In native mode, the leading BOM |
| 2973 | mark is skipped, in all other modes, it is copied to the output |
| 2974 | stream as-is (giving a ZWNBSP character). */ |
| 2975 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2976 | if (size >= 2) { |
| 2977 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2978 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2979 | if (bom == 0xFEFF) { |
| 2980 | q += 2; |
| 2981 | bo = -1; |
| 2982 | } |
| 2983 | else if (bom == 0xFFFE) { |
| 2984 | q += 2; |
| 2985 | bo = 1; |
| 2986 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2987 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2988 | if (bom == 0xFEFF) { |
| 2989 | q += 2; |
| 2990 | bo = 1; |
| 2991 | } |
| 2992 | else if (bom == 0xFFFE) { |
| 2993 | q += 2; |
| 2994 | bo = -1; |
| 2995 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2996 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2997 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2998 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2999 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3000 | if (bo == -1) { |
| 3001 | /* force LE */ |
| 3002 | ihi = 1; |
| 3003 | ilo = 0; |
| 3004 | } |
| 3005 | else if (bo == 1) { |
| 3006 | /* force BE */ |
| 3007 | ihi = 0; |
| 3008 | ilo = 1; |
| 3009 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3010 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3011 | native_ordering = ilo < ihi; |
| 3012 | #else |
| 3013 | native_ordering = ilo > ihi; |
| 3014 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3015 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3016 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3017 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3018 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3019 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3020 | reads are more expensive, better to defer to another iteration. */ |
| 3021 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3022 | /* Fast path for runs of non-surrogate chars. */ |
| 3023 | register const unsigned char *_q = q; |
| 3024 | Py_UNICODE *_p = p; |
| 3025 | if (native_ordering) { |
| 3026 | /* Native ordering is simple: as long as the input cannot |
| 3027 | possibly contain a surrogate char, do an unrolled copy |
| 3028 | of several 16-bit code points to the target object. |
| 3029 | The non-surrogate check is done on several input bytes |
| 3030 | at a time (as many as a C 'long' can contain). */ |
| 3031 | while (_q < aligned_end) { |
| 3032 | unsigned long data = * (unsigned long *) _q; |
| 3033 | if (data & FAST_CHAR_MASK) |
| 3034 | break; |
| 3035 | _p[0] = ((unsigned short *) _q)[0]; |
| 3036 | _p[1] = ((unsigned short *) _q)[1]; |
| 3037 | #if (SIZEOF_LONG == 8) |
| 3038 | _p[2] = ((unsigned short *) _q)[2]; |
| 3039 | _p[3] = ((unsigned short *) _q)[3]; |
| 3040 | #endif |
| 3041 | _q += SIZEOF_LONG; |
| 3042 | _p += SIZEOF_LONG / 2; |
| 3043 | } |
| 3044 | } |
| 3045 | else { |
| 3046 | /* Byteswapped ordering is similar, but we must decompose |
| 3047 | the copy bytewise, and take care of zero'ing out the |
| 3048 | upper bytes if the target object is in 32-bit units |
| 3049 | (that is, in UCS-4 builds). */ |
| 3050 | while (_q < aligned_end) { |
| 3051 | unsigned long data = * (unsigned long *) _q; |
| 3052 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3053 | break; |
| 3054 | /* Zero upper bytes in UCS-4 builds */ |
| 3055 | #if (Py_UNICODE_SIZE > 2) |
| 3056 | _p[0] = 0; |
| 3057 | _p[1] = 0; |
| 3058 | #if (SIZEOF_LONG == 8) |
| 3059 | _p[2] = 0; |
| 3060 | _p[3] = 0; |
| 3061 | #endif |
| 3062 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3063 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3064 | fill the two last bytes of each 4-byte unit. */ |
| 3065 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3066 | # define OFF 2 |
| 3067 | #else |
| 3068 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3069 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3070 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3071 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3072 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3073 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3074 | #if (SIZEOF_LONG == 8) |
| 3075 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3076 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3077 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3078 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3079 | #endif |
| 3080 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3081 | _q += SIZEOF_LONG; |
| 3082 | _p += SIZEOF_LONG / 2; |
| 3083 | } |
| 3084 | } |
| 3085 | p = _p; |
| 3086 | q = _q; |
| 3087 | if (q >= e) |
| 3088 | break; |
| 3089 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3090 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3091 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3092 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3093 | |
| 3094 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3095 | *p++ = ch; |
| 3096 | continue; |
| 3097 | } |
| 3098 | |
| 3099 | /* UTF-16 code pair: */ |
| 3100 | if (q > e) { |
| 3101 | errmsg = "unexpected end of data"; |
| 3102 | startinpos = (((const char *)q) - 2) - starts; |
| 3103 | endinpos = ((const char *)e) + 1 - starts; |
| 3104 | goto utf16Error; |
| 3105 | } |
| 3106 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3107 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3108 | q += 2; |
| 3109 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3110 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3111 | *p++ = ch; |
| 3112 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3113 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3114 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3115 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3116 | continue; |
| 3117 | } |
| 3118 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3119 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3120 | startinpos = (((const char *)q)-4)-starts; |
| 3121 | endinpos = startinpos+2; |
| 3122 | goto utf16Error; |
| 3123 | } |
| 3124 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3125 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3126 | errmsg = "illegal encoding"; |
| 3127 | startinpos = (((const char *)q)-2)-starts; |
| 3128 | endinpos = startinpos+2; |
| 3129 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3130 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3131 | utf16Error: |
| 3132 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3133 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3134 | errors, |
| 3135 | &errorHandler, |
| 3136 | "utf16", errmsg, |
| 3137 | &starts, |
| 3138 | (const char **)&e, |
| 3139 | &startinpos, |
| 3140 | &endinpos, |
| 3141 | &exc, |
| 3142 | (const char **)&q, |
| 3143 | &unicode, |
| 3144 | &outpos, |
| 3145 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3146 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3147 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3148 | /* remaining byte at the end? (size should be even) */ |
| 3149 | if (e == q) { |
| 3150 | if (!consumed) { |
| 3151 | errmsg = "truncated data"; |
| 3152 | startinpos = ((const char *)q) - starts; |
| 3153 | endinpos = ((const char *)e) + 1 - starts; |
| 3154 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3155 | if (unicode_decode_call_errorhandler( |
| 3156 | errors, |
| 3157 | &errorHandler, |
| 3158 | "utf16", errmsg, |
| 3159 | &starts, |
| 3160 | (const char **)&e, |
| 3161 | &startinpos, |
| 3162 | &endinpos, |
| 3163 | &exc, |
| 3164 | (const char **)&q, |
| 3165 | &unicode, |
| 3166 | &outpos, |
| 3167 | &p)) |
| 3168 | goto onError; |
| 3169 | /* The remaining input chars are ignored if the callback |
| 3170 | chooses to skip the input */ |
| 3171 | } |
| 3172 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3173 | |
| 3174 | if (byteorder) |
| 3175 | *byteorder = bo; |
| 3176 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3177 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3178 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3180 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3181 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3182 | goto onError; |
| 3183 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3184 | Py_XDECREF(errorHandler); |
| 3185 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3186 | return (PyObject *)unicode; |
| 3187 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3188 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3189 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3190 | Py_XDECREF(errorHandler); |
| 3191 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3192 | return NULL; |
| 3193 | } |
| 3194 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3195 | #undef FAST_CHAR_MASK |
| 3196 | #undef SWAPPED_FAST_CHAR_MASK |
| 3197 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3198 | PyObject * |
| 3199 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3200 | Py_ssize_t size, |
| 3201 | const char *errors, |
| 3202 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3203 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3204 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3205 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3206 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3207 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3208 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3209 | #else |
| 3210 | const int pairs = 0; |
| 3211 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3212 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3213 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3214 | int ihi = 1, ilo = 0; |
| 3215 | #else |
| 3216 | int ihi = 0, ilo = 1; |
| 3217 | #endif |
| 3218 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3219 | #define STORECHAR(CH) \ |
| 3220 | do { \ |
| 3221 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3222 | p[ilo] = (CH) & 0xff; \ |
| 3223 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3224 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3225 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3226 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3227 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3228 | if (s[i] >= 0x10000) |
| 3229 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3230 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3231 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3232 | if (size > PY_SSIZE_T_MAX || |
| 3233 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3234 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3235 | nsize = size + pairs + (byteorder == 0); |
| 3236 | bytesize = nsize * 2; |
| 3237 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3238 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3239 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3240 | if (v == NULL) |
| 3241 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3242 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3243 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3244 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3245 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3246 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3247 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3248 | |
| 3249 | if (byteorder == -1) { |
| 3250 | /* force LE */ |
| 3251 | ihi = 1; |
| 3252 | ilo = 0; |
| 3253 | } |
| 3254 | else if (byteorder == 1) { |
| 3255 | /* force BE */ |
| 3256 | ihi = 0; |
| 3257 | ilo = 1; |
| 3258 | } |
| 3259 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3260 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3261 | Py_UNICODE ch = *s++; |
| 3262 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3263 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3264 | if (ch >= 0x10000) { |
| 3265 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3266 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3267 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3268 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3269 | STORECHAR(ch); |
| 3270 | if (ch2) |
| 3271 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3272 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3273 | |
| 3274 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3275 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3276 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3280 | { |
| 3281 | if (!PyUnicode_Check(unicode)) { |
| 3282 | PyErr_BadArgument(); |
| 3283 | return NULL; |
| 3284 | } |
| 3285 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3286 | PyUnicode_GET_SIZE(unicode), |
| 3287 | NULL, |
| 3288 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3292 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3293 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3294 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3295 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3296 | Py_ssize_t size, |
| 3297 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3298 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3299 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3300 | Py_ssize_t startinpos; |
| 3301 | Py_ssize_t endinpos; |
| 3302 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3303 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3304 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3305 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3306 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3307 | char* message; |
| 3308 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3309 | PyObject *errorHandler = NULL; |
| 3310 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3311 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3312 | /* Escaped strings will always be longer than the resulting |
| 3313 | 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] | 3314 | length after conversion to the true value. |
| 3315 | (but if the error callback returns a long replacement string |
| 3316 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 | v = _PyUnicode_New(size); |
| 3318 | if (v == NULL) |
| 3319 | goto onError; |
| 3320 | if (size == 0) |
| 3321 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3322 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3323 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3324 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3325 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3326 | while (s < end) { |
| 3327 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3328 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3329 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3330 | |
| 3331 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3332 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3333 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3334 | continue; |
| 3335 | } |
| 3336 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3337 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3338 | /* \ - Escapes */ |
| 3339 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3340 | c = *s++; |
| 3341 | if (s > end) |
| 3342 | c = '\0'; /* Invalid after \ */ |
| 3343 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3344 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3345 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3346 | case '\n': break; |
| 3347 | case '\\': *p++ = '\\'; break; |
| 3348 | case '\'': *p++ = '\''; break; |
| 3349 | case '\"': *p++ = '\"'; break; |
| 3350 | case 'b': *p++ = '\b'; break; |
| 3351 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3352 | case 't': *p++ = '\t'; break; |
| 3353 | case 'n': *p++ = '\n'; break; |
| 3354 | case 'r': *p++ = '\r'; break; |
| 3355 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3356 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3357 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3358 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3359 | case '0': case '1': case '2': case '3': |
| 3360 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3361 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3362 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3363 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3364 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3365 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3366 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3367 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3368 | break; |
| 3369 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3370 | /* hex escapes */ |
| 3371 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3372 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3373 | digits = 2; |
| 3374 | message = "truncated \\xXX escape"; |
| 3375 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3376 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3377 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3379 | digits = 4; |
| 3380 | message = "truncated \\uXXXX escape"; |
| 3381 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3382 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3383 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3384 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3385 | digits = 8; |
| 3386 | message = "truncated \\UXXXXXXXX escape"; |
| 3387 | hexescape: |
| 3388 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3389 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3390 | if (s+digits>end) { |
| 3391 | endinpos = size; |
| 3392 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3393 | errors, &errorHandler, |
| 3394 | "unicodeescape", "end of string in escape sequence", |
| 3395 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3396 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3397 | goto onError; |
| 3398 | goto nextByte; |
| 3399 | } |
| 3400 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3401 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3402 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3403 | endinpos = (s+i+1)-starts; |
| 3404 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3405 | errors, &errorHandler, |
| 3406 | "unicodeescape", message, |
| 3407 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3408 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3409 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3410 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3411 | } |
| 3412 | chr = (chr<<4) & ~0xF; |
| 3413 | if (c >= '0' && c <= '9') |
| 3414 | chr += c - '0'; |
| 3415 | else if (c >= 'a' && c <= 'f') |
| 3416 | chr += 10 + c - 'a'; |
| 3417 | else |
| 3418 | chr += 10 + c - 'A'; |
| 3419 | } |
| 3420 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3421 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3422 | /* _decoding_error will have already written into the |
| 3423 | target buffer. */ |
| 3424 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3425 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3426 | /* when we get here, chr is a 32-bit unicode character */ |
| 3427 | if (chr <= 0xffff) |
| 3428 | /* UCS-2 character */ |
| 3429 | *p++ = (Py_UNICODE) chr; |
| 3430 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3431 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3432 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3433 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3434 | *p++ = chr; |
| 3435 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3436 | chr -= 0x10000L; |
| 3437 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3438 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3439 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3440 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3441 | endinpos = s-starts; |
| 3442 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3443 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3444 | errors, &errorHandler, |
| 3445 | "unicodeescape", "illegal Unicode character", |
| 3446 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3447 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3448 | goto onError; |
| 3449 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3450 | break; |
| 3451 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3452 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3453 | case 'N': |
| 3454 | message = "malformed \\N character escape"; |
| 3455 | if (ucnhash_CAPI == NULL) { |
| 3456 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3457 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3458 | if (ucnhash_CAPI == NULL) |
| 3459 | goto ucnhashError; |
| 3460 | } |
| 3461 | if (*s == '{') { |
| 3462 | const char *start = s+1; |
| 3463 | /* look for the closing brace */ |
| 3464 | while (*s != '}' && s < end) |
| 3465 | s++; |
| 3466 | if (s > start && s < end && *s == '}') { |
| 3467 | /* found a name. look it up in the unicode database */ |
| 3468 | message = "unknown Unicode character name"; |
| 3469 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3470 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3471 | goto store; |
| 3472 | } |
| 3473 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3474 | endinpos = s-starts; |
| 3475 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3476 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3477 | errors, &errorHandler, |
| 3478 | "unicodeescape", message, |
| 3479 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3480 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3481 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3482 | break; |
| 3483 | |
| 3484 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3485 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3486 | message = "\\ at end of string"; |
| 3487 | s--; |
| 3488 | endinpos = s-starts; |
| 3489 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3490 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3491 | errors, &errorHandler, |
| 3492 | "unicodeescape", message, |
| 3493 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3494 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3495 | goto onError; |
| 3496 | } |
| 3497 | else { |
| 3498 | *p++ = '\\'; |
| 3499 | *p++ = (unsigned char)s[-1]; |
| 3500 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3501 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3502 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3503 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3504 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3505 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3506 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3507 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3508 | Py_XDECREF(errorHandler); |
| 3509 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3510 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3511 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3512 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3513 | PyErr_SetString( |
| 3514 | PyExc_UnicodeError, |
| 3515 | "\\N escapes not supported (can't load unicodedata module)" |
| 3516 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3517 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3518 | Py_XDECREF(errorHandler); |
| 3519 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3520 | return NULL; |
| 3521 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3522 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3523 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3524 | Py_XDECREF(errorHandler); |
| 3525 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3526 | return NULL; |
| 3527 | } |
| 3528 | |
| 3529 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3530 | |
| 3531 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3532 | appropriate. |
| 3533 | |
| 3534 | */ |
| 3535 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3536 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3537 | Py_ssize_t size, |
| 3538 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3539 | { |
| 3540 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3541 | |
| 3542 | while (size-- > 0) { |
| 3543 | if (*s == ch) |
| 3544 | return s; |
| 3545 | s++; |
| 3546 | } |
| 3547 | |
| 3548 | return NULL; |
| 3549 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3550 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3551 | static const char *hexdigits = "0123456789abcdef"; |
| 3552 | |
| 3553 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3554 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3555 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3556 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3557 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3558 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3559 | #ifdef Py_UNICODE_WIDE |
| 3560 | const Py_ssize_t expandsize = 10; |
| 3561 | #else |
| 3562 | const Py_ssize_t expandsize = 6; |
| 3563 | #endif |
| 3564 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3565 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3566 | better to choose a different scheme. Perhaps scan the |
| 3567 | first N-chars of the string and allocate based on that size. |
| 3568 | */ |
| 3569 | /* Initial allocation is based on the longest-possible unichr |
| 3570 | escape. |
| 3571 | |
| 3572 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3573 | unichr, so in this case it's the longest unichr escape. In |
| 3574 | narrow (UTF-16) builds this is five chars per source unichr |
| 3575 | since there are two unichrs in the surrogate pair, so in narrow |
| 3576 | (UTF-16) builds it's not the longest unichr escape. |
| 3577 | |
| 3578 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3579 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3580 | escape. |
| 3581 | */ |
| 3582 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3583 | if (size == 0) |
| 3584 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3585 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3586 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3587 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3588 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3589 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3590 | 2 |
| 3591 | + expandsize*size |
| 3592 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3593 | if (repr == NULL) |
| 3594 | return NULL; |
| 3595 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3596 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3597 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3598 | while (size-- > 0) { |
| 3599 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3600 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3601 | /* Escape backslashes */ |
| 3602 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3603 | *p++ = '\\'; |
| 3604 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3605 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3606 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3607 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3608 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3609 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3610 | else if (ch >= 0x10000) { |
| 3611 | *p++ = '\\'; |
| 3612 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3613 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3614 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3615 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3616 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3617 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3618 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3619 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3620 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3621 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3622 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3623 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3624 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3625 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3626 | Py_UNICODE ch2; |
| 3627 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3628 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3629 | ch2 = *s++; |
| 3630 | size--; |
| 3631 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3632 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3633 | *p++ = '\\'; |
| 3634 | *p++ = 'U'; |
| 3635 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3636 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3637 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3638 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3639 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3640 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3641 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3642 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3643 | continue; |
| 3644 | } |
| 3645 | /* Fall through: isolated surrogates are copied as-is */ |
| 3646 | s--; |
| 3647 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3648 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3649 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3650 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3651 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3652 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3653 | *p++ = '\\'; |
| 3654 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3655 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3656 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3657 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3658 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3659 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3660 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3661 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3662 | else if (ch == '\t') { |
| 3663 | *p++ = '\\'; |
| 3664 | *p++ = 't'; |
| 3665 | } |
| 3666 | else if (ch == '\n') { |
| 3667 | *p++ = '\\'; |
| 3668 | *p++ = 'n'; |
| 3669 | } |
| 3670 | else if (ch == '\r') { |
| 3671 | *p++ = '\\'; |
| 3672 | *p++ = 'r'; |
| 3673 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3674 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3675 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3676 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3677 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3678 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3679 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3680 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3681 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3683 | /* Copy everything else as-is */ |
| 3684 | else |
| 3685 | *p++ = (char) ch; |
| 3686 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3687 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3688 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3689 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3690 | return NULL; |
| 3691 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3694 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3696 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3697 | if (!PyUnicode_Check(unicode)) { |
| 3698 | PyErr_BadArgument(); |
| 3699 | return NULL; |
| 3700 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3701 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3702 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3703 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3707 | |
| 3708 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3709 | Py_ssize_t size, |
| 3710 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3711 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3712 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3713 | Py_ssize_t startinpos; |
| 3714 | Py_ssize_t endinpos; |
| 3715 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3716 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3717 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3718 | const char *end; |
| 3719 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3720 | PyObject *errorHandler = NULL; |
| 3721 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3723 | /* Escaped strings will always be longer than the resulting |
| 3724 | 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] | 3725 | length after conversion to the true value. (But decoding error |
| 3726 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3727 | v = _PyUnicode_New(size); |
| 3728 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3729 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3730 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3731 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3732 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3733 | end = s + size; |
| 3734 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3735 | unsigned char c; |
| 3736 | Py_UCS4 x; |
| 3737 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3738 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3739 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3740 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3741 | if (*s != '\\') { |
| 3742 | *p++ = (unsigned char)*s++; |
| 3743 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3744 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3745 | startinpos = s-starts; |
| 3746 | |
| 3747 | /* \u-escapes are only interpreted iff the number of leading |
| 3748 | backslashes if odd */ |
| 3749 | bs = s; |
| 3750 | for (;s < end;) { |
| 3751 | if (*s != '\\') |
| 3752 | break; |
| 3753 | *p++ = (unsigned char)*s++; |
| 3754 | } |
| 3755 | if (((s - bs) & 1) == 0 || |
| 3756 | s >= end || |
| 3757 | (*s != 'u' && *s != 'U')) { |
| 3758 | continue; |
| 3759 | } |
| 3760 | p--; |
| 3761 | count = *s=='u' ? 4 : 8; |
| 3762 | s++; |
| 3763 | |
| 3764 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3765 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3766 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3767 | c = (unsigned char)*s; |
| 3768 | if (!ISXDIGIT(c)) { |
| 3769 | endinpos = s-starts; |
| 3770 | if (unicode_decode_call_errorhandler( |
| 3771 | errors, &errorHandler, |
| 3772 | "rawunicodeescape", "truncated \\uXXXX", |
| 3773 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3774 | &v, &outpos, &p)) |
| 3775 | goto onError; |
| 3776 | goto nextByte; |
| 3777 | } |
| 3778 | x = (x<<4) & ~0xF; |
| 3779 | if (c >= '0' && c <= '9') |
| 3780 | x += c - '0'; |
| 3781 | else if (c >= 'a' && c <= 'f') |
| 3782 | x += 10 + c - 'a'; |
| 3783 | else |
| 3784 | x += 10 + c - 'A'; |
| 3785 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3786 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3787 | /* UCS-2 character */ |
| 3788 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3789 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3790 | /* UCS-4 character. Either store directly, or as |
| 3791 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3792 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3793 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3794 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3795 | x -= 0x10000L; |
| 3796 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3797 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3798 | #endif |
| 3799 | } else { |
| 3800 | endinpos = s-starts; |
| 3801 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3802 | if (unicode_decode_call_errorhandler( |
| 3803 | errors, &errorHandler, |
| 3804 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3805 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3806 | &v, &outpos, &p)) |
| 3807 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3808 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3809 | nextByte: |
| 3810 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3811 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3812 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3813 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3814 | Py_XDECREF(errorHandler); |
| 3815 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3816 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3817 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3818 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3819 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3820 | Py_XDECREF(errorHandler); |
| 3821 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3822 | return NULL; |
| 3823 | } |
| 3824 | |
| 3825 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3826 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3827 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3828 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3829 | char *p; |
| 3830 | char *q; |
| 3831 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3832 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3833 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3834 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3835 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3836 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3837 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3838 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3839 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3840 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3841 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3842 | if (repr == NULL) |
| 3843 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3844 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3845 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3846 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3847 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3848 | while (size-- > 0) { |
| 3849 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3850 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3851 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3852 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3853 | *p++ = '\\'; |
| 3854 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3855 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3856 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3857 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3858 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3859 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3860 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3861 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3862 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3863 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3864 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3865 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3866 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3867 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3868 | Py_UNICODE ch2; |
| 3869 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3870 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3871 | ch2 = *s++; |
| 3872 | size--; |
| 3873 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3874 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3875 | *p++ = '\\'; |
| 3876 | *p++ = 'U'; |
| 3877 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3878 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3879 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3880 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3881 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3882 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3883 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3884 | *p++ = hexdigits[ucs & 0xf]; |
| 3885 | continue; |
| 3886 | } |
| 3887 | /* Fall through: isolated surrogates are copied as-is */ |
| 3888 | s--; |
| 3889 | size++; |
| 3890 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3891 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3892 | /* Map 16-bit characters to '\uxxxx' */ |
| 3893 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3894 | *p++ = '\\'; |
| 3895 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3896 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3897 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3898 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3899 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3900 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3901 | /* Copy everything else as-is */ |
| 3902 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3903 | *p++ = (char) ch; |
| 3904 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3905 | size = p - q; |
| 3906 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3907 | assert(size > 0); |
| 3908 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3909 | return NULL; |
| 3910 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3914 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3915 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3916 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3917 | PyErr_BadArgument(); |
| 3918 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3919 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3920 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3921 | PyUnicode_GET_SIZE(unicode)); |
| 3922 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3923 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3926 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3927 | |
| 3928 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3929 | Py_ssize_t size, |
| 3930 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3931 | { |
| 3932 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3933 | Py_ssize_t startinpos; |
| 3934 | Py_ssize_t endinpos; |
| 3935 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3936 | PyUnicodeObject *v; |
| 3937 | Py_UNICODE *p; |
| 3938 | const char *end; |
| 3939 | const char *reason; |
| 3940 | PyObject *errorHandler = NULL; |
| 3941 | PyObject *exc = NULL; |
| 3942 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3943 | #ifdef Py_UNICODE_WIDE |
| 3944 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3945 | #endif |
| 3946 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3947 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3948 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3949 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3950 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3951 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3952 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3953 | p = PyUnicode_AS_UNICODE(v); |
| 3954 | end = s + size; |
| 3955 | |
| 3956 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3957 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3958 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3959 | some malformed UCS-4 data. */ |
| 3960 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3961 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3962 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3963 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3964 | end-s < Py_UNICODE_SIZE |
| 3965 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3966 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3967 | startinpos = s - starts; |
| 3968 | if (end-s < Py_UNICODE_SIZE) { |
| 3969 | endinpos = end-starts; |
| 3970 | reason = "truncated input"; |
| 3971 | } |
| 3972 | else { |
| 3973 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3974 | reason = "illegal code point (> 0x10FFFF)"; |
| 3975 | } |
| 3976 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3977 | if (unicode_decode_call_errorhandler( |
| 3978 | errors, &errorHandler, |
| 3979 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3980 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3981 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3982 | goto onError; |
| 3983 | } |
| 3984 | } |
| 3985 | else { |
| 3986 | p++; |
| 3987 | s += Py_UNICODE_SIZE; |
| 3988 | } |
| 3989 | } |
| 3990 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3991 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3992 | goto onError; |
| 3993 | Py_XDECREF(errorHandler); |
| 3994 | Py_XDECREF(exc); |
| 3995 | return (PyObject *)v; |
| 3996 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3997 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3998 | Py_XDECREF(v); |
| 3999 | Py_XDECREF(errorHandler); |
| 4000 | Py_XDECREF(exc); |
| 4001 | return NULL; |
| 4002 | } |
| 4003 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4004 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4005 | |
| 4006 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4007 | Py_ssize_t size, |
| 4008 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4009 | { |
| 4010 | PyUnicodeObject *v; |
| 4011 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4012 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4013 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4014 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4015 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4016 | Py_UNICODE r = *(unsigned char*)s; |
| 4017 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4020 | v = _PyUnicode_New(size); |
| 4021 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4022 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4023 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4024 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4025 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4026 | e = s + size; |
| 4027 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4028 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4029 | unrolled_end = e - 4; |
| 4030 | while (s < unrolled_end) { |
| 4031 | p[0] = (unsigned char) s[0]; |
| 4032 | p[1] = (unsigned char) s[1]; |
| 4033 | p[2] = (unsigned char) s[2]; |
| 4034 | p[3] = (unsigned char) s[3]; |
| 4035 | s += 4; |
| 4036 | p += 4; |
| 4037 | } |
| 4038 | while (s < e) |
| 4039 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4040 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4041 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4042 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4043 | Py_XDECREF(v); |
| 4044 | return NULL; |
| 4045 | } |
| 4046 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4047 | /* create or adjust a UnicodeEncodeError */ |
| 4048 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4049 | const char *encoding, |
| 4050 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4051 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4052 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4053 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4054 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4055 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4056 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4057 | } |
| 4058 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4059 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4060 | goto onError; |
| 4061 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4062 | goto onError; |
| 4063 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4064 | goto onError; |
| 4065 | return; |
| 4066 | onError: |
| 4067 | Py_DECREF(*exceptionObject); |
| 4068 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | } |
| 4070 | } |
| 4071 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4072 | /* raises a UnicodeEncodeError */ |
| 4073 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4074 | const char *encoding, |
| 4075 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4076 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4077 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4078 | { |
| 4079 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4080 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4081 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4082 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | /* error handling callback helper: |
| 4086 | build arguments, call the callback and check the arguments, |
| 4087 | put the result into newpos and return the replacement string, which |
| 4088 | has to be freed by the caller */ |
| 4089 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4090 | PyObject **errorHandler, |
| 4091 | const char *encoding, const char *reason, |
| 4092 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4093 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4094 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4095 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4096 | 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] | 4097 | |
| 4098 | PyObject *restuple; |
| 4099 | PyObject *resunicode; |
| 4100 | |
| 4101 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4102 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4103 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4104 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4108 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4109 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4110 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4111 | |
| 4112 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4113 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4114 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4115 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4116 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4117 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4118 | Py_DECREF(restuple); |
| 4119 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4120 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4121 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4122 | &resunicode, newpos)) { |
| 4123 | Py_DECREF(restuple); |
| 4124 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4125 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4126 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4127 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4128 | Py_DECREF(restuple); |
| 4129 | return NULL; |
| 4130 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4131 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4132 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4133 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4134 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4135 | Py_DECREF(restuple); |
| 4136 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4137 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4138 | Py_INCREF(resunicode); |
| 4139 | Py_DECREF(restuple); |
| 4140 | return resunicode; |
| 4141 | } |
| 4142 | |
| 4143 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4144 | Py_ssize_t size, |
| 4145 | const char *errors, |
| 4146 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4147 | { |
| 4148 | /* output object */ |
| 4149 | PyObject *res; |
| 4150 | /* pointers to the beginning and end+1 of input */ |
| 4151 | const Py_UNICODE *startp = p; |
| 4152 | const Py_UNICODE *endp = p + size; |
| 4153 | /* pointer to the beginning of the unencodable characters */ |
| 4154 | /* const Py_UNICODE *badp = NULL; */ |
| 4155 | /* pointer into the output */ |
| 4156 | char *str; |
| 4157 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4158 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4159 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4160 | 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] | 4161 | PyObject *errorHandler = NULL; |
| 4162 | PyObject *exc = NULL; |
| 4163 | /* the following variable is used for caching string comparisons |
| 4164 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4165 | int known_errorHandler = -1; |
| 4166 | |
| 4167 | /* allocate enough for a simple encoding without |
| 4168 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4169 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4170 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4171 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4172 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4173 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4174 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4175 | ressize = size; |
| 4176 | |
| 4177 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4178 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4179 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4180 | /* can we encode this? */ |
| 4181 | if (c<limit) { |
| 4182 | /* no overflow check, because we know that the space is enough */ |
| 4183 | *str++ = (char)c; |
| 4184 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4185 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4186 | else { |
| 4187 | Py_ssize_t unicodepos = p-startp; |
| 4188 | Py_ssize_t requiredsize; |
| 4189 | PyObject *repunicode; |
| 4190 | Py_ssize_t repsize; |
| 4191 | Py_ssize_t newpos; |
| 4192 | Py_ssize_t respos; |
| 4193 | Py_UNICODE *uni2; |
| 4194 | /* startpos for collecting unencodable chars */ |
| 4195 | const Py_UNICODE *collstart = p; |
| 4196 | const Py_UNICODE *collend = p; |
| 4197 | /* find all unecodable characters */ |
| 4198 | while ((collend < endp) && ((*collend)>=limit)) |
| 4199 | ++collend; |
| 4200 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4201 | if (known_errorHandler==-1) { |
| 4202 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4203 | known_errorHandler = 1; |
| 4204 | else if (!strcmp(errors, "replace")) |
| 4205 | known_errorHandler = 2; |
| 4206 | else if (!strcmp(errors, "ignore")) |
| 4207 | known_errorHandler = 3; |
| 4208 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4209 | known_errorHandler = 4; |
| 4210 | else |
| 4211 | known_errorHandler = 0; |
| 4212 | } |
| 4213 | switch (known_errorHandler) { |
| 4214 | case 1: /* strict */ |
| 4215 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4216 | goto onError; |
| 4217 | case 2: /* replace */ |
| 4218 | while (collstart++<collend) |
| 4219 | *str++ = '?'; /* fall through */ |
| 4220 | case 3: /* ignore */ |
| 4221 | p = collend; |
| 4222 | break; |
| 4223 | case 4: /* xmlcharrefreplace */ |
| 4224 | respos = str - PyBytes_AS_STRING(res); |
| 4225 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4226 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4227 | if (*p<10) |
| 4228 | repsize += 2+1+1; |
| 4229 | else if (*p<100) |
| 4230 | repsize += 2+2+1; |
| 4231 | else if (*p<1000) |
| 4232 | repsize += 2+3+1; |
| 4233 | else if (*p<10000) |
| 4234 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4235 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4236 | else |
| 4237 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4238 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4239 | else if (*p<100000) |
| 4240 | repsize += 2+5+1; |
| 4241 | else if (*p<1000000) |
| 4242 | repsize += 2+6+1; |
| 4243 | else |
| 4244 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4245 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4246 | } |
| 4247 | requiredsize = respos+repsize+(endp-collend); |
| 4248 | if (requiredsize > ressize) { |
| 4249 | if (requiredsize<2*ressize) |
| 4250 | requiredsize = 2*ressize; |
| 4251 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4252 | goto onError; |
| 4253 | str = PyBytes_AS_STRING(res) + respos; |
| 4254 | ressize = requiredsize; |
| 4255 | } |
| 4256 | /* generate replacement (temporarily (mis)uses p) */ |
| 4257 | for (p = collstart; p < collend; ++p) { |
| 4258 | str += sprintf(str, "&#%d;", (int)*p); |
| 4259 | } |
| 4260 | p = collend; |
| 4261 | break; |
| 4262 | default: |
| 4263 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4264 | encoding, reason, startp, size, &exc, |
| 4265 | collstart-startp, collend-startp, &newpos); |
| 4266 | if (repunicode == NULL) |
| 4267 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4268 | if (PyBytes_Check(repunicode)) { |
| 4269 | /* Directly copy bytes result to output. */ |
| 4270 | repsize = PyBytes_Size(repunicode); |
| 4271 | if (repsize > 1) { |
| 4272 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4273 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4274 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4275 | Py_DECREF(repunicode); |
| 4276 | goto onError; |
| 4277 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4278 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4279 | ressize += repsize-1; |
| 4280 | } |
| 4281 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4282 | str += repsize; |
| 4283 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4284 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4285 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4286 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4287 | /* need more space? (at least enough for what we |
| 4288 | have+the replacement+the rest of the string, so |
| 4289 | we won't have to check space for encodable characters) */ |
| 4290 | respos = str - PyBytes_AS_STRING(res); |
| 4291 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4292 | requiredsize = respos+repsize+(endp-collend); |
| 4293 | if (requiredsize > ressize) { |
| 4294 | if (requiredsize<2*ressize) |
| 4295 | requiredsize = 2*ressize; |
| 4296 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4297 | Py_DECREF(repunicode); |
| 4298 | goto onError; |
| 4299 | } |
| 4300 | str = PyBytes_AS_STRING(res) + respos; |
| 4301 | ressize = requiredsize; |
| 4302 | } |
| 4303 | /* check if there is anything unencodable in the replacement |
| 4304 | and copy it to the output */ |
| 4305 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4306 | c = *uni2; |
| 4307 | if (c >= limit) { |
| 4308 | raise_encode_exception(&exc, encoding, startp, size, |
| 4309 | unicodepos, unicodepos+1, reason); |
| 4310 | Py_DECREF(repunicode); |
| 4311 | goto onError; |
| 4312 | } |
| 4313 | *str = (char)c; |
| 4314 | } |
| 4315 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4316 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4317 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4318 | } |
| 4319 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4320 | /* Resize if we allocated to much */ |
| 4321 | size = str - PyBytes_AS_STRING(res); |
| 4322 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4323 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4324 | if (_PyBytes_Resize(&res, size) < 0) |
| 4325 | goto onError; |
| 4326 | } |
| 4327 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4328 | Py_XDECREF(errorHandler); |
| 4329 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4330 | return res; |
| 4331 | |
| 4332 | onError: |
| 4333 | Py_XDECREF(res); |
| 4334 | Py_XDECREF(errorHandler); |
| 4335 | Py_XDECREF(exc); |
| 4336 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4339 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4340 | Py_ssize_t size, |
| 4341 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4342 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4343 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4347 | { |
| 4348 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4349 | PyErr_BadArgument(); |
| 4350 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4351 | } |
| 4352 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4353 | PyUnicode_GET_SIZE(unicode), |
| 4354 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4358 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4359 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4360 | Py_ssize_t size, |
| 4361 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4362 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4363 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4364 | PyUnicodeObject *v; |
| 4365 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4366 | Py_ssize_t startinpos; |
| 4367 | Py_ssize_t endinpos; |
| 4368 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4369 | const char *e; |
| 4370 | PyObject *errorHandler = NULL; |
| 4371 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4373 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4374 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4375 | Py_UNICODE r = *(unsigned char*)s; |
| 4376 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4377 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4378 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4379 | v = _PyUnicode_New(size); |
| 4380 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4381 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4382 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4383 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4384 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4385 | e = s + size; |
| 4386 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4387 | register unsigned char c = (unsigned char)*s; |
| 4388 | if (c < 128) { |
| 4389 | *p++ = c; |
| 4390 | ++s; |
| 4391 | } |
| 4392 | else { |
| 4393 | startinpos = s-starts; |
| 4394 | endinpos = startinpos + 1; |
| 4395 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4396 | if (unicode_decode_call_errorhandler( |
| 4397 | errors, &errorHandler, |
| 4398 | "ascii", "ordinal not in range(128)", |
| 4399 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4400 | &v, &outpos, &p)) |
| 4401 | goto onError; |
| 4402 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4403 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4404 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4405 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4406 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4407 | Py_XDECREF(errorHandler); |
| 4408 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4409 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4410 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4411 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4412 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4413 | Py_XDECREF(errorHandler); |
| 4414 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4415 | return NULL; |
| 4416 | } |
| 4417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4418 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4419 | Py_ssize_t size, |
| 4420 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4422 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4426 | { |
| 4427 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4428 | PyErr_BadArgument(); |
| 4429 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4430 | } |
| 4431 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4432 | PyUnicode_GET_SIZE(unicode), |
| 4433 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4436 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4437 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4438 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4439 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4440 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4441 | #define NEED_RETRY |
| 4442 | #endif |
| 4443 | |
| 4444 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4445 | a) it assumes an incomplete character consists of a single byte, and |
| 4446 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4447 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4448 | |
| 4449 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4450 | { |
| 4451 | const char *curr = s + offset; |
| 4452 | |
| 4453 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4454 | const char *prev = CharPrev(s, curr); |
| 4455 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4456 | } |
| 4457 | return 0; |
| 4458 | } |
| 4459 | |
| 4460 | /* |
| 4461 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4462 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4463 | */ |
| 4464 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4465 | const char *s, /* MBCS string */ |
| 4466 | int size, /* sizeof MBCS string */ |
| 4467 | int final) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4468 | { |
| 4469 | Py_UNICODE *p; |
| 4470 | Py_ssize_t n = 0; |
| 4471 | int usize = 0; |
| 4472 | |
| 4473 | assert(size >= 0); |
| 4474 | |
| 4475 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4476 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4477 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4478 | |
| 4479 | /* First get the size of the result */ |
| 4480 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4481 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4482 | if (usize == 0) { |
| 4483 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4484 | return -1; |
| 4485 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4489 | /* Create unicode object */ |
| 4490 | *v = _PyUnicode_New(usize); |
| 4491 | if (*v == NULL) |
| 4492 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4493 | } |
| 4494 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4495 | /* Extend unicode object */ |
| 4496 | n = PyUnicode_GET_SIZE(*v); |
| 4497 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4498 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | /* Do the conversion */ |
| 4502 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4503 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4504 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4505 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4506 | return -1; |
| 4507 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | return size; |
| 4511 | } |
| 4512 | |
| 4513 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4514 | Py_ssize_t size, |
| 4515 | const char *errors, |
| 4516 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4517 | { |
| 4518 | PyUnicodeObject *v = NULL; |
| 4519 | int done; |
| 4520 | |
| 4521 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4522 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4523 | |
| 4524 | #ifdef NEED_RETRY |
| 4525 | retry: |
| 4526 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4527 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4528 | else |
| 4529 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4530 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4531 | |
| 4532 | if (done < 0) { |
| 4533 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4534 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4538 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4539 | |
| 4540 | #ifdef NEED_RETRY |
| 4541 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4542 | s += done; |
| 4543 | size -= done; |
| 4544 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4545 | } |
| 4546 | #endif |
| 4547 | |
| 4548 | return (PyObject *)v; |
| 4549 | } |
| 4550 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4551 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4552 | Py_ssize_t size, |
| 4553 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4554 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4555 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4556 | } |
| 4557 | |
| 4558 | /* |
| 4559 | * Convert unicode into string object (MBCS). |
| 4560 | * Returns 0 if succeed, -1 otherwise. |
| 4561 | */ |
| 4562 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4563 | const Py_UNICODE *p, /* unicode */ |
| 4564 | int size) /* size of unicode */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4565 | { |
| 4566 | int mbcssize = 0; |
| 4567 | Py_ssize_t n = 0; |
| 4568 | |
| 4569 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4570 | |
| 4571 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4572 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4573 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4574 | if (mbcssize == 0) { |
| 4575 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4576 | return -1; |
| 4577 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4578 | } |
| 4579 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4580 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4581 | /* Create string object */ |
| 4582 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4583 | if (*repr == NULL) |
| 4584 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4585 | } |
| 4586 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4587 | /* Extend string object */ |
| 4588 | n = PyBytes_Size(*repr); |
| 4589 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4590 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | /* Do the conversion */ |
| 4594 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4595 | char *s = PyBytes_AS_STRING(*repr) + n; |
| 4596 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4597 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4598 | return -1; |
| 4599 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4606 | Py_ssize_t size, |
| 4607 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4608 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4609 | PyObject *repr = NULL; |
| 4610 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4611 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4612 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4613 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4614 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4615 | ret = encode_mbcs(&repr, p, INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4616 | else |
| 4617 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4618 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4619 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4620 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4621 | Py_XDECREF(repr); |
| 4622 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4623 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4624 | |
| 4625 | #ifdef NEED_RETRY |
| 4626 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4627 | p += INT_MAX; |
| 4628 | size -= INT_MAX; |
| 4629 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4630 | } |
| 4631 | #endif |
| 4632 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4633 | return repr; |
| 4634 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4635 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4636 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4637 | { |
| 4638 | if (!PyUnicode_Check(unicode)) { |
| 4639 | PyErr_BadArgument(); |
| 4640 | return NULL; |
| 4641 | } |
| 4642 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4643 | PyUnicode_GET_SIZE(unicode), |
| 4644 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4647 | #undef NEED_RETRY |
| 4648 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4649 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4650 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4651 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4653 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4654 | Py_ssize_t size, |
| 4655 | PyObject *mapping, |
| 4656 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4657 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4658 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4659 | Py_ssize_t startinpos; |
| 4660 | Py_ssize_t endinpos; |
| 4661 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4662 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4663 | PyUnicodeObject *v; |
| 4664 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4665 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4666 | PyObject *errorHandler = NULL; |
| 4667 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4668 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4669 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4670 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4671 | /* Default to Latin-1 */ |
| 4672 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4673 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4674 | |
| 4675 | v = _PyUnicode_New(size); |
| 4676 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4677 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4678 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4679 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4680 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4681 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4682 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4683 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4684 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4685 | while (s < e) { |
| 4686 | unsigned char ch = *s; |
| 4687 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4688 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4689 | if (ch < maplen) |
| 4690 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4691 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4692 | if (x == 0xfffe) { |
| 4693 | /* undefined mapping */ |
| 4694 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4695 | startinpos = s-starts; |
| 4696 | endinpos = startinpos+1; |
| 4697 | if (unicode_decode_call_errorhandler( |
| 4698 | errors, &errorHandler, |
| 4699 | "charmap", "character maps to <undefined>", |
| 4700 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4701 | &v, &outpos, &p)) { |
| 4702 | goto onError; |
| 4703 | } |
| 4704 | continue; |
| 4705 | } |
| 4706 | *p++ = x; |
| 4707 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4708 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4709 | } |
| 4710 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4711 | while (s < e) { |
| 4712 | unsigned char ch = *s; |
| 4713 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4714 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4715 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4716 | w = PyLong_FromLong((long)ch); |
| 4717 | if (w == NULL) |
| 4718 | goto onError; |
| 4719 | x = PyObject_GetItem(mapping, w); |
| 4720 | Py_DECREF(w); |
| 4721 | if (x == NULL) { |
| 4722 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4723 | /* No mapping found means: mapping is undefined. */ |
| 4724 | PyErr_Clear(); |
| 4725 | x = Py_None; |
| 4726 | Py_INCREF(x); |
| 4727 | } else |
| 4728 | goto onError; |
| 4729 | } |
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 | /* Apply mapping */ |
| 4732 | if (PyLong_Check(x)) { |
| 4733 | long value = PyLong_AS_LONG(x); |
| 4734 | if (value < 0 || value > 65535) { |
| 4735 | PyErr_SetString(PyExc_TypeError, |
| 4736 | "character mapping must be in range(65536)"); |
| 4737 | Py_DECREF(x); |
| 4738 | goto onError; |
| 4739 | } |
| 4740 | *p++ = (Py_UNICODE)value; |
| 4741 | } |
| 4742 | else if (x == Py_None) { |
| 4743 | /* undefined mapping */ |
| 4744 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4745 | startinpos = s-starts; |
| 4746 | endinpos = startinpos+1; |
| 4747 | if (unicode_decode_call_errorhandler( |
| 4748 | errors, &errorHandler, |
| 4749 | "charmap", "character maps to <undefined>", |
| 4750 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4751 | &v, &outpos, &p)) { |
| 4752 | Py_DECREF(x); |
| 4753 | goto onError; |
| 4754 | } |
| 4755 | Py_DECREF(x); |
| 4756 | continue; |
| 4757 | } |
| 4758 | else if (PyUnicode_Check(x)) { |
| 4759 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4760 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4761 | if (targetsize == 1) |
| 4762 | /* 1-1 mapping */ |
| 4763 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4764 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4765 | else if (targetsize > 1) { |
| 4766 | /* 1-n mapping */ |
| 4767 | if (targetsize > extrachars) { |
| 4768 | /* resize first */ |
| 4769 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4770 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4771 | (targetsize << 2); |
| 4772 | extrachars += needed; |
| 4773 | /* XXX overflow detection missing */ |
| 4774 | if (_PyUnicode_Resize(&v, |
| 4775 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4776 | Py_DECREF(x); |
| 4777 | goto onError; |
| 4778 | } |
| 4779 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4780 | } |
| 4781 | Py_UNICODE_COPY(p, |
| 4782 | PyUnicode_AS_UNICODE(x), |
| 4783 | targetsize); |
| 4784 | p += targetsize; |
| 4785 | extrachars -= targetsize; |
| 4786 | } |
| 4787 | /* 1-0 mapping: skip the character */ |
| 4788 | } |
| 4789 | else { |
| 4790 | /* wrong return value */ |
| 4791 | PyErr_SetString(PyExc_TypeError, |
| 4792 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4793 | Py_DECREF(x); |
| 4794 | goto onError; |
| 4795 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4796 | Py_DECREF(x); |
| 4797 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4798 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4799 | } |
| 4800 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4801 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4802 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4803 | Py_XDECREF(errorHandler); |
| 4804 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4805 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4806 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4807 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4808 | Py_XDECREF(errorHandler); |
| 4809 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4810 | Py_XDECREF(v); |
| 4811 | return NULL; |
| 4812 | } |
| 4813 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4814 | /* Charmap encoding: the lookup table */ |
| 4815 | |
| 4816 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4817 | PyObject_HEAD |
| 4818 | unsigned char level1[32]; |
| 4819 | int count2, count3; |
| 4820 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4821 | }; |
| 4822 | |
| 4823 | static PyObject* |
| 4824 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4825 | { |
| 4826 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4827 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4828 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4832 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4833 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4834 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4835 | }; |
| 4836 | |
| 4837 | static void |
| 4838 | encoding_map_dealloc(PyObject* o) |
| 4839 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4840 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4844 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4845 | "EncodingMap", /*tp_name*/ |
| 4846 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4847 | 0, /*tp_itemsize*/ |
| 4848 | /* methods */ |
| 4849 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4850 | 0, /*tp_print*/ |
| 4851 | 0, /*tp_getattr*/ |
| 4852 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4853 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4854 | 0, /*tp_repr*/ |
| 4855 | 0, /*tp_as_number*/ |
| 4856 | 0, /*tp_as_sequence*/ |
| 4857 | 0, /*tp_as_mapping*/ |
| 4858 | 0, /*tp_hash*/ |
| 4859 | 0, /*tp_call*/ |
| 4860 | 0, /*tp_str*/ |
| 4861 | 0, /*tp_getattro*/ |
| 4862 | 0, /*tp_setattro*/ |
| 4863 | 0, /*tp_as_buffer*/ |
| 4864 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4865 | 0, /*tp_doc*/ |
| 4866 | 0, /*tp_traverse*/ |
| 4867 | 0, /*tp_clear*/ |
| 4868 | 0, /*tp_richcompare*/ |
| 4869 | 0, /*tp_weaklistoffset*/ |
| 4870 | 0, /*tp_iter*/ |
| 4871 | 0, /*tp_iternext*/ |
| 4872 | encoding_map_methods, /*tp_methods*/ |
| 4873 | 0, /*tp_members*/ |
| 4874 | 0, /*tp_getset*/ |
| 4875 | 0, /*tp_base*/ |
| 4876 | 0, /*tp_dict*/ |
| 4877 | 0, /*tp_descr_get*/ |
| 4878 | 0, /*tp_descr_set*/ |
| 4879 | 0, /*tp_dictoffset*/ |
| 4880 | 0, /*tp_init*/ |
| 4881 | 0, /*tp_alloc*/ |
| 4882 | 0, /*tp_new*/ |
| 4883 | 0, /*tp_free*/ |
| 4884 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4885 | }; |
| 4886 | |
| 4887 | PyObject* |
| 4888 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4889 | { |
| 4890 | Py_UNICODE *decode; |
| 4891 | PyObject *result; |
| 4892 | struct encoding_map *mresult; |
| 4893 | int i; |
| 4894 | int need_dict = 0; |
| 4895 | unsigned char level1[32]; |
| 4896 | unsigned char level2[512]; |
| 4897 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4898 | int count2 = 0, count3 = 0; |
| 4899 | |
| 4900 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4901 | PyErr_BadArgument(); |
| 4902 | return NULL; |
| 4903 | } |
| 4904 | decode = PyUnicode_AS_UNICODE(string); |
| 4905 | memset(level1, 0xFF, sizeof level1); |
| 4906 | memset(level2, 0xFF, sizeof level2); |
| 4907 | |
| 4908 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4909 | or if there are non-BMP characters, we need to use |
| 4910 | a mapping dictionary. */ |
| 4911 | if (decode[0] != 0) |
| 4912 | need_dict = 1; |
| 4913 | for (i = 1; i < 256; i++) { |
| 4914 | int l1, l2; |
| 4915 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4916 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4917 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4918 | #endif |
| 4919 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4920 | need_dict = 1; |
| 4921 | break; |
| 4922 | } |
| 4923 | if (decode[i] == 0xFFFE) |
| 4924 | /* unmapped character */ |
| 4925 | continue; |
| 4926 | l1 = decode[i] >> 11; |
| 4927 | l2 = decode[i] >> 7; |
| 4928 | if (level1[l1] == 0xFF) |
| 4929 | level1[l1] = count2++; |
| 4930 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4931 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4935 | need_dict = 1; |
| 4936 | |
| 4937 | if (need_dict) { |
| 4938 | PyObject *result = PyDict_New(); |
| 4939 | PyObject *key, *value; |
| 4940 | if (!result) |
| 4941 | return NULL; |
| 4942 | for (i = 0; i < 256; i++) { |
| 4943 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4944 | key = PyLong_FromLong(decode[i]); |
| 4945 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4946 | if (!key || !value) |
| 4947 | goto failed1; |
| 4948 | if (PyDict_SetItem(result, key, value) == -1) |
| 4949 | goto failed1; |
| 4950 | Py_DECREF(key); |
| 4951 | Py_DECREF(value); |
| 4952 | } |
| 4953 | return result; |
| 4954 | failed1: |
| 4955 | Py_XDECREF(key); |
| 4956 | Py_XDECREF(value); |
| 4957 | Py_DECREF(result); |
| 4958 | return NULL; |
| 4959 | } |
| 4960 | |
| 4961 | /* Create a three-level trie */ |
| 4962 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4963 | 16*count2 + 128*count3 - 1); |
| 4964 | if (!result) |
| 4965 | return PyErr_NoMemory(); |
| 4966 | PyObject_Init(result, &EncodingMapType); |
| 4967 | mresult = (struct encoding_map*)result; |
| 4968 | mresult->count2 = count2; |
| 4969 | mresult->count3 = count3; |
| 4970 | mlevel1 = mresult->level1; |
| 4971 | mlevel2 = mresult->level23; |
| 4972 | mlevel3 = mresult->level23 + 16*count2; |
| 4973 | memcpy(mlevel1, level1, 32); |
| 4974 | memset(mlevel2, 0xFF, 16*count2); |
| 4975 | memset(mlevel3, 0, 128*count3); |
| 4976 | count3 = 0; |
| 4977 | for (i = 1; i < 256; i++) { |
| 4978 | int o1, o2, o3, i2, i3; |
| 4979 | if (decode[i] == 0xFFFE) |
| 4980 | /* unmapped character */ |
| 4981 | continue; |
| 4982 | o1 = decode[i]>>11; |
| 4983 | o2 = (decode[i]>>7) & 0xF; |
| 4984 | i2 = 16*mlevel1[o1] + o2; |
| 4985 | if (mlevel2[i2] == 0xFF) |
| 4986 | mlevel2[i2] = count3++; |
| 4987 | o3 = decode[i] & 0x7F; |
| 4988 | i3 = 128*mlevel2[i2] + o3; |
| 4989 | mlevel3[i3] = i; |
| 4990 | } |
| 4991 | return result; |
| 4992 | } |
| 4993 | |
| 4994 | static int |
| 4995 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4996 | { |
| 4997 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4998 | int l1 = c>>11; |
| 4999 | int l2 = (c>>7) & 0xF; |
| 5000 | int l3 = c & 0x7F; |
| 5001 | int i; |
| 5002 | |
| 5003 | #ifdef Py_UNICODE_WIDE |
| 5004 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5005 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5006 | } |
| 5007 | #endif |
| 5008 | if (c == 0) |
| 5009 | return 0; |
| 5010 | /* level 1*/ |
| 5011 | i = map->level1[l1]; |
| 5012 | if (i == 0xFF) { |
| 5013 | return -1; |
| 5014 | } |
| 5015 | /* level 2*/ |
| 5016 | i = map->level23[16*i+l2]; |
| 5017 | if (i == 0xFF) { |
| 5018 | return -1; |
| 5019 | } |
| 5020 | /* level 3 */ |
| 5021 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5022 | if (i == 0) { |
| 5023 | return -1; |
| 5024 | } |
| 5025 | return i; |
| 5026 | } |
| 5027 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5028 | /* Lookup the character ch in the mapping. If the character |
| 5029 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5030 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5031 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5032 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5033 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5034 | PyObject *x; |
| 5035 | |
| 5036 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5037 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5038 | x = PyObject_GetItem(mapping, w); |
| 5039 | Py_DECREF(w); |
| 5040 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5041 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5042 | /* No mapping found means: mapping is undefined. */ |
| 5043 | PyErr_Clear(); |
| 5044 | x = Py_None; |
| 5045 | Py_INCREF(x); |
| 5046 | return x; |
| 5047 | } else |
| 5048 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5049 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5050 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5051 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5052 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | long value = PyLong_AS_LONG(x); |
| 5054 | if (value < 0 || value > 255) { |
| 5055 | PyErr_SetString(PyExc_TypeError, |
| 5056 | "character mapping must be in range(256)"); |
| 5057 | Py_DECREF(x); |
| 5058 | return NULL; |
| 5059 | } |
| 5060 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5061 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5062 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5063 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5064 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | /* wrong return value */ |
| 5066 | PyErr_Format(PyExc_TypeError, |
| 5067 | "character mapping must return integer, bytes or None, not %.400s", |
| 5068 | x->ob_type->tp_name); |
| 5069 | Py_DECREF(x); |
| 5070 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5071 | } |
| 5072 | } |
| 5073 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5074 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5075 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5076 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5077 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5078 | /* exponentially overallocate to minimize reallocations */ |
| 5079 | if (requiredsize < 2*outsize) |
| 5080 | requiredsize = 2*outsize; |
| 5081 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5082 | return -1; |
| 5083 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5084 | } |
| 5085 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5086 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5087 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5088 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5089 | /* 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] | 5090 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5091 | space is available. Return a new reference to the object that |
| 5092 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5093 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5094 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5095 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5096 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5097 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5098 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5099 | PyObject *rep; |
| 5100 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5101 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5102 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5103 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5104 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5105 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5106 | if (res == -1) |
| 5107 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5108 | if (outsize<requiredsize) |
| 5109 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5110 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5111 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5112 | outstart[(*outpos)++] = (char)res; |
| 5113 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5117 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5118 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5119 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5120 | Py_DECREF(rep); |
| 5121 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5122 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5123 | if (PyLong_Check(rep)) { |
| 5124 | Py_ssize_t requiredsize = *outpos+1; |
| 5125 | if (outsize<requiredsize) |
| 5126 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5127 | Py_DECREF(rep); |
| 5128 | return enc_EXCEPTION; |
| 5129 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5130 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5131 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5132 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5133 | else { |
| 5134 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5135 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5136 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5137 | if (outsize<requiredsize) |
| 5138 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5139 | Py_DECREF(rep); |
| 5140 | return enc_EXCEPTION; |
| 5141 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5142 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5143 | memcpy(outstart + *outpos, repchars, repsize); |
| 5144 | *outpos += repsize; |
| 5145 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5146 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5147 | Py_DECREF(rep); |
| 5148 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | /* handle an error in PyUnicode_EncodeCharmap |
| 5152 | Return 0 on success, -1 on error */ |
| 5153 | static |
| 5154 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5155 | 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] | 5156 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5157 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5158 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5159 | { |
| 5160 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5161 | Py_ssize_t repsize; |
| 5162 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5163 | Py_UNICODE *uni2; |
| 5164 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5165 | Py_ssize_t collstartpos = *inpos; |
| 5166 | Py_ssize_t collendpos = *inpos+1; |
| 5167 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5168 | char *encoding = "charmap"; |
| 5169 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5170 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5171 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5172 | /* find all unencodable characters */ |
| 5173 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5174 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5175 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5176 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5177 | if (res != -1) |
| 5178 | break; |
| 5179 | ++collendpos; |
| 5180 | continue; |
| 5181 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5182 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5183 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5184 | if (rep==NULL) |
| 5185 | return -1; |
| 5186 | else if (rep!=Py_None) { |
| 5187 | Py_DECREF(rep); |
| 5188 | break; |
| 5189 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5190 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5191 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5192 | } |
| 5193 | /* cache callback name lookup |
| 5194 | * (if not done yet, i.e. it's the first error) */ |
| 5195 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5196 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5197 | *known_errorHandler = 1; |
| 5198 | else if (!strcmp(errors, "replace")) |
| 5199 | *known_errorHandler = 2; |
| 5200 | else if (!strcmp(errors, "ignore")) |
| 5201 | *known_errorHandler = 3; |
| 5202 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5203 | *known_errorHandler = 4; |
| 5204 | else |
| 5205 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5206 | } |
| 5207 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5208 | case 1: /* strict */ |
| 5209 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5210 | return -1; |
| 5211 | case 2: /* replace */ |
| 5212 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5213 | x = charmapencode_output('?', mapping, res, respos); |
| 5214 | if (x==enc_EXCEPTION) { |
| 5215 | return -1; |
| 5216 | } |
| 5217 | else if (x==enc_FAILED) { |
| 5218 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5219 | return -1; |
| 5220 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5221 | } |
| 5222 | /* fall through */ |
| 5223 | case 3: /* ignore */ |
| 5224 | *inpos = collendpos; |
| 5225 | break; |
| 5226 | case 4: /* xmlcharrefreplace */ |
| 5227 | /* generate replacement (temporarily (mis)uses p) */ |
| 5228 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5229 | char buffer[2+29+1+1]; |
| 5230 | char *cp; |
| 5231 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5232 | for (cp = buffer; *cp; ++cp) { |
| 5233 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5234 | if (x==enc_EXCEPTION) |
| 5235 | return -1; |
| 5236 | else if (x==enc_FAILED) { |
| 5237 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5238 | return -1; |
| 5239 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5240 | } |
| 5241 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5242 | *inpos = collendpos; |
| 5243 | break; |
| 5244 | default: |
| 5245 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5246 | encoding, reason, p, size, exceptionObject, |
| 5247 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5248 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5249 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5250 | if (PyBytes_Check(repunicode)) { |
| 5251 | /* Directly copy bytes result to output. */ |
| 5252 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5253 | Py_ssize_t requiredsize; |
| 5254 | repsize = PyBytes_Size(repunicode); |
| 5255 | requiredsize = *respos + repsize; |
| 5256 | if (requiredsize > outsize) |
| 5257 | /* Make room for all additional bytes. */ |
| 5258 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5259 | Py_DECREF(repunicode); |
| 5260 | return -1; |
| 5261 | } |
| 5262 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5263 | PyBytes_AsString(repunicode), repsize); |
| 5264 | *respos += repsize; |
| 5265 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5266 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5267 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5268 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5269 | /* generate replacement */ |
| 5270 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5271 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5272 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5273 | if (x==enc_EXCEPTION) { |
| 5274 | return -1; |
| 5275 | } |
| 5276 | else if (x==enc_FAILED) { |
| 5277 | Py_DECREF(repunicode); |
| 5278 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5279 | return -1; |
| 5280 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5281 | } |
| 5282 | *inpos = newpos; |
| 5283 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5284 | } |
| 5285 | return 0; |
| 5286 | } |
| 5287 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5288 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5289 | Py_ssize_t size, |
| 5290 | PyObject *mapping, |
| 5291 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5292 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5293 | /* output object */ |
| 5294 | PyObject *res = NULL; |
| 5295 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5296 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5297 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5298 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5299 | PyObject *errorHandler = NULL; |
| 5300 | PyObject *exc = NULL; |
| 5301 | /* the following variable is used for caching string comparisons |
| 5302 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5303 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5304 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5305 | |
| 5306 | /* Default to Latin-1 */ |
| 5307 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5308 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5309 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5310 | /* allocate enough for a simple encoding without |
| 5311 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5312 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5313 | if (res == NULL) |
| 5314 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5315 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5316 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5317 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5318 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5319 | /* try to encode it */ |
| 5320 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5321 | if (x==enc_EXCEPTION) /* error */ |
| 5322 | goto onError; |
| 5323 | if (x==enc_FAILED) { /* unencodable character */ |
| 5324 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5325 | &exc, |
| 5326 | &known_errorHandler, &errorHandler, errors, |
| 5327 | &res, &respos)) { |
| 5328 | goto onError; |
| 5329 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5330 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5331 | else |
| 5332 | /* done with this character => adjust input position */ |
| 5333 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5334 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5335 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5336 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5337 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5338 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5339 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5340 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5341 | Py_XDECREF(exc); |
| 5342 | Py_XDECREF(errorHandler); |
| 5343 | return res; |
| 5344 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5345 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5346 | Py_XDECREF(res); |
| 5347 | Py_XDECREF(exc); |
| 5348 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5349 | return NULL; |
| 5350 | } |
| 5351 | |
| 5352 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5353 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5354 | { |
| 5355 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5356 | PyErr_BadArgument(); |
| 5357 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 | } |
| 5359 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5360 | PyUnicode_GET_SIZE(unicode), |
| 5361 | mapping, |
| 5362 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5365 | /* create or adjust a UnicodeTranslateError */ |
| 5366 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5367 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5368 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5369 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5371 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5372 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5373 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5374 | } |
| 5375 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5376 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5377 | goto onError; |
| 5378 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5379 | goto onError; |
| 5380 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5381 | goto onError; |
| 5382 | return; |
| 5383 | onError: |
| 5384 | Py_DECREF(*exceptionObject); |
| 5385 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | } |
| 5387 | } |
| 5388 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5389 | /* raises a UnicodeTranslateError */ |
| 5390 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5391 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5392 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5393 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5394 | { |
| 5395 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5396 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5397 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5398 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5399 | } |
| 5400 | |
| 5401 | /* error handling callback helper: |
| 5402 | build arguments, call the callback and check the arguments, |
| 5403 | put the result into newpos and return the replacement string, which |
| 5404 | has to be freed by the caller */ |
| 5405 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5406 | PyObject **errorHandler, |
| 5407 | const char *reason, |
| 5408 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5409 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5410 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5411 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5412 | 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] | 5413 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5414 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5415 | PyObject *restuple; |
| 5416 | PyObject *resunicode; |
| 5417 | |
| 5418 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5419 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5420 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5421 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5425 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5426 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5427 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5428 | |
| 5429 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5430 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5431 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5432 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5433 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5434 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5435 | Py_DECREF(restuple); |
| 5436 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5437 | } |
| 5438 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5439 | &resunicode, &i_newpos)) { |
| 5440 | Py_DECREF(restuple); |
| 5441 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5442 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5443 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5444 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5445 | else |
| 5446 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5447 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5448 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5449 | Py_DECREF(restuple); |
| 5450 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5451 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5452 | Py_INCREF(resunicode); |
| 5453 | Py_DECREF(restuple); |
| 5454 | return resunicode; |
| 5455 | } |
| 5456 | |
| 5457 | /* Lookup the character ch in the mapping and put the result in result, |
| 5458 | which must be decrefed by the caller. |
| 5459 | Return 0 on success, -1 on error */ |
| 5460 | static |
| 5461 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5462 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5463 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5464 | PyObject *x; |
| 5465 | |
| 5466 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5467 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5468 | x = PyObject_GetItem(mapping, w); |
| 5469 | Py_DECREF(w); |
| 5470 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5471 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5472 | /* No mapping found means: use 1:1 mapping. */ |
| 5473 | PyErr_Clear(); |
| 5474 | *result = NULL; |
| 5475 | return 0; |
| 5476 | } else |
| 5477 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5478 | } |
| 5479 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5480 | *result = x; |
| 5481 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5482 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5483 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5484 | long value = PyLong_AS_LONG(x); |
| 5485 | long max = PyUnicode_GetMax(); |
| 5486 | if (value < 0 || value > max) { |
| 5487 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5488 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5489 | Py_DECREF(x); |
| 5490 | return -1; |
| 5491 | } |
| 5492 | *result = x; |
| 5493 | return 0; |
| 5494 | } |
| 5495 | else if (PyUnicode_Check(x)) { |
| 5496 | *result = x; |
| 5497 | return 0; |
| 5498 | } |
| 5499 | else { |
| 5500 | /* wrong return value */ |
| 5501 | PyErr_SetString(PyExc_TypeError, |
| 5502 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5503 | Py_DECREF(x); |
| 5504 | return -1; |
| 5505 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5506 | } |
| 5507 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5508 | if not reallocate and adjust various state variables. |
| 5509 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5510 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5511 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5512 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5513 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5514 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5515 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5516 | /* remember old output position */ |
| 5517 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5518 | /* exponentially overallocate to minimize reallocations */ |
| 5519 | if (requiredsize < 2 * oldsize) |
| 5520 | requiredsize = 2 * oldsize; |
| 5521 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5522 | return -1; |
| 5523 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5524 | } |
| 5525 | return 0; |
| 5526 | } |
| 5527 | /* lookup the character, put the result in the output string and adjust |
| 5528 | various state variables. Return a new reference to the object that |
| 5529 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5530 | undefined (in which case no character was written). |
| 5531 | The called must decref result. |
| 5532 | Return 0 on success, -1 on error. */ |
| 5533 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5534 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5535 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5536 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5537 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5538 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5539 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5540 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5541 | /* not found => default to 1:1 mapping */ |
| 5542 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5543 | } |
| 5544 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5545 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5546 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5547 | /* no overflow check, because we know that the space is enough */ |
| 5548 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5549 | } |
| 5550 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5551 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5552 | if (repsize==1) { |
| 5553 | /* no overflow check, because we know that the space is enough */ |
| 5554 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5555 | } |
| 5556 | else if (repsize!=0) { |
| 5557 | /* more than one character */ |
| 5558 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5559 | (insize - (curinp-startinp)) + |
| 5560 | repsize - 1; |
| 5561 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5562 | return -1; |
| 5563 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5564 | *outp += repsize; |
| 5565 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5566 | } |
| 5567 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5568 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5569 | return 0; |
| 5570 | } |
| 5571 | |
| 5572 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5573 | Py_ssize_t size, |
| 5574 | PyObject *mapping, |
| 5575 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5576 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5577 | /* output object */ |
| 5578 | PyObject *res = NULL; |
| 5579 | /* pointers to the beginning and end+1 of input */ |
| 5580 | const Py_UNICODE *startp = p; |
| 5581 | const Py_UNICODE *endp = p + size; |
| 5582 | /* pointer into the output */ |
| 5583 | Py_UNICODE *str; |
| 5584 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5585 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5586 | char *reason = "character maps to <undefined>"; |
| 5587 | PyObject *errorHandler = NULL; |
| 5588 | PyObject *exc = NULL; |
| 5589 | /* the following variable is used for caching string comparisons |
| 5590 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5591 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5592 | int known_errorHandler = -1; |
| 5593 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5595 | PyErr_BadArgument(); |
| 5596 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5598 | |
| 5599 | /* allocate enough for a simple 1:1 translation without |
| 5600 | replacements, if we need more, we'll resize */ |
| 5601 | res = PyUnicode_FromUnicode(NULL, size); |
| 5602 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5603 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5604 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5605 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5606 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5607 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5608 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5609 | /* try to encode it */ |
| 5610 | PyObject *x = NULL; |
| 5611 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5612 | Py_XDECREF(x); |
| 5613 | goto onError; |
| 5614 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5615 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5616 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5617 | ++p; |
| 5618 | else { /* untranslatable character */ |
| 5619 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5620 | Py_ssize_t repsize; |
| 5621 | Py_ssize_t newpos; |
| 5622 | Py_UNICODE *uni2; |
| 5623 | /* startpos for collecting untranslatable chars */ |
| 5624 | const Py_UNICODE *collstart = p; |
| 5625 | const Py_UNICODE *collend = p+1; |
| 5626 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5628 | /* find all untranslatable characters */ |
| 5629 | while (collend < endp) { |
| 5630 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5631 | goto onError; |
| 5632 | Py_XDECREF(x); |
| 5633 | if (x!=Py_None) |
| 5634 | break; |
| 5635 | ++collend; |
| 5636 | } |
| 5637 | /* cache callback name lookup |
| 5638 | * (if not done yet, i.e. it's the first error) */ |
| 5639 | if (known_errorHandler==-1) { |
| 5640 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5641 | known_errorHandler = 1; |
| 5642 | else if (!strcmp(errors, "replace")) |
| 5643 | known_errorHandler = 2; |
| 5644 | else if (!strcmp(errors, "ignore")) |
| 5645 | known_errorHandler = 3; |
| 5646 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5647 | known_errorHandler = 4; |
| 5648 | else |
| 5649 | known_errorHandler = 0; |
| 5650 | } |
| 5651 | switch (known_errorHandler) { |
| 5652 | case 1: /* strict */ |
| 5653 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5654 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5655 | case 2: /* replace */ |
| 5656 | /* No need to check for space, this is a 1:1 replacement */ |
| 5657 | for (coll = collstart; coll<collend; ++coll) |
| 5658 | *str++ = '?'; |
| 5659 | /* fall through */ |
| 5660 | case 3: /* ignore */ |
| 5661 | p = collend; |
| 5662 | break; |
| 5663 | case 4: /* xmlcharrefreplace */ |
| 5664 | /* generate replacement (temporarily (mis)uses p) */ |
| 5665 | for (p = collstart; p < collend; ++p) { |
| 5666 | char buffer[2+29+1+1]; |
| 5667 | char *cp; |
| 5668 | sprintf(buffer, "&#%d;", (int)*p); |
| 5669 | if (charmaptranslate_makespace(&res, &str, |
| 5670 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5671 | goto onError; |
| 5672 | for (cp = buffer; *cp; ++cp) |
| 5673 | *str++ = *cp; |
| 5674 | } |
| 5675 | p = collend; |
| 5676 | break; |
| 5677 | default: |
| 5678 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5679 | reason, startp, size, &exc, |
| 5680 | collstart-startp, collend-startp, &newpos); |
| 5681 | if (repunicode == NULL) |
| 5682 | goto onError; |
| 5683 | /* generate replacement */ |
| 5684 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5685 | if (charmaptranslate_makespace(&res, &str, |
| 5686 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5687 | Py_DECREF(repunicode); |
| 5688 | goto onError; |
| 5689 | } |
| 5690 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5691 | *str++ = *uni2; |
| 5692 | p = startp + newpos; |
| 5693 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5694 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5695 | } |
| 5696 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5697 | /* Resize if we allocated to much */ |
| 5698 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5699 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5700 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5701 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5702 | } |
| 5703 | Py_XDECREF(exc); |
| 5704 | Py_XDECREF(errorHandler); |
| 5705 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5706 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5707 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5708 | Py_XDECREF(res); |
| 5709 | Py_XDECREF(exc); |
| 5710 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5711 | return NULL; |
| 5712 | } |
| 5713 | |
| 5714 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5715 | PyObject *mapping, |
| 5716 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | { |
| 5718 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5719 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5720 | str = PyUnicode_FromObject(str); |
| 5721 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5722 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5724 | PyUnicode_GET_SIZE(str), |
| 5725 | mapping, |
| 5726 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | Py_DECREF(str); |
| 5728 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5729 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5730 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5731 | Py_XDECREF(str); |
| 5732 | return NULL; |
| 5733 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5734 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5735 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5736 | |
| 5737 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5738 | Py_ssize_t length, |
| 5739 | char *output, |
| 5740 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5741 | { |
| 5742 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5743 | PyObject *errorHandler = NULL; |
| 5744 | PyObject *exc = NULL; |
| 5745 | const char *encoding = "decimal"; |
| 5746 | const char *reason = "invalid decimal Unicode string"; |
| 5747 | /* the following variable is used for caching string comparisons |
| 5748 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5749 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5750 | |
| 5751 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5752 | PyErr_BadArgument(); |
| 5753 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | p = s; |
| 5757 | end = s + length; |
| 5758 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5759 | register Py_UNICODE ch = *p; |
| 5760 | int decimal; |
| 5761 | PyObject *repunicode; |
| 5762 | Py_ssize_t repsize; |
| 5763 | Py_ssize_t newpos; |
| 5764 | Py_UNICODE *uni2; |
| 5765 | Py_UNICODE *collstart; |
| 5766 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5767 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5768 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5769 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5770 | ++p; |
| 5771 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5772 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5773 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5774 | if (decimal >= 0) { |
| 5775 | *output++ = '0' + decimal; |
| 5776 | ++p; |
| 5777 | continue; |
| 5778 | } |
| 5779 | if (0 < ch && ch < 256) { |
| 5780 | *output++ = (char)ch; |
| 5781 | ++p; |
| 5782 | continue; |
| 5783 | } |
| 5784 | /* All other characters are considered unencodable */ |
| 5785 | collstart = p; |
| 5786 | collend = p+1; |
| 5787 | while (collend < end) { |
| 5788 | if ((0 < *collend && *collend < 256) || |
| 5789 | !Py_UNICODE_ISSPACE(*collend) || |
| 5790 | Py_UNICODE_TODECIMAL(*collend)) |
| 5791 | break; |
| 5792 | } |
| 5793 | /* cache callback name lookup |
| 5794 | * (if not done yet, i.e. it's the first error) */ |
| 5795 | if (known_errorHandler==-1) { |
| 5796 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5797 | known_errorHandler = 1; |
| 5798 | else if (!strcmp(errors, "replace")) |
| 5799 | known_errorHandler = 2; |
| 5800 | else if (!strcmp(errors, "ignore")) |
| 5801 | known_errorHandler = 3; |
| 5802 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5803 | known_errorHandler = 4; |
| 5804 | else |
| 5805 | known_errorHandler = 0; |
| 5806 | } |
| 5807 | switch (known_errorHandler) { |
| 5808 | case 1: /* strict */ |
| 5809 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5810 | goto onError; |
| 5811 | case 2: /* replace */ |
| 5812 | for (p = collstart; p < collend; ++p) |
| 5813 | *output++ = '?'; |
| 5814 | /* fall through */ |
| 5815 | case 3: /* ignore */ |
| 5816 | p = collend; |
| 5817 | break; |
| 5818 | case 4: /* xmlcharrefreplace */ |
| 5819 | /* generate replacement (temporarily (mis)uses p) */ |
| 5820 | for (p = collstart; p < collend; ++p) |
| 5821 | output += sprintf(output, "&#%d;", (int)*p); |
| 5822 | p = collend; |
| 5823 | break; |
| 5824 | default: |
| 5825 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5826 | encoding, reason, s, length, &exc, |
| 5827 | collstart-s, collend-s, &newpos); |
| 5828 | if (repunicode == NULL) |
| 5829 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5830 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5831 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5832 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 5833 | Py_DECREF(repunicode); |
| 5834 | goto onError; |
| 5835 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5836 | /* generate replacement */ |
| 5837 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5838 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5839 | Py_UNICODE ch = *uni2; |
| 5840 | if (Py_UNICODE_ISSPACE(ch)) |
| 5841 | *output++ = ' '; |
| 5842 | else { |
| 5843 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5844 | if (decimal >= 0) |
| 5845 | *output++ = '0' + decimal; |
| 5846 | else if (0 < ch && ch < 256) |
| 5847 | *output++ = (char)ch; |
| 5848 | else { |
| 5849 | Py_DECREF(repunicode); |
| 5850 | raise_encode_exception(&exc, encoding, |
| 5851 | s, length, collstart-s, collend-s, reason); |
| 5852 | goto onError; |
| 5853 | } |
| 5854 | } |
| 5855 | } |
| 5856 | p = s + newpos; |
| 5857 | Py_DECREF(repunicode); |
| 5858 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5859 | } |
| 5860 | /* 0-terminate the output string */ |
| 5861 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5862 | Py_XDECREF(exc); |
| 5863 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5864 | return 0; |
| 5865 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5866 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5867 | Py_XDECREF(exc); |
| 5868 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5869 | return -1; |
| 5870 | } |
| 5871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | /* --- Helpers ------------------------------------------------------------ */ |
| 5873 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5874 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5875 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5876 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5877 | /* Include _ParseTupleFinds from find.h */ |
| 5878 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5879 | #include "stringlib/find.h" |
| 5880 | #include "stringlib/partition.h" |
| 5881 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5882 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 5883 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5884 | #include "stringlib/localeutil.h" |
| 5885 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5886 | /* helper macro to fixup start/end slice values */ |
| 5887 | #define FIX_START_END(obj) \ |
| 5888 | if (start < 0) \ |
| 5889 | start += (obj)->length; \ |
| 5890 | if (start < 0) \ |
| 5891 | start = 0; \ |
| 5892 | if (end > (obj)->length) \ |
| 5893 | end = (obj)->length; \ |
| 5894 | if (end < 0) \ |
| 5895 | end += (obj)->length; \ |
| 5896 | if (end < 0) \ |
| 5897 | end = 0; |
| 5898 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5899 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5900 | PyObject *substr, |
| 5901 | Py_ssize_t start, |
| 5902 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5904 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5905 | PyUnicodeObject* str_obj; |
| 5906 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5907 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5908 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5909 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5910 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5911 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5912 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5913 | Py_DECREF(str_obj); |
| 5914 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5915 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5916 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5917 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5918 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5919 | result = stringlib_count( |
| 5920 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5921 | ); |
| 5922 | |
| 5923 | Py_DECREF(sub_obj); |
| 5924 | Py_DECREF(str_obj); |
| 5925 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | return result; |
| 5927 | } |
| 5928 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5929 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5930 | PyObject *sub, |
| 5931 | Py_ssize_t start, |
| 5932 | Py_ssize_t end, |
| 5933 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5935 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5936 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5937 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5938 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5939 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5940 | sub = PyUnicode_FromObject(sub); |
| 5941 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5942 | Py_DECREF(str); |
| 5943 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5945 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5946 | if (direction > 0) |
| 5947 | result = stringlib_find_slice( |
| 5948 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5949 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5950 | start, end |
| 5951 | ); |
| 5952 | else |
| 5953 | result = stringlib_rfind_slice( |
| 5954 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5955 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5956 | start, end |
| 5957 | ); |
| 5958 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5960 | Py_DECREF(sub); |
| 5961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5962 | return result; |
| 5963 | } |
| 5964 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5965 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5966 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5967 | PyUnicodeObject *substring, |
| 5968 | Py_ssize_t start, |
| 5969 | Py_ssize_t end, |
| 5970 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5971 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 | if (substring->length == 0) |
| 5973 | return 1; |
| 5974 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5975 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | |
| 5977 | end -= substring->length; |
| 5978 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5979 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | |
| 5981 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5982 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5983 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | } else { |
| 5985 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5986 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | } |
| 5988 | |
| 5989 | return 0; |
| 5990 | } |
| 5991 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5992 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5993 | PyObject *substr, |
| 5994 | Py_ssize_t start, |
| 5995 | Py_ssize_t end, |
| 5996 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5998 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | str = PyUnicode_FromObject(str); |
| 6001 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6002 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | substr = PyUnicode_FromObject(substr); |
| 6004 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6005 | Py_DECREF(str); |
| 6006 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6010 | (PyUnicodeObject *)substr, |
| 6011 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | Py_DECREF(str); |
| 6013 | Py_DECREF(substr); |
| 6014 | return result; |
| 6015 | } |
| 6016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6017 | /* Apply fixfct filter to the Unicode object self and return a |
| 6018 | reference to the modified object */ |
| 6019 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6020 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6022 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | { |
| 6024 | |
| 6025 | PyUnicodeObject *u; |
| 6026 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6027 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6029 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6030 | |
| 6031 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6032 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6033 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6034 | /* fixfct should return TRUE if it modified the buffer. If |
| 6035 | FALSE, return a reference to the original buffer instead |
| 6036 | (to save space, not time) */ |
| 6037 | Py_INCREF(self); |
| 6038 | Py_DECREF(u); |
| 6039 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6040 | } |
| 6041 | return (PyObject*) u; |
| 6042 | } |
| 6043 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6044 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | int fixupper(PyUnicodeObject *self) |
| 6046 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6047 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | Py_UNICODE *s = self->str; |
| 6049 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6052 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6053 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | ch = Py_UNICODE_TOUPPER(*s); |
| 6055 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6057 | *s = ch; |
| 6058 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | s++; |
| 6060 | } |
| 6061 | |
| 6062 | return status; |
| 6063 | } |
| 6064 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6065 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | int fixlower(PyUnicodeObject *self) |
| 6067 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6068 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | Py_UNICODE *s = self->str; |
| 6070 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6073 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6074 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6075 | ch = Py_UNICODE_TOLOWER(*s); |
| 6076 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6077 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6078 | *s = ch; |
| 6079 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | s++; |
| 6081 | } |
| 6082 | |
| 6083 | return status; |
| 6084 | } |
| 6085 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6086 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6087 | int fixswapcase(PyUnicodeObject *self) |
| 6088 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6089 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6090 | Py_UNICODE *s = self->str; |
| 6091 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6092 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | while (len-- > 0) { |
| 6094 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6095 | *s = Py_UNICODE_TOLOWER(*s); |
| 6096 | status = 1; |
| 6097 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6098 | *s = Py_UNICODE_TOUPPER(*s); |
| 6099 | status = 1; |
| 6100 | } |
| 6101 | s++; |
| 6102 | } |
| 6103 | |
| 6104 | return status; |
| 6105 | } |
| 6106 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6107 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6108 | int fixcapitalize(PyUnicodeObject *self) |
| 6109 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6110 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6111 | Py_UNICODE *s = self->str; |
| 6112 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6113 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6114 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6116 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6117 | *s = Py_UNICODE_TOUPPER(*s); |
| 6118 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6119 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6120 | s++; |
| 6121 | while (--len > 0) { |
| 6122 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6123 | *s = Py_UNICODE_TOLOWER(*s); |
| 6124 | status = 1; |
| 6125 | } |
| 6126 | s++; |
| 6127 | } |
| 6128 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | } |
| 6130 | |
| 6131 | static |
| 6132 | int fixtitle(PyUnicodeObject *self) |
| 6133 | { |
| 6134 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6135 | register Py_UNICODE *e; |
| 6136 | int previous_is_cased; |
| 6137 | |
| 6138 | /* Shortcut for single character strings */ |
| 6139 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6140 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6141 | if (*p != ch) { |
| 6142 | *p = ch; |
| 6143 | return 1; |
| 6144 | } |
| 6145 | else |
| 6146 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6148 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | e = p + PyUnicode_GET_SIZE(self); |
| 6150 | previous_is_cased = 0; |
| 6151 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6152 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6153 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6154 | if (previous_is_cased) |
| 6155 | *p = Py_UNICODE_TOLOWER(ch); |
| 6156 | else |
| 6157 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6158 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6159 | if (Py_UNICODE_ISLOWER(ch) || |
| 6160 | Py_UNICODE_ISUPPER(ch) || |
| 6161 | Py_UNICODE_ISTITLE(ch)) |
| 6162 | previous_is_cased = 1; |
| 6163 | else |
| 6164 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 | } |
| 6166 | return 1; |
| 6167 | } |
| 6168 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6169 | PyObject * |
| 6170 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6171 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6172 | const Py_UNICODE blank = ' '; |
| 6173 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6174 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6175 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6176 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6177 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6178 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6179 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6180 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6181 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6182 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6183 | fseq = PySequence_Fast(seq, ""); |
| 6184 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6185 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6186 | } |
| 6187 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6188 | /* NOTE: the following code can't call back into Python code, |
| 6189 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6190 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6191 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6192 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6193 | /* If empty sequence, return u"". */ |
| 6194 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6195 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6196 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6197 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6198 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6199 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6200 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | item = items[0]; |
| 6202 | if (PyUnicode_CheckExact(item)) { |
| 6203 | Py_INCREF(item); |
| 6204 | res = (PyUnicodeObject *)item; |
| 6205 | goto Done; |
| 6206 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6207 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6208 | else { |
| 6209 | /* Set up sep and seplen */ |
| 6210 | if (separator == NULL) { |
| 6211 | sep = ␣ |
| 6212 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6213 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6214 | else { |
| 6215 | if (!PyUnicode_Check(separator)) { |
| 6216 | PyErr_Format(PyExc_TypeError, |
| 6217 | "separator: expected str instance," |
| 6218 | " %.80s found", |
| 6219 | Py_TYPE(separator)->tp_name); |
| 6220 | goto onError; |
| 6221 | } |
| 6222 | sep = PyUnicode_AS_UNICODE(separator); |
| 6223 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6224 | } |
| 6225 | } |
| 6226 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6227 | /* There are at least two things to join, or else we have a subclass |
| 6228 | * of str in the sequence. |
| 6229 | * Do a pre-pass to figure out the total amount of space we'll |
| 6230 | * need (sz), and see whether all argument are strings. |
| 6231 | */ |
| 6232 | sz = 0; |
| 6233 | for (i = 0; i < seqlen; i++) { |
| 6234 | const Py_ssize_t old_sz = sz; |
| 6235 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6236 | if (!PyUnicode_Check(item)) { |
| 6237 | PyErr_Format(PyExc_TypeError, |
| 6238 | "sequence item %zd: expected str instance," |
| 6239 | " %.80s found", |
| 6240 | i, Py_TYPE(item)->tp_name); |
| 6241 | goto onError; |
| 6242 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6243 | sz += PyUnicode_GET_SIZE(item); |
| 6244 | if (i != 0) |
| 6245 | sz += seplen; |
| 6246 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6247 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6248 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6249 | goto onError; |
| 6250 | } |
| 6251 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6252 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6253 | res = _PyUnicode_New(sz); |
| 6254 | if (res == NULL) |
| 6255 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6256 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6257 | /* Catenate everything. */ |
| 6258 | res_p = PyUnicode_AS_UNICODE(res); |
| 6259 | for (i = 0; i < seqlen; ++i) { |
| 6260 | Py_ssize_t itemlen; |
| 6261 | item = items[i]; |
| 6262 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6263 | /* Copy item, and maybe the separator. */ |
| 6264 | if (i) { |
| 6265 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6266 | res_p += seplen; |
| 6267 | } |
| 6268 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6269 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6270 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6271 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6272 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6273 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6274 | return (PyObject *)res; |
| 6275 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6276 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6277 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6278 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 | return NULL; |
| 6280 | } |
| 6281 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6282 | static |
| 6283 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6284 | Py_ssize_t left, |
| 6285 | Py_ssize_t right, |
| 6286 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6287 | { |
| 6288 | PyUnicodeObject *u; |
| 6289 | |
| 6290 | if (left < 0) |
| 6291 | left = 0; |
| 6292 | if (right < 0) |
| 6293 | right = 0; |
| 6294 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6295 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6296 | Py_INCREF(self); |
| 6297 | return self; |
| 6298 | } |
| 6299 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6300 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6301 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6302 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6303 | return NULL; |
| 6304 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6305 | u = _PyUnicode_New(left + self->length + right); |
| 6306 | if (u) { |
| 6307 | if (left) |
| 6308 | Py_UNICODE_FILL(u->str, fill, left); |
| 6309 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6310 | if (right) |
| 6311 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6312 | } |
| 6313 | |
| 6314 | return u; |
| 6315 | } |
| 6316 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6317 | #define SPLIT_APPEND(data, left, right) \ |
| 6318 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 6319 | if (!str) \ |
| 6320 | goto onError; \ |
| 6321 | if (PyList_Append(list, str)) { \ |
| 6322 | Py_DECREF(str); \ |
| 6323 | goto onError; \ |
| 6324 | } \ |
| 6325 | else \ |
| 6326 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6327 | |
| 6328 | static |
| 6329 | PyObject *split_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6330 | PyObject *list, |
| 6331 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6332 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6333 | register Py_ssize_t i; |
| 6334 | register Py_ssize_t j; |
| 6335 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6336 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6337 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6338 | |
| 6339 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6340 | /* find a token */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6341 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6342 | i++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6343 | j = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6344 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
| 6345 | i++; |
| 6346 | if (j < i) { |
| 6347 | if (maxcount-- <= 0) |
| 6348 | break; |
| 6349 | SPLIT_APPEND(buf, j, i); |
| 6350 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
| 6351 | i++; |
| 6352 | j = i; |
| 6353 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | } |
| 6355 | if (j < len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6356 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | } |
| 6358 | return list; |
| 6359 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6360 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6361 | Py_DECREF(list); |
| 6362 | return NULL; |
| 6363 | } |
| 6364 | |
| 6365 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6366 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6367 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6368 | register Py_ssize_t i; |
| 6369 | register Py_ssize_t j; |
| 6370 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 | PyObject *list; |
| 6372 | PyObject *str; |
| 6373 | Py_UNICODE *data; |
| 6374 | |
| 6375 | string = PyUnicode_FromObject(string); |
| 6376 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6377 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6378 | data = PyUnicode_AS_UNICODE(string); |
| 6379 | len = PyUnicode_GET_SIZE(string); |
| 6380 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6381 | list = PyList_New(0); |
| 6382 | if (!list) |
| 6383 | goto onError; |
| 6384 | |
| 6385 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6386 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6387 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6388 | /* Find a line and append it */ |
| 6389 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
| 6390 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6392 | /* Skip the line break reading CRLF as one line break */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6393 | eol = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6394 | if (i < len) { |
| 6395 | if (data[i] == '\r' && i + 1 < len && |
| 6396 | data[i+1] == '\n') |
| 6397 | i += 2; |
| 6398 | else |
| 6399 | i++; |
| 6400 | if (keepends) |
| 6401 | eol = i; |
| 6402 | } |
| 6403 | SPLIT_APPEND(data, j, eol); |
| 6404 | j = 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(data, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
| 6410 | Py_DECREF(string); |
| 6411 | return list; |
| 6412 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6413 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6414 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6415 | Py_DECREF(string); |
| 6416 | return NULL; |
| 6417 | } |
| 6418 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6419 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | PyObject *split_char(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6421 | PyObject *list, |
| 6422 | Py_UNICODE ch, |
| 6423 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6425 | register Py_ssize_t i; |
| 6426 | register Py_ssize_t j; |
| 6427 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6428 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6429 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6430 | |
| 6431 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6432 | if (buf[i] == ch) { |
| 6433 | if (maxcount-- <= 0) |
| 6434 | break; |
| 6435 | SPLIT_APPEND(buf, j, i); |
| 6436 | i = j = i + 1; |
| 6437 | } else |
| 6438 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 | } |
| 6440 | if (j <= len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6441 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 | } |
| 6443 | return list; |
| 6444 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6445 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | Py_DECREF(list); |
| 6447 | return NULL; |
| 6448 | } |
| 6449 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6450 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6451 | PyObject *split_substring(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6452 | PyObject *list, |
| 6453 | PyUnicodeObject *substring, |
| 6454 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6455 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6456 | register Py_ssize_t i; |
| 6457 | register Py_ssize_t j; |
| 6458 | Py_ssize_t len = self->length; |
| 6459 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 | PyObject *str; |
| 6461 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 6462 | for (i = j = 0; i <= len - sublen; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6463 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6464 | if (maxcount-- <= 0) |
| 6465 | break; |
| 6466 | SPLIT_APPEND(self->str, j, i); |
| 6467 | i = j = i + sublen; |
| 6468 | } else |
| 6469 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6470 | } |
| 6471 | if (j <= len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6472 | SPLIT_APPEND(self->str, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6473 | } |
| 6474 | return list; |
| 6475 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6476 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6477 | Py_DECREF(list); |
| 6478 | return NULL; |
| 6479 | } |
| 6480 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6481 | static |
| 6482 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6483 | PyObject *list, |
| 6484 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6485 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6486 | register Py_ssize_t i; |
| 6487 | register Py_ssize_t j; |
| 6488 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6489 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6490 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6491 | |
| 6492 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6493 | /* find a token */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6494 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6495 | i--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6496 | j = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
| 6498 | i--; |
| 6499 | if (j > i) { |
| 6500 | if (maxcount-- <= 0) |
| 6501 | break; |
| 6502 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6503 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
| 6504 | i--; |
| 6505 | j = i; |
| 6506 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6507 | } |
| 6508 | if (j >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6509 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6510 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6511 | if (PyList_Reverse(list) < 0) |
| 6512 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6513 | return list; |
| 6514 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6515 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6516 | Py_DECREF(list); |
| 6517 | return NULL; |
| 6518 | } |
| 6519 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6520 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6521 | PyObject *rsplit_char(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6522 | PyObject *list, |
| 6523 | Py_UNICODE ch, |
| 6524 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6525 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6526 | register Py_ssize_t i; |
| 6527 | register Py_ssize_t j; |
| 6528 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6529 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6530 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6531 | |
| 6532 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6533 | if (buf[i] == ch) { |
| 6534 | if (maxcount-- <= 0) |
| 6535 | break; |
| 6536 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6537 | j = i = i - 1; |
| 6538 | } else |
| 6539 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6540 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 6541 | if (j >= -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6542 | SPLIT_APPEND(buf, 0, j + 1); |
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 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6553 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6554 | PyObject *rsplit_substring(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6555 | PyObject *list, |
| 6556 | PyUnicodeObject *substring, |
| 6557 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6558 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6559 | register Py_ssize_t i; |
| 6560 | register Py_ssize_t j; |
| 6561 | Py_ssize_t len = self->length; |
| 6562 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6563 | PyObject *str; |
| 6564 | |
| 6565 | for (i = len - sublen, j = len; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6567 | if (maxcount-- <= 0) |
| 6568 | break; |
| 6569 | SPLIT_APPEND(self->str, i + sublen, j); |
| 6570 | j = i; |
| 6571 | i -= sublen; |
| 6572 | } else |
| 6573 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6574 | } |
| 6575 | if (j >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6576 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6577 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6578 | if (PyList_Reverse(list) < 0) |
| 6579 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6580 | return list; |
| 6581 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6582 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6583 | Py_DECREF(list); |
| 6584 | return NULL; |
| 6585 | } |
| 6586 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6587 | #undef SPLIT_APPEND |
| 6588 | |
| 6589 | static |
| 6590 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | PyUnicodeObject *substring, |
| 6592 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6593 | { |
| 6594 | PyObject *list; |
| 6595 | |
| 6596 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6597 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | |
| 6599 | list = PyList_New(0); |
| 6600 | if (!list) |
| 6601 | return NULL; |
| 6602 | |
| 6603 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6604 | return split_whitespace(self,list,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6605 | |
| 6606 | else if (substring->length == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6607 | return split_char(self,list,substring->str[0],maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6608 | |
| 6609 | else if (substring->length == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6610 | Py_DECREF(list); |
| 6611 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6612 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | } |
| 6614 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | return split_substring(self,list,substring,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6618 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6619 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6620 | PyUnicodeObject *substring, |
| 6621 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6622 | { |
| 6623 | PyObject *list; |
| 6624 | |
| 6625 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6626 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6627 | |
| 6628 | list = PyList_New(0); |
| 6629 | if (!list) |
| 6630 | return NULL; |
| 6631 | |
| 6632 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | return rsplit_whitespace(self,list,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6634 | |
| 6635 | else if (substring->length == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6636 | return rsplit_char(self,list,substring->str[0],maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6637 | |
| 6638 | else if (substring->length == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6639 | Py_DECREF(list); |
| 6640 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6641 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6642 | } |
| 6643 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6644 | return rsplit_substring(self,list,substring,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6645 | } |
| 6646 | |
| 6647 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6649 | PyUnicodeObject *str1, |
| 6650 | PyUnicodeObject *str2, |
| 6651 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6652 | { |
| 6653 | PyUnicodeObject *u; |
| 6654 | |
| 6655 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6656 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6658 | if (str1->length == str2->length) { |
| 6659 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6660 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6661 | if (str1->length == 1) { |
| 6662 | /* replace characters */ |
| 6663 | Py_UNICODE u1, u2; |
| 6664 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6665 | goto nothing; |
| 6666 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6667 | if (!u) |
| 6668 | return NULL; |
| 6669 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6670 | u1 = str1->str[0]; |
| 6671 | u2 = str2->str[0]; |
| 6672 | for (i = 0; i < u->length; i++) |
| 6673 | if (u->str[i] == u1) { |
| 6674 | if (--maxcount < 0) |
| 6675 | break; |
| 6676 | u->str[i] = u2; |
| 6677 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6679 | i = fastsearch( |
| 6680 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6682 | if (i < 0) |
| 6683 | goto nothing; |
| 6684 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6685 | if (!u) |
| 6686 | return NULL; |
| 6687 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6688 | while (i <= self->length - str1->length) |
| 6689 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6690 | if (--maxcount < 0) |
| 6691 | break; |
| 6692 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6693 | i += str1->length; |
| 6694 | } else |
| 6695 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6697 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6698 | |
| 6699 | Py_ssize_t n, i, j, e; |
| 6700 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6701 | Py_UNICODE *p; |
| 6702 | |
| 6703 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6704 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6705 | if (n > maxcount) |
| 6706 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6707 | if (n == 0) |
| 6708 | goto nothing; |
| 6709 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6710 | delta = (str2->length - str1->length); |
| 6711 | if (delta == 0) { |
| 6712 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6713 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6714 | product = n * (str2->length - str1->length); |
| 6715 | if ((product / (str2->length - str1->length)) != n) { |
| 6716 | PyErr_SetString(PyExc_OverflowError, |
| 6717 | "replace string is too long"); |
| 6718 | return NULL; |
| 6719 | } |
| 6720 | new_size = self->length + product; |
| 6721 | if (new_size < 0) { |
| 6722 | PyErr_SetString(PyExc_OverflowError, |
| 6723 | "replace string is too long"); |
| 6724 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | } |
| 6726 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6727 | u = _PyUnicode_New(new_size); |
| 6728 | if (!u) |
| 6729 | return NULL; |
| 6730 | i = 0; |
| 6731 | p = u->str; |
| 6732 | e = self->length - str1->length; |
| 6733 | if (str1->length > 0) { |
| 6734 | while (n-- > 0) { |
| 6735 | /* look for next match */ |
| 6736 | j = i; |
| 6737 | while (j <= e) { |
| 6738 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6739 | break; |
| 6740 | j++; |
| 6741 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6742 | if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6743 | if (j > e) |
| 6744 | break; |
| 6745 | /* copy unchanged part [i:j] */ |
| 6746 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6747 | p += j - i; |
| 6748 | } |
| 6749 | /* copy substitution string */ |
| 6750 | if (str2->length > 0) { |
| 6751 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6752 | p += str2->length; |
| 6753 | } |
| 6754 | i = j + str1->length; |
| 6755 | } |
| 6756 | if (i < self->length) |
| 6757 | /* copy tail [i:] */ |
| 6758 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6759 | } else { |
| 6760 | /* interleave */ |
| 6761 | while (n > 0) { |
| 6762 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6763 | p += str2->length; |
| 6764 | if (--n <= 0) |
| 6765 | break; |
| 6766 | *p++ = self->str[i++]; |
| 6767 | } |
| 6768 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6769 | } |
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 (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6772 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6773 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6774 | /* nothing to replace; return original string (when possible) */ |
| 6775 | if (PyUnicode_CheckExact(self)) { |
| 6776 | Py_INCREF(self); |
| 6777 | return (PyObject *) self; |
| 6778 | } |
| 6779 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | } |
| 6781 | |
| 6782 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6784 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6785 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6786 | \n\ |
| 6787 | 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] | 6788 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | |
| 6790 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6791 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6793 | return fixup(self, fixtitle); |
| 6794 | } |
| 6795 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6796 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6797 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6798 | \n\ |
| 6799 | Return a capitalized version of S, i.e. make the first character\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6800 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6801 | |
| 6802 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6803 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6805 | return fixup(self, fixcapitalize); |
| 6806 | } |
| 6807 | |
| 6808 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6809 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6810 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | \n\ |
| 6812 | 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] | 6813 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6814 | |
| 6815 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6816 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6817 | { |
| 6818 | PyObject *list; |
| 6819 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6820 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6821 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6822 | /* Split into words */ |
| 6823 | list = split(self, NULL, -1); |
| 6824 | if (!list) |
| 6825 | return NULL; |
| 6826 | |
| 6827 | /* Capitalize each word */ |
| 6828 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6829 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6830 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6831 | if (item == NULL) |
| 6832 | goto onError; |
| 6833 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6834 | PyList_SET_ITEM(list, i, item); |
| 6835 | } |
| 6836 | |
| 6837 | /* Join the words to form a new string */ |
| 6838 | item = PyUnicode_Join(NULL, list); |
| 6839 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6840 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | Py_DECREF(list); |
| 6842 | return (PyObject *)item; |
| 6843 | } |
| 6844 | #endif |
| 6845 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6846 | /* Argument converter. Coerces to a single unicode character */ |
| 6847 | |
| 6848 | static int |
| 6849 | convert_uc(PyObject *obj, void *addr) |
| 6850 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6851 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6852 | PyObject *uniobj; |
| 6853 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6854 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6855 | uniobj = PyUnicode_FromObject(obj); |
| 6856 | if (uniobj == NULL) { |
| 6857 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6858 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6859 | return 0; |
| 6860 | } |
| 6861 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6862 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6863 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6864 | Py_DECREF(uniobj); |
| 6865 | return 0; |
| 6866 | } |
| 6867 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6868 | *fillcharloc = unistr[0]; |
| 6869 | Py_DECREF(uniobj); |
| 6870 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6871 | } |
| 6872 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6873 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6874 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6875 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6876 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6877 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | |
| 6879 | static PyObject * |
| 6880 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6881 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6882 | Py_ssize_t marg, left; |
| 6883 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6884 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6885 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6886 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | return NULL; |
| 6888 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6889 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6890 | Py_INCREF(self); |
| 6891 | return (PyObject*) self; |
| 6892 | } |
| 6893 | |
| 6894 | marg = width - self->length; |
| 6895 | left = marg / 2 + (marg & width & 1); |
| 6896 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6897 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6898 | } |
| 6899 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6900 | #if 0 |
| 6901 | |
| 6902 | /* This code should go into some future Unicode collation support |
| 6903 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6904 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6905 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6906 | /* speedy UTF-16 code point order comparison */ |
| 6907 | /* gleaned from: */ |
| 6908 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6909 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6910 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6911 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6912 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6913 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6914 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6915 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6916 | }; |
| 6917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 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 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6922 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +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 | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6931 | |
| 6932 | c1 = *s1++; |
| 6933 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6934 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6935 | if (c1 > (1<<11) * 26) |
| 6936 | c1 += utf16Fixup[c1>>11]; |
| 6937 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6938 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6939 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6940 | |
| 6941 | if (c1 != c2) |
| 6942 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6943 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6944 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
| 6947 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6948 | } |
| 6949 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6950 | #else |
| 6951 | |
| 6952 | static int |
| 6953 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6954 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6955 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6956 | |
| 6957 | Py_UNICODE *s1 = str1->str; |
| 6958 | Py_UNICODE *s2 = str2->str; |
| 6959 | |
| 6960 | len1 = str1->length; |
| 6961 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6962 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6963 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6964 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6965 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6966 | c1 = *s1++; |
| 6967 | c2 = *s2++; |
| 6968 | |
| 6969 | if (c1 != c2) |
| 6970 | return (c1 < c2) ? -1 : 1; |
| 6971 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6972 | len1--; len2--; |
| 6973 | } |
| 6974 | |
| 6975 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6976 | } |
| 6977 | |
| 6978 | #endif |
| 6979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6980 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6981 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6983 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6984 | return unicode_compare((PyUnicodeObject *)left, |
| 6985 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6986 | PyErr_Format(PyExc_TypeError, |
| 6987 | "Can't compare %.100s and %.100s", |
| 6988 | left->ob_type->tp_name, |
| 6989 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6990 | return -1; |
| 6991 | } |
| 6992 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6993 | int |
| 6994 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6995 | { |
| 6996 | int i; |
| 6997 | Py_UNICODE *id; |
| 6998 | assert(PyUnicode_Check(uni)); |
| 6999 | id = PyUnicode_AS_UNICODE(uni); |
| 7000 | /* Compare Unicode string and source character set string */ |
| 7001 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7002 | if (id[i] != str[i]) |
| 7003 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 7004 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 7005 | to C strings identical up to that point. */ |
| 7006 | if (PyUnicode_GET_SIZE(uni) != i) |
| 7007 | /* We'll say the Python string is longer. */ |
| 7008 | return 1; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7009 | if (id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7010 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7011 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7012 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 7013 | return 0; |
| 7014 | } |
| 7015 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7016 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7017 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7018 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7019 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7020 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 7021 | PyObject *right, |
| 7022 | int op) |
| 7023 | { |
| 7024 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7025 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7026 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 7027 | PyObject *v; |
| 7028 | if (((PyUnicodeObject *) left)->length != |
| 7029 | ((PyUnicodeObject *) right)->length) { |
| 7030 | if (op == Py_EQ) { |
| 7031 | Py_INCREF(Py_False); |
| 7032 | return Py_False; |
| 7033 | } |
| 7034 | if (op == Py_NE) { |
| 7035 | Py_INCREF(Py_True); |
| 7036 | return Py_True; |
| 7037 | } |
| 7038 | } |
| 7039 | if (left == right) |
| 7040 | result = 0; |
| 7041 | else |
| 7042 | result = unicode_compare((PyUnicodeObject *)left, |
| 7043 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7044 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7045 | /* Convert the return value to a Boolean */ |
| 7046 | switch (op) { |
| 7047 | case Py_EQ: |
| 7048 | v = TEST_COND(result == 0); |
| 7049 | break; |
| 7050 | case Py_NE: |
| 7051 | v = TEST_COND(result != 0); |
| 7052 | break; |
| 7053 | case Py_LE: |
| 7054 | v = TEST_COND(result <= 0); |
| 7055 | break; |
| 7056 | case Py_GE: |
| 7057 | v = TEST_COND(result >= 0); |
| 7058 | break; |
| 7059 | case Py_LT: |
| 7060 | v = TEST_COND(result == -1); |
| 7061 | break; |
| 7062 | case Py_GT: |
| 7063 | v = TEST_COND(result == 1); |
| 7064 | break; |
| 7065 | default: |
| 7066 | PyErr_BadArgument(); |
| 7067 | return NULL; |
| 7068 | } |
| 7069 | Py_INCREF(v); |
| 7070 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7071 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7072 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7073 | Py_INCREF(Py_NotImplemented); |
| 7074 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7075 | } |
| 7076 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7077 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7078 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7079 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7080 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7081 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7082 | |
| 7083 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7084 | sub = PyUnicode_FromObject(element); |
| 7085 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7086 | PyErr_Format(PyExc_TypeError, |
| 7087 | "'in <string>' requires string as left operand, not %s", |
| 7088 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7089 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7090 | } |
| 7091 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7092 | str = PyUnicode_FromObject(container); |
| 7093 | if (!str) { |
| 7094 | Py_DECREF(sub); |
| 7095 | return -1; |
| 7096 | } |
| 7097 | |
| 7098 | result = stringlib_contains_obj(str, sub); |
| 7099 | |
| 7100 | Py_DECREF(str); |
| 7101 | Py_DECREF(sub); |
| 7102 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7103 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7104 | } |
| 7105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7106 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7107 | |
| 7108 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7109 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7110 | { |
| 7111 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7112 | |
| 7113 | /* Coerce the two arguments */ |
| 7114 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7115 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7116 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7117 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7118 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7119 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7120 | |
| 7121 | /* Shortcuts */ |
| 7122 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7123 | Py_DECREF(v); |
| 7124 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7125 | } |
| 7126 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7127 | Py_DECREF(u); |
| 7128 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7129 | } |
| 7130 | |
| 7131 | /* Concat the two Unicode strings */ |
| 7132 | w = _PyUnicode_New(u->length + v->length); |
| 7133 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7134 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7135 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7136 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7137 | |
| 7138 | Py_DECREF(u); |
| 7139 | Py_DECREF(v); |
| 7140 | return (PyObject *)w; |
| 7141 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7142 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | Py_XDECREF(u); |
| 7144 | Py_XDECREF(v); |
| 7145 | return NULL; |
| 7146 | } |
| 7147 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7148 | void |
| 7149 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7150 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7151 | PyObject *new; |
| 7152 | if (*pleft == NULL) |
| 7153 | return; |
| 7154 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7155 | Py_DECREF(*pleft); |
| 7156 | *pleft = NULL; |
| 7157 | return; |
| 7158 | } |
| 7159 | new = PyUnicode_Concat(*pleft, right); |
| 7160 | Py_DECREF(*pleft); |
| 7161 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7162 | } |
| 7163 | |
| 7164 | void |
| 7165 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7166 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7167 | PyUnicode_Append(pleft, right); |
| 7168 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7171 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7172 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7173 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7174 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7175 | 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] | 7176 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7177 | |
| 7178 | static PyObject * |
| 7179 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7180 | { |
| 7181 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7182 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7183 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7184 | PyObject *result; |
| 7185 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7186 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7187 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7188 | return NULL; |
| 7189 | |
| 7190 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7191 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7192 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7193 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7194 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7195 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7196 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7197 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7198 | stringlib_count(self->str + start, end - start, |
| 7199 | substring->str, substring->length) |
| 7200 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7201 | |
| 7202 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7204 | return result; |
| 7205 | } |
| 7206 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7207 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7208 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7209 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7210 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7211 | 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] | 7212 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7213 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7214 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7215 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7216 | |
| 7217 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7218 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7219 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7220 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7221 | char *encoding = NULL; |
| 7222 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7223 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7224 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7225 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7226 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7227 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7228 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7229 | if (v == NULL) |
| 7230 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7231 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7232 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7233 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7234 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7235 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7236 | Py_DECREF(v); |
| 7237 | return NULL; |
| 7238 | } |
| 7239 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7240 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7241 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7242 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7243 | } |
| 7244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7245 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7246 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7247 | \n\ |
| 7248 | 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] | 7249 | 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] | 7250 | |
| 7251 | static PyObject* |
| 7252 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7253 | { |
| 7254 | Py_UNICODE *e; |
| 7255 | Py_UNICODE *p; |
| 7256 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7257 | Py_UNICODE *qe; |
| 7258 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7259 | PyUnicodeObject *u; |
| 7260 | int tabsize = 8; |
| 7261 | |
| 7262 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7263 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7264 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7265 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7266 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7267 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7268 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7269 | for (p = self->str; p < e; p++) |
| 7270 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7271 | if (tabsize > 0) { |
| 7272 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7273 | if (j > PY_SSIZE_T_MAX - incr) |
| 7274 | goto overflow1; |
| 7275 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7276 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7277 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7278 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7279 | if (j > PY_SSIZE_T_MAX - 1) |
| 7280 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7281 | j++; |
| 7282 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7283 | if (i > PY_SSIZE_T_MAX - j) |
| 7284 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7285 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7286 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7287 | } |
| 7288 | } |
| 7289 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7290 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7291 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7292 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7293 | /* Second pass: create output string and fill it */ |
| 7294 | u = _PyUnicode_New(i + j); |
| 7295 | if (!u) |
| 7296 | return NULL; |
| 7297 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7298 | j = 0; /* same as in first pass */ |
| 7299 | q = u->str; /* next output char */ |
| 7300 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7301 | |
| 7302 | for (p = self->str; p < e; p++) |
| 7303 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7304 | if (tabsize > 0) { |
| 7305 | i = tabsize - (j % tabsize); |
| 7306 | j += i; |
| 7307 | while (i--) { |
| 7308 | if (q >= qe) |
| 7309 | goto overflow2; |
| 7310 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7311 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7312 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7313 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7314 | else { |
| 7315 | if (q >= qe) |
| 7316 | goto overflow2; |
| 7317 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7318 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7319 | if (*p == '\n' || *p == '\r') |
| 7320 | j = 0; |
| 7321 | } |
| 7322 | |
| 7323 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7324 | |
| 7325 | overflow2: |
| 7326 | Py_DECREF(u); |
| 7327 | overflow1: |
| 7328 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7329 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7330 | } |
| 7331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7332 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7333 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7334 | \n\ |
| 7335 | 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] | 7336 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7337 | arguments start and end are interpreted as in slice notation.\n\ |
| 7338 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7339 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7340 | |
| 7341 | static PyObject * |
| 7342 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7343 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7344 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7345 | Py_ssize_t start; |
| 7346 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7347 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7348 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7349 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7350 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7351 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7352 | result = stringlib_find_slice( |
| 7353 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7354 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7355 | start, end |
| 7356 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 | |
| 7358 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7359 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7360 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7361 | } |
| 7362 | |
| 7363 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7364 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7365 | { |
| 7366 | if (index < 0 || index >= self->length) { |
| 7367 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7368 | return NULL; |
| 7369 | } |
| 7370 | |
| 7371 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7372 | } |
| 7373 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7374 | /* Believe it or not, this produces the same value for ASCII strings |
| 7375 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7376 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7377 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7378 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7379 | Py_ssize_t len; |
| 7380 | Py_UNICODE *p; |
| 7381 | long x; |
| 7382 | |
| 7383 | if (self->hash != -1) |
| 7384 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7385 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7386 | p = self->str; |
| 7387 | x = *p << 7; |
| 7388 | while (--len >= 0) |
| 7389 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7390 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7391 | if (x == -1) |
| 7392 | x = -2; |
| 7393 | self->hash = x; |
| 7394 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7395 | } |
| 7396 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7397 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7398 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7399 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7400 | 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] | 7401 | |
| 7402 | static PyObject * |
| 7403 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7404 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7405 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7406 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7407 | Py_ssize_t start; |
| 7408 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7409 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7410 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7412 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7413 | result = stringlib_find_slice( |
| 7414 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7415 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7416 | start, end |
| 7417 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7418 | |
| 7419 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7420 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7421 | if (result < 0) { |
| 7422 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7423 | return NULL; |
| 7424 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7425 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7426 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7427 | } |
| 7428 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7429 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7430 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7431 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7432 | 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] | 7433 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7434 | |
| 7435 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7436 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | { |
| 7438 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7439 | register const Py_UNICODE *e; |
| 7440 | int cased; |
| 7441 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | /* Shortcut for single character strings */ |
| 7443 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7444 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7445 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7446 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7447 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7448 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7450 | e = p + PyUnicode_GET_SIZE(self); |
| 7451 | cased = 0; |
| 7452 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7453 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7454 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7455 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7456 | return PyBool_FromLong(0); |
| 7457 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7458 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7459 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7460 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7463 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7464 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7465 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7466 | 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] | 7467 | at least one cased character in S, 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_isupper(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; |
| 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_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7480 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7481 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7482 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7484 | e = p + PyUnicode_GET_SIZE(self); |
| 7485 | cased = 0; |
| 7486 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7487 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7488 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7489 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7490 | return PyBool_FromLong(0); |
| 7491 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7492 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7493 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7494 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7495 | } |
| 7496 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7497 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7498 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7499 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7500 | Return True if S is a titlecased string and there is at least one\n\ |
| 7501 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7502 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7503 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | |
| 7505 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7506 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7507 | { |
| 7508 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7509 | register const Py_UNICODE *e; |
| 7510 | int cased, previous_is_cased; |
| 7511 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7512 | /* Shortcut for single character strings */ |
| 7513 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7514 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7515 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7516 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7517 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7518 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7519 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | e = p + PyUnicode_GET_SIZE(self); |
| 7522 | cased = 0; |
| 7523 | previous_is_cased = 0; |
| 7524 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7525 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7526 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7527 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7528 | if (previous_is_cased) |
| 7529 | return PyBool_FromLong(0); |
| 7530 | previous_is_cased = 1; |
| 7531 | cased = 1; |
| 7532 | } |
| 7533 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7534 | if (!previous_is_cased) |
| 7535 | return PyBool_FromLong(0); |
| 7536 | previous_is_cased = 1; |
| 7537 | cased = 1; |
| 7538 | } |
| 7539 | else |
| 7540 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7541 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7542 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7543 | } |
| 7544 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7545 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7546 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7547 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7548 | Return True if all characters in S are whitespace\n\ |
| 7549 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7550 | |
| 7551 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7552 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7553 | { |
| 7554 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7555 | register const Py_UNICODE *e; |
| 7556 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7557 | /* Shortcut for single character strings */ |
| 7558 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7559 | Py_UNICODE_ISSPACE(*p)) |
| 7560 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7561 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7562 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7563 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7564 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7565 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7566 | e = p + PyUnicode_GET_SIZE(self); |
| 7567 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7568 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7569 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7570 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7571 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7572 | } |
| 7573 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7574 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7575 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7576 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7577 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7578 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7579 | |
| 7580 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7581 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7582 | { |
| 7583 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7584 | register const Py_UNICODE *e; |
| 7585 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7586 | /* Shortcut for single character strings */ |
| 7587 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7588 | Py_UNICODE_ISALPHA(*p)) |
| 7589 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7590 | |
| 7591 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7592 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7593 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7594 | |
| 7595 | e = p + PyUnicode_GET_SIZE(self); |
| 7596 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7597 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7598 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7599 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7600 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7601 | } |
| 7602 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7603 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7604 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7605 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7606 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7607 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7608 | |
| 7609 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7610 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7611 | { |
| 7612 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7613 | register const Py_UNICODE *e; |
| 7614 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7615 | /* Shortcut for single character strings */ |
| 7616 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7617 | Py_UNICODE_ISALNUM(*p)) |
| 7618 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7619 | |
| 7620 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7621 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7622 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7623 | |
| 7624 | e = p + PyUnicode_GET_SIZE(self); |
| 7625 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7626 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7627 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7628 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7629 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7630 | } |
| 7631 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7632 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7633 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7634 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7635 | 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] | 7636 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7637 | |
| 7638 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7639 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7640 | { |
| 7641 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7642 | register const Py_UNICODE *e; |
| 7643 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7644 | /* Shortcut for single character strings */ |
| 7645 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7646 | Py_UNICODE_ISDECIMAL(*p)) |
| 7647 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7648 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7649 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7650 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7651 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7653 | e = p + PyUnicode_GET_SIZE(self); |
| 7654 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7655 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7656 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7657 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7658 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7659 | } |
| 7660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7661 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7662 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7663 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7664 | Return True if all characters in S are digits\n\ |
| 7665 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7666 | |
| 7667 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7668 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7669 | { |
| 7670 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7671 | register const Py_UNICODE *e; |
| 7672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7673 | /* Shortcut for single character strings */ |
| 7674 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7675 | Py_UNICODE_ISDIGIT(*p)) |
| 7676 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7677 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7678 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7679 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7680 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7682 | e = p + PyUnicode_GET_SIZE(self); |
| 7683 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7684 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7685 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7686 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7687 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7688 | } |
| 7689 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7690 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7691 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7692 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7693 | 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] | 7694 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7695 | |
| 7696 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7697 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7698 | { |
| 7699 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7700 | register const Py_UNICODE *e; |
| 7701 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7702 | /* Shortcut for single character strings */ |
| 7703 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7704 | Py_UNICODE_ISNUMERIC(*p)) |
| 7705 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7706 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7707 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7708 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7709 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7710 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7711 | e = p + PyUnicode_GET_SIZE(self); |
| 7712 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7713 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7714 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7715 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7716 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | } |
| 7718 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7719 | int |
| 7720 | PyUnicode_IsIdentifier(PyObject *self) |
| 7721 | { |
| 7722 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7723 | register const Py_UNICODE *e; |
| 7724 | |
| 7725 | /* Special case for empty strings */ |
| 7726 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7727 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7728 | |
| 7729 | /* PEP 3131 says that the first character must be in |
| 7730 | XID_Start and subsequent characters in XID_Continue, |
| 7731 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7732 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7733 | letters, digits, underscore). However, given the current |
| 7734 | definition of XID_Start and XID_Continue, it is sufficient |
| 7735 | to check just for these, except that _ must be allowed |
| 7736 | as starting an identifier. */ |
| 7737 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7738 | return 0; |
| 7739 | |
| 7740 | e = p + PyUnicode_GET_SIZE(self); |
| 7741 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7742 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7743 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7744 | } |
| 7745 | return 1; |
| 7746 | } |
| 7747 | |
| 7748 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7749 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7750 | \n\ |
| 7751 | Return True if S is a valid identifier according\n\ |
| 7752 | to the language definition."); |
| 7753 | |
| 7754 | static PyObject* |
| 7755 | unicode_isidentifier(PyObject *self) |
| 7756 | { |
| 7757 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7758 | } |
| 7759 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7760 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7761 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7762 | \n\ |
| 7763 | Return True if all characters in S are considered\n\ |
| 7764 | printable in repr() or S is empty, False otherwise."); |
| 7765 | |
| 7766 | static PyObject* |
| 7767 | unicode_isprintable(PyObject *self) |
| 7768 | { |
| 7769 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7770 | register const Py_UNICODE *e; |
| 7771 | |
| 7772 | /* Shortcut for single character strings */ |
| 7773 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7774 | Py_RETURN_TRUE; |
| 7775 | } |
| 7776 | |
| 7777 | e = p + PyUnicode_GET_SIZE(self); |
| 7778 | for (; p < e; p++) { |
| 7779 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7780 | Py_RETURN_FALSE; |
| 7781 | } |
| 7782 | } |
| 7783 | Py_RETURN_TRUE; |
| 7784 | } |
| 7785 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7786 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7787 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7788 | \n\ |
| 7789 | Return a string which is the concatenation of the strings in the\n\ |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7790 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7791 | |
| 7792 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7793 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7795 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7798 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7799 | unicode_length(PyUnicodeObject *self) |
| 7800 | { |
| 7801 | return self->length; |
| 7802 | } |
| 7803 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7804 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7805 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7806 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7807 | 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] | 7808 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7809 | |
| 7810 | static PyObject * |
| 7811 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7812 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7813 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7814 | Py_UNICODE fillchar = ' '; |
| 7815 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7816 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | return NULL; |
| 7818 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7819 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7820 | Py_INCREF(self); |
| 7821 | return (PyObject*) self; |
| 7822 | } |
| 7823 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7824 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7825 | } |
| 7826 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7827 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7828 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7829 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7830 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | |
| 7832 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7833 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7835 | return fixup(self, fixlower); |
| 7836 | } |
| 7837 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7838 | #define LEFTSTRIP 0 |
| 7839 | #define RIGHTSTRIP 1 |
| 7840 | #define BOTHSTRIP 2 |
| 7841 | |
| 7842 | /* Arrays indexed by above */ |
| 7843 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7844 | |
| 7845 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7846 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7847 | /* externally visible for str.strip(unicode) */ |
| 7848 | PyObject * |
| 7849 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7850 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7851 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7852 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7853 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7854 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7855 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7856 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7857 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7858 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7859 | i = 0; |
| 7860 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7861 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7862 | i++; |
| 7863 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7864 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7865 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7866 | j = len; |
| 7867 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7868 | do { |
| 7869 | j--; |
| 7870 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7871 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7872 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7873 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7874 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7875 | Py_INCREF(self); |
| 7876 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7877 | } |
| 7878 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7879 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7880 | } |
| 7881 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7882 | |
| 7883 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7884 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7885 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7886 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7887 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7888 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7889 | i = 0; |
| 7890 | if (striptype != RIGHTSTRIP) { |
| 7891 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7892 | i++; |
| 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 | j = len; |
| 7897 | if (striptype != LEFTSTRIP) { |
| 7898 | do { |
| 7899 | j--; |
| 7900 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7901 | j++; |
| 7902 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7903 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7904 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7905 | Py_INCREF(self); |
| 7906 | return (PyObject*)self; |
| 7907 | } |
| 7908 | else |
| 7909 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7910 | } |
| 7911 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7912 | |
| 7913 | static PyObject * |
| 7914 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7915 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7916 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7917 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7918 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7919 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7920 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7921 | if (sep != NULL && sep != Py_None) { |
| 7922 | if (PyUnicode_Check(sep)) |
| 7923 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7924 | else { |
| 7925 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7926 | "%s arg must be None or str", |
| 7927 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7928 | return NULL; |
| 7929 | } |
| 7930 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7931 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7932 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7933 | } |
| 7934 | |
| 7935 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7936 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7937 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7938 | \n\ |
| 7939 | Return a copy of the string S with leading and trailing\n\ |
| 7940 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7941 | 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] | 7942 | |
| 7943 | static PyObject * |
| 7944 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7945 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7946 | if (PyTuple_GET_SIZE(args) == 0) |
| 7947 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7948 | else |
| 7949 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7950 | } |
| 7951 | |
| 7952 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7953 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7954 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7955 | \n\ |
| 7956 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7957 | 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] | 7958 | |
| 7959 | static PyObject * |
| 7960 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7961 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7962 | if (PyTuple_GET_SIZE(args) == 0) |
| 7963 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7964 | else |
| 7965 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7966 | } |
| 7967 | |
| 7968 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7969 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7970 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7971 | \n\ |
| 7972 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7973 | 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] | 7974 | |
| 7975 | static PyObject * |
| 7976 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7977 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7978 | if (PyTuple_GET_SIZE(args) == 0) |
| 7979 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7980 | else |
| 7981 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7982 | } |
| 7983 | |
| 7984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7985 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7986 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7987 | { |
| 7988 | PyUnicodeObject *u; |
| 7989 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7990 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7991 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7992 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7993 | if (len < 1) { |
| 7994 | Py_INCREF(unicode_empty); |
| 7995 | return (PyObject *)unicode_empty; |
| 7996 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7997 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7998 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | /* no repeat, return original string */ |
| 8000 | Py_INCREF(str); |
| 8001 | return (PyObject*) str; |
| 8002 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8003 | |
| 8004 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 8005 | * needed doesn't overflow size_t |
| 8006 | */ |
| 8007 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8008 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 8009 | PyErr_SetString(PyExc_OverflowError, |
| 8010 | "repeated string is too long"); |
| 8011 | return NULL; |
| 8012 | } |
| 8013 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 8014 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 8015 | PyErr_SetString(PyExc_OverflowError, |
| 8016 | "repeated string is too long"); |
| 8017 | return NULL; |
| 8018 | } |
| 8019 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8020 | if (!u) |
| 8021 | return NULL; |
| 8022 | |
| 8023 | p = u->str; |
| 8024 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8025 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8026 | Py_UNICODE_FILL(p, str->str[0], len); |
| 8027 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 8028 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 8029 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8030 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 8031 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8032 | Py_UNICODE_COPY(p+done, p, n); |
| 8033 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8034 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | } |
| 8036 | |
| 8037 | return (PyObject*) u; |
| 8038 | } |
| 8039 | |
| 8040 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8041 | PyObject *subobj, |
| 8042 | PyObject *replobj, |
| 8043 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8044 | { |
| 8045 | PyObject *self; |
| 8046 | PyObject *str1; |
| 8047 | PyObject *str2; |
| 8048 | PyObject *result; |
| 8049 | |
| 8050 | self = PyUnicode_FromObject(obj); |
| 8051 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8052 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | str1 = PyUnicode_FromObject(subobj); |
| 8054 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8055 | Py_DECREF(self); |
| 8056 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8057 | } |
| 8058 | str2 = PyUnicode_FromObject(replobj); |
| 8059 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8060 | Py_DECREF(self); |
| 8061 | Py_DECREF(str1); |
| 8062 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8063 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8064 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8065 | (PyUnicodeObject *)str1, |
| 8066 | (PyUnicodeObject *)str2, |
| 8067 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8068 | Py_DECREF(self); |
| 8069 | Py_DECREF(str1); |
| 8070 | Py_DECREF(str2); |
| 8071 | return result; |
| 8072 | } |
| 8073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8074 | PyDoc_STRVAR(replace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8075 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8076 | \n\ |
| 8077 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8078 | old replaced by new. If the optional argument count is\n\ |
| 8079 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8080 | |
| 8081 | static PyObject* |
| 8082 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8083 | { |
| 8084 | PyUnicodeObject *str1; |
| 8085 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8086 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8087 | PyObject *result; |
| 8088 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8089 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8090 | return NULL; |
| 8091 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8092 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8093 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8094 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8095 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8096 | Py_DECREF(str1); |
| 8097 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8098 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8099 | |
| 8100 | result = replace(self, str1, str2, maxcount); |
| 8101 | |
| 8102 | Py_DECREF(str1); |
| 8103 | Py_DECREF(str2); |
| 8104 | return result; |
| 8105 | } |
| 8106 | |
| 8107 | static |
| 8108 | PyObject *unicode_repr(PyObject *unicode) |
| 8109 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8110 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8111 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8112 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8113 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8114 | |
| 8115 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8116 | better to choose a different scheme. Perhaps scan the |
| 8117 | first N-chars of the string and allocate based on that size. |
| 8118 | */ |
| 8119 | /* Initial allocation is based on the longest-possible unichr |
| 8120 | escape. |
| 8121 | |
| 8122 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8123 | unichr, so in this case it's the longest unichr escape. In |
| 8124 | narrow (UTF-16) builds this is five chars per source unichr |
| 8125 | since there are two unichrs in the surrogate pair, so in narrow |
| 8126 | (UTF-16) builds it's not the longest unichr escape. |
| 8127 | |
| 8128 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8129 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8130 | escape. |
| 8131 | */ |
| 8132 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8133 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8134 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8135 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8136 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8137 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8138 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8139 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8140 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8141 | if (repr == NULL) |
| 8142 | return NULL; |
| 8143 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8144 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8145 | |
| 8146 | /* Add quote */ |
| 8147 | *p++ = (findchar(s, size, '\'') && |
| 8148 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8149 | while (size-- > 0) { |
| 8150 | Py_UNICODE ch = *s++; |
| 8151 | |
| 8152 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8153 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8154 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8155 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8156 | continue; |
| 8157 | } |
| 8158 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8159 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8160 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8161 | *p++ = '\\'; |
| 8162 | *p++ = 't'; |
| 8163 | } |
| 8164 | else if (ch == '\n') { |
| 8165 | *p++ = '\\'; |
| 8166 | *p++ = 'n'; |
| 8167 | } |
| 8168 | else if (ch == '\r') { |
| 8169 | *p++ = '\\'; |
| 8170 | *p++ = 'r'; |
| 8171 | } |
| 8172 | |
| 8173 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8174 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8175 | *p++ = '\\'; |
| 8176 | *p++ = 'x'; |
| 8177 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8178 | *p++ = hexdigits[ch & 0x000F]; |
| 8179 | } |
| 8180 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8181 | /* Copy ASCII characters as-is */ |
| 8182 | else if (ch < 0x7F) { |
| 8183 | *p++ = ch; |
| 8184 | } |
| 8185 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8186 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8187 | else { |
| 8188 | Py_UCS4 ucs = ch; |
| 8189 | |
| 8190 | #ifndef Py_UNICODE_WIDE |
| 8191 | Py_UNICODE ch2 = 0; |
| 8192 | /* Get code point from surrogate pair */ |
| 8193 | if (size > 0) { |
| 8194 | ch2 = *s; |
| 8195 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8196 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8197 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8198 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8199 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8200 | size--; |
| 8201 | } |
| 8202 | } |
| 8203 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8204 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8205 | (categories Z* and C* except ASCII space) |
| 8206 | */ |
| 8207 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8208 | /* Map 8-bit characters to '\xhh' */ |
| 8209 | if (ucs <= 0xff) { |
| 8210 | *p++ = '\\'; |
| 8211 | *p++ = 'x'; |
| 8212 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8213 | *p++ = hexdigits[ch & 0x000F]; |
| 8214 | } |
| 8215 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8216 | else if (ucs >= 0x10000) { |
| 8217 | *p++ = '\\'; |
| 8218 | *p++ = 'U'; |
| 8219 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8220 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8221 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8222 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8223 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8224 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8225 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8226 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8227 | } |
| 8228 | /* Map 16-bit characters to '\uxxxx' */ |
| 8229 | else { |
| 8230 | *p++ = '\\'; |
| 8231 | *p++ = 'u'; |
| 8232 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8233 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8234 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8235 | *p++ = hexdigits[ucs & 0x000F]; |
| 8236 | } |
| 8237 | } |
| 8238 | /* Copy characters as-is */ |
| 8239 | else { |
| 8240 | *p++ = ch; |
| 8241 | #ifndef Py_UNICODE_WIDE |
| 8242 | if (ucs >= 0x10000) |
| 8243 | *p++ = ch2; |
| 8244 | #endif |
| 8245 | } |
| 8246 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8247 | } |
| 8248 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8249 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8250 | |
| 8251 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8252 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8253 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8254 | } |
| 8255 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8256 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8257 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8258 | \n\ |
| 8259 | 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] | 8260 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8261 | arguments start and end are interpreted as in slice notation.\n\ |
| 8262 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8263 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8264 | |
| 8265 | static PyObject * |
| 8266 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8267 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8268 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8269 | Py_ssize_t start; |
| 8270 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8271 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8273 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8274 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8276 | result = stringlib_rfind_slice( |
| 8277 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8278 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8279 | start, end |
| 8280 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8281 | |
| 8282 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8283 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8284 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8285 | } |
| 8286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8287 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8288 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8289 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8290 | 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] | 8291 | |
| 8292 | static PyObject * |
| 8293 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8294 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8295 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8296 | Py_ssize_t start; |
| 8297 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8298 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8299 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8300 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8301 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8302 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8303 | result = stringlib_rfind_slice( |
| 8304 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8305 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8306 | start, end |
| 8307 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8308 | |
| 8309 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8310 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8311 | if (result < 0) { |
| 8312 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8313 | return NULL; |
| 8314 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8315 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8316 | } |
| 8317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8318 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8319 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8320 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8321 | 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] | 8322 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8323 | |
| 8324 | static PyObject * |
| 8325 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8326 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8327 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8328 | Py_UNICODE fillchar = ' '; |
| 8329 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8330 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | return NULL; |
| 8332 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8333 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | Py_INCREF(self); |
| 8335 | return (PyObject*) self; |
| 8336 | } |
| 8337 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8338 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8339 | } |
| 8340 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8341 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8342 | PyObject *sep, |
| 8343 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8344 | { |
| 8345 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8347 | s = PyUnicode_FromObject(s); |
| 8348 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8349 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8350 | if (sep != NULL) { |
| 8351 | sep = PyUnicode_FromObject(sep); |
| 8352 | if (sep == NULL) { |
| 8353 | Py_DECREF(s); |
| 8354 | return NULL; |
| 8355 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8356 | } |
| 8357 | |
| 8358 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8359 | |
| 8360 | Py_DECREF(s); |
| 8361 | Py_XDECREF(sep); |
| 8362 | return result; |
| 8363 | } |
| 8364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8365 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8366 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8367 | \n\ |
| 8368 | Return a list of the words in S, using sep as the\n\ |
| 8369 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8370 | 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] | 8371 | whitespace string is a separator and empty strings are\n\ |
| 8372 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8373 | |
| 8374 | static PyObject* |
| 8375 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8376 | { |
| 8377 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8378 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8379 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8380 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8381 | return NULL; |
| 8382 | |
| 8383 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8384 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8385 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8386 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8387 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8389 | } |
| 8390 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8391 | PyObject * |
| 8392 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8393 | { |
| 8394 | PyObject* str_obj; |
| 8395 | PyObject* sep_obj; |
| 8396 | PyObject* out; |
| 8397 | |
| 8398 | str_obj = PyUnicode_FromObject(str_in); |
| 8399 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8400 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8401 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8402 | if (!sep_obj) { |
| 8403 | Py_DECREF(str_obj); |
| 8404 | return NULL; |
| 8405 | } |
| 8406 | |
| 8407 | out = stringlib_partition( |
| 8408 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8409 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8410 | ); |
| 8411 | |
| 8412 | Py_DECREF(sep_obj); |
| 8413 | Py_DECREF(str_obj); |
| 8414 | |
| 8415 | return out; |
| 8416 | } |
| 8417 | |
| 8418 | |
| 8419 | PyObject * |
| 8420 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8421 | { |
| 8422 | PyObject* str_obj; |
| 8423 | PyObject* sep_obj; |
| 8424 | PyObject* out; |
| 8425 | |
| 8426 | str_obj = PyUnicode_FromObject(str_in); |
| 8427 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8428 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8429 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8430 | if (!sep_obj) { |
| 8431 | Py_DECREF(str_obj); |
| 8432 | return NULL; |
| 8433 | } |
| 8434 | |
| 8435 | out = stringlib_rpartition( |
| 8436 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8437 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8438 | ); |
| 8439 | |
| 8440 | Py_DECREF(sep_obj); |
| 8441 | Py_DECREF(str_obj); |
| 8442 | |
| 8443 | return out; |
| 8444 | } |
| 8445 | |
| 8446 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8447 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8448 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8449 | 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] | 8450 | 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] | 8451 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8452 | |
| 8453 | static PyObject* |
| 8454 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8455 | { |
| 8456 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8457 | } |
| 8458 | |
| 8459 | PyDoc_STRVAR(rpartition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8460 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8461 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8462 | 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] | 8463 | 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] | 8464 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8465 | |
| 8466 | static PyObject* |
| 8467 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8468 | { |
| 8469 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8470 | } |
| 8471 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8472 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8473 | PyObject *sep, |
| 8474 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8475 | { |
| 8476 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8477 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8478 | s = PyUnicode_FromObject(s); |
| 8479 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8480 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8481 | if (sep != NULL) { |
| 8482 | sep = PyUnicode_FromObject(sep); |
| 8483 | if (sep == NULL) { |
| 8484 | Py_DECREF(s); |
| 8485 | return NULL; |
| 8486 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8487 | } |
| 8488 | |
| 8489 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8490 | |
| 8491 | Py_DECREF(s); |
| 8492 | Py_XDECREF(sep); |
| 8493 | return result; |
| 8494 | } |
| 8495 | |
| 8496 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8497 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8498 | \n\ |
| 8499 | Return a list of the words in S, using sep as the\n\ |
| 8500 | delimiter string, starting at the end of the string and\n\ |
| 8501 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8502 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8503 | is a separator."); |
| 8504 | |
| 8505 | static PyObject* |
| 8506 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8507 | { |
| 8508 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8509 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8510 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8511 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8512 | return NULL; |
| 8513 | |
| 8514 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8515 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8516 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8517 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8518 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8519 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8520 | } |
| 8521 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8522 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8524 | \n\ |
| 8525 | 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] | 8526 | 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] | 8527 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8528 | |
| 8529 | static PyObject* |
| 8530 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8531 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8532 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8533 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8534 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8535 | return NULL; |
| 8536 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8537 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8538 | } |
| 8539 | |
| 8540 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8541 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8542 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8543 | if (PyUnicode_CheckExact(self)) { |
| 8544 | Py_INCREF(self); |
| 8545 | return self; |
| 8546 | } else |
| 8547 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8548 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8549 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8550 | } |
| 8551 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8552 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8553 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8554 | \n\ |
| 8555 | 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] | 8556 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8557 | |
| 8558 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8559 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8560 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8561 | return fixup(self, fixswapcase); |
| 8562 | } |
| 8563 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8564 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8565 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8566 | \n\ |
| 8567 | Return a translation table usable for str.translate().\n\ |
| 8568 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8569 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8570 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8571 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8572 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8573 | character at the same position in y. If there is a third argument, it\n\ |
| 8574 | must be a string, whose characters will be mapped to None in the result."); |
| 8575 | |
| 8576 | static PyObject* |
| 8577 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8578 | { |
| 8579 | PyObject *x, *y = NULL, *z = NULL; |
| 8580 | PyObject *new = NULL, *key, *value; |
| 8581 | Py_ssize_t i = 0; |
| 8582 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8583 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8584 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8585 | return NULL; |
| 8586 | new = PyDict_New(); |
| 8587 | if (!new) |
| 8588 | return NULL; |
| 8589 | if (y != NULL) { |
| 8590 | /* x must be a string too, of equal length */ |
| 8591 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8592 | if (!PyUnicode_Check(x)) { |
| 8593 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8594 | "be a string if there is a second argument"); |
| 8595 | goto err; |
| 8596 | } |
| 8597 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8598 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8599 | "arguments must have equal length"); |
| 8600 | goto err; |
| 8601 | } |
| 8602 | /* create entries for translating chars in x to those in y */ |
| 8603 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8604 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8605 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8606 | if (!key || !value) |
| 8607 | goto err; |
| 8608 | res = PyDict_SetItem(new, key, value); |
| 8609 | Py_DECREF(key); |
| 8610 | Py_DECREF(value); |
| 8611 | if (res < 0) |
| 8612 | goto err; |
| 8613 | } |
| 8614 | /* create entries for deleting chars in z */ |
| 8615 | if (z != NULL) { |
| 8616 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8617 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8618 | if (!key) |
| 8619 | goto err; |
| 8620 | res = PyDict_SetItem(new, key, Py_None); |
| 8621 | Py_DECREF(key); |
| 8622 | if (res < 0) |
| 8623 | goto err; |
| 8624 | } |
| 8625 | } |
| 8626 | } else { |
| 8627 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8628 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8629 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8630 | "to maketrans it must be a dict"); |
| 8631 | goto err; |
| 8632 | } |
| 8633 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8634 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8635 | if (PyUnicode_Check(key)) { |
| 8636 | /* convert string keys to integer keys */ |
| 8637 | PyObject *newkey; |
| 8638 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8639 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8640 | "table must be of length 1"); |
| 8641 | goto err; |
| 8642 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8643 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8644 | if (!newkey) |
| 8645 | goto err; |
| 8646 | res = PyDict_SetItem(new, newkey, value); |
| 8647 | Py_DECREF(newkey); |
| 8648 | if (res < 0) |
| 8649 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8650 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8651 | /* just keep integer keys */ |
| 8652 | if (PyDict_SetItem(new, key, value) < 0) |
| 8653 | goto err; |
| 8654 | } else { |
| 8655 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8656 | "be strings or integers"); |
| 8657 | goto err; |
| 8658 | } |
| 8659 | } |
| 8660 | } |
| 8661 | return new; |
| 8662 | err: |
| 8663 | Py_DECREF(new); |
| 8664 | return NULL; |
| 8665 | } |
| 8666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8667 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8668 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8669 | \n\ |
| 8670 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8671 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8672 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8673 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8674 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8675 | |
| 8676 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8677 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8678 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8679 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8680 | } |
| 8681 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8682 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8683 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8684 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8685 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8686 | |
| 8687 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8688 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8689 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8690 | return fixup(self, fixupper); |
| 8691 | } |
| 8692 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8693 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8694 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8695 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8696 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8697 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8698 | |
| 8699 | static PyObject * |
| 8700 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8701 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8702 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8703 | PyUnicodeObject *u; |
| 8704 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8705 | Py_ssize_t width; |
| 8706 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8707 | return NULL; |
| 8708 | |
| 8709 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8710 | if (PyUnicode_CheckExact(self)) { |
| 8711 | Py_INCREF(self); |
| 8712 | return (PyObject*) self; |
| 8713 | } |
| 8714 | else |
| 8715 | return PyUnicode_FromUnicode( |
| 8716 | PyUnicode_AS_UNICODE(self), |
| 8717 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8718 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8719 | } |
| 8720 | |
| 8721 | fill = width - self->length; |
| 8722 | |
| 8723 | u = pad(self, fill, 0, '0'); |
| 8724 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8725 | if (u == NULL) |
| 8726 | return NULL; |
| 8727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8729 | /* move sign to beginning of string */ |
| 8730 | u->str[0] = u->str[fill]; |
| 8731 | u->str[fill] = '0'; |
| 8732 | } |
| 8733 | |
| 8734 | return (PyObject*) u; |
| 8735 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8736 | |
| 8737 | #if 0 |
| 8738 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8739 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8740 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8741 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 | } |
| 8743 | #endif |
| 8744 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8745 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8746 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8747 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8748 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8749 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8750 | With optional end, stop comparing S at that position.\n\ |
| 8751 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8752 | |
| 8753 | static PyObject * |
| 8754 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8755 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8756 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8757 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8758 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8759 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8760 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8761 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8762 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8763 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8764 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8765 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8766 | if (PyTuple_Check(subobj)) { |
| 8767 | Py_ssize_t i; |
| 8768 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8769 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8770 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8771 | if (substring == NULL) |
| 8772 | return NULL; |
| 8773 | result = tailmatch(self, substring, start, end, -1); |
| 8774 | Py_DECREF(substring); |
| 8775 | if (result) { |
| 8776 | Py_RETURN_TRUE; |
| 8777 | } |
| 8778 | } |
| 8779 | /* nothing matched */ |
| 8780 | Py_RETURN_FALSE; |
| 8781 | } |
| 8782 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8783 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8784 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8785 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8786 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8787 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8788 | } |
| 8789 | |
| 8790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8791 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8792 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8793 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8794 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8795 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8796 | With optional end, stop comparing S at that position.\n\ |
| 8797 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8798 | |
| 8799 | static PyObject * |
| 8800 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8801 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8802 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8803 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8804 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8805 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8806 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8807 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8808 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8809 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8810 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8811 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8812 | if (PyTuple_Check(subobj)) { |
| 8813 | Py_ssize_t i; |
| 8814 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8815 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8816 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8817 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8818 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8819 | result = tailmatch(self, substring, start, end, +1); |
| 8820 | Py_DECREF(substring); |
| 8821 | if (result) { |
| 8822 | Py_RETURN_TRUE; |
| 8823 | } |
| 8824 | } |
| 8825 | Py_RETURN_FALSE; |
| 8826 | } |
| 8827 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8828 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8829 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8830 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8831 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8832 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8833 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8834 | } |
| 8835 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8836 | #include "stringlib/string_format.h" |
| 8837 | |
| 8838 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8839 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8840 | \n\ |
| 8841 | "); |
| 8842 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8843 | static PyObject * |
| 8844 | unicode__format__(PyObject* self, PyObject* args) |
| 8845 | { |
| 8846 | PyObject *format_spec; |
| 8847 | |
| 8848 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8849 | return NULL; |
| 8850 | |
| 8851 | return _PyUnicode_FormatAdvanced(self, |
| 8852 | PyUnicode_AS_UNICODE(format_spec), |
| 8853 | PyUnicode_GET_SIZE(format_spec)); |
| 8854 | } |
| 8855 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8856 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8857 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8858 | \n\ |
| 8859 | "); |
| 8860 | |
| 8861 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8862 | unicode__sizeof__(PyUnicodeObject *v) |
| 8863 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8864 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8865 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8866 | } |
| 8867 | |
| 8868 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8869 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8870 | |
| 8871 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8872 | unicode_getnewargs(PyUnicodeObject *v) |
| 8873 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8874 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8875 | } |
| 8876 | |
| 8877 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8878 | static PyMethodDef unicode_methods[] = { |
| 8879 | |
| 8880 | /* Order is according to common usage: often used methods should |
| 8881 | appear first, since lookup is done sequentially. */ |
| 8882 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8883 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8884 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8885 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8886 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8887 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8888 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8889 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8890 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8891 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8892 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8893 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8894 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8895 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8896 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8897 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8898 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8899 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8900 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8901 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8902 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8903 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8904 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8905 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8906 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8907 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8908 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8909 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8910 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8911 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8912 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8913 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8914 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8915 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8916 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8917 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8918 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8919 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8920 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8921 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8922 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8923 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8924 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8925 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8926 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8927 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8928 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8929 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8930 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8931 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8932 | #endif |
| 8933 | |
| 8934 | #if 0 |
| 8935 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8936 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8937 | #endif |
| 8938 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8939 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8940 | {NULL, NULL} |
| 8941 | }; |
| 8942 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8943 | static PyObject * |
| 8944 | unicode_mod(PyObject *v, PyObject *w) |
| 8945 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | if (!PyUnicode_Check(v)) { |
| 8947 | Py_INCREF(Py_NotImplemented); |
| 8948 | return Py_NotImplemented; |
| 8949 | } |
| 8950 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8951 | } |
| 8952 | |
| 8953 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8954 | 0, /*nb_add*/ |
| 8955 | 0, /*nb_subtract*/ |
| 8956 | 0, /*nb_multiply*/ |
| 8957 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8958 | }; |
| 8959 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8960 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8961 | (lenfunc) unicode_length, /* sq_length */ |
| 8962 | PyUnicode_Concat, /* sq_concat */ |
| 8963 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8964 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8965 | 0, /* sq_slice */ |
| 8966 | 0, /* sq_ass_item */ |
| 8967 | 0, /* sq_ass_slice */ |
| 8968 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8969 | }; |
| 8970 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8971 | static PyObject* |
| 8972 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8973 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8974 | if (PyIndex_Check(item)) { |
| 8975 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8976 | if (i == -1 && PyErr_Occurred()) |
| 8977 | return NULL; |
| 8978 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8979 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8980 | return unicode_getitem(self, i); |
| 8981 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8982 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8983 | Py_UNICODE* source_buf; |
| 8984 | Py_UNICODE* result_buf; |
| 8985 | PyObject* result; |
| 8986 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8987 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8988 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8989 | return NULL; |
| 8990 | } |
| 8991 | |
| 8992 | if (slicelength <= 0) { |
| 8993 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8994 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8995 | PyUnicode_CheckExact(self)) { |
| 8996 | Py_INCREF(self); |
| 8997 | return (PyObject *)self; |
| 8998 | } else if (step == 1) { |
| 8999 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9000 | } else { |
| 9001 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9002 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 9003 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9004 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9005 | if (result_buf == NULL) |
| 9006 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9007 | |
| 9008 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 9009 | result_buf[i] = source_buf[cur]; |
| 9010 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9011 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9012 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9013 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9014 | return result; |
| 9015 | } |
| 9016 | } else { |
| 9017 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 9018 | return NULL; |
| 9019 | } |
| 9020 | } |
| 9021 | |
| 9022 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9023 | (lenfunc)unicode_length, /* mp_length */ |
| 9024 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 9025 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9026 | }; |
| 9027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9029 | /* Helpers for PyUnicode_Format() */ |
| 9030 | |
| 9031 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9032 | 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] | 9033 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9034 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9035 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9036 | (*p_argidx)++; |
| 9037 | if (arglen < 0) |
| 9038 | return args; |
| 9039 | else |
| 9040 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9041 | } |
| 9042 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9043 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9044 | return NULL; |
| 9045 | } |
| 9046 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9047 | /* 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] | 9048 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9049 | static PyObject * |
| 9050 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9051 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9052 | char *p; |
| 9053 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9054 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 | x = PyFloat_AsDouble(v); |
| 9057 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9058 | return NULL; |
| 9059 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9060 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9061 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9062 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9063 | p = PyOS_double_to_string(x, type, prec, |
| 9064 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9065 | if (p == NULL) |
| 9066 | return NULL; |
| 9067 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9068 | PyMem_Free(p); |
| 9069 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9070 | } |
| 9071 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9072 | static PyObject* |
| 9073 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9074 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9075 | char *buf; |
| 9076 | int len; |
| 9077 | PyObject *str; /* temporary string object. */ |
| 9078 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9079 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9080 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9081 | if (!str) |
| 9082 | return NULL; |
| 9083 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9084 | Py_DECREF(str); |
| 9085 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9086 | } |
| 9087 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9088 | static int |
| 9089 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9090 | size_t buflen, |
| 9091 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9092 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9093 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9094 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9095 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9096 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9097 | buf[1] = '\0'; |
| 9098 | return 1; |
| 9099 | } |
| 9100 | #ifndef Py_UNICODE_WIDE |
| 9101 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9102 | /* Decode a valid surrogate pair */ |
| 9103 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9104 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9105 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9106 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9107 | buf[0] = c0; |
| 9108 | buf[1] = c1; |
| 9109 | buf[2] = '\0'; |
| 9110 | return 2; |
| 9111 | } |
| 9112 | } |
| 9113 | #endif |
| 9114 | goto onError; |
| 9115 | } |
| 9116 | else { |
| 9117 | /* Integer input truncated to a character */ |
| 9118 | long x; |
| 9119 | x = PyLong_AsLong(v); |
| 9120 | if (x == -1 && PyErr_Occurred()) |
| 9121 | goto onError; |
| 9122 | |
| 9123 | if (x < 0 || x > 0x10ffff) { |
| 9124 | PyErr_SetString(PyExc_OverflowError, |
| 9125 | "%c arg not in range(0x110000)"); |
| 9126 | return -1; |
| 9127 | } |
| 9128 | |
| 9129 | #ifndef Py_UNICODE_WIDE |
| 9130 | if (x > 0xffff) { |
| 9131 | x -= 0x10000; |
| 9132 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9133 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9134 | return 2; |
| 9135 | } |
| 9136 | #endif |
| 9137 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9138 | buf[1] = '\0'; |
| 9139 | return 1; |
| 9140 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9141 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9142 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9143 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9144 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9145 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9146 | } |
| 9147 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9148 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9149 | 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] | 9150 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9151 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9152 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9153 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9154 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9155 | { |
| 9156 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9157 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9158 | int args_owned = 0; |
| 9159 | PyUnicodeObject *result = NULL; |
| 9160 | PyObject *dict = NULL; |
| 9161 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9162 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9163 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9164 | PyErr_BadInternalCall(); |
| 9165 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9166 | } |
| 9167 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9168 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9169 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9170 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9171 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9172 | |
| 9173 | reslen = rescnt = fmtcnt + 100; |
| 9174 | result = _PyUnicode_New(reslen); |
| 9175 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9176 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9177 | res = PyUnicode_AS_UNICODE(result); |
| 9178 | |
| 9179 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9180 | arglen = PyTuple_Size(args); |
| 9181 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9182 | } |
| 9183 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9184 | arglen = -1; |
| 9185 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9186 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9187 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9188 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9189 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9190 | |
| 9191 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9192 | if (*fmt != '%') { |
| 9193 | if (--rescnt < 0) { |
| 9194 | rescnt = fmtcnt + 100; |
| 9195 | reslen += rescnt; |
| 9196 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9197 | goto onError; |
| 9198 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9199 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9200 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9201 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9202 | } |
| 9203 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9204 | /* Got a format specifier */ |
| 9205 | int flags = 0; |
| 9206 | Py_ssize_t width = -1; |
| 9207 | int prec = -1; |
| 9208 | Py_UNICODE c = '\0'; |
| 9209 | Py_UNICODE fill; |
| 9210 | int isnumok; |
| 9211 | PyObject *v = NULL; |
| 9212 | PyObject *temp = NULL; |
| 9213 | Py_UNICODE *pbuf; |
| 9214 | Py_UNICODE sign; |
| 9215 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9216 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9217 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9218 | fmt++; |
| 9219 | if (*fmt == '(') { |
| 9220 | Py_UNICODE *keystart; |
| 9221 | Py_ssize_t keylen; |
| 9222 | PyObject *key; |
| 9223 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9224 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9225 | if (dict == NULL) { |
| 9226 | PyErr_SetString(PyExc_TypeError, |
| 9227 | "format requires a mapping"); |
| 9228 | goto onError; |
| 9229 | } |
| 9230 | ++fmt; |
| 9231 | --fmtcnt; |
| 9232 | keystart = fmt; |
| 9233 | /* Skip over balanced parentheses */ |
| 9234 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9235 | if (*fmt == ')') |
| 9236 | --pcount; |
| 9237 | else if (*fmt == '(') |
| 9238 | ++pcount; |
| 9239 | fmt++; |
| 9240 | } |
| 9241 | keylen = fmt - keystart - 1; |
| 9242 | if (fmtcnt < 0 || pcount > 0) { |
| 9243 | PyErr_SetString(PyExc_ValueError, |
| 9244 | "incomplete format key"); |
| 9245 | goto onError; |
| 9246 | } |
| 9247 | #if 0 |
| 9248 | /* keys are converted to strings using UTF-8 and |
| 9249 | then looked up since Python uses strings to hold |
| 9250 | variables names etc. in its namespaces and we |
| 9251 | wouldn't want to break common idioms. */ |
| 9252 | key = PyUnicode_EncodeUTF8(keystart, |
| 9253 | keylen, |
| 9254 | NULL); |
| 9255 | #else |
| 9256 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9257 | #endif |
| 9258 | if (key == NULL) |
| 9259 | goto onError; |
| 9260 | if (args_owned) { |
| 9261 | Py_DECREF(args); |
| 9262 | args_owned = 0; |
| 9263 | } |
| 9264 | args = PyObject_GetItem(dict, key); |
| 9265 | Py_DECREF(key); |
| 9266 | if (args == NULL) { |
| 9267 | goto onError; |
| 9268 | } |
| 9269 | args_owned = 1; |
| 9270 | arglen = -1; |
| 9271 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9272 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9273 | while (--fmtcnt >= 0) { |
| 9274 | switch (c = *fmt++) { |
| 9275 | case '-': flags |= F_LJUST; continue; |
| 9276 | case '+': flags |= F_SIGN; continue; |
| 9277 | case ' ': flags |= F_BLANK; continue; |
| 9278 | case '#': flags |= F_ALT; continue; |
| 9279 | case '0': flags |= F_ZERO; continue; |
| 9280 | } |
| 9281 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9282 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9283 | if (c == '*') { |
| 9284 | v = getnextarg(args, arglen, &argidx); |
| 9285 | if (v == NULL) |
| 9286 | goto onError; |
| 9287 | if (!PyLong_Check(v)) { |
| 9288 | PyErr_SetString(PyExc_TypeError, |
| 9289 | "* wants int"); |
| 9290 | goto onError; |
| 9291 | } |
| 9292 | width = PyLong_AsLong(v); |
| 9293 | if (width == -1 && PyErr_Occurred()) |
| 9294 | goto onError; |
| 9295 | if (width < 0) { |
| 9296 | flags |= F_LJUST; |
| 9297 | width = -width; |
| 9298 | } |
| 9299 | if (--fmtcnt >= 0) |
| 9300 | c = *fmt++; |
| 9301 | } |
| 9302 | else if (c >= '0' && c <= '9') { |
| 9303 | width = c - '0'; |
| 9304 | while (--fmtcnt >= 0) { |
| 9305 | c = *fmt++; |
| 9306 | if (c < '0' || c > '9') |
| 9307 | break; |
| 9308 | if ((width*10) / 10 != width) { |
| 9309 | PyErr_SetString(PyExc_ValueError, |
| 9310 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9311 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9312 | } |
| 9313 | width = width*10 + (c - '0'); |
| 9314 | } |
| 9315 | } |
| 9316 | if (c == '.') { |
| 9317 | prec = 0; |
| 9318 | if (--fmtcnt >= 0) |
| 9319 | c = *fmt++; |
| 9320 | if (c == '*') { |
| 9321 | v = getnextarg(args, arglen, &argidx); |
| 9322 | if (v == NULL) |
| 9323 | goto onError; |
| 9324 | if (!PyLong_Check(v)) { |
| 9325 | PyErr_SetString(PyExc_TypeError, |
| 9326 | "* wants int"); |
| 9327 | goto onError; |
| 9328 | } |
| 9329 | prec = PyLong_AsLong(v); |
| 9330 | if (prec == -1 && PyErr_Occurred()) |
| 9331 | goto onError; |
| 9332 | if (prec < 0) |
| 9333 | prec = 0; |
| 9334 | if (--fmtcnt >= 0) |
| 9335 | c = *fmt++; |
| 9336 | } |
| 9337 | else if (c >= '0' && c <= '9') { |
| 9338 | prec = c - '0'; |
| 9339 | while (--fmtcnt >= 0) { |
| 9340 | c = Py_CHARMASK(*fmt++); |
| 9341 | if (c < '0' || c > '9') |
| 9342 | break; |
| 9343 | if ((prec*10) / 10 != prec) { |
| 9344 | PyErr_SetString(PyExc_ValueError, |
| 9345 | "prec too big"); |
| 9346 | goto onError; |
| 9347 | } |
| 9348 | prec = prec*10 + (c - '0'); |
| 9349 | } |
| 9350 | } |
| 9351 | } /* prec */ |
| 9352 | if (fmtcnt >= 0) { |
| 9353 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9354 | if (--fmtcnt >= 0) |
| 9355 | c = *fmt++; |
| 9356 | } |
| 9357 | } |
| 9358 | if (fmtcnt < 0) { |
| 9359 | PyErr_SetString(PyExc_ValueError, |
| 9360 | "incomplete format"); |
| 9361 | goto onError; |
| 9362 | } |
| 9363 | if (c != '%') { |
| 9364 | v = getnextarg(args, arglen, &argidx); |
| 9365 | if (v == NULL) |
| 9366 | goto onError; |
| 9367 | } |
| 9368 | sign = 0; |
| 9369 | fill = ' '; |
| 9370 | switch (c) { |
| 9371 | |
| 9372 | case '%': |
| 9373 | pbuf = formatbuf; |
| 9374 | /* presume that buffer length is at least 1 */ |
| 9375 | pbuf[0] = '%'; |
| 9376 | len = 1; |
| 9377 | break; |
| 9378 | |
| 9379 | case 's': |
| 9380 | case 'r': |
| 9381 | case 'a': |
| 9382 | if (PyUnicode_Check(v) && c == 's') { |
| 9383 | temp = v; |
| 9384 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9385 | } |
| 9386 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9387 | if (c == 's') |
| 9388 | temp = PyObject_Str(v); |
| 9389 | else if (c == 'r') |
| 9390 | temp = PyObject_Repr(v); |
| 9391 | else |
| 9392 | temp = PyObject_ASCII(v); |
| 9393 | if (temp == NULL) |
| 9394 | goto onError; |
| 9395 | if (PyUnicode_Check(temp)) |
| 9396 | /* nothing to do */; |
| 9397 | else { |
| 9398 | Py_DECREF(temp); |
| 9399 | PyErr_SetString(PyExc_TypeError, |
| 9400 | "%s argument has non-string str()"); |
| 9401 | goto onError; |
| 9402 | } |
| 9403 | } |
| 9404 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9405 | len = PyUnicode_GET_SIZE(temp); |
| 9406 | if (prec >= 0 && len > prec) |
| 9407 | len = prec; |
| 9408 | break; |
| 9409 | |
| 9410 | case 'i': |
| 9411 | case 'd': |
| 9412 | case 'u': |
| 9413 | case 'o': |
| 9414 | case 'x': |
| 9415 | case 'X': |
| 9416 | if (c == 'i') |
| 9417 | c = 'd'; |
| 9418 | isnumok = 0; |
| 9419 | if (PyNumber_Check(v)) { |
| 9420 | PyObject *iobj=NULL; |
| 9421 | |
| 9422 | if (PyLong_Check(v)) { |
| 9423 | iobj = v; |
| 9424 | Py_INCREF(iobj); |
| 9425 | } |
| 9426 | else { |
| 9427 | iobj = PyNumber_Long(v); |
| 9428 | } |
| 9429 | if (iobj!=NULL) { |
| 9430 | if (PyLong_Check(iobj)) { |
| 9431 | isnumok = 1; |
| 9432 | temp = formatlong(iobj, flags, prec, c); |
| 9433 | Py_DECREF(iobj); |
| 9434 | if (!temp) |
| 9435 | goto onError; |
| 9436 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9437 | len = PyUnicode_GET_SIZE(temp); |
| 9438 | sign = 1; |
| 9439 | } |
| 9440 | else { |
| 9441 | Py_DECREF(iobj); |
| 9442 | } |
| 9443 | } |
| 9444 | } |
| 9445 | if (!isnumok) { |
| 9446 | PyErr_Format(PyExc_TypeError, |
| 9447 | "%%%c format: a number is required, " |
| 9448 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9449 | goto onError; |
| 9450 | } |
| 9451 | if (flags & F_ZERO) |
| 9452 | fill = '0'; |
| 9453 | break; |
| 9454 | |
| 9455 | case 'e': |
| 9456 | case 'E': |
| 9457 | case 'f': |
| 9458 | case 'F': |
| 9459 | case 'g': |
| 9460 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9461 | temp = formatfloat(v, flags, prec, c); |
| 9462 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9463 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9464 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9465 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9466 | sign = 1; |
| 9467 | if (flags & F_ZERO) |
| 9468 | fill = '0'; |
| 9469 | break; |
| 9470 | |
| 9471 | case 'c': |
| 9472 | pbuf = formatbuf; |
| 9473 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9474 | if (len < 0) |
| 9475 | goto onError; |
| 9476 | break; |
| 9477 | |
| 9478 | default: |
| 9479 | PyErr_Format(PyExc_ValueError, |
| 9480 | "unsupported format character '%c' (0x%x) " |
| 9481 | "at index %zd", |
| 9482 | (31<=c && c<=126) ? (char)c : '?', |
| 9483 | (int)c, |
| 9484 | (Py_ssize_t)(fmt - 1 - |
| 9485 | PyUnicode_AS_UNICODE(uformat))); |
| 9486 | goto onError; |
| 9487 | } |
| 9488 | if (sign) { |
| 9489 | if (*pbuf == '-' || *pbuf == '+') { |
| 9490 | sign = *pbuf++; |
| 9491 | len--; |
| 9492 | } |
| 9493 | else if (flags & F_SIGN) |
| 9494 | sign = '+'; |
| 9495 | else if (flags & F_BLANK) |
| 9496 | sign = ' '; |
| 9497 | else |
| 9498 | sign = 0; |
| 9499 | } |
| 9500 | if (width < len) |
| 9501 | width = len; |
| 9502 | if (rescnt - (sign != 0) < width) { |
| 9503 | reslen -= rescnt; |
| 9504 | rescnt = width + fmtcnt + 100; |
| 9505 | reslen += rescnt; |
| 9506 | if (reslen < 0) { |
| 9507 | Py_XDECREF(temp); |
| 9508 | PyErr_NoMemory(); |
| 9509 | goto onError; |
| 9510 | } |
| 9511 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9512 | Py_XDECREF(temp); |
| 9513 | goto onError; |
| 9514 | } |
| 9515 | res = PyUnicode_AS_UNICODE(result) |
| 9516 | + reslen - rescnt; |
| 9517 | } |
| 9518 | if (sign) { |
| 9519 | if (fill != ' ') |
| 9520 | *res++ = sign; |
| 9521 | rescnt--; |
| 9522 | if (width > len) |
| 9523 | width--; |
| 9524 | } |
| 9525 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9526 | assert(pbuf[0] == '0'); |
| 9527 | assert(pbuf[1] == c); |
| 9528 | if (fill != ' ') { |
| 9529 | *res++ = *pbuf++; |
| 9530 | *res++ = *pbuf++; |
| 9531 | } |
| 9532 | rescnt -= 2; |
| 9533 | width -= 2; |
| 9534 | if (width < 0) |
| 9535 | width = 0; |
| 9536 | len -= 2; |
| 9537 | } |
| 9538 | if (width > len && !(flags & F_LJUST)) { |
| 9539 | do { |
| 9540 | --rescnt; |
| 9541 | *res++ = fill; |
| 9542 | } while (--width > len); |
| 9543 | } |
| 9544 | if (fill == ' ') { |
| 9545 | if (sign) |
| 9546 | *res++ = sign; |
| 9547 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9548 | assert(pbuf[0] == '0'); |
| 9549 | assert(pbuf[1] == c); |
| 9550 | *res++ = *pbuf++; |
| 9551 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9552 | } |
| 9553 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9554 | Py_UNICODE_COPY(res, pbuf, len); |
| 9555 | res += len; |
| 9556 | rescnt -= len; |
| 9557 | while (--width >= len) { |
| 9558 | --rescnt; |
| 9559 | *res++ = ' '; |
| 9560 | } |
| 9561 | if (dict && (argidx < arglen) && c != '%') { |
| 9562 | PyErr_SetString(PyExc_TypeError, |
| 9563 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9564 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9565 | goto onError; |
| 9566 | } |
| 9567 | Py_XDECREF(temp); |
| 9568 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9569 | } /* until end */ |
| 9570 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9571 | PyErr_SetString(PyExc_TypeError, |
| 9572 | "not all arguments converted during string formatting"); |
| 9573 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9574 | } |
| 9575 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9576 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9577 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9578 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9579 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9580 | } |
| 9581 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9582 | return (PyObject *)result; |
| 9583 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9584 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9585 | Py_XDECREF(result); |
| 9586 | Py_DECREF(uformat); |
| 9587 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9588 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9589 | } |
| 9590 | return NULL; |
| 9591 | } |
| 9592 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9593 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9594 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9595 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9596 | static PyObject * |
| 9597 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9598 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9599 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9600 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9601 | char *encoding = NULL; |
| 9602 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9603 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9604 | if (type != &PyUnicode_Type) |
| 9605 | return unicode_subtype_new(type, args, kwds); |
| 9606 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9607 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9608 | return NULL; |
| 9609 | if (x == NULL) |
| 9610 | return (PyObject *)_PyUnicode_New(0); |
| 9611 | if (encoding == NULL && errors == NULL) |
| 9612 | return PyObject_Str(x); |
| 9613 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9614 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9615 | } |
| 9616 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9617 | static PyObject * |
| 9618 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9619 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9620 | PyUnicodeObject *tmp, *pnew; |
| 9621 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9622 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9623 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9624 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9625 | if (tmp == NULL) |
| 9626 | return NULL; |
| 9627 | assert(PyUnicode_Check(tmp)); |
| 9628 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9629 | if (pnew == NULL) { |
| 9630 | Py_DECREF(tmp); |
| 9631 | return NULL; |
| 9632 | } |
| 9633 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9634 | if (pnew->str == NULL) { |
| 9635 | _Py_ForgetReference((PyObject *)pnew); |
| 9636 | PyObject_Del(pnew); |
| 9637 | Py_DECREF(tmp); |
| 9638 | return PyErr_NoMemory(); |
| 9639 | } |
| 9640 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9641 | pnew->length = n; |
| 9642 | pnew->hash = tmp->hash; |
| 9643 | Py_DECREF(tmp); |
| 9644 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9645 | } |
| 9646 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9647 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9648 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9649 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9650 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9651 | encoding defaults to the current default string encoding.\n\ |
| 9652 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9653 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9654 | static PyObject *unicode_iter(PyObject *seq); |
| 9655 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9656 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9657 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9658 | "str", /* tp_name */ |
| 9659 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9660 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9661 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9662 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9663 | 0, /* tp_print */ |
| 9664 | 0, /* tp_getattr */ |
| 9665 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9666 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9667 | unicode_repr, /* tp_repr */ |
| 9668 | &unicode_as_number, /* tp_as_number */ |
| 9669 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9670 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9671 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9672 | 0, /* tp_call*/ |
| 9673 | (reprfunc) unicode_str, /* tp_str */ |
| 9674 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9675 | 0, /* tp_setattro */ |
| 9676 | 0, /* tp_as_buffer */ |
| 9677 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9678 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9679 | unicode_doc, /* tp_doc */ |
| 9680 | 0, /* tp_traverse */ |
| 9681 | 0, /* tp_clear */ |
| 9682 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9683 | 0, /* tp_weaklistoffset */ |
| 9684 | unicode_iter, /* tp_iter */ |
| 9685 | 0, /* tp_iternext */ |
| 9686 | unicode_methods, /* tp_methods */ |
| 9687 | 0, /* tp_members */ |
| 9688 | 0, /* tp_getset */ |
| 9689 | &PyBaseObject_Type, /* tp_base */ |
| 9690 | 0, /* tp_dict */ |
| 9691 | 0, /* tp_descr_get */ |
| 9692 | 0, /* tp_descr_set */ |
| 9693 | 0, /* tp_dictoffset */ |
| 9694 | 0, /* tp_init */ |
| 9695 | 0, /* tp_alloc */ |
| 9696 | unicode_new, /* tp_new */ |
| 9697 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9698 | }; |
| 9699 | |
| 9700 | /* Initialize the Unicode implementation */ |
| 9701 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9702 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9703 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9704 | int i; |
| 9705 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9706 | /* XXX - move this array to unicodectype.c ? */ |
| 9707 | Py_UNICODE linebreak[] = { |
| 9708 | 0x000A, /* LINE FEED */ |
| 9709 | 0x000D, /* CARRIAGE RETURN */ |
| 9710 | 0x001C, /* FILE SEPARATOR */ |
| 9711 | 0x001D, /* GROUP SEPARATOR */ |
| 9712 | 0x001E, /* RECORD SEPARATOR */ |
| 9713 | 0x0085, /* NEXT LINE */ |
| 9714 | 0x2028, /* LINE SEPARATOR */ |
| 9715 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9716 | }; |
| 9717 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9718 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9719 | free_list = NULL; |
| 9720 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9721 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9722 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9723 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9724 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9725 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9726 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9727 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9728 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9729 | |
| 9730 | /* initialize the linebreak bloom filter */ |
| 9731 | bloom_linebreak = make_bloom_mask( |
| 9732 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9733 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9734 | |
| 9735 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9736 | } |
| 9737 | |
| 9738 | /* Finalize the Unicode implementation */ |
| 9739 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9740 | int |
| 9741 | PyUnicode_ClearFreeList(void) |
| 9742 | { |
| 9743 | int freelist_size = numfree; |
| 9744 | PyUnicodeObject *u; |
| 9745 | |
| 9746 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9747 | PyUnicodeObject *v = u; |
| 9748 | u = *(PyUnicodeObject **)u; |
| 9749 | if (v->str) |
| 9750 | PyObject_DEL(v->str); |
| 9751 | Py_XDECREF(v->defenc); |
| 9752 | PyObject_Del(v); |
| 9753 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9754 | } |
| 9755 | free_list = NULL; |
| 9756 | assert(numfree == 0); |
| 9757 | return freelist_size; |
| 9758 | } |
| 9759 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9760 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9761 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9762 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9763 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9764 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9765 | Py_XDECREF(unicode_empty); |
| 9766 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9767 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9768 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9769 | if (unicode_latin1[i]) { |
| 9770 | Py_DECREF(unicode_latin1[i]); |
| 9771 | unicode_latin1[i] = NULL; |
| 9772 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9773 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9774 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9775 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9776 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9777 | void |
| 9778 | PyUnicode_InternInPlace(PyObject **p) |
| 9779 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9780 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9781 | PyObject *t; |
| 9782 | if (s == NULL || !PyUnicode_Check(s)) |
| 9783 | Py_FatalError( |
| 9784 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9785 | /* If it's a subclass, we don't really know what putting |
| 9786 | it in the interned dict might do. */ |
| 9787 | if (!PyUnicode_CheckExact(s)) |
| 9788 | return; |
| 9789 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9790 | return; |
| 9791 | if (interned == NULL) { |
| 9792 | interned = PyDict_New(); |
| 9793 | if (interned == NULL) { |
| 9794 | PyErr_Clear(); /* Don't leave an exception */ |
| 9795 | return; |
| 9796 | } |
| 9797 | } |
| 9798 | /* It might be that the GetItem call fails even |
| 9799 | though the key is present in the dictionary, |
| 9800 | namely when this happens during a stack overflow. */ |
| 9801 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9802 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9803 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9804 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9805 | if (t) { |
| 9806 | Py_INCREF(t); |
| 9807 | Py_DECREF(*p); |
| 9808 | *p = t; |
| 9809 | return; |
| 9810 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9811 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9812 | PyThreadState_GET()->recursion_critical = 1; |
| 9813 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9814 | PyErr_Clear(); |
| 9815 | PyThreadState_GET()->recursion_critical = 0; |
| 9816 | return; |
| 9817 | } |
| 9818 | PyThreadState_GET()->recursion_critical = 0; |
| 9819 | /* The two references in interned are not counted by refcnt. |
| 9820 | The deallocator will take care of this */ |
| 9821 | Py_REFCNT(s) -= 2; |
| 9822 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9823 | } |
| 9824 | |
| 9825 | void |
| 9826 | PyUnicode_InternImmortal(PyObject **p) |
| 9827 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9828 | PyUnicode_InternInPlace(p); |
| 9829 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9830 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9831 | Py_INCREF(*p); |
| 9832 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9833 | } |
| 9834 | |
| 9835 | PyObject * |
| 9836 | PyUnicode_InternFromString(const char *cp) |
| 9837 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9838 | PyObject *s = PyUnicode_FromString(cp); |
| 9839 | if (s == NULL) |
| 9840 | return NULL; |
| 9841 | PyUnicode_InternInPlace(&s); |
| 9842 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9843 | } |
| 9844 | |
| 9845 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9846 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9847 | PyObject *keys; |
| 9848 | PyUnicodeObject *s; |
| 9849 | Py_ssize_t i, n; |
| 9850 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9851 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9852 | if (interned == NULL || !PyDict_Check(interned)) |
| 9853 | return; |
| 9854 | keys = PyDict_Keys(interned); |
| 9855 | if (keys == NULL || !PyList_Check(keys)) { |
| 9856 | PyErr_Clear(); |
| 9857 | return; |
| 9858 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9859 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9860 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9861 | detector, interned unicode strings are not forcibly deallocated; |
| 9862 | rather, we give them their stolen references back, and then clear |
| 9863 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9864 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9865 | n = PyList_GET_SIZE(keys); |
| 9866 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9867 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9868 | for (i = 0; i < n; i++) { |
| 9869 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9870 | switch (s->state) { |
| 9871 | case SSTATE_NOT_INTERNED: |
| 9872 | /* XXX Shouldn't happen */ |
| 9873 | break; |
| 9874 | case SSTATE_INTERNED_IMMORTAL: |
| 9875 | Py_REFCNT(s) += 1; |
| 9876 | immortal_size += s->length; |
| 9877 | break; |
| 9878 | case SSTATE_INTERNED_MORTAL: |
| 9879 | Py_REFCNT(s) += 2; |
| 9880 | mortal_size += s->length; |
| 9881 | break; |
| 9882 | default: |
| 9883 | Py_FatalError("Inconsistent interned string state."); |
| 9884 | } |
| 9885 | s->state = SSTATE_NOT_INTERNED; |
| 9886 | } |
| 9887 | fprintf(stderr, "total size of all interned strings: " |
| 9888 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9889 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9890 | Py_DECREF(keys); |
| 9891 | PyDict_Clear(interned); |
| 9892 | Py_DECREF(interned); |
| 9893 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9894 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9895 | |
| 9896 | |
| 9897 | /********************* Unicode Iterator **************************/ |
| 9898 | |
| 9899 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9900 | PyObject_HEAD |
| 9901 | Py_ssize_t it_index; |
| 9902 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9903 | } unicodeiterobject; |
| 9904 | |
| 9905 | static void |
| 9906 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9907 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9908 | _PyObject_GC_UNTRACK(it); |
| 9909 | Py_XDECREF(it->it_seq); |
| 9910 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9911 | } |
| 9912 | |
| 9913 | static int |
| 9914 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9915 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9916 | Py_VISIT(it->it_seq); |
| 9917 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9918 | } |
| 9919 | |
| 9920 | static PyObject * |
| 9921 | unicodeiter_next(unicodeiterobject *it) |
| 9922 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9923 | PyUnicodeObject *seq; |
| 9924 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9925 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9926 | assert(it != NULL); |
| 9927 | seq = it->it_seq; |
| 9928 | if (seq == NULL) |
| 9929 | return NULL; |
| 9930 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9931 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9932 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9933 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9934 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9935 | if (item != NULL) |
| 9936 | ++it->it_index; |
| 9937 | return item; |
| 9938 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9939 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9940 | Py_DECREF(seq); |
| 9941 | it->it_seq = NULL; |
| 9942 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9943 | } |
| 9944 | |
| 9945 | static PyObject * |
| 9946 | unicodeiter_len(unicodeiterobject *it) |
| 9947 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9948 | Py_ssize_t len = 0; |
| 9949 | if (it->it_seq) |
| 9950 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9951 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9952 | } |
| 9953 | |
| 9954 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9955 | |
| 9956 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9957 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9958 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9959 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9960 | }; |
| 9961 | |
| 9962 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9963 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9964 | "str_iterator", /* tp_name */ |
| 9965 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9966 | 0, /* tp_itemsize */ |
| 9967 | /* methods */ |
| 9968 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9969 | 0, /* tp_print */ |
| 9970 | 0, /* tp_getattr */ |
| 9971 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9972 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9973 | 0, /* tp_repr */ |
| 9974 | 0, /* tp_as_number */ |
| 9975 | 0, /* tp_as_sequence */ |
| 9976 | 0, /* tp_as_mapping */ |
| 9977 | 0, /* tp_hash */ |
| 9978 | 0, /* tp_call */ |
| 9979 | 0, /* tp_str */ |
| 9980 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9981 | 0, /* tp_setattro */ |
| 9982 | 0, /* tp_as_buffer */ |
| 9983 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9984 | 0, /* tp_doc */ |
| 9985 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9986 | 0, /* tp_clear */ |
| 9987 | 0, /* tp_richcompare */ |
| 9988 | 0, /* tp_weaklistoffset */ |
| 9989 | PyObject_SelfIter, /* tp_iter */ |
| 9990 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9991 | unicodeiter_methods, /* tp_methods */ |
| 9992 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9993 | }; |
| 9994 | |
| 9995 | static PyObject * |
| 9996 | unicode_iter(PyObject *seq) |
| 9997 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9998 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9999 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10000 | if (!PyUnicode_Check(seq)) { |
| 10001 | PyErr_BadInternalCall(); |
| 10002 | return NULL; |
| 10003 | } |
| 10004 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 10005 | if (it == NULL) |
| 10006 | return NULL; |
| 10007 | it->it_index = 0; |
| 10008 | Py_INCREF(seq); |
| 10009 | it->it_seq = (PyUnicodeObject *)seq; |
| 10010 | _PyObject_GC_TRACK(it); |
| 10011 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 10012 | } |
| 10013 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10014 | size_t |
| 10015 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 10016 | { |
| 10017 | int res = 0; |
| 10018 | while(*u++) |
| 10019 | res++; |
| 10020 | return res; |
| 10021 | } |
| 10022 | |
| 10023 | Py_UNICODE* |
| 10024 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10025 | { |
| 10026 | Py_UNICODE *u = s1; |
| 10027 | while ((*u++ = *s2++)); |
| 10028 | return s1; |
| 10029 | } |
| 10030 | |
| 10031 | Py_UNICODE* |
| 10032 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 10033 | { |
| 10034 | Py_UNICODE *u = s1; |
| 10035 | while ((*u++ = *s2++)) |
| 10036 | if (n-- == 0) |
| 10037 | break; |
| 10038 | return s1; |
| 10039 | } |
| 10040 | |
| 10041 | int |
| 10042 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10043 | { |
| 10044 | while (*s1 && *s2 && *s1 == *s2) |
| 10045 | s1++, s2++; |
| 10046 | if (*s1 && *s2) |
| 10047 | return (*s1 < *s2) ? -1 : +1; |
| 10048 | if (*s1) |
| 10049 | return 1; |
| 10050 | if (*s2) |
| 10051 | return -1; |
| 10052 | return 0; |
| 10053 | } |
| 10054 | |
| 10055 | Py_UNICODE* |
| 10056 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10057 | { |
| 10058 | const Py_UNICODE *p; |
| 10059 | for (p = s; *p; p++) |
| 10060 | if (*p == c) |
| 10061 | return (Py_UNICODE*)p; |
| 10062 | return NULL; |
| 10063 | } |
| 10064 | |
| 10065 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10066 | #ifdef __cplusplus |
| 10067 | } |
| 10068 | #endif |
| 10069 | |
| 10070 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 10071 | /* |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10072 | Local variables: |
| 10073 | c-basic-offset: 4 |
| 10074 | indent-tabs-mode: nil |
| 10075 | End: |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 10076 | */ |