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 |
| 670 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 671 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 672 | *fmt++ = '%'; |
| 673 | if (width) { |
| 674 | if (zeropad) |
| 675 | *fmt++ = '0'; |
| 676 | fmt += sprintf(fmt, "%d", width); |
| 677 | } |
| 678 | if (precision) |
| 679 | fmt += sprintf(fmt, ".%d", precision); |
| 680 | if (longflag) |
| 681 | *fmt++ = 'l'; |
| 682 | else if (size_tflag) { |
| 683 | char *f = PY_FORMAT_SIZE_T; |
| 684 | while (*f) |
| 685 | *fmt++ = *f++; |
| 686 | } |
| 687 | *fmt++ = c; |
| 688 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 691 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 692 | |
| 693 | PyObject * |
| 694 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 695 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 696 | va_list count; |
| 697 | Py_ssize_t callcount = 0; |
| 698 | PyObject **callresults = NULL; |
| 699 | PyObject **callresult = NULL; |
| 700 | Py_ssize_t n = 0; |
| 701 | int width = 0; |
| 702 | int precision = 0; |
| 703 | int zeropad; |
| 704 | const char* f; |
| 705 | Py_UNICODE *s; |
| 706 | PyObject *string; |
| 707 | /* used by sprintf */ |
| 708 | char buffer[21]; |
| 709 | /* use abuffer instead of buffer, if we need more space |
| 710 | * (which can happen if there's a format specifier with width). */ |
| 711 | char *abuffer = NULL; |
| 712 | char *realbuffer; |
| 713 | Py_ssize_t abuffersize = 0; |
| 714 | char fmt[60]; /* should be enough for %0width.precisionld */ |
| 715 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 716 | |
| 717 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 718 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 719 | #else |
| 720 | #ifdef __va_copy |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 721 | __va_copy(count, vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 722 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 723 | count = vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 724 | #endif |
| 725 | #endif |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 726 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 727 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 728 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 729 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 730 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 731 | if (*f == '%') { |
| 732 | if (*(f+1)=='%') |
| 733 | continue; |
| 734 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 735 | ++callcount; |
| 736 | while (ISDIGIT((unsigned)*f)) |
| 737 | width = (width*10) + *f++ - '0'; |
| 738 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 739 | ; |
| 740 | if (*f == 's') |
| 741 | ++callcount; |
| 742 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 743 | } |
| 744 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 745 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 746 | if (callcount) { |
| 747 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 748 | if (!callresults) { |
| 749 | PyErr_NoMemory(); |
| 750 | return NULL; |
| 751 | } |
| 752 | callresult = callresults; |
| 753 | } |
| 754 | /* step 3: figure out how large a buffer we need */ |
| 755 | for (f = format; *f; f++) { |
| 756 | if (*f == '%') { |
| 757 | const char* p = f; |
| 758 | width = 0; |
| 759 | while (ISDIGIT((unsigned)*f)) |
| 760 | width = (width*10) + *f++ - '0'; |
| 761 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 762 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 763 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 764 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 765 | * they don't affect the amount of space we reserve. |
| 766 | */ |
| 767 | if ((*f == 'l' || *f == 'z') && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 768 | (f[1] == 'd' || f[1] == 'u')) |
| 769 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 770 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 771 | switch (*f) { |
| 772 | case 'c': |
| 773 | (void)va_arg(count, int); |
| 774 | /* fall through... */ |
| 775 | case '%': |
| 776 | n++; |
| 777 | break; |
| 778 | case 'd': case 'u': case 'i': case 'x': |
| 779 | (void) va_arg(count, int); |
| 780 | /* 20 bytes is enough to hold a 64-bit |
| 781 | integer. Decimal takes the most space. |
| 782 | This isn't enough for octal. |
| 783 | If a width is specified we need more |
| 784 | (which we allocate later). */ |
| 785 | if (width < 20) |
| 786 | width = 20; |
| 787 | n += width; |
| 788 | if (abuffersize < width) |
| 789 | abuffersize = width; |
| 790 | break; |
| 791 | case 's': |
| 792 | { |
| 793 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 794 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 795 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 796 | if (!str) |
| 797 | goto fail; |
| 798 | n += PyUnicode_GET_SIZE(str); |
| 799 | /* Remember the str and switch to the next slot */ |
| 800 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 801 | break; |
| 802 | } |
| 803 | case 'U': |
| 804 | { |
| 805 | PyObject *obj = va_arg(count, PyObject *); |
| 806 | assert(obj && PyUnicode_Check(obj)); |
| 807 | n += PyUnicode_GET_SIZE(obj); |
| 808 | break; |
| 809 | } |
| 810 | case 'V': |
| 811 | { |
| 812 | PyObject *obj = va_arg(count, PyObject *); |
| 813 | const char *str = va_arg(count, const char *); |
| 814 | assert(obj || str); |
| 815 | assert(!obj || PyUnicode_Check(obj)); |
| 816 | if (obj) |
| 817 | n += PyUnicode_GET_SIZE(obj); |
| 818 | else |
| 819 | n += strlen(str); |
| 820 | break; |
| 821 | } |
| 822 | case 'S': |
| 823 | { |
| 824 | PyObject *obj = va_arg(count, PyObject *); |
| 825 | PyObject *str; |
| 826 | assert(obj); |
| 827 | str = PyObject_Str(obj); |
| 828 | if (!str) |
| 829 | goto fail; |
| 830 | n += PyUnicode_GET_SIZE(str); |
| 831 | /* Remember the str and switch to the next slot */ |
| 832 | *callresult++ = str; |
| 833 | break; |
| 834 | } |
| 835 | case 'R': |
| 836 | { |
| 837 | PyObject *obj = va_arg(count, PyObject *); |
| 838 | PyObject *repr; |
| 839 | assert(obj); |
| 840 | repr = PyObject_Repr(obj); |
| 841 | if (!repr) |
| 842 | goto fail; |
| 843 | n += PyUnicode_GET_SIZE(repr); |
| 844 | /* Remember the repr and switch to the next slot */ |
| 845 | *callresult++ = repr; |
| 846 | break; |
| 847 | } |
| 848 | case 'A': |
| 849 | { |
| 850 | PyObject *obj = va_arg(count, PyObject *); |
| 851 | PyObject *ascii; |
| 852 | assert(obj); |
| 853 | ascii = PyObject_ASCII(obj); |
| 854 | if (!ascii) |
| 855 | goto fail; |
| 856 | n += PyUnicode_GET_SIZE(ascii); |
| 857 | /* Remember the repr and switch to the next slot */ |
| 858 | *callresult++ = ascii; |
| 859 | break; |
| 860 | } |
| 861 | case 'p': |
| 862 | (void) va_arg(count, int); |
| 863 | /* maximum 64-bit pointer representation: |
| 864 | * 0xffffffffffffffff |
| 865 | * so 19 characters is enough. |
| 866 | * XXX I count 18 -- what's the extra for? |
| 867 | */ |
| 868 | n += 19; |
| 869 | break; |
| 870 | default: |
| 871 | /* if we stumble upon an unknown |
| 872 | formatting code, copy the rest of |
| 873 | the format string to the output |
| 874 | string. (we cannot just skip the |
| 875 | code, since there's no way to know |
| 876 | what's in the argument list) */ |
| 877 | n += strlen(p); |
| 878 | goto expand; |
| 879 | } |
| 880 | } else |
| 881 | n++; |
| 882 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 883 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 884 | if (abuffersize > 20) { |
| 885 | abuffer = PyObject_Malloc(abuffersize); |
| 886 | if (!abuffer) { |
| 887 | PyErr_NoMemory(); |
| 888 | goto fail; |
| 889 | } |
| 890 | realbuffer = abuffer; |
| 891 | } |
| 892 | else |
| 893 | realbuffer = buffer; |
| 894 | /* step 4: fill the buffer */ |
| 895 | /* Since we've analyzed how much space we need for the worst case, |
| 896 | we don't have to resize the string. |
| 897 | There can be no errors beyond this point. */ |
| 898 | string = PyUnicode_FromUnicode(NULL, n); |
| 899 | if (!string) |
| 900 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 901 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 902 | s = PyUnicode_AS_UNICODE(string); |
| 903 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 904 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 905 | for (f = format; *f; f++) { |
| 906 | if (*f == '%') { |
| 907 | const char* p = f++; |
| 908 | int longflag = 0; |
| 909 | int size_tflag = 0; |
| 910 | zeropad = (*f == '0'); |
| 911 | /* parse the width.precision part */ |
| 912 | width = 0; |
| 913 | while (ISDIGIT((unsigned)*f)) |
| 914 | width = (width*10) + *f++ - '0'; |
| 915 | precision = 0; |
| 916 | if (*f == '.') { |
| 917 | f++; |
| 918 | while (ISDIGIT((unsigned)*f)) |
| 919 | precision = (precision*10) + *f++ - '0'; |
| 920 | } |
| 921 | /* handle the long flag, but only for %ld and %lu. |
| 922 | others can be added when necessary. */ |
| 923 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 924 | longflag = 1; |
| 925 | ++f; |
| 926 | } |
| 927 | /* handle the size_t flag. */ |
| 928 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 929 | size_tflag = 1; |
| 930 | ++f; |
| 931 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 932 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 933 | switch (*f) { |
| 934 | case 'c': |
| 935 | *s++ = va_arg(vargs, int); |
| 936 | break; |
| 937 | case 'd': |
| 938 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
| 939 | if (longflag) |
| 940 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
| 941 | else if (size_tflag) |
| 942 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 943 | else |
| 944 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 945 | appendstring(realbuffer); |
| 946 | break; |
| 947 | case 'u': |
| 948 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
| 949 | if (longflag) |
| 950 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
| 951 | else if (size_tflag) |
| 952 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 953 | else |
| 954 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 955 | appendstring(realbuffer); |
| 956 | break; |
| 957 | case 'i': |
| 958 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 959 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 960 | appendstring(realbuffer); |
| 961 | break; |
| 962 | case 'x': |
| 963 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 964 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 965 | appendstring(realbuffer); |
| 966 | break; |
| 967 | case 's': |
| 968 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 969 | /* unused, since we already have the result */ |
| 970 | (void) va_arg(vargs, char *); |
| 971 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 972 | PyUnicode_GET_SIZE(*callresult)); |
| 973 | s += PyUnicode_GET_SIZE(*callresult); |
| 974 | /* We're done with the unicode()/repr() => forget it */ |
| 975 | Py_DECREF(*callresult); |
| 976 | /* switch to next unicode()/repr() result */ |
| 977 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 978 | break; |
| 979 | } |
| 980 | case 'U': |
| 981 | { |
| 982 | PyObject *obj = va_arg(vargs, PyObject *); |
| 983 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 984 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 985 | s += size; |
| 986 | break; |
| 987 | } |
| 988 | case 'V': |
| 989 | { |
| 990 | PyObject *obj = va_arg(vargs, PyObject *); |
| 991 | const char *str = va_arg(vargs, const char *); |
| 992 | if (obj) { |
| 993 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 994 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 995 | s += size; |
| 996 | } else { |
| 997 | appendstring(str); |
| 998 | } |
| 999 | break; |
| 1000 | } |
| 1001 | case 'S': |
| 1002 | case 'R': |
| 1003 | { |
| 1004 | Py_UNICODE *ucopy; |
| 1005 | Py_ssize_t usize; |
| 1006 | Py_ssize_t upos; |
| 1007 | /* unused, since we already have the result */ |
| 1008 | (void) va_arg(vargs, PyObject *); |
| 1009 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1010 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1011 | for (upos = 0; upos<usize;) |
| 1012 | *s++ = ucopy[upos++]; |
| 1013 | /* We're done with the unicode()/repr() => forget it */ |
| 1014 | Py_DECREF(*callresult); |
| 1015 | /* switch to next unicode()/repr() result */ |
| 1016 | ++callresult; |
| 1017 | break; |
| 1018 | } |
| 1019 | case 'p': |
| 1020 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1021 | /* %p is ill-defined: ensure leading 0x. */ |
| 1022 | if (buffer[1] == 'X') |
| 1023 | buffer[1] = 'x'; |
| 1024 | else if (buffer[1] != 'x') { |
| 1025 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1026 | buffer[0] = '0'; |
| 1027 | buffer[1] = 'x'; |
| 1028 | } |
| 1029 | appendstring(buffer); |
| 1030 | break; |
| 1031 | case '%': |
| 1032 | *s++ = '%'; |
| 1033 | break; |
| 1034 | default: |
| 1035 | appendstring(p); |
| 1036 | goto end; |
| 1037 | } |
| 1038 | } else |
| 1039 | *s++ = *f; |
| 1040 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1041 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1042 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1043 | if (callresults) |
| 1044 | PyObject_Free(callresults); |
| 1045 | if (abuffer) |
| 1046 | PyObject_Free(abuffer); |
| 1047 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1048 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1049 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1050 | if (callresults) { |
| 1051 | PyObject **callresult2 = callresults; |
| 1052 | while (callresult2 < callresult) { |
| 1053 | Py_DECREF(*callresult2); |
| 1054 | ++callresult2; |
| 1055 | } |
| 1056 | PyObject_Free(callresults); |
| 1057 | } |
| 1058 | if (abuffer) |
| 1059 | PyObject_Free(abuffer); |
| 1060 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | #undef appendstring |
| 1064 | |
| 1065 | PyObject * |
| 1066 | PyUnicode_FromFormat(const char *format, ...) |
| 1067 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1068 | PyObject* ret; |
| 1069 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1070 | |
| 1071 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1072 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1073 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1074 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1075 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1076 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1077 | va_end(vargs); |
| 1078 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1081 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1082 | wchar_t *w, |
| 1083 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1084 | { |
| 1085 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1086 | PyErr_BadInternalCall(); |
| 1087 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1088 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1091 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1092 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1093 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1094 | #ifdef HAVE_USABLE_WCHAR_T |
| 1095 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1096 | #else |
| 1097 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1098 | register Py_UNICODE *u; |
| 1099 | register Py_ssize_t i; |
| 1100 | u = PyUnicode_AS_UNICODE(unicode); |
| 1101 | for (i = size; i > 0; i--) |
| 1102 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1103 | } |
| 1104 | #endif |
| 1105 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1106 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1107 | return PyUnicode_GET_SIZE(unicode); |
| 1108 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1109 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | #endif |
| 1113 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1114 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1115 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1116 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1117 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1118 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1119 | PyErr_SetString(PyExc_ValueError, |
| 1120 | "chr() arg not in range(0x110000)"); |
| 1121 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1122 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1123 | |
| 1124 | #ifndef Py_UNICODE_WIDE |
| 1125 | if (ordinal > 0xffff) { |
| 1126 | ordinal -= 0x10000; |
| 1127 | s[0] = 0xD800 | (ordinal >> 10); |
| 1128 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1129 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1130 | } |
| 1131 | #endif |
| 1132 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1133 | s[0] = (Py_UNICODE)ordinal; |
| 1134 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1137 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1138 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1139 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1140 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1141 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1142 | Py_INCREF(obj); |
| 1143 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1144 | } |
| 1145 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1146 | /* For a Unicode subtype that's not a Unicode object, |
| 1147 | return a true Unicode object with the same data. */ |
| 1148 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1149 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1150 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1151 | PyErr_Format(PyExc_TypeError, |
| 1152 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1153 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1154 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1158 | const char *encoding, |
| 1159 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1160 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1161 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1162 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1163 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1164 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1165 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1166 | PyErr_BadInternalCall(); |
| 1167 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1168 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1169 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1170 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1171 | PyErr_SetString(PyExc_TypeError, |
| 1172 | "decoding str is not supported"); |
| 1173 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1174 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1175 | |
| 1176 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1177 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1178 | s = PyBytes_AS_STRING(obj); |
| 1179 | len = PyBytes_GET_SIZE(obj); |
| 1180 | } |
| 1181 | else if (PyByteArray_Check(obj)) { |
| 1182 | s = PyByteArray_AS_STRING(obj); |
| 1183 | len = PyByteArray_GET_SIZE(obj); |
| 1184 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1185 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1186 | /* Overwrite the error message with something more useful in |
| 1187 | case of a TypeError. */ |
| 1188 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1189 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1190 | "coercing to str: need string or buffer, " |
| 1191 | "%.80s found", |
| 1192 | Py_TYPE(obj)->tp_name); |
| 1193 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1194 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1195 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1196 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1197 | if (len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1198 | Py_INCREF(unicode_empty); |
| 1199 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1200 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1201 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1202 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1203 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1204 | return v; |
| 1205 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1206 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1207 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1211 | Py_ssize_t size, |
| 1212 | const char *encoding, |
| 1213 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1214 | { |
| 1215 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1216 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1217 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1218 | char *l; |
| 1219 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1220 | |
| 1221 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1222 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1223 | |
| 1224 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1225 | catch e.g. UTF_8 */ |
| 1226 | e = encoding; |
| 1227 | l = lower; |
| 1228 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1229 | if (ISUPPER(*e)) { |
| 1230 | *l++ = TOLOWER(*e++); |
| 1231 | } |
| 1232 | else if (*e == '_') { |
| 1233 | *l++ = '-'; |
| 1234 | e++; |
| 1235 | } |
| 1236 | else { |
| 1237 | *l++ = *e++; |
| 1238 | } |
| 1239 | } |
| 1240 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1243 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1244 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1245 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1246 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1247 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1248 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1249 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1250 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1251 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1252 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1253 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1254 | else if (strcmp(lower, "utf-16") == 0) |
| 1255 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1256 | else if (strcmp(lower, "utf-32") == 0) |
| 1257 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1258 | |
| 1259 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1260 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1261 | 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] | 1262 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1263 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1264 | if (buffer == NULL) |
| 1265 | goto onError; |
| 1266 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1267 | if (unicode == NULL) |
| 1268 | goto onError; |
| 1269 | if (!PyUnicode_Check(unicode)) { |
| 1270 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1271 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1272 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1273 | Py_DECREF(unicode); |
| 1274 | goto onError; |
| 1275 | } |
| 1276 | Py_DECREF(buffer); |
| 1277 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1278 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1279 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1280 | Py_XDECREF(buffer); |
| 1281 | return NULL; |
| 1282 | } |
| 1283 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1284 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1285 | const char *encoding, |
| 1286 | const char *errors) |
| 1287 | { |
| 1288 | PyObject *v; |
| 1289 | |
| 1290 | if (!PyUnicode_Check(unicode)) { |
| 1291 | PyErr_BadArgument(); |
| 1292 | goto onError; |
| 1293 | } |
| 1294 | |
| 1295 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1296 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1297 | |
| 1298 | /* Decode via the codec registry */ |
| 1299 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1300 | if (v == NULL) |
| 1301 | goto onError; |
| 1302 | return v; |
| 1303 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1304 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1305 | return NULL; |
| 1306 | } |
| 1307 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1308 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1309 | const char *encoding, |
| 1310 | const char *errors) |
| 1311 | { |
| 1312 | PyObject *v; |
| 1313 | |
| 1314 | if (!PyUnicode_Check(unicode)) { |
| 1315 | PyErr_BadArgument(); |
| 1316 | goto onError; |
| 1317 | } |
| 1318 | |
| 1319 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1320 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1321 | |
| 1322 | /* Decode via the codec registry */ |
| 1323 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1324 | if (v == NULL) |
| 1325 | goto onError; |
| 1326 | if (!PyUnicode_Check(v)) { |
| 1327 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1328 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1329 | Py_TYPE(v)->tp_name); |
| 1330 | Py_DECREF(v); |
| 1331 | goto onError; |
| 1332 | } |
| 1333 | return v; |
| 1334 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1335 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1336 | return NULL; |
| 1337 | } |
| 1338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1339 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1340 | Py_ssize_t size, |
| 1341 | const char *encoding, |
| 1342 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1343 | { |
| 1344 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1345 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1346 | unicode = PyUnicode_FromUnicode(s, size); |
| 1347 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1348 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1349 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1350 | Py_DECREF(unicode); |
| 1351 | return v; |
| 1352 | } |
| 1353 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1354 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1355 | const char *encoding, |
| 1356 | const char *errors) |
| 1357 | { |
| 1358 | PyObject *v; |
| 1359 | |
| 1360 | if (!PyUnicode_Check(unicode)) { |
| 1361 | PyErr_BadArgument(); |
| 1362 | goto onError; |
| 1363 | } |
| 1364 | |
| 1365 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1366 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1367 | |
| 1368 | /* Encode via the codec registry */ |
| 1369 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1370 | if (v == NULL) |
| 1371 | goto onError; |
| 1372 | return v; |
| 1373 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1374 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1375 | return NULL; |
| 1376 | } |
| 1377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1378 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1379 | const char *encoding, |
| 1380 | const char *errors) |
| 1381 | { |
| 1382 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1383 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1384 | if (!PyUnicode_Check(unicode)) { |
| 1385 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1386 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1388 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1389 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1390 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1391 | |
| 1392 | /* Shortcuts for common default encodings */ |
| 1393 | if (errors == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1394 | if (strcmp(encoding, "utf-8") == 0) |
| 1395 | return PyUnicode_AsUTF8String(unicode); |
| 1396 | else if (strcmp(encoding, "latin-1") == 0) |
| 1397 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1398 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1399 | else if (strcmp(encoding, "mbcs") == 0) |
| 1400 | return PyUnicode_AsMBCSString(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1401 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1402 | else if (strcmp(encoding, "ascii") == 0) |
| 1403 | return PyUnicode_AsASCIIString(unicode); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1404 | /* During bootstrap, we may need to find the encodings |
| 1405 | package, to load the file system encoding, and require the |
| 1406 | file system encoding in order to load the encodings |
| 1407 | package. |
| 1408 | |
| 1409 | Break out of this dependency by assuming that the path to |
| 1410 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1411 | instead, if the file system encoding is the locale's |
| 1412 | encoding. */ |
| 1413 | else if (Py_FileSystemDefaultEncoding && |
| 1414 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1415 | !PyThreadState_GET()->interp->codecs_initialized) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1416 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1417 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1418 | |
| 1419 | /* Encode via the codec registry */ |
| 1420 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1421 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1422 | return NULL; |
| 1423 | |
| 1424 | /* The normal path */ |
| 1425 | if (PyBytes_Check(v)) |
| 1426 | return v; |
| 1427 | |
| 1428 | /* 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] | 1429 | if (PyByteArray_Check(v)) { |
| 1430 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1431 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1432 | PyOS_snprintf(msg, sizeof(msg), |
| 1433 | "encoder %s returned buffer instead of bytes", |
| 1434 | encoding); |
| 1435 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1436 | Py_DECREF(v); |
| 1437 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1438 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1439 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1440 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1441 | Py_DECREF(v); |
| 1442 | return b; |
| 1443 | } |
| 1444 | |
| 1445 | PyErr_Format(PyExc_TypeError, |
| 1446 | "encoder did not return a bytes object (type=%.400s)", |
| 1447 | Py_TYPE(v)->tp_name); |
| 1448 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1449 | return NULL; |
| 1450 | } |
| 1451 | |
| 1452 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1453 | const char *encoding, |
| 1454 | const char *errors) |
| 1455 | { |
| 1456 | PyObject *v; |
| 1457 | |
| 1458 | if (!PyUnicode_Check(unicode)) { |
| 1459 | PyErr_BadArgument(); |
| 1460 | goto onError; |
| 1461 | } |
| 1462 | |
| 1463 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1464 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1465 | |
| 1466 | /* Encode via the codec registry */ |
| 1467 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1468 | if (v == NULL) |
| 1469 | goto onError; |
| 1470 | if (!PyUnicode_Check(v)) { |
| 1471 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1472 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1473 | Py_TYPE(v)->tp_name); |
| 1474 | Py_DECREF(v); |
| 1475 | goto onError; |
| 1476 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1477 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1478 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1479 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1480 | return NULL; |
| 1481 | } |
| 1482 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1483 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1484 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1485 | { |
| 1486 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1487 | if (v) |
| 1488 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1489 | if (errors != NULL) |
| 1490 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1491 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1492 | PyUnicode_GET_SIZE(unicode), |
| 1493 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1494 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1495 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1496 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1497 | return v; |
| 1498 | } |
| 1499 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1500 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1501 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1502 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1503 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1504 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1505 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1506 | PyObject* |
| 1507 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1508 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1509 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1510 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1511 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1512 | bootstrapping process where the codecs aren't ready yet. |
| 1513 | */ |
| 1514 | if (Py_FileSystemDefaultEncoding) { |
| 1515 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1516 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1517 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1518 | } |
| 1519 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1520 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1521 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1522 | } |
| 1523 | #endif |
| 1524 | return PyUnicode_Decode(s, size, |
| 1525 | Py_FileSystemDefaultEncoding, |
| 1526 | "replace"); |
| 1527 | } |
| 1528 | else { |
| 1529 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1530 | } |
| 1531 | } |
| 1532 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1533 | /* Convert the argument to a bytes object, according to the file |
| 1534 | system encoding */ |
| 1535 | |
| 1536 | int |
| 1537 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1538 | { |
| 1539 | PyObject *output = NULL; |
| 1540 | Py_ssize_t size; |
| 1541 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1542 | if (arg == NULL) { |
| 1543 | Py_DECREF(*(PyObject**)addr); |
| 1544 | return 1; |
| 1545 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1546 | if (PyBytes_Check(arg) || PyByteArray_Check(arg)) { |
| 1547 | output = arg; |
| 1548 | Py_INCREF(output); |
| 1549 | } |
| 1550 | else { |
| 1551 | arg = PyUnicode_FromObject(arg); |
| 1552 | if (!arg) |
| 1553 | return 0; |
| 1554 | output = PyUnicode_AsEncodedObject(arg, |
| 1555 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1556 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1557 | Py_DECREF(arg); |
| 1558 | if (!output) |
| 1559 | return 0; |
| 1560 | if (!PyBytes_Check(output)) { |
| 1561 | Py_DECREF(output); |
| 1562 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1563 | return 0; |
| 1564 | } |
| 1565 | } |
| 1566 | if (PyBytes_Check(output)) { |
| 1567 | size = PyBytes_GET_SIZE(output); |
| 1568 | data = PyBytes_AS_STRING(output); |
| 1569 | } |
| 1570 | else { |
| 1571 | size = PyByteArray_GET_SIZE(output); |
| 1572 | data = PyByteArray_AS_STRING(output); |
| 1573 | } |
| 1574 | if (size != strlen(data)) { |
| 1575 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1576 | Py_DECREF(output); |
| 1577 | return 0; |
| 1578 | } |
| 1579 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1580 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1584 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1585 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1586 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1587 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1588 | if (!PyUnicode_Check(unicode)) { |
| 1589 | PyErr_BadArgument(); |
| 1590 | return NULL; |
| 1591 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1592 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1593 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1594 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1595 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1596 | *psize = PyBytes_GET_SIZE(bytes); |
| 1597 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1601 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1602 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1603 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1606 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1607 | { |
| 1608 | if (!PyUnicode_Check(unicode)) { |
| 1609 | PyErr_BadArgument(); |
| 1610 | goto onError; |
| 1611 | } |
| 1612 | return PyUnicode_AS_UNICODE(unicode); |
| 1613 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1614 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1615 | return NULL; |
| 1616 | } |
| 1617 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1618 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1619 | { |
| 1620 | if (!PyUnicode_Check(unicode)) { |
| 1621 | PyErr_BadArgument(); |
| 1622 | goto onError; |
| 1623 | } |
| 1624 | return PyUnicode_GET_SIZE(unicode); |
| 1625 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1626 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1627 | return -1; |
| 1628 | } |
| 1629 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1630 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1631 | { |
| 1632 | return unicode_default_encoding; |
| 1633 | } |
| 1634 | |
| 1635 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1636 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1637 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1638 | PyErr_Format(PyExc_ValueError, |
| 1639 | "Can only set default encoding to %s", |
| 1640 | unicode_default_encoding); |
| 1641 | return -1; |
| 1642 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1643 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1646 | /* error handling callback helper: |
| 1647 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1648 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1649 | and adjust various state variables. |
| 1650 | return 0 on success, -1 on error |
| 1651 | */ |
| 1652 | |
| 1653 | static |
| 1654 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1655 | const char *encoding, const char *reason, |
| 1656 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1657 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1658 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1659 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1660 | 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] | 1661 | |
| 1662 | PyObject *restuple = NULL; |
| 1663 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1664 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1665 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1666 | Py_ssize_t requiredsize; |
| 1667 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1668 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1669 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1670 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1671 | int res = -1; |
| 1672 | |
| 1673 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1674 | *errorHandler = PyCodec_LookupError(errors); |
| 1675 | if (*errorHandler == NULL) |
| 1676 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1680 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1681 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
| 1682 | if (*exceptionObject == NULL) |
| 1683 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1684 | } |
| 1685 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1686 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1687 | goto onError; |
| 1688 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1689 | goto onError; |
| 1690 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1691 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1695 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1696 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1697 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1698 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1699 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1700 | } |
| 1701 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1702 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1703 | |
| 1704 | /* Copy back the bytes variables, which might have been modified by the |
| 1705 | callback */ |
| 1706 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1707 | if (!inputobj) |
| 1708 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1709 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1710 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1711 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1712 | *input = PyBytes_AS_STRING(inputobj); |
| 1713 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1714 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1715 | /* we can DECREF safely, as the exception has another reference, |
| 1716 | so the object won't go away. */ |
| 1717 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1718 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1719 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1720 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1721 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1722 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1723 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1724 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1725 | |
| 1726 | /* need more space? (at least enough for what we |
| 1727 | have+the replacement+the rest of the string (starting |
| 1728 | at the new input position), so we won't have to check space |
| 1729 | when there are no errors in the rest of the string) */ |
| 1730 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1731 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1732 | requiredsize = *outpos + repsize + insize-newpos; |
| 1733 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1734 | if (requiredsize<2*outsize) |
| 1735 | requiredsize = 2*outsize; |
| 1736 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1737 | goto onError; |
| 1738 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1739 | } |
| 1740 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1741 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1742 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1743 | *outptr += repsize; |
| 1744 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1745 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1746 | /* we made it! */ |
| 1747 | res = 0; |
| 1748 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1749 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1750 | Py_XDECREF(restuple); |
| 1751 | return res; |
| 1752 | } |
| 1753 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1754 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1755 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1756 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1757 | |
| 1758 | /* Three simple macros defining base-64. */ |
| 1759 | |
| 1760 | /* Is c a base-64 character? */ |
| 1761 | |
| 1762 | #define IS_BASE64(c) \ |
| 1763 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1764 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1765 | ((c) >= '0' && (c) <= '9') || \ |
| 1766 | (c) == '+' || (c) == '/') |
| 1767 | |
| 1768 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1769 | |
| 1770 | #define FROM_BASE64(c) \ |
| 1771 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1772 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1773 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1774 | (c) == '+' ? 62 : 63) |
| 1775 | |
| 1776 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1777 | |
| 1778 | #define TO_BASE64(n) \ |
| 1779 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1780 | |
| 1781 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1782 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1783 | * byte not decoding to itself is the + which begins a base64 |
| 1784 | * string. */ |
| 1785 | |
| 1786 | #define DECODE_DIRECT(c) \ |
| 1787 | ((c) <= 127 && (c) != '+') |
| 1788 | |
| 1789 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1790 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1791 | * the above). See RFC2152. This array identifies these different |
| 1792 | * sets: |
| 1793 | * 0 : "Set D" |
| 1794 | * alphanumeric and '(),-./:? |
| 1795 | * 1 : "Set O" |
| 1796 | * !"#$%&*;<=>@[]^_`{|} |
| 1797 | * 2 : "whitespace" |
| 1798 | * ht nl cr sp |
| 1799 | * 3 : special (must be base64 encoded) |
| 1800 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1801 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1802 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1803 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1804 | char utf7_category[128] = { |
| 1805 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 1806 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 1807 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 1808 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1809 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 1810 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 1811 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 1812 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 1813 | /* @ A B C D E F G H I J K L M N O */ |
| 1814 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1815 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 1816 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 1817 | /* ` a b c d e f g h i j k l m n o */ |
| 1818 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1819 | /* p q r s t u v w x y z { | } ~ del */ |
| 1820 | 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] | 1821 | }; |
| 1822 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1823 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 1824 | * answer depends on whether we are encoding set O as itself, and also |
| 1825 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 1826 | * clear that the answers to these questions vary between |
| 1827 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1828 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1829 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 1830 | ((c) < 128 && (c) > 0 && \ |
| 1831 | ((utf7_category[(c)] == 0) || \ |
| 1832 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 1833 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1834 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1835 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1836 | Py_ssize_t size, |
| 1837 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1838 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1839 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1840 | } |
| 1841 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1842 | /* The decoder. The only state we preserve is our read position, |
| 1843 | * i.e. how many characters we have consumed. So if we end in the |
| 1844 | * middle of a shift sequence we have to back off the read position |
| 1845 | * and the output to the beginning of the sequence, otherwise we lose |
| 1846 | * all the shift state (seen bits, number of bits seen, high |
| 1847 | * surrogate). */ |
| 1848 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1849 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1850 | Py_ssize_t size, |
| 1851 | const char *errors, |
| 1852 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1853 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1854 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1855 | Py_ssize_t startinpos; |
| 1856 | Py_ssize_t endinpos; |
| 1857 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1858 | const char *e; |
| 1859 | PyUnicodeObject *unicode; |
| 1860 | Py_UNICODE *p; |
| 1861 | const char *errmsg = ""; |
| 1862 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1863 | Py_UNICODE *shiftOutStart; |
| 1864 | unsigned int base64bits = 0; |
| 1865 | unsigned long base64buffer = 0; |
| 1866 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1867 | PyObject *errorHandler = NULL; |
| 1868 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1869 | |
| 1870 | unicode = _PyUnicode_New(size); |
| 1871 | if (!unicode) |
| 1872 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1873 | if (size == 0) { |
| 1874 | if (consumed) |
| 1875 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1876 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1877 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1878 | |
| 1879 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1880 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1881 | e = s + size; |
| 1882 | |
| 1883 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1884 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1885 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1886 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1887 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1888 | if (inShift) { /* in a base-64 section */ |
| 1889 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 1890 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 1891 | base64bits += 6; |
| 1892 | s++; |
| 1893 | if (base64bits >= 16) { |
| 1894 | /* we have enough bits for a UTF-16 value */ |
| 1895 | Py_UNICODE outCh = (Py_UNICODE) |
| 1896 | (base64buffer >> (base64bits-16)); |
| 1897 | base64bits -= 16; |
| 1898 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 1899 | if (surrogate) { |
| 1900 | /* expecting a second surrogate */ |
| 1901 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 1902 | #ifdef Py_UNICODE_WIDE |
| 1903 | *p++ = (((surrogate & 0x3FF)<<10) |
| 1904 | | (outCh & 0x3FF)) + 0x10000; |
| 1905 | #else |
| 1906 | *p++ = surrogate; |
| 1907 | *p++ = outCh; |
| 1908 | #endif |
| 1909 | surrogate = 0; |
| 1910 | } |
| 1911 | else { |
| 1912 | surrogate = 0; |
| 1913 | errmsg = "second surrogate missing"; |
| 1914 | goto utf7Error; |
| 1915 | } |
| 1916 | } |
| 1917 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 1918 | /* first surrogate */ |
| 1919 | surrogate = outCh; |
| 1920 | } |
| 1921 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 1922 | errmsg = "unexpected second surrogate"; |
| 1923 | goto utf7Error; |
| 1924 | } |
| 1925 | else { |
| 1926 | *p++ = outCh; |
| 1927 | } |
| 1928 | } |
| 1929 | } |
| 1930 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1931 | inShift = 0; |
| 1932 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1933 | if (surrogate) { |
| 1934 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1935 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1936 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1937 | if (base64bits > 0) { /* left-over bits */ |
| 1938 | if (base64bits >= 6) { |
| 1939 | /* We've seen at least one base-64 character */ |
| 1940 | errmsg = "partial character in shift sequence"; |
| 1941 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1942 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1943 | else { |
| 1944 | /* Some bits remain; they should be zero */ |
| 1945 | if (base64buffer != 0) { |
| 1946 | errmsg = "non-zero padding bits in shift sequence"; |
| 1947 | goto utf7Error; |
| 1948 | } |
| 1949 | } |
| 1950 | } |
| 1951 | if (ch != '-') { |
| 1952 | /* '-' is absorbed; other terminating |
| 1953 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1954 | *p++ = ch; |
| 1955 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1959 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1960 | s++; /* consume '+' */ |
| 1961 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1962 | s++; |
| 1963 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1964 | } |
| 1965 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1966 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1967 | shiftOutStart = p; |
| 1968 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1969 | } |
| 1970 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1971 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1972 | *p++ = ch; |
| 1973 | s++; |
| 1974 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1975 | else { |
| 1976 | startinpos = s-starts; |
| 1977 | s++; |
| 1978 | errmsg = "unexpected special character"; |
| 1979 | goto utf7Error; |
| 1980 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1981 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1982 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1983 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1984 | endinpos = s-starts; |
| 1985 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1986 | errors, &errorHandler, |
| 1987 | "utf7", errmsg, |
| 1988 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 1989 | &unicode, &outpos, &p)) |
| 1990 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1993 | /* end of string */ |
| 1994 | |
| 1995 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 1996 | /* if we're in an inconsistent state, that's an error */ |
| 1997 | if (surrogate || |
| 1998 | (base64bits >= 6) || |
| 1999 | (base64bits > 0 && base64buffer != 0)) { |
| 2000 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2001 | endinpos = size; |
| 2002 | if (unicode_decode_call_errorhandler( |
| 2003 | errors, &errorHandler, |
| 2004 | "utf7", "unterminated shift sequence", |
| 2005 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2006 | &unicode, &outpos, &p)) |
| 2007 | goto onError; |
| 2008 | if (s < e) |
| 2009 | goto restart; |
| 2010 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2011 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2012 | |
| 2013 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2014 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2015 | if (inShift) { |
| 2016 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2017 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2018 | } |
| 2019 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2020 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2021 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2022 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2023 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2024 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2025 | goto onError; |
| 2026 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2027 | Py_XDECREF(errorHandler); |
| 2028 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2029 | return (PyObject *)unicode; |
| 2030 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2031 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2032 | Py_XDECREF(errorHandler); |
| 2033 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2034 | Py_DECREF(unicode); |
| 2035 | return NULL; |
| 2036 | } |
| 2037 | |
| 2038 | |
| 2039 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2040 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2041 | int base64SetO, |
| 2042 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2043 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2044 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2045 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2046 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2047 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2048 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2049 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2050 | unsigned int base64bits = 0; |
| 2051 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2052 | char * out; |
| 2053 | char * start; |
| 2054 | |
| 2055 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2056 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2057 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2058 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2059 | return PyErr_NoMemory(); |
| 2060 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2061 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2062 | if (v == NULL) |
| 2063 | return NULL; |
| 2064 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2065 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2066 | for (;i < size; ++i) { |
| 2067 | Py_UNICODE ch = s[i]; |
| 2068 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2069 | if (inShift) { |
| 2070 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2071 | /* shifting out */ |
| 2072 | if (base64bits) { /* output remaining bits */ |
| 2073 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2074 | base64buffer = 0; |
| 2075 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2076 | } |
| 2077 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2078 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2079 | so no '-' is required, except if the character is itself a '-' */ |
| 2080 | if (IS_BASE64(ch) || ch == '-') { |
| 2081 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2082 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2083 | *out++ = (char) ch; |
| 2084 | } |
| 2085 | else { |
| 2086 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2087 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2088 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2089 | else { /* not in a shift sequence */ |
| 2090 | if (ch == '+') { |
| 2091 | *out++ = '+'; |
| 2092 | *out++ = '-'; |
| 2093 | } |
| 2094 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2095 | *out++ = (char) ch; |
| 2096 | } |
| 2097 | else { |
| 2098 | *out++ = '+'; |
| 2099 | inShift = 1; |
| 2100 | goto encode_char; |
| 2101 | } |
| 2102 | } |
| 2103 | continue; |
| 2104 | encode_char: |
| 2105 | #ifdef Py_UNICODE_WIDE |
| 2106 | if (ch >= 0x10000) { |
| 2107 | /* code first surrogate */ |
| 2108 | base64bits += 16; |
| 2109 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2110 | while (base64bits >= 6) { |
| 2111 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2112 | base64bits -= 6; |
| 2113 | } |
| 2114 | /* prepare second surrogate */ |
| 2115 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2116 | } |
| 2117 | #endif |
| 2118 | base64bits += 16; |
| 2119 | base64buffer = (base64buffer << 16) | ch; |
| 2120 | while (base64bits >= 6) { |
| 2121 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2122 | base64bits -= 6; |
| 2123 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2124 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2125 | if (base64bits) |
| 2126 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2127 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2128 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2129 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2130 | return NULL; |
| 2131 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2134 | #undef IS_BASE64 |
| 2135 | #undef FROM_BASE64 |
| 2136 | #undef TO_BASE64 |
| 2137 | #undef DECODE_DIRECT |
| 2138 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2139 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2140 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2141 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2142 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2143 | char utf8_code_length[256] = { |
| 2144 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 2145 | illegal prefix. see RFC 2279 for details */ |
| 2146 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2147 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2148 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2149 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2150 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2151 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2152 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2153 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2154 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2155 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2156 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2157 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2158 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2159 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2160 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2161 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 2162 | }; |
| 2163 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2164 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2165 | Py_ssize_t size, |
| 2166 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2167 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2168 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2169 | } |
| 2170 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2171 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2172 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2173 | |
| 2174 | /* Mask to quickly check whether a C 'long' contains a |
| 2175 | non-ASCII, UTF8-encoded char. */ |
| 2176 | #if (SIZEOF_LONG == 8) |
| 2177 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2178 | #elif (SIZEOF_LONG == 4) |
| 2179 | # define ASCII_CHAR_MASK 0x80808080L |
| 2180 | #else |
| 2181 | # error C 'long' size should be either 4 or 8! |
| 2182 | #endif |
| 2183 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2184 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2185 | Py_ssize_t size, |
| 2186 | const char *errors, |
| 2187 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2188 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2189 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2190 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2191 | Py_ssize_t startinpos; |
| 2192 | Py_ssize_t endinpos; |
| 2193 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2194 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2195 | PyUnicodeObject *unicode; |
| 2196 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2197 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2198 | PyObject *errorHandler = NULL; |
| 2199 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2200 | |
| 2201 | /* Note: size will always be longer than the resulting Unicode |
| 2202 | character count */ |
| 2203 | unicode = _PyUnicode_New(size); |
| 2204 | if (!unicode) |
| 2205 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2206 | if (size == 0) { |
| 2207 | if (consumed) |
| 2208 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2209 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2210 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2211 | |
| 2212 | /* Unpack UTF-8 encoded data */ |
| 2213 | p = unicode->str; |
| 2214 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2215 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2216 | |
| 2217 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2218 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2219 | |
| 2220 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2221 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2222 | input will consist of an overwhelming majority of ASCII |
| 2223 | characters, we try to optimize for this case by checking |
| 2224 | as many characters as a C 'long' can contain. |
| 2225 | First, check if we can do an aligned read, as most CPUs have |
| 2226 | a penalty for unaligned reads. |
| 2227 | */ |
| 2228 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2229 | /* Help register allocation */ |
| 2230 | register const char *_s = s; |
| 2231 | register Py_UNICODE *_p = p; |
| 2232 | while (_s < aligned_end) { |
| 2233 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2234 | and do a fast unrolled copy if it only contains ASCII |
| 2235 | characters. */ |
| 2236 | unsigned long data = *(unsigned long *) _s; |
| 2237 | if (data & ASCII_CHAR_MASK) |
| 2238 | break; |
| 2239 | _p[0] = (unsigned char) _s[0]; |
| 2240 | _p[1] = (unsigned char) _s[1]; |
| 2241 | _p[2] = (unsigned char) _s[2]; |
| 2242 | _p[3] = (unsigned char) _s[3]; |
| 2243 | #if (SIZEOF_LONG == 8) |
| 2244 | _p[4] = (unsigned char) _s[4]; |
| 2245 | _p[5] = (unsigned char) _s[5]; |
| 2246 | _p[6] = (unsigned char) _s[6]; |
| 2247 | _p[7] = (unsigned char) _s[7]; |
| 2248 | #endif |
| 2249 | _s += SIZEOF_LONG; |
| 2250 | _p += SIZEOF_LONG; |
| 2251 | } |
| 2252 | s = _s; |
| 2253 | p = _p; |
| 2254 | if (s == e) |
| 2255 | break; |
| 2256 | ch = (unsigned char)*s; |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2261 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2262 | s++; |
| 2263 | continue; |
| 2264 | } |
| 2265 | |
| 2266 | n = utf8_code_length[ch]; |
| 2267 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2268 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2269 | if (consumed) |
| 2270 | break; |
| 2271 | else { |
| 2272 | errmsg = "unexpected end of data"; |
| 2273 | startinpos = s-starts; |
| 2274 | endinpos = size; |
| 2275 | goto utf8Error; |
| 2276 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2277 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2278 | |
| 2279 | switch (n) { |
| 2280 | |
| 2281 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2282 | errmsg = "unexpected code byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2283 | startinpos = s-starts; |
| 2284 | endinpos = startinpos+1; |
| 2285 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2286 | |
| 2287 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2288 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2289 | startinpos = s-starts; |
| 2290 | endinpos = startinpos+1; |
| 2291 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2292 | |
| 2293 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2294 | if ((s[1] & 0xc0) != 0x80) { |
| 2295 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2296 | startinpos = s-starts; |
| 2297 | endinpos = startinpos+2; |
| 2298 | goto utf8Error; |
| 2299 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2301 | if (ch < 0x80) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2302 | startinpos = s-starts; |
| 2303 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2304 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2305 | goto utf8Error; |
| 2306 | } |
| 2307 | else |
| 2308 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2309 | break; |
| 2310 | |
| 2311 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2312 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2313 | (s[2] & 0xc0) != 0x80) { |
| 2314 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2315 | startinpos = s-starts; |
| 2316 | endinpos = startinpos+3; |
| 2317 | goto utf8Error; |
| 2318 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2319 | 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] | 2320 | if (ch < 0x0800 || (ch >= 0xd800 && ch <= 0xDFFF)) { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2321 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2322 | startinpos = s-starts; |
| 2323 | endinpos = startinpos+3; |
| 2324 | goto utf8Error; |
| 2325 | } |
| 2326 | else |
| 2327 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2328 | break; |
| 2329 | |
| 2330 | case 4: |
| 2331 | if ((s[1] & 0xc0) != 0x80 || |
| 2332 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2333 | (s[3] & 0xc0) != 0x80) { |
| 2334 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2335 | startinpos = s-starts; |
| 2336 | endinpos = startinpos+4; |
| 2337 | goto utf8Error; |
| 2338 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2339 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2340 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2341 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2342 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2343 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2344 | || (ch > 0x10ffff)) /* maximum value allowed for |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2345 | UTF-16 */ |
| 2346 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2347 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2348 | startinpos = s-starts; |
| 2349 | endinpos = startinpos+4; |
| 2350 | goto utf8Error; |
| 2351 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2352 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2353 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2354 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2355 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2356 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2357 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2358 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2359 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2360 | /* high surrogate = top 10 bits added to D800 */ |
| 2361 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2362 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2363 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2364 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2365 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2366 | break; |
| 2367 | |
| 2368 | default: |
| 2369 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2370 | errmsg = "unsupported Unicode code range"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2371 | startinpos = s-starts; |
| 2372 | endinpos = startinpos+n; |
| 2373 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2374 | } |
| 2375 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2376 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2377 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2378 | utf8Error: |
| 2379 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2380 | if (unicode_decode_call_errorhandler( |
| 2381 | errors, &errorHandler, |
| 2382 | "utf8", errmsg, |
| 2383 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2384 | &unicode, &outpos, &p)) |
| 2385 | goto onError; |
| 2386 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2387 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2388 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2389 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2390 | |
| 2391 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2392 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2393 | goto onError; |
| 2394 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2395 | Py_XDECREF(errorHandler); |
| 2396 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2397 | return (PyObject *)unicode; |
| 2398 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2399 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2400 | Py_XDECREF(errorHandler); |
| 2401 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2402 | Py_DECREF(unicode); |
| 2403 | return NULL; |
| 2404 | } |
| 2405 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2406 | #undef ASCII_CHAR_MASK |
| 2407 | |
| 2408 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2409 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2410 | and allocate exactly as much space needed at the end. Else allocate the |
| 2411 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2412 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2413 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2414 | PyObject * |
| 2415 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2416 | Py_ssize_t size, |
| 2417 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2418 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2419 | #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] | 2420 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2421 | Py_ssize_t i; /* index into s of next input byte */ |
| 2422 | PyObject *result; /* result string object */ |
| 2423 | char *p; /* next free byte in output buffer */ |
| 2424 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2425 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2426 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2427 | PyObject *errorHandler = NULL; |
| 2428 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2429 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2430 | assert(s != NULL); |
| 2431 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2432 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2433 | if (size <= MAX_SHORT_UNICHARS) { |
| 2434 | /* Write into the stack buffer; nallocated can't overflow. |
| 2435 | * At the end, we'll allocate exactly as much heap space as it |
| 2436 | * turns out we need. |
| 2437 | */ |
| 2438 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2439 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2440 | p = stackbuf; |
| 2441 | } |
| 2442 | else { |
| 2443 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2444 | nallocated = size * 4; |
| 2445 | if (nallocated / 4 != size) /* overflow! */ |
| 2446 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2447 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2448 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2449 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2450 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2451 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2452 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2453 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2454 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2455 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2456 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2457 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2458 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2459 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2460 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2461 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2462 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2463 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2464 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2465 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2466 | /* Encode UCS2 Unicode ordinals */ |
| 2467 | if (ch < 0x10000) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2468 | #ifndef Py_UNICODE_WIDE |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2469 | /* Special case: check for high surrogate */ |
| 2470 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2471 | Py_UCS4 ch2 = s[i]; |
| 2472 | /* Check for low surrogate and combine the two to |
| 2473 | form a UCS4 value */ |
| 2474 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2475 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2476 | i++; |
| 2477 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2478 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2479 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2480 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2481 | #endif |
| 2482 | if (ch >= 0xd800 && ch <= 0xdfff) { |
| 2483 | Py_ssize_t newpos; |
| 2484 | PyObject *rep; |
| 2485 | char *prep; |
| 2486 | int k; |
| 2487 | rep = unicode_encode_call_errorhandler |
| 2488 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2489 | s, size, &exc, i-1, i, &newpos); |
| 2490 | if (!rep) |
| 2491 | goto error; |
| 2492 | /* Implementation limitations: only support error handler that return |
| 2493 | bytes, and only support up to four replacement bytes. */ |
| 2494 | if (!PyBytes_Check(rep)) { |
| 2495 | PyErr_SetString(PyExc_TypeError, "error handler should have returned bytes"); |
| 2496 | Py_DECREF(rep); |
| 2497 | goto error; |
| 2498 | } |
| 2499 | if (PyBytes_Size(rep) > 4) { |
| 2500 | PyErr_SetString(PyExc_TypeError, "error handler returned too many bytes"); |
| 2501 | Py_DECREF(rep); |
| 2502 | goto error; |
| 2503 | } |
| 2504 | prep = PyBytes_AsString(rep); |
| 2505 | for(k = PyBytes_Size(rep); k > 0; k--) |
| 2506 | *p++ = *prep++; |
| 2507 | Py_DECREF(rep); |
| 2508 | continue; |
| 2509 | |
| 2510 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2511 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2512 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2513 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2514 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2515 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2516 | encodeUCS4: |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2517 | /* Encode UCS4 Unicode ordinals */ |
| 2518 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2519 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2520 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2521 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2522 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2523 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2524 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2525 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2526 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2527 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2528 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2529 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2530 | } |
| 2531 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2532 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2533 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2534 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2535 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2536 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2537 | Py_XDECREF(errorHandler); |
| 2538 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2539 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2540 | error: |
| 2541 | Py_XDECREF(errorHandler); |
| 2542 | Py_XDECREF(exc); |
| 2543 | Py_XDECREF(result); |
| 2544 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2545 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2546 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2549 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2550 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2551 | if (!PyUnicode_Check(unicode)) { |
| 2552 | PyErr_BadArgument(); |
| 2553 | return NULL; |
| 2554 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2555 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2556 | PyUnicode_GET_SIZE(unicode), |
| 2557 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2560 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2561 | |
| 2562 | PyObject * |
| 2563 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2564 | Py_ssize_t size, |
| 2565 | const char *errors, |
| 2566 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2567 | { |
| 2568 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2569 | } |
| 2570 | |
| 2571 | PyObject * |
| 2572 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2573 | Py_ssize_t size, |
| 2574 | const char *errors, |
| 2575 | int *byteorder, |
| 2576 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2577 | { |
| 2578 | const char *starts = s; |
| 2579 | Py_ssize_t startinpos; |
| 2580 | Py_ssize_t endinpos; |
| 2581 | Py_ssize_t outpos; |
| 2582 | PyUnicodeObject *unicode; |
| 2583 | Py_UNICODE *p; |
| 2584 | #ifndef Py_UNICODE_WIDE |
| 2585 | int i, pairs; |
| 2586 | #else |
| 2587 | const int pairs = 0; |
| 2588 | #endif |
| 2589 | const unsigned char *q, *e; |
| 2590 | int bo = 0; /* assume native ordering by default */ |
| 2591 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2592 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2593 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2594 | int iorder[] = {0, 1, 2, 3}; |
| 2595 | #else |
| 2596 | int iorder[] = {3, 2, 1, 0}; |
| 2597 | #endif |
| 2598 | PyObject *errorHandler = NULL; |
| 2599 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2600 | /* On narrow builds we split characters outside the BMP into two |
| 2601 | codepoints => count how much extra space we need. */ |
| 2602 | #ifndef Py_UNICODE_WIDE |
| 2603 | for (i = pairs = 0; i < size/4; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2604 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2605 | pairs++; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2606 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2607 | |
| 2608 | /* This might be one to much, because of a BOM */ |
| 2609 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2610 | if (!unicode) |
| 2611 | return NULL; |
| 2612 | if (size == 0) |
| 2613 | return (PyObject *)unicode; |
| 2614 | |
| 2615 | /* Unpack UTF-32 encoded data */ |
| 2616 | p = unicode->str; |
| 2617 | q = (unsigned char *)s; |
| 2618 | e = q + size; |
| 2619 | |
| 2620 | if (byteorder) |
| 2621 | bo = *byteorder; |
| 2622 | |
| 2623 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2624 | byte order setting accordingly. In native mode, the leading BOM |
| 2625 | mark is skipped, in all other modes, it is copied to the output |
| 2626 | stream as-is (giving a ZWNBSP character). */ |
| 2627 | if (bo == 0) { |
| 2628 | if (size >= 4) { |
| 2629 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2630 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2631 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2632 | if (bom == 0x0000FEFF) { |
| 2633 | q += 4; |
| 2634 | bo = -1; |
| 2635 | } |
| 2636 | else if (bom == 0xFFFE0000) { |
| 2637 | q += 4; |
| 2638 | bo = 1; |
| 2639 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2640 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2641 | if (bom == 0x0000FEFF) { |
| 2642 | q += 4; |
| 2643 | bo = 1; |
| 2644 | } |
| 2645 | else if (bom == 0xFFFE0000) { |
| 2646 | q += 4; |
| 2647 | bo = -1; |
| 2648 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2649 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2650 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | if (bo == -1) { |
| 2654 | /* force LE */ |
| 2655 | iorder[0] = 0; |
| 2656 | iorder[1] = 1; |
| 2657 | iorder[2] = 2; |
| 2658 | iorder[3] = 3; |
| 2659 | } |
| 2660 | else if (bo == 1) { |
| 2661 | /* force BE */ |
| 2662 | iorder[0] = 3; |
| 2663 | iorder[1] = 2; |
| 2664 | iorder[2] = 1; |
| 2665 | iorder[3] = 0; |
| 2666 | } |
| 2667 | |
| 2668 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2669 | Py_UCS4 ch; |
| 2670 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2671 | if (e-q<4) { |
| 2672 | if (consumed) |
| 2673 | break; |
| 2674 | errmsg = "truncated data"; |
| 2675 | startinpos = ((const char *)q)-starts; |
| 2676 | endinpos = ((const char *)e)-starts; |
| 2677 | goto utf32Error; |
| 2678 | /* The remaining input chars are ignored if the callback |
| 2679 | chooses to skip the input */ |
| 2680 | } |
| 2681 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2682 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2683 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2684 | if (ch >= 0x110000) |
| 2685 | { |
| 2686 | errmsg = "codepoint not in range(0x110000)"; |
| 2687 | startinpos = ((const char *)q)-starts; |
| 2688 | endinpos = startinpos+4; |
| 2689 | goto utf32Error; |
| 2690 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2691 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2692 | if (ch >= 0x10000) |
| 2693 | { |
| 2694 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2695 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2696 | } |
| 2697 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2698 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2699 | *p++ = ch; |
| 2700 | q += 4; |
| 2701 | continue; |
| 2702 | utf32Error: |
| 2703 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2704 | if (unicode_decode_call_errorhandler( |
| 2705 | errors, &errorHandler, |
| 2706 | "utf32", errmsg, |
| 2707 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2708 | &unicode, &outpos, &p)) |
| 2709 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | if (byteorder) |
| 2713 | *byteorder = bo; |
| 2714 | |
| 2715 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2716 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2717 | |
| 2718 | /* Adjust length */ |
| 2719 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2720 | goto onError; |
| 2721 | |
| 2722 | Py_XDECREF(errorHandler); |
| 2723 | Py_XDECREF(exc); |
| 2724 | return (PyObject *)unicode; |
| 2725 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2726 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2727 | Py_DECREF(unicode); |
| 2728 | Py_XDECREF(errorHandler); |
| 2729 | Py_XDECREF(exc); |
| 2730 | return NULL; |
| 2731 | } |
| 2732 | |
| 2733 | PyObject * |
| 2734 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2735 | Py_ssize_t size, |
| 2736 | const char *errors, |
| 2737 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2738 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2739 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2740 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2741 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2742 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2743 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2744 | #else |
| 2745 | const int pairs = 0; |
| 2746 | #endif |
| 2747 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2748 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2749 | int iorder[] = {0, 1, 2, 3}; |
| 2750 | #else |
| 2751 | int iorder[] = {3, 2, 1, 0}; |
| 2752 | #endif |
| 2753 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2754 | #define STORECHAR(CH) \ |
| 2755 | do { \ |
| 2756 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2757 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2758 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2759 | p[iorder[0]] = (CH) & 0xff; \ |
| 2760 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2761 | } while(0) |
| 2762 | |
| 2763 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2764 | so we need less space. */ |
| 2765 | #ifndef Py_UNICODE_WIDE |
| 2766 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2767 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2768 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2769 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2770 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2771 | nsize = (size - pairs + (byteorder == 0)); |
| 2772 | bytesize = nsize * 4; |
| 2773 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2774 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2775 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2776 | if (v == NULL) |
| 2777 | return NULL; |
| 2778 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2779 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2780 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2781 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2782 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2783 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2784 | |
| 2785 | if (byteorder == -1) { |
| 2786 | /* force LE */ |
| 2787 | iorder[0] = 0; |
| 2788 | iorder[1] = 1; |
| 2789 | iorder[2] = 2; |
| 2790 | iorder[3] = 3; |
| 2791 | } |
| 2792 | else if (byteorder == 1) { |
| 2793 | /* force BE */ |
| 2794 | iorder[0] = 3; |
| 2795 | iorder[1] = 2; |
| 2796 | iorder[2] = 1; |
| 2797 | iorder[3] = 0; |
| 2798 | } |
| 2799 | |
| 2800 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2801 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2802 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2803 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2804 | Py_UCS4 ch2 = *s; |
| 2805 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2806 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2807 | s++; |
| 2808 | size--; |
| 2809 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2810 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2811 | #endif |
| 2812 | STORECHAR(ch); |
| 2813 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2814 | |
| 2815 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2816 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2817 | #undef STORECHAR |
| 2818 | } |
| 2819 | |
| 2820 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2821 | { |
| 2822 | if (!PyUnicode_Check(unicode)) { |
| 2823 | PyErr_BadArgument(); |
| 2824 | return NULL; |
| 2825 | } |
| 2826 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2827 | PyUnicode_GET_SIZE(unicode), |
| 2828 | NULL, |
| 2829 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2832 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2833 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2834 | PyObject * |
| 2835 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2836 | Py_ssize_t size, |
| 2837 | const char *errors, |
| 2838 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2840 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2841 | } |
| 2842 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2843 | /* Two masks for fast checking of whether a C 'long' may contain |
| 2844 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 2845 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 2846 | rare in most input. |
| 2847 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 2848 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2849 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2850 | #if (SIZEOF_LONG == 8) |
| 2851 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 2852 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 2853 | #elif (SIZEOF_LONG == 4) |
| 2854 | # define FAST_CHAR_MASK 0x80008000L |
| 2855 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 2856 | #else |
| 2857 | # error C 'long' size should be either 4 or 8! |
| 2858 | #endif |
| 2859 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2860 | PyObject * |
| 2861 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2862 | Py_ssize_t size, |
| 2863 | const char *errors, |
| 2864 | int *byteorder, |
| 2865 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2866 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2867 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2868 | Py_ssize_t startinpos; |
| 2869 | Py_ssize_t endinpos; |
| 2870 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2871 | PyUnicodeObject *unicode; |
| 2872 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2873 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2874 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2875 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2876 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2877 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2878 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2879 | int ihi = 1, ilo = 0; |
| 2880 | #else |
| 2881 | int ihi = 0, ilo = 1; |
| 2882 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2883 | PyObject *errorHandler = NULL; |
| 2884 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2885 | |
| 2886 | /* Note: size will always be longer than the resulting Unicode |
| 2887 | character count */ |
| 2888 | unicode = _PyUnicode_New(size); |
| 2889 | if (!unicode) |
| 2890 | return NULL; |
| 2891 | if (size == 0) |
| 2892 | return (PyObject *)unicode; |
| 2893 | |
| 2894 | /* Unpack UTF-16 encoded data */ |
| 2895 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2896 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2897 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2898 | |
| 2899 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2900 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2901 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2902 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2903 | byte order setting accordingly. In native mode, the leading BOM |
| 2904 | mark is skipped, in all other modes, it is copied to the output |
| 2905 | stream as-is (giving a ZWNBSP character). */ |
| 2906 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2907 | if (size >= 2) { |
| 2908 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2909 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2910 | if (bom == 0xFEFF) { |
| 2911 | q += 2; |
| 2912 | bo = -1; |
| 2913 | } |
| 2914 | else if (bom == 0xFFFE) { |
| 2915 | q += 2; |
| 2916 | bo = 1; |
| 2917 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2918 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2919 | if (bom == 0xFEFF) { |
| 2920 | q += 2; |
| 2921 | bo = 1; |
| 2922 | } |
| 2923 | else if (bom == 0xFFFE) { |
| 2924 | q += 2; |
| 2925 | bo = -1; |
| 2926 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2927 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2928 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2929 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2930 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2931 | if (bo == -1) { |
| 2932 | /* force LE */ |
| 2933 | ihi = 1; |
| 2934 | ilo = 0; |
| 2935 | } |
| 2936 | else if (bo == 1) { |
| 2937 | /* force BE */ |
| 2938 | ihi = 0; |
| 2939 | ilo = 1; |
| 2940 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2941 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2942 | native_ordering = ilo < ihi; |
| 2943 | #else |
| 2944 | native_ordering = ilo > ihi; |
| 2945 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2946 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2947 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2948 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2949 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2950 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 2951 | reads are more expensive, better to defer to another iteration. */ |
| 2952 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 2953 | /* Fast path for runs of non-surrogate chars. */ |
| 2954 | register const unsigned char *_q = q; |
| 2955 | Py_UNICODE *_p = p; |
| 2956 | if (native_ordering) { |
| 2957 | /* Native ordering is simple: as long as the input cannot |
| 2958 | possibly contain a surrogate char, do an unrolled copy |
| 2959 | of several 16-bit code points to the target object. |
| 2960 | The non-surrogate check is done on several input bytes |
| 2961 | at a time (as many as a C 'long' can contain). */ |
| 2962 | while (_q < aligned_end) { |
| 2963 | unsigned long data = * (unsigned long *) _q; |
| 2964 | if (data & FAST_CHAR_MASK) |
| 2965 | break; |
| 2966 | _p[0] = ((unsigned short *) _q)[0]; |
| 2967 | _p[1] = ((unsigned short *) _q)[1]; |
| 2968 | #if (SIZEOF_LONG == 8) |
| 2969 | _p[2] = ((unsigned short *) _q)[2]; |
| 2970 | _p[3] = ((unsigned short *) _q)[3]; |
| 2971 | #endif |
| 2972 | _q += SIZEOF_LONG; |
| 2973 | _p += SIZEOF_LONG / 2; |
| 2974 | } |
| 2975 | } |
| 2976 | else { |
| 2977 | /* Byteswapped ordering is similar, but we must decompose |
| 2978 | the copy bytewise, and take care of zero'ing out the |
| 2979 | upper bytes if the target object is in 32-bit units |
| 2980 | (that is, in UCS-4 builds). */ |
| 2981 | while (_q < aligned_end) { |
| 2982 | unsigned long data = * (unsigned long *) _q; |
| 2983 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 2984 | break; |
| 2985 | /* Zero upper bytes in UCS-4 builds */ |
| 2986 | #if (Py_UNICODE_SIZE > 2) |
| 2987 | _p[0] = 0; |
| 2988 | _p[1] = 0; |
| 2989 | #if (SIZEOF_LONG == 8) |
| 2990 | _p[2] = 0; |
| 2991 | _p[3] = 0; |
| 2992 | #endif |
| 2993 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 2994 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 2995 | fill the two last bytes of each 4-byte unit. */ |
| 2996 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 2997 | # define OFF 2 |
| 2998 | #else |
| 2999 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3000 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3001 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3002 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3003 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3004 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3005 | #if (SIZEOF_LONG == 8) |
| 3006 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3007 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3008 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3009 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3010 | #endif |
| 3011 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3012 | _q += SIZEOF_LONG; |
| 3013 | _p += SIZEOF_LONG / 2; |
| 3014 | } |
| 3015 | } |
| 3016 | p = _p; |
| 3017 | q = _q; |
| 3018 | if (q >= e) |
| 3019 | break; |
| 3020 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3021 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3022 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3023 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3024 | |
| 3025 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3026 | *p++ = ch; |
| 3027 | continue; |
| 3028 | } |
| 3029 | |
| 3030 | /* UTF-16 code pair: */ |
| 3031 | if (q > e) { |
| 3032 | errmsg = "unexpected end of data"; |
| 3033 | startinpos = (((const char *)q) - 2) - starts; |
| 3034 | endinpos = ((const char *)e) + 1 - starts; |
| 3035 | goto utf16Error; |
| 3036 | } |
| 3037 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3038 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3039 | q += 2; |
| 3040 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3041 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3042 | *p++ = ch; |
| 3043 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3044 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3045 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3046 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3047 | continue; |
| 3048 | } |
| 3049 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3050 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3051 | startinpos = (((const char *)q)-4)-starts; |
| 3052 | endinpos = startinpos+2; |
| 3053 | goto utf16Error; |
| 3054 | } |
| 3055 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3056 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3057 | errmsg = "illegal encoding"; |
| 3058 | startinpos = (((const char *)q)-2)-starts; |
| 3059 | endinpos = startinpos+2; |
| 3060 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3061 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3062 | utf16Error: |
| 3063 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3064 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3065 | errors, |
| 3066 | &errorHandler, |
| 3067 | "utf16", errmsg, |
| 3068 | &starts, |
| 3069 | (const char **)&e, |
| 3070 | &startinpos, |
| 3071 | &endinpos, |
| 3072 | &exc, |
| 3073 | (const char **)&q, |
| 3074 | &unicode, |
| 3075 | &outpos, |
| 3076 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3077 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3078 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3079 | /* remaining byte at the end? (size should be even) */ |
| 3080 | if (e == q) { |
| 3081 | if (!consumed) { |
| 3082 | errmsg = "truncated data"; |
| 3083 | startinpos = ((const char *)q) - starts; |
| 3084 | endinpos = ((const char *)e) + 1 - starts; |
| 3085 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3086 | if (unicode_decode_call_errorhandler( |
| 3087 | errors, |
| 3088 | &errorHandler, |
| 3089 | "utf16", errmsg, |
| 3090 | &starts, |
| 3091 | (const char **)&e, |
| 3092 | &startinpos, |
| 3093 | &endinpos, |
| 3094 | &exc, |
| 3095 | (const char **)&q, |
| 3096 | &unicode, |
| 3097 | &outpos, |
| 3098 | &p)) |
| 3099 | goto onError; |
| 3100 | /* The remaining input chars are ignored if the callback |
| 3101 | chooses to skip the input */ |
| 3102 | } |
| 3103 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3104 | |
| 3105 | if (byteorder) |
| 3106 | *byteorder = bo; |
| 3107 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3108 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3109 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3111 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3112 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | goto onError; |
| 3114 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3115 | Py_XDECREF(errorHandler); |
| 3116 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3117 | return (PyObject *)unicode; |
| 3118 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3119 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3120 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3121 | Py_XDECREF(errorHandler); |
| 3122 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3123 | return NULL; |
| 3124 | } |
| 3125 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3126 | #undef FAST_CHAR_MASK |
| 3127 | #undef SWAPPED_FAST_CHAR_MASK |
| 3128 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3129 | PyObject * |
| 3130 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3131 | Py_ssize_t size, |
| 3132 | const char *errors, |
| 3133 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3134 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3135 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3136 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3137 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3138 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3139 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3140 | #else |
| 3141 | const int pairs = 0; |
| 3142 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3143 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3144 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3145 | int ihi = 1, ilo = 0; |
| 3146 | #else |
| 3147 | int ihi = 0, ilo = 1; |
| 3148 | #endif |
| 3149 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3150 | #define STORECHAR(CH) \ |
| 3151 | do { \ |
| 3152 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3153 | p[ilo] = (CH) & 0xff; \ |
| 3154 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3155 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3156 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3157 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3158 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3159 | if (s[i] >= 0x10000) |
| 3160 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3161 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3162 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3163 | if (size > PY_SSIZE_T_MAX || |
| 3164 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3165 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3166 | nsize = size + pairs + (byteorder == 0); |
| 3167 | bytesize = nsize * 2; |
| 3168 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3169 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3170 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3171 | if (v == NULL) |
| 3172 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3173 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3174 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3175 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3176 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3177 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3178 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3179 | |
| 3180 | if (byteorder == -1) { |
| 3181 | /* force LE */ |
| 3182 | ihi = 1; |
| 3183 | ilo = 0; |
| 3184 | } |
| 3185 | else if (byteorder == 1) { |
| 3186 | /* force BE */ |
| 3187 | ihi = 0; |
| 3188 | ilo = 1; |
| 3189 | } |
| 3190 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3191 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3192 | Py_UNICODE ch = *s++; |
| 3193 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3194 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3195 | if (ch >= 0x10000) { |
| 3196 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3197 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3198 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3199 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3200 | STORECHAR(ch); |
| 3201 | if (ch2) |
| 3202 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3203 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3204 | |
| 3205 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3206 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3207 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3211 | { |
| 3212 | if (!PyUnicode_Check(unicode)) { |
| 3213 | PyErr_BadArgument(); |
| 3214 | return NULL; |
| 3215 | } |
| 3216 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3217 | PyUnicode_GET_SIZE(unicode), |
| 3218 | NULL, |
| 3219 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3223 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3224 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3226 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3227 | Py_ssize_t size, |
| 3228 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3229 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3230 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3231 | Py_ssize_t startinpos; |
| 3232 | Py_ssize_t endinpos; |
| 3233 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3234 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3235 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3236 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3237 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3238 | char* message; |
| 3239 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3240 | PyObject *errorHandler = NULL; |
| 3241 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3243 | /* Escaped strings will always be longer than the resulting |
| 3244 | 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] | 3245 | length after conversion to the true value. |
| 3246 | (but if the error callback returns a long replacement string |
| 3247 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3248 | v = _PyUnicode_New(size); |
| 3249 | if (v == NULL) |
| 3250 | goto onError; |
| 3251 | if (size == 0) |
| 3252 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3253 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3254 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3255 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3256 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3257 | while (s < end) { |
| 3258 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3259 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3260 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3261 | |
| 3262 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3263 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3264 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3265 | continue; |
| 3266 | } |
| 3267 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3268 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3269 | /* \ - Escapes */ |
| 3270 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3271 | c = *s++; |
| 3272 | if (s > end) |
| 3273 | c = '\0'; /* Invalid after \ */ |
| 3274 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3275 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3276 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3277 | case '\n': break; |
| 3278 | case '\\': *p++ = '\\'; break; |
| 3279 | case '\'': *p++ = '\''; break; |
| 3280 | case '\"': *p++ = '\"'; break; |
| 3281 | case 'b': *p++ = '\b'; break; |
| 3282 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3283 | case 't': *p++ = '\t'; break; |
| 3284 | case 'n': *p++ = '\n'; break; |
| 3285 | case 'r': *p++ = '\r'; break; |
| 3286 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3287 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3288 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3289 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3290 | case '0': case '1': case '2': case '3': |
| 3291 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3292 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3293 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3294 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3295 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3296 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3297 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3298 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3299 | break; |
| 3300 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3301 | /* hex escapes */ |
| 3302 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3303 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3304 | digits = 2; |
| 3305 | message = "truncated \\xXX escape"; |
| 3306 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3307 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3308 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3309 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3310 | digits = 4; |
| 3311 | message = "truncated \\uXXXX escape"; |
| 3312 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3313 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3314 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3315 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3316 | digits = 8; |
| 3317 | message = "truncated \\UXXXXXXXX escape"; |
| 3318 | hexescape: |
| 3319 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3320 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3321 | if (s+digits>end) { |
| 3322 | endinpos = size; |
| 3323 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3324 | errors, &errorHandler, |
| 3325 | "unicodeescape", "end of string in escape sequence", |
| 3326 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3327 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3328 | goto onError; |
| 3329 | goto nextByte; |
| 3330 | } |
| 3331 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3332 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3333 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3334 | endinpos = (s+i+1)-starts; |
| 3335 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3336 | errors, &errorHandler, |
| 3337 | "unicodeescape", message, |
| 3338 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3339 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3340 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3341 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3342 | } |
| 3343 | chr = (chr<<4) & ~0xF; |
| 3344 | if (c >= '0' && c <= '9') |
| 3345 | chr += c - '0'; |
| 3346 | else if (c >= 'a' && c <= 'f') |
| 3347 | chr += 10 + c - 'a'; |
| 3348 | else |
| 3349 | chr += 10 + c - 'A'; |
| 3350 | } |
| 3351 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3352 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3353 | /* _decoding_error will have already written into the |
| 3354 | target buffer. */ |
| 3355 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3356 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3357 | /* when we get here, chr is a 32-bit unicode character */ |
| 3358 | if (chr <= 0xffff) |
| 3359 | /* UCS-2 character */ |
| 3360 | *p++ = (Py_UNICODE) chr; |
| 3361 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3362 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3363 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3364 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3365 | *p++ = chr; |
| 3366 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3367 | chr -= 0x10000L; |
| 3368 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3369 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3370 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3371 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3372 | endinpos = s-starts; |
| 3373 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3374 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3375 | errors, &errorHandler, |
| 3376 | "unicodeescape", "illegal Unicode character", |
| 3377 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3378 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3379 | goto onError; |
| 3380 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3381 | break; |
| 3382 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3383 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3384 | case 'N': |
| 3385 | message = "malformed \\N character escape"; |
| 3386 | if (ucnhash_CAPI == NULL) { |
| 3387 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3388 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3389 | if (ucnhash_CAPI == NULL) |
| 3390 | goto ucnhashError; |
| 3391 | } |
| 3392 | if (*s == '{') { |
| 3393 | const char *start = s+1; |
| 3394 | /* look for the closing brace */ |
| 3395 | while (*s != '}' && s < end) |
| 3396 | s++; |
| 3397 | if (s > start && s < end && *s == '}') { |
| 3398 | /* found a name. look it up in the unicode database */ |
| 3399 | message = "unknown Unicode character name"; |
| 3400 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3401 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3402 | goto store; |
| 3403 | } |
| 3404 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3405 | endinpos = s-starts; |
| 3406 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3407 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3408 | errors, &errorHandler, |
| 3409 | "unicodeescape", message, |
| 3410 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3411 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3412 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3413 | break; |
| 3414 | |
| 3415 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3416 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3417 | message = "\\ at end of string"; |
| 3418 | s--; |
| 3419 | endinpos = s-starts; |
| 3420 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3421 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3422 | errors, &errorHandler, |
| 3423 | "unicodeescape", message, |
| 3424 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3425 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3426 | goto onError; |
| 3427 | } |
| 3428 | else { |
| 3429 | *p++ = '\\'; |
| 3430 | *p++ = (unsigned char)s[-1]; |
| 3431 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3432 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3433 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3434 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3435 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3436 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3437 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3438 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3439 | Py_XDECREF(errorHandler); |
| 3440 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3441 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3442 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3443 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3444 | PyErr_SetString( |
| 3445 | PyExc_UnicodeError, |
| 3446 | "\\N escapes not supported (can't load unicodedata module)" |
| 3447 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3448 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3449 | Py_XDECREF(errorHandler); |
| 3450 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3451 | return NULL; |
| 3452 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3453 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3454 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3455 | Py_XDECREF(errorHandler); |
| 3456 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3457 | return NULL; |
| 3458 | } |
| 3459 | |
| 3460 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3461 | |
| 3462 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3463 | appropriate. |
| 3464 | |
| 3465 | */ |
| 3466 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3467 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3468 | Py_ssize_t size, |
| 3469 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3470 | { |
| 3471 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3472 | |
| 3473 | while (size-- > 0) { |
| 3474 | if (*s == ch) |
| 3475 | return s; |
| 3476 | s++; |
| 3477 | } |
| 3478 | |
| 3479 | return NULL; |
| 3480 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3481 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3482 | static const char *hexdigits = "0123456789abcdef"; |
| 3483 | |
| 3484 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3485 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3486 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3487 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3488 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3489 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3490 | #ifdef Py_UNICODE_WIDE |
| 3491 | const Py_ssize_t expandsize = 10; |
| 3492 | #else |
| 3493 | const Py_ssize_t expandsize = 6; |
| 3494 | #endif |
| 3495 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3496 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3497 | better to choose a different scheme. Perhaps scan the |
| 3498 | first N-chars of the string and allocate based on that size. |
| 3499 | */ |
| 3500 | /* Initial allocation is based on the longest-possible unichr |
| 3501 | escape. |
| 3502 | |
| 3503 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3504 | unichr, so in this case it's the longest unichr escape. In |
| 3505 | narrow (UTF-16) builds this is five chars per source unichr |
| 3506 | since there are two unichrs in the surrogate pair, so in narrow |
| 3507 | (UTF-16) builds it's not the longest unichr escape. |
| 3508 | |
| 3509 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3510 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3511 | escape. |
| 3512 | */ |
| 3513 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3514 | if (size == 0) |
| 3515 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3516 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3517 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3518 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3519 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3520 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3521 | 2 |
| 3522 | + expandsize*size |
| 3523 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3524 | if (repr == NULL) |
| 3525 | return NULL; |
| 3526 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3527 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3528 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3529 | while (size-- > 0) { |
| 3530 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3531 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3532 | /* Escape backslashes */ |
| 3533 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3534 | *p++ = '\\'; |
| 3535 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3536 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3537 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3538 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3539 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3540 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3541 | else if (ch >= 0x10000) { |
| 3542 | *p++ = '\\'; |
| 3543 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3544 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3545 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3546 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3547 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3548 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3549 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3550 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3551 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3552 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3553 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3554 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3555 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3556 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3557 | Py_UNICODE ch2; |
| 3558 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3559 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3560 | ch2 = *s++; |
| 3561 | size--; |
| 3562 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3563 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3564 | *p++ = '\\'; |
| 3565 | *p++ = 'U'; |
| 3566 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3567 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3568 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3569 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3570 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3571 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3572 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3573 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3574 | continue; |
| 3575 | } |
| 3576 | /* Fall through: isolated surrogates are copied as-is */ |
| 3577 | s--; |
| 3578 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3579 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3580 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3582 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3583 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3584 | *p++ = '\\'; |
| 3585 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3586 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3587 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3588 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3589 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3590 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3591 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3592 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3593 | else if (ch == '\t') { |
| 3594 | *p++ = '\\'; |
| 3595 | *p++ = 't'; |
| 3596 | } |
| 3597 | else if (ch == '\n') { |
| 3598 | *p++ = '\\'; |
| 3599 | *p++ = 'n'; |
| 3600 | } |
| 3601 | else if (ch == '\r') { |
| 3602 | *p++ = '\\'; |
| 3603 | *p++ = 'r'; |
| 3604 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3605 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3606 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3607 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3608 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3609 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3610 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3611 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3612 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3614 | /* Copy everything else as-is */ |
| 3615 | else |
| 3616 | *p++ = (char) ch; |
| 3617 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3618 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3619 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3620 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3621 | return NULL; |
| 3622 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3625 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3626 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3627 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3628 | if (!PyUnicode_Check(unicode)) { |
| 3629 | PyErr_BadArgument(); |
| 3630 | return NULL; |
| 3631 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3632 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3633 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3634 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3638 | |
| 3639 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3640 | Py_ssize_t size, |
| 3641 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3642 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3643 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3644 | Py_ssize_t startinpos; |
| 3645 | Py_ssize_t endinpos; |
| 3646 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3647 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3648 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3649 | const char *end; |
| 3650 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3651 | PyObject *errorHandler = NULL; |
| 3652 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3654 | /* Escaped strings will always be longer than the resulting |
| 3655 | 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] | 3656 | length after conversion to the true value. (But decoding error |
| 3657 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3658 | v = _PyUnicode_New(size); |
| 3659 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3660 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3661 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3662 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3663 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3664 | end = s + size; |
| 3665 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3666 | unsigned char c; |
| 3667 | Py_UCS4 x; |
| 3668 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3669 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3670 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3671 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3672 | if (*s != '\\') { |
| 3673 | *p++ = (unsigned char)*s++; |
| 3674 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3675 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3676 | startinpos = s-starts; |
| 3677 | |
| 3678 | /* \u-escapes are only interpreted iff the number of leading |
| 3679 | backslashes if odd */ |
| 3680 | bs = s; |
| 3681 | for (;s < end;) { |
| 3682 | if (*s != '\\') |
| 3683 | break; |
| 3684 | *p++ = (unsigned char)*s++; |
| 3685 | } |
| 3686 | if (((s - bs) & 1) == 0 || |
| 3687 | s >= end || |
| 3688 | (*s != 'u' && *s != 'U')) { |
| 3689 | continue; |
| 3690 | } |
| 3691 | p--; |
| 3692 | count = *s=='u' ? 4 : 8; |
| 3693 | s++; |
| 3694 | |
| 3695 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3696 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3697 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3698 | c = (unsigned char)*s; |
| 3699 | if (!ISXDIGIT(c)) { |
| 3700 | endinpos = s-starts; |
| 3701 | if (unicode_decode_call_errorhandler( |
| 3702 | errors, &errorHandler, |
| 3703 | "rawunicodeescape", "truncated \\uXXXX", |
| 3704 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3705 | &v, &outpos, &p)) |
| 3706 | goto onError; |
| 3707 | goto nextByte; |
| 3708 | } |
| 3709 | x = (x<<4) & ~0xF; |
| 3710 | if (c >= '0' && c <= '9') |
| 3711 | x += c - '0'; |
| 3712 | else if (c >= 'a' && c <= 'f') |
| 3713 | x += 10 + c - 'a'; |
| 3714 | else |
| 3715 | x += 10 + c - 'A'; |
| 3716 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3717 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3718 | /* UCS-2 character */ |
| 3719 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3720 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3721 | /* UCS-4 character. Either store directly, or as |
| 3722 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3723 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3724 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3725 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3726 | x -= 0x10000L; |
| 3727 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3728 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3729 | #endif |
| 3730 | } else { |
| 3731 | endinpos = s-starts; |
| 3732 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3733 | if (unicode_decode_call_errorhandler( |
| 3734 | errors, &errorHandler, |
| 3735 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3736 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3737 | &v, &outpos, &p)) |
| 3738 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3739 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3740 | nextByte: |
| 3741 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3742 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3743 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3744 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3745 | Py_XDECREF(errorHandler); |
| 3746 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3747 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3748 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3749 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3750 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3751 | Py_XDECREF(errorHandler); |
| 3752 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3753 | return NULL; |
| 3754 | } |
| 3755 | |
| 3756 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3757 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3758 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3759 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3760 | char *p; |
| 3761 | char *q; |
| 3762 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3763 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3764 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3765 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3766 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3767 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3768 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3769 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3770 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3771 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3772 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3773 | if (repr == NULL) |
| 3774 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3775 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3776 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3777 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3778 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3779 | while (size-- > 0) { |
| 3780 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3781 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3782 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3783 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3784 | *p++ = '\\'; |
| 3785 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3786 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3787 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3788 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3789 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3790 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3791 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3792 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3793 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3794 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3795 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3796 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3797 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3798 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3799 | Py_UNICODE ch2; |
| 3800 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3801 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3802 | ch2 = *s++; |
| 3803 | size--; |
| 3804 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3805 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3806 | *p++ = '\\'; |
| 3807 | *p++ = 'U'; |
| 3808 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3809 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3810 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3811 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3812 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3813 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3814 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3815 | *p++ = hexdigits[ucs & 0xf]; |
| 3816 | continue; |
| 3817 | } |
| 3818 | /* Fall through: isolated surrogates are copied as-is */ |
| 3819 | s--; |
| 3820 | size++; |
| 3821 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3822 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3823 | /* Map 16-bit characters to '\uxxxx' */ |
| 3824 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3825 | *p++ = '\\'; |
| 3826 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3827 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3828 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3829 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3830 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3831 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3832 | /* Copy everything else as-is */ |
| 3833 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3834 | *p++ = (char) ch; |
| 3835 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3836 | size = p - q; |
| 3837 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3838 | assert(size > 0); |
| 3839 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3840 | return NULL; |
| 3841 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3845 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3846 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3847 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3848 | PyErr_BadArgument(); |
| 3849 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3850 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3851 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3852 | PyUnicode_GET_SIZE(unicode)); |
| 3853 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3854 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3857 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3858 | |
| 3859 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3860 | Py_ssize_t size, |
| 3861 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3862 | { |
| 3863 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3864 | Py_ssize_t startinpos; |
| 3865 | Py_ssize_t endinpos; |
| 3866 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3867 | PyUnicodeObject *v; |
| 3868 | Py_UNICODE *p; |
| 3869 | const char *end; |
| 3870 | const char *reason; |
| 3871 | PyObject *errorHandler = NULL; |
| 3872 | PyObject *exc = NULL; |
| 3873 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3874 | #ifdef Py_UNICODE_WIDE |
| 3875 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3876 | #endif |
| 3877 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3878 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3879 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3880 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3881 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3882 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3883 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3884 | p = PyUnicode_AS_UNICODE(v); |
| 3885 | end = s + size; |
| 3886 | |
| 3887 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3888 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3889 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3890 | some malformed UCS-4 data. */ |
| 3891 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3892 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3893 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3894 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3895 | end-s < Py_UNICODE_SIZE |
| 3896 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3897 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3898 | startinpos = s - starts; |
| 3899 | if (end-s < Py_UNICODE_SIZE) { |
| 3900 | endinpos = end-starts; |
| 3901 | reason = "truncated input"; |
| 3902 | } |
| 3903 | else { |
| 3904 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3905 | reason = "illegal code point (> 0x10FFFF)"; |
| 3906 | } |
| 3907 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3908 | if (unicode_decode_call_errorhandler( |
| 3909 | errors, &errorHandler, |
| 3910 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3911 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3912 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3913 | goto onError; |
| 3914 | } |
| 3915 | } |
| 3916 | else { |
| 3917 | p++; |
| 3918 | s += Py_UNICODE_SIZE; |
| 3919 | } |
| 3920 | } |
| 3921 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3922 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3923 | goto onError; |
| 3924 | Py_XDECREF(errorHandler); |
| 3925 | Py_XDECREF(exc); |
| 3926 | return (PyObject *)v; |
| 3927 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3928 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3929 | Py_XDECREF(v); |
| 3930 | Py_XDECREF(errorHandler); |
| 3931 | Py_XDECREF(exc); |
| 3932 | return NULL; |
| 3933 | } |
| 3934 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3935 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3936 | |
| 3937 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3938 | Py_ssize_t size, |
| 3939 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3940 | { |
| 3941 | PyUnicodeObject *v; |
| 3942 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3943 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3944 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3945 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3946 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3947 | Py_UNICODE r = *(unsigned char*)s; |
| 3948 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3951 | v = _PyUnicode_New(size); |
| 3952 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3953 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3954 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3955 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3956 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3957 | e = s + size; |
| 3958 | /* Unrolling the copy makes it much faster by reducing the looping |
| 3959 | overhead. This is similar to what many memcpy() implementations do. */ |
| 3960 | unrolled_end = e - 4; |
| 3961 | while (s < unrolled_end) { |
| 3962 | p[0] = (unsigned char) s[0]; |
| 3963 | p[1] = (unsigned char) s[1]; |
| 3964 | p[2] = (unsigned char) s[2]; |
| 3965 | p[3] = (unsigned char) s[3]; |
| 3966 | s += 4; |
| 3967 | p += 4; |
| 3968 | } |
| 3969 | while (s < e) |
| 3970 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3971 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3972 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3973 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3974 | Py_XDECREF(v); |
| 3975 | return NULL; |
| 3976 | } |
| 3977 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3978 | /* create or adjust a UnicodeEncodeError */ |
| 3979 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3980 | const char *encoding, |
| 3981 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3982 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3983 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3984 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3985 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3986 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3987 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3988 | } |
| 3989 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3990 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3991 | goto onError; |
| 3992 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3993 | goto onError; |
| 3994 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3995 | goto onError; |
| 3996 | return; |
| 3997 | onError: |
| 3998 | Py_DECREF(*exceptionObject); |
| 3999 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4000 | } |
| 4001 | } |
| 4002 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4003 | /* raises a UnicodeEncodeError */ |
| 4004 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4005 | const char *encoding, |
| 4006 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4007 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4008 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4009 | { |
| 4010 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4011 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4012 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4013 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | /* error handling callback helper: |
| 4017 | build arguments, call the callback and check the arguments, |
| 4018 | put the result into newpos and return the replacement string, which |
| 4019 | has to be freed by the caller */ |
| 4020 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4021 | PyObject **errorHandler, |
| 4022 | const char *encoding, const char *reason, |
| 4023 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4024 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4025 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4026 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4027 | 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] | 4028 | |
| 4029 | PyObject *restuple; |
| 4030 | PyObject *resunicode; |
| 4031 | |
| 4032 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4033 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4034 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4035 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4039 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4040 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4041 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4042 | |
| 4043 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4044 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4045 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4046 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4047 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4048 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4049 | Py_DECREF(restuple); |
| 4050 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4051 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4052 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4053 | &resunicode, newpos)) { |
| 4054 | Py_DECREF(restuple); |
| 4055 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4056 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4057 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4058 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4059 | Py_DECREF(restuple); |
| 4060 | return NULL; |
| 4061 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4062 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4063 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4064 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4065 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4066 | Py_DECREF(restuple); |
| 4067 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4068 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4069 | Py_INCREF(resunicode); |
| 4070 | Py_DECREF(restuple); |
| 4071 | return resunicode; |
| 4072 | } |
| 4073 | |
| 4074 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4075 | Py_ssize_t size, |
| 4076 | const char *errors, |
| 4077 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4078 | { |
| 4079 | /* output object */ |
| 4080 | PyObject *res; |
| 4081 | /* pointers to the beginning and end+1 of input */ |
| 4082 | const Py_UNICODE *startp = p; |
| 4083 | const Py_UNICODE *endp = p + size; |
| 4084 | /* pointer to the beginning of the unencodable characters */ |
| 4085 | /* const Py_UNICODE *badp = NULL; */ |
| 4086 | /* pointer into the output */ |
| 4087 | char *str; |
| 4088 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4089 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4090 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4091 | 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] | 4092 | PyObject *errorHandler = NULL; |
| 4093 | PyObject *exc = NULL; |
| 4094 | /* the following variable is used for caching string comparisons |
| 4095 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4096 | int known_errorHandler = -1; |
| 4097 | |
| 4098 | /* allocate enough for a simple encoding without |
| 4099 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4100 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4101 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4102 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4103 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4104 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4105 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4106 | ressize = size; |
| 4107 | |
| 4108 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4109 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4110 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4111 | /* can we encode this? */ |
| 4112 | if (c<limit) { |
| 4113 | /* no overflow check, because we know that the space is enough */ |
| 4114 | *str++ = (char)c; |
| 4115 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4116 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4117 | else { |
| 4118 | Py_ssize_t unicodepos = p-startp; |
| 4119 | Py_ssize_t requiredsize; |
| 4120 | PyObject *repunicode; |
| 4121 | Py_ssize_t repsize; |
| 4122 | Py_ssize_t newpos; |
| 4123 | Py_ssize_t respos; |
| 4124 | Py_UNICODE *uni2; |
| 4125 | /* startpos for collecting unencodable chars */ |
| 4126 | const Py_UNICODE *collstart = p; |
| 4127 | const Py_UNICODE *collend = p; |
| 4128 | /* find all unecodable characters */ |
| 4129 | while ((collend < endp) && ((*collend)>=limit)) |
| 4130 | ++collend; |
| 4131 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4132 | if (known_errorHandler==-1) { |
| 4133 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4134 | known_errorHandler = 1; |
| 4135 | else if (!strcmp(errors, "replace")) |
| 4136 | known_errorHandler = 2; |
| 4137 | else if (!strcmp(errors, "ignore")) |
| 4138 | known_errorHandler = 3; |
| 4139 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4140 | known_errorHandler = 4; |
| 4141 | else |
| 4142 | known_errorHandler = 0; |
| 4143 | } |
| 4144 | switch (known_errorHandler) { |
| 4145 | case 1: /* strict */ |
| 4146 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4147 | goto onError; |
| 4148 | case 2: /* replace */ |
| 4149 | while (collstart++<collend) |
| 4150 | *str++ = '?'; /* fall through */ |
| 4151 | case 3: /* ignore */ |
| 4152 | p = collend; |
| 4153 | break; |
| 4154 | case 4: /* xmlcharrefreplace */ |
| 4155 | respos = str - PyBytes_AS_STRING(res); |
| 4156 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4157 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4158 | if (*p<10) |
| 4159 | repsize += 2+1+1; |
| 4160 | else if (*p<100) |
| 4161 | repsize += 2+2+1; |
| 4162 | else if (*p<1000) |
| 4163 | repsize += 2+3+1; |
| 4164 | else if (*p<10000) |
| 4165 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4166 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4167 | else |
| 4168 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4169 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4170 | else if (*p<100000) |
| 4171 | repsize += 2+5+1; |
| 4172 | else if (*p<1000000) |
| 4173 | repsize += 2+6+1; |
| 4174 | else |
| 4175 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4176 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4177 | } |
| 4178 | requiredsize = respos+repsize+(endp-collend); |
| 4179 | if (requiredsize > ressize) { |
| 4180 | if (requiredsize<2*ressize) |
| 4181 | requiredsize = 2*ressize; |
| 4182 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4183 | goto onError; |
| 4184 | str = PyBytes_AS_STRING(res) + respos; |
| 4185 | ressize = requiredsize; |
| 4186 | } |
| 4187 | /* generate replacement (temporarily (mis)uses p) */ |
| 4188 | for (p = collstart; p < collend; ++p) { |
| 4189 | str += sprintf(str, "&#%d;", (int)*p); |
| 4190 | } |
| 4191 | p = collend; |
| 4192 | break; |
| 4193 | default: |
| 4194 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4195 | encoding, reason, startp, size, &exc, |
| 4196 | collstart-startp, collend-startp, &newpos); |
| 4197 | if (repunicode == NULL) |
| 4198 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4199 | if (PyBytes_Check(repunicode)) { |
| 4200 | /* Directly copy bytes result to output. */ |
| 4201 | repsize = PyBytes_Size(repunicode); |
| 4202 | if (repsize > 1) { |
| 4203 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4204 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4205 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4206 | Py_DECREF(repunicode); |
| 4207 | goto onError; |
| 4208 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4209 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4210 | ressize += repsize-1; |
| 4211 | } |
| 4212 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4213 | str += repsize; |
| 4214 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4215 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4216 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4217 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4218 | /* need more space? (at least enough for what we |
| 4219 | have+the replacement+the rest of the string, so |
| 4220 | we won't have to check space for encodable characters) */ |
| 4221 | respos = str - PyBytes_AS_STRING(res); |
| 4222 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4223 | requiredsize = respos+repsize+(endp-collend); |
| 4224 | if (requiredsize > ressize) { |
| 4225 | if (requiredsize<2*ressize) |
| 4226 | requiredsize = 2*ressize; |
| 4227 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4228 | Py_DECREF(repunicode); |
| 4229 | goto onError; |
| 4230 | } |
| 4231 | str = PyBytes_AS_STRING(res) + respos; |
| 4232 | ressize = requiredsize; |
| 4233 | } |
| 4234 | /* check if there is anything unencodable in the replacement |
| 4235 | and copy it to the output */ |
| 4236 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4237 | c = *uni2; |
| 4238 | if (c >= limit) { |
| 4239 | raise_encode_exception(&exc, encoding, startp, size, |
| 4240 | unicodepos, unicodepos+1, reason); |
| 4241 | Py_DECREF(repunicode); |
| 4242 | goto onError; |
| 4243 | } |
| 4244 | *str = (char)c; |
| 4245 | } |
| 4246 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4247 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4248 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4249 | } |
| 4250 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4251 | /* Resize if we allocated to much */ |
| 4252 | size = str - PyBytes_AS_STRING(res); |
| 4253 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4254 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4255 | if (_PyBytes_Resize(&res, size) < 0) |
| 4256 | goto onError; |
| 4257 | } |
| 4258 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4259 | Py_XDECREF(errorHandler); |
| 4260 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4261 | return res; |
| 4262 | |
| 4263 | onError: |
| 4264 | Py_XDECREF(res); |
| 4265 | Py_XDECREF(errorHandler); |
| 4266 | Py_XDECREF(exc); |
| 4267 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4270 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4271 | Py_ssize_t size, |
| 4272 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4273 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4274 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4278 | { |
| 4279 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4280 | PyErr_BadArgument(); |
| 4281 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4282 | } |
| 4283 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4284 | PyUnicode_GET_SIZE(unicode), |
| 4285 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4290 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4291 | Py_ssize_t size, |
| 4292 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4293 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4294 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4295 | PyUnicodeObject *v; |
| 4296 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4297 | Py_ssize_t startinpos; |
| 4298 | Py_ssize_t endinpos; |
| 4299 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4300 | const char *e; |
| 4301 | PyObject *errorHandler = NULL; |
| 4302 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4304 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4305 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4306 | Py_UNICODE r = *(unsigned char*)s; |
| 4307 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4308 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4309 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4310 | v = _PyUnicode_New(size); |
| 4311 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4312 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4313 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4314 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4315 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4316 | e = s + size; |
| 4317 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4318 | register unsigned char c = (unsigned char)*s; |
| 4319 | if (c < 128) { |
| 4320 | *p++ = c; |
| 4321 | ++s; |
| 4322 | } |
| 4323 | else { |
| 4324 | startinpos = s-starts; |
| 4325 | endinpos = startinpos + 1; |
| 4326 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4327 | if (unicode_decode_call_errorhandler( |
| 4328 | errors, &errorHandler, |
| 4329 | "ascii", "ordinal not in range(128)", |
| 4330 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4331 | &v, &outpos, &p)) |
| 4332 | goto onError; |
| 4333 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4334 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4335 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4336 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4337 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4338 | Py_XDECREF(errorHandler); |
| 4339 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4340 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4341 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4342 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4343 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4344 | Py_XDECREF(errorHandler); |
| 4345 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4346 | return NULL; |
| 4347 | } |
| 4348 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4349 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4350 | Py_ssize_t size, |
| 4351 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4352 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4353 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4357 | { |
| 4358 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4359 | PyErr_BadArgument(); |
| 4360 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4361 | } |
| 4362 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4363 | PyUnicode_GET_SIZE(unicode), |
| 4364 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4367 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4368 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4369 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4370 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4371 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4372 | #define NEED_RETRY |
| 4373 | #endif |
| 4374 | |
| 4375 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4376 | a) it assumes an incomplete character consists of a single byte, and |
| 4377 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4378 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4379 | |
| 4380 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4381 | { |
| 4382 | const char *curr = s + offset; |
| 4383 | |
| 4384 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4385 | const char *prev = CharPrev(s, curr); |
| 4386 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4387 | } |
| 4388 | return 0; |
| 4389 | } |
| 4390 | |
| 4391 | /* |
| 4392 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4393 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4394 | */ |
| 4395 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4396 | const char *s, /* MBCS string */ |
| 4397 | int size, /* sizeof MBCS string */ |
| 4398 | int final) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4399 | { |
| 4400 | Py_UNICODE *p; |
| 4401 | Py_ssize_t n = 0; |
| 4402 | int usize = 0; |
| 4403 | |
| 4404 | assert(size >= 0); |
| 4405 | |
| 4406 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4407 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4408 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4409 | |
| 4410 | /* First get the size of the result */ |
| 4411 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4412 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4413 | if (usize == 0) { |
| 4414 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4415 | return -1; |
| 4416 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4420 | /* Create unicode object */ |
| 4421 | *v = _PyUnicode_New(usize); |
| 4422 | if (*v == NULL) |
| 4423 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4424 | } |
| 4425 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4426 | /* Extend unicode object */ |
| 4427 | n = PyUnicode_GET_SIZE(*v); |
| 4428 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4429 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | /* Do the conversion */ |
| 4433 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4434 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4435 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4436 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4437 | return -1; |
| 4438 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | return size; |
| 4442 | } |
| 4443 | |
| 4444 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4445 | Py_ssize_t size, |
| 4446 | const char *errors, |
| 4447 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4448 | { |
| 4449 | PyUnicodeObject *v = NULL; |
| 4450 | int done; |
| 4451 | |
| 4452 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4453 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4454 | |
| 4455 | #ifdef NEED_RETRY |
| 4456 | retry: |
| 4457 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4458 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4459 | else |
| 4460 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4461 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4462 | |
| 4463 | if (done < 0) { |
| 4464 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4465 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
| 4468 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4469 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4470 | |
| 4471 | #ifdef NEED_RETRY |
| 4472 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4473 | s += done; |
| 4474 | size -= done; |
| 4475 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4476 | } |
| 4477 | #endif |
| 4478 | |
| 4479 | return (PyObject *)v; |
| 4480 | } |
| 4481 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4482 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4483 | Py_ssize_t size, |
| 4484 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4485 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4486 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4487 | } |
| 4488 | |
| 4489 | /* |
| 4490 | * Convert unicode into string object (MBCS). |
| 4491 | * Returns 0 if succeed, -1 otherwise. |
| 4492 | */ |
| 4493 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4494 | const Py_UNICODE *p, /* unicode */ |
| 4495 | int size) /* size of unicode */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4496 | { |
| 4497 | int mbcssize = 0; |
| 4498 | Py_ssize_t n = 0; |
| 4499 | |
| 4500 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4501 | |
| 4502 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4503 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4504 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4505 | if (mbcssize == 0) { |
| 4506 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4507 | return -1; |
| 4508 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4511 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4512 | /* Create string object */ |
| 4513 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4514 | if (*repr == NULL) |
| 4515 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4516 | } |
| 4517 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4518 | /* Extend string object */ |
| 4519 | n = PyBytes_Size(*repr); |
| 4520 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4521 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
| 4524 | /* Do the conversion */ |
| 4525 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4526 | char *s = PyBytes_AS_STRING(*repr) + n; |
| 4527 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4528 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4529 | return -1; |
| 4530 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
| 4533 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4537 | Py_ssize_t size, |
| 4538 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4539 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4540 | PyObject *repr = NULL; |
| 4541 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4542 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4543 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4544 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4545 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4546 | ret = encode_mbcs(&repr, p, INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4547 | else |
| 4548 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4549 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4550 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4551 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4552 | Py_XDECREF(repr); |
| 4553 | return NULL; |
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 | |
| 4556 | #ifdef NEED_RETRY |
| 4557 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4558 | p += INT_MAX; |
| 4559 | size -= INT_MAX; |
| 4560 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4561 | } |
| 4562 | #endif |
| 4563 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4564 | return repr; |
| 4565 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4566 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4567 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4568 | { |
| 4569 | if (!PyUnicode_Check(unicode)) { |
| 4570 | PyErr_BadArgument(); |
| 4571 | return NULL; |
| 4572 | } |
| 4573 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4574 | PyUnicode_GET_SIZE(unicode), |
| 4575 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4578 | #undef NEED_RETRY |
| 4579 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4580 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4582 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4583 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4584 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4585 | Py_ssize_t size, |
| 4586 | PyObject *mapping, |
| 4587 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4588 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4589 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4590 | Py_ssize_t startinpos; |
| 4591 | Py_ssize_t endinpos; |
| 4592 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4593 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4594 | PyUnicodeObject *v; |
| 4595 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4596 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4597 | PyObject *errorHandler = NULL; |
| 4598 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4599 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4600 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4601 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4602 | /* Default to Latin-1 */ |
| 4603 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4604 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4605 | |
| 4606 | v = _PyUnicode_New(size); |
| 4607 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4608 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4609 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4610 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4611 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4612 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4613 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4614 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4615 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4616 | while (s < e) { |
| 4617 | unsigned char ch = *s; |
| 4618 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4619 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4620 | if (ch < maplen) |
| 4621 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4622 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4623 | if (x == 0xfffe) { |
| 4624 | /* undefined mapping */ |
| 4625 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4626 | startinpos = s-starts; |
| 4627 | endinpos = startinpos+1; |
| 4628 | if (unicode_decode_call_errorhandler( |
| 4629 | errors, &errorHandler, |
| 4630 | "charmap", "character maps to <undefined>", |
| 4631 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4632 | &v, &outpos, &p)) { |
| 4633 | goto onError; |
| 4634 | } |
| 4635 | continue; |
| 4636 | } |
| 4637 | *p++ = x; |
| 4638 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4639 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4640 | } |
| 4641 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4642 | while (s < e) { |
| 4643 | unsigned char ch = *s; |
| 4644 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4645 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4646 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4647 | w = PyLong_FromLong((long)ch); |
| 4648 | if (w == NULL) |
| 4649 | goto onError; |
| 4650 | x = PyObject_GetItem(mapping, w); |
| 4651 | Py_DECREF(w); |
| 4652 | if (x == NULL) { |
| 4653 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4654 | /* No mapping found means: mapping is undefined. */ |
| 4655 | PyErr_Clear(); |
| 4656 | x = Py_None; |
| 4657 | Py_INCREF(x); |
| 4658 | } else |
| 4659 | goto onError; |
| 4660 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4661 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4662 | /* Apply mapping */ |
| 4663 | if (PyLong_Check(x)) { |
| 4664 | long value = PyLong_AS_LONG(x); |
| 4665 | if (value < 0 || value > 65535) { |
| 4666 | PyErr_SetString(PyExc_TypeError, |
| 4667 | "character mapping must be in range(65536)"); |
| 4668 | Py_DECREF(x); |
| 4669 | goto onError; |
| 4670 | } |
| 4671 | *p++ = (Py_UNICODE)value; |
| 4672 | } |
| 4673 | else if (x == Py_None) { |
| 4674 | /* undefined mapping */ |
| 4675 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4676 | startinpos = s-starts; |
| 4677 | endinpos = startinpos+1; |
| 4678 | if (unicode_decode_call_errorhandler( |
| 4679 | errors, &errorHandler, |
| 4680 | "charmap", "character maps to <undefined>", |
| 4681 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4682 | &v, &outpos, &p)) { |
| 4683 | Py_DECREF(x); |
| 4684 | goto onError; |
| 4685 | } |
| 4686 | Py_DECREF(x); |
| 4687 | continue; |
| 4688 | } |
| 4689 | else if (PyUnicode_Check(x)) { |
| 4690 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4691 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4692 | if (targetsize == 1) |
| 4693 | /* 1-1 mapping */ |
| 4694 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4695 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4696 | else if (targetsize > 1) { |
| 4697 | /* 1-n mapping */ |
| 4698 | if (targetsize > extrachars) { |
| 4699 | /* resize first */ |
| 4700 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4701 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4702 | (targetsize << 2); |
| 4703 | extrachars += needed; |
| 4704 | /* XXX overflow detection missing */ |
| 4705 | if (_PyUnicode_Resize(&v, |
| 4706 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4707 | Py_DECREF(x); |
| 4708 | goto onError; |
| 4709 | } |
| 4710 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4711 | } |
| 4712 | Py_UNICODE_COPY(p, |
| 4713 | PyUnicode_AS_UNICODE(x), |
| 4714 | targetsize); |
| 4715 | p += targetsize; |
| 4716 | extrachars -= targetsize; |
| 4717 | } |
| 4718 | /* 1-0 mapping: skip the character */ |
| 4719 | } |
| 4720 | else { |
| 4721 | /* wrong return value */ |
| 4722 | PyErr_SetString(PyExc_TypeError, |
| 4723 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4724 | Py_DECREF(x); |
| 4725 | goto onError; |
| 4726 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4727 | Py_DECREF(x); |
| 4728 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4729 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4730 | } |
| 4731 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4732 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4733 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4734 | Py_XDECREF(errorHandler); |
| 4735 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4736 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4737 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4738 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4739 | Py_XDECREF(errorHandler); |
| 4740 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4741 | Py_XDECREF(v); |
| 4742 | return NULL; |
| 4743 | } |
| 4744 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4745 | /* Charmap encoding: the lookup table */ |
| 4746 | |
| 4747 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4748 | PyObject_HEAD |
| 4749 | unsigned char level1[32]; |
| 4750 | int count2, count3; |
| 4751 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4752 | }; |
| 4753 | |
| 4754 | static PyObject* |
| 4755 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4756 | { |
| 4757 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4758 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4759 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4763 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4764 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4765 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4766 | }; |
| 4767 | |
| 4768 | static void |
| 4769 | encoding_map_dealloc(PyObject* o) |
| 4770 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4771 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4775 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4776 | "EncodingMap", /*tp_name*/ |
| 4777 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4778 | 0, /*tp_itemsize*/ |
| 4779 | /* methods */ |
| 4780 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4781 | 0, /*tp_print*/ |
| 4782 | 0, /*tp_getattr*/ |
| 4783 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4784 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4785 | 0, /*tp_repr*/ |
| 4786 | 0, /*tp_as_number*/ |
| 4787 | 0, /*tp_as_sequence*/ |
| 4788 | 0, /*tp_as_mapping*/ |
| 4789 | 0, /*tp_hash*/ |
| 4790 | 0, /*tp_call*/ |
| 4791 | 0, /*tp_str*/ |
| 4792 | 0, /*tp_getattro*/ |
| 4793 | 0, /*tp_setattro*/ |
| 4794 | 0, /*tp_as_buffer*/ |
| 4795 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4796 | 0, /*tp_doc*/ |
| 4797 | 0, /*tp_traverse*/ |
| 4798 | 0, /*tp_clear*/ |
| 4799 | 0, /*tp_richcompare*/ |
| 4800 | 0, /*tp_weaklistoffset*/ |
| 4801 | 0, /*tp_iter*/ |
| 4802 | 0, /*tp_iternext*/ |
| 4803 | encoding_map_methods, /*tp_methods*/ |
| 4804 | 0, /*tp_members*/ |
| 4805 | 0, /*tp_getset*/ |
| 4806 | 0, /*tp_base*/ |
| 4807 | 0, /*tp_dict*/ |
| 4808 | 0, /*tp_descr_get*/ |
| 4809 | 0, /*tp_descr_set*/ |
| 4810 | 0, /*tp_dictoffset*/ |
| 4811 | 0, /*tp_init*/ |
| 4812 | 0, /*tp_alloc*/ |
| 4813 | 0, /*tp_new*/ |
| 4814 | 0, /*tp_free*/ |
| 4815 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4816 | }; |
| 4817 | |
| 4818 | PyObject* |
| 4819 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4820 | { |
| 4821 | Py_UNICODE *decode; |
| 4822 | PyObject *result; |
| 4823 | struct encoding_map *mresult; |
| 4824 | int i; |
| 4825 | int need_dict = 0; |
| 4826 | unsigned char level1[32]; |
| 4827 | unsigned char level2[512]; |
| 4828 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4829 | int count2 = 0, count3 = 0; |
| 4830 | |
| 4831 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4832 | PyErr_BadArgument(); |
| 4833 | return NULL; |
| 4834 | } |
| 4835 | decode = PyUnicode_AS_UNICODE(string); |
| 4836 | memset(level1, 0xFF, sizeof level1); |
| 4837 | memset(level2, 0xFF, sizeof level2); |
| 4838 | |
| 4839 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4840 | or if there are non-BMP characters, we need to use |
| 4841 | a mapping dictionary. */ |
| 4842 | if (decode[0] != 0) |
| 4843 | need_dict = 1; |
| 4844 | for (i = 1; i < 256; i++) { |
| 4845 | int l1, l2; |
| 4846 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4847 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4848 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4849 | #endif |
| 4850 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4851 | need_dict = 1; |
| 4852 | break; |
| 4853 | } |
| 4854 | if (decode[i] == 0xFFFE) |
| 4855 | /* unmapped character */ |
| 4856 | continue; |
| 4857 | l1 = decode[i] >> 11; |
| 4858 | l2 = decode[i] >> 7; |
| 4859 | if (level1[l1] == 0xFF) |
| 4860 | level1[l1] = count2++; |
| 4861 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4862 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4866 | need_dict = 1; |
| 4867 | |
| 4868 | if (need_dict) { |
| 4869 | PyObject *result = PyDict_New(); |
| 4870 | PyObject *key, *value; |
| 4871 | if (!result) |
| 4872 | return NULL; |
| 4873 | for (i = 0; i < 256; i++) { |
| 4874 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4875 | key = PyLong_FromLong(decode[i]); |
| 4876 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4877 | if (!key || !value) |
| 4878 | goto failed1; |
| 4879 | if (PyDict_SetItem(result, key, value) == -1) |
| 4880 | goto failed1; |
| 4881 | Py_DECREF(key); |
| 4882 | Py_DECREF(value); |
| 4883 | } |
| 4884 | return result; |
| 4885 | failed1: |
| 4886 | Py_XDECREF(key); |
| 4887 | Py_XDECREF(value); |
| 4888 | Py_DECREF(result); |
| 4889 | return NULL; |
| 4890 | } |
| 4891 | |
| 4892 | /* Create a three-level trie */ |
| 4893 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4894 | 16*count2 + 128*count3 - 1); |
| 4895 | if (!result) |
| 4896 | return PyErr_NoMemory(); |
| 4897 | PyObject_Init(result, &EncodingMapType); |
| 4898 | mresult = (struct encoding_map*)result; |
| 4899 | mresult->count2 = count2; |
| 4900 | mresult->count3 = count3; |
| 4901 | mlevel1 = mresult->level1; |
| 4902 | mlevel2 = mresult->level23; |
| 4903 | mlevel3 = mresult->level23 + 16*count2; |
| 4904 | memcpy(mlevel1, level1, 32); |
| 4905 | memset(mlevel2, 0xFF, 16*count2); |
| 4906 | memset(mlevel3, 0, 128*count3); |
| 4907 | count3 = 0; |
| 4908 | for (i = 1; i < 256; i++) { |
| 4909 | int o1, o2, o3, i2, i3; |
| 4910 | if (decode[i] == 0xFFFE) |
| 4911 | /* unmapped character */ |
| 4912 | continue; |
| 4913 | o1 = decode[i]>>11; |
| 4914 | o2 = (decode[i]>>7) & 0xF; |
| 4915 | i2 = 16*mlevel1[o1] + o2; |
| 4916 | if (mlevel2[i2] == 0xFF) |
| 4917 | mlevel2[i2] = count3++; |
| 4918 | o3 = decode[i] & 0x7F; |
| 4919 | i3 = 128*mlevel2[i2] + o3; |
| 4920 | mlevel3[i3] = i; |
| 4921 | } |
| 4922 | return result; |
| 4923 | } |
| 4924 | |
| 4925 | static int |
| 4926 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4927 | { |
| 4928 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4929 | int l1 = c>>11; |
| 4930 | int l2 = (c>>7) & 0xF; |
| 4931 | int l3 = c & 0x7F; |
| 4932 | int i; |
| 4933 | |
| 4934 | #ifdef Py_UNICODE_WIDE |
| 4935 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4936 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4937 | } |
| 4938 | #endif |
| 4939 | if (c == 0) |
| 4940 | return 0; |
| 4941 | /* level 1*/ |
| 4942 | i = map->level1[l1]; |
| 4943 | if (i == 0xFF) { |
| 4944 | return -1; |
| 4945 | } |
| 4946 | /* level 2*/ |
| 4947 | i = map->level23[16*i+l2]; |
| 4948 | if (i == 0xFF) { |
| 4949 | return -1; |
| 4950 | } |
| 4951 | /* level 3 */ |
| 4952 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4953 | if (i == 0) { |
| 4954 | return -1; |
| 4955 | } |
| 4956 | return i; |
| 4957 | } |
| 4958 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4959 | /* Lookup the character ch in the mapping. If the character |
| 4960 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4961 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4962 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4963 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4964 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4965 | PyObject *x; |
| 4966 | |
| 4967 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4968 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4969 | x = PyObject_GetItem(mapping, w); |
| 4970 | Py_DECREF(w); |
| 4971 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4972 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4973 | /* No mapping found means: mapping is undefined. */ |
| 4974 | PyErr_Clear(); |
| 4975 | x = Py_None; |
| 4976 | Py_INCREF(x); |
| 4977 | return x; |
| 4978 | } else |
| 4979 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4980 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4981 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4982 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4983 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4984 | long value = PyLong_AS_LONG(x); |
| 4985 | if (value < 0 || value > 255) { |
| 4986 | PyErr_SetString(PyExc_TypeError, |
| 4987 | "character mapping must be in range(256)"); |
| 4988 | Py_DECREF(x); |
| 4989 | return NULL; |
| 4990 | } |
| 4991 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4992 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4993 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4994 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4995 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4996 | /* wrong return value */ |
| 4997 | PyErr_Format(PyExc_TypeError, |
| 4998 | "character mapping must return integer, bytes or None, not %.400s", |
| 4999 | x->ob_type->tp_name); |
| 5000 | Py_DECREF(x); |
| 5001 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5002 | } |
| 5003 | } |
| 5004 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5005 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5006 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5007 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5008 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5009 | /* exponentially overallocate to minimize reallocations */ |
| 5010 | if (requiredsize < 2*outsize) |
| 5011 | requiredsize = 2*outsize; |
| 5012 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5013 | return -1; |
| 5014 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5017 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5018 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5019 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5020 | /* 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] | 5021 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5022 | space is available. Return a new reference to the object that |
| 5023 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5024 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5025 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5026 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5027 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5028 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5029 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5030 | PyObject *rep; |
| 5031 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5032 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5033 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5034 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5035 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5036 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5037 | if (res == -1) |
| 5038 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5039 | if (outsize<requiredsize) |
| 5040 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5041 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5042 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5043 | outstart[(*outpos)++] = (char)res; |
| 5044 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5048 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5049 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5050 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5051 | Py_DECREF(rep); |
| 5052 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5053 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5054 | if (PyLong_Check(rep)) { |
| 5055 | Py_ssize_t requiredsize = *outpos+1; |
| 5056 | if (outsize<requiredsize) |
| 5057 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5058 | Py_DECREF(rep); |
| 5059 | return enc_EXCEPTION; |
| 5060 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5061 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5062 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5063 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5064 | else { |
| 5065 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5066 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5067 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5068 | if (outsize<requiredsize) |
| 5069 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5070 | Py_DECREF(rep); |
| 5071 | return enc_EXCEPTION; |
| 5072 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5073 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5074 | memcpy(outstart + *outpos, repchars, repsize); |
| 5075 | *outpos += repsize; |
| 5076 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5077 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5078 | Py_DECREF(rep); |
| 5079 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | /* handle an error in PyUnicode_EncodeCharmap |
| 5083 | Return 0 on success, -1 on error */ |
| 5084 | static |
| 5085 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5086 | 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] | 5087 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5088 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5089 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5090 | { |
| 5091 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5092 | Py_ssize_t repsize; |
| 5093 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5094 | Py_UNICODE *uni2; |
| 5095 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5096 | Py_ssize_t collstartpos = *inpos; |
| 5097 | Py_ssize_t collendpos = *inpos+1; |
| 5098 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5099 | char *encoding = "charmap"; |
| 5100 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5101 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5102 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5103 | /* find all unencodable characters */ |
| 5104 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5105 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5106 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5107 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5108 | if (res != -1) |
| 5109 | break; |
| 5110 | ++collendpos; |
| 5111 | continue; |
| 5112 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5113 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5114 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5115 | if (rep==NULL) |
| 5116 | return -1; |
| 5117 | else if (rep!=Py_None) { |
| 5118 | Py_DECREF(rep); |
| 5119 | break; |
| 5120 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5121 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5122 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5123 | } |
| 5124 | /* cache callback name lookup |
| 5125 | * (if not done yet, i.e. it's the first error) */ |
| 5126 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5127 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5128 | *known_errorHandler = 1; |
| 5129 | else if (!strcmp(errors, "replace")) |
| 5130 | *known_errorHandler = 2; |
| 5131 | else if (!strcmp(errors, "ignore")) |
| 5132 | *known_errorHandler = 3; |
| 5133 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5134 | *known_errorHandler = 4; |
| 5135 | else |
| 5136 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5137 | } |
| 5138 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5139 | case 1: /* strict */ |
| 5140 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5141 | return -1; |
| 5142 | case 2: /* replace */ |
| 5143 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5144 | x = charmapencode_output('?', mapping, res, respos); |
| 5145 | if (x==enc_EXCEPTION) { |
| 5146 | return -1; |
| 5147 | } |
| 5148 | else if (x==enc_FAILED) { |
| 5149 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5150 | return -1; |
| 5151 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5152 | } |
| 5153 | /* fall through */ |
| 5154 | case 3: /* ignore */ |
| 5155 | *inpos = collendpos; |
| 5156 | break; |
| 5157 | case 4: /* xmlcharrefreplace */ |
| 5158 | /* generate replacement (temporarily (mis)uses p) */ |
| 5159 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5160 | char buffer[2+29+1+1]; |
| 5161 | char *cp; |
| 5162 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5163 | for (cp = buffer; *cp; ++cp) { |
| 5164 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5165 | if (x==enc_EXCEPTION) |
| 5166 | return -1; |
| 5167 | else if (x==enc_FAILED) { |
| 5168 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5169 | return -1; |
| 5170 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5171 | } |
| 5172 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5173 | *inpos = collendpos; |
| 5174 | break; |
| 5175 | default: |
| 5176 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5177 | encoding, reason, p, size, exceptionObject, |
| 5178 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5179 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5180 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5181 | if (PyBytes_Check(repunicode)) { |
| 5182 | /* Directly copy bytes result to output. */ |
| 5183 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5184 | Py_ssize_t requiredsize; |
| 5185 | repsize = PyBytes_Size(repunicode); |
| 5186 | requiredsize = *respos + repsize; |
| 5187 | if (requiredsize > outsize) |
| 5188 | /* Make room for all additional bytes. */ |
| 5189 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5190 | Py_DECREF(repunicode); |
| 5191 | return -1; |
| 5192 | } |
| 5193 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5194 | PyBytes_AsString(repunicode), repsize); |
| 5195 | *respos += repsize; |
| 5196 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5197 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5198 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5199 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5200 | /* generate replacement */ |
| 5201 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5202 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5203 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5204 | if (x==enc_EXCEPTION) { |
| 5205 | return -1; |
| 5206 | } |
| 5207 | else if (x==enc_FAILED) { |
| 5208 | Py_DECREF(repunicode); |
| 5209 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5210 | return -1; |
| 5211 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5212 | } |
| 5213 | *inpos = newpos; |
| 5214 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5215 | } |
| 5216 | return 0; |
| 5217 | } |
| 5218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5220 | Py_ssize_t size, |
| 5221 | PyObject *mapping, |
| 5222 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5223 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5224 | /* output object */ |
| 5225 | PyObject *res = NULL; |
| 5226 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5227 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5228 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5229 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5230 | PyObject *errorHandler = NULL; |
| 5231 | PyObject *exc = NULL; |
| 5232 | /* the following variable is used for caching string comparisons |
| 5233 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5234 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5235 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5236 | |
| 5237 | /* Default to Latin-1 */ |
| 5238 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5239 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5240 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5241 | /* allocate enough for a simple encoding without |
| 5242 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5243 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5244 | if (res == NULL) |
| 5245 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5246 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5247 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5248 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5249 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5250 | /* try to encode it */ |
| 5251 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5252 | if (x==enc_EXCEPTION) /* error */ |
| 5253 | goto onError; |
| 5254 | if (x==enc_FAILED) { /* unencodable character */ |
| 5255 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5256 | &exc, |
| 5257 | &known_errorHandler, &errorHandler, errors, |
| 5258 | &res, &respos)) { |
| 5259 | goto onError; |
| 5260 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5261 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5262 | else |
| 5263 | /* done with this character => adjust input position */ |
| 5264 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5266 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5267 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5268 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5269 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5270 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5271 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5272 | Py_XDECREF(exc); |
| 5273 | Py_XDECREF(errorHandler); |
| 5274 | return res; |
| 5275 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5276 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5277 | Py_XDECREF(res); |
| 5278 | Py_XDECREF(exc); |
| 5279 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5280 | return NULL; |
| 5281 | } |
| 5282 | |
| 5283 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5284 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5285 | { |
| 5286 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5287 | PyErr_BadArgument(); |
| 5288 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5289 | } |
| 5290 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5291 | PyUnicode_GET_SIZE(unicode), |
| 5292 | mapping, |
| 5293 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5296 | /* create or adjust a UnicodeTranslateError */ |
| 5297 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5298 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5299 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5300 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5302 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5303 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5304 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5305 | } |
| 5306 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5307 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5308 | goto onError; |
| 5309 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5310 | goto onError; |
| 5311 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5312 | goto onError; |
| 5313 | return; |
| 5314 | onError: |
| 5315 | Py_DECREF(*exceptionObject); |
| 5316 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5317 | } |
| 5318 | } |
| 5319 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5320 | /* raises a UnicodeTranslateError */ |
| 5321 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5322 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5323 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5324 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5325 | { |
| 5326 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5327 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5328 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5329 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
| 5332 | /* error handling callback helper: |
| 5333 | build arguments, call the callback and check the arguments, |
| 5334 | put the result into newpos and return the replacement string, which |
| 5335 | has to be freed by the caller */ |
| 5336 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5337 | PyObject **errorHandler, |
| 5338 | const char *reason, |
| 5339 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5340 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5341 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5342 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5343 | 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] | 5344 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5345 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5346 | PyObject *restuple; |
| 5347 | PyObject *resunicode; |
| 5348 | |
| 5349 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5350 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5351 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5352 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5356 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5357 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5358 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5359 | |
| 5360 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5361 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5362 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5363 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5364 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5365 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5366 | Py_DECREF(restuple); |
| 5367 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5368 | } |
| 5369 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5370 | &resunicode, &i_newpos)) { |
| 5371 | Py_DECREF(restuple); |
| 5372 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5373 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5374 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5375 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5376 | else |
| 5377 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5378 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5380 | Py_DECREF(restuple); |
| 5381 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5382 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5383 | Py_INCREF(resunicode); |
| 5384 | Py_DECREF(restuple); |
| 5385 | return resunicode; |
| 5386 | } |
| 5387 | |
| 5388 | /* Lookup the character ch in the mapping and put the result in result, |
| 5389 | which must be decrefed by the caller. |
| 5390 | Return 0 on success, -1 on error */ |
| 5391 | static |
| 5392 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5393 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5394 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5395 | PyObject *x; |
| 5396 | |
| 5397 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5398 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5399 | x = PyObject_GetItem(mapping, w); |
| 5400 | Py_DECREF(w); |
| 5401 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5402 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5403 | /* No mapping found means: use 1:1 mapping. */ |
| 5404 | PyErr_Clear(); |
| 5405 | *result = NULL; |
| 5406 | return 0; |
| 5407 | } else |
| 5408 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5409 | } |
| 5410 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5411 | *result = x; |
| 5412 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5413 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5414 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5415 | long value = PyLong_AS_LONG(x); |
| 5416 | long max = PyUnicode_GetMax(); |
| 5417 | if (value < 0 || value > max) { |
| 5418 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5419 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5420 | Py_DECREF(x); |
| 5421 | return -1; |
| 5422 | } |
| 5423 | *result = x; |
| 5424 | return 0; |
| 5425 | } |
| 5426 | else if (PyUnicode_Check(x)) { |
| 5427 | *result = x; |
| 5428 | return 0; |
| 5429 | } |
| 5430 | else { |
| 5431 | /* wrong return value */ |
| 5432 | PyErr_SetString(PyExc_TypeError, |
| 5433 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5434 | Py_DECREF(x); |
| 5435 | return -1; |
| 5436 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5437 | } |
| 5438 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5439 | if not reallocate and adjust various state variables. |
| 5440 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5441 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5442 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5443 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5444 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5445 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5446 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5447 | /* remember old output position */ |
| 5448 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5449 | /* exponentially overallocate to minimize reallocations */ |
| 5450 | if (requiredsize < 2 * oldsize) |
| 5451 | requiredsize = 2 * oldsize; |
| 5452 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5453 | return -1; |
| 5454 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5455 | } |
| 5456 | return 0; |
| 5457 | } |
| 5458 | /* lookup the character, put the result in the output string and adjust |
| 5459 | various state variables. Return a new reference to the object that |
| 5460 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5461 | undefined (in which case no character was written). |
| 5462 | The called must decref result. |
| 5463 | Return 0 on success, -1 on error. */ |
| 5464 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5465 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5466 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5467 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5468 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5469 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5470 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5471 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5472 | /* not found => default to 1:1 mapping */ |
| 5473 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5474 | } |
| 5475 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5476 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5477 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5478 | /* no overflow check, because we know that the space is enough */ |
| 5479 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5480 | } |
| 5481 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5482 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5483 | if (repsize==1) { |
| 5484 | /* no overflow check, because we know that the space is enough */ |
| 5485 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5486 | } |
| 5487 | else if (repsize!=0) { |
| 5488 | /* more than one character */ |
| 5489 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5490 | (insize - (curinp-startinp)) + |
| 5491 | repsize - 1; |
| 5492 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5493 | return -1; |
| 5494 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5495 | *outp += repsize; |
| 5496 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5497 | } |
| 5498 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5499 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5500 | return 0; |
| 5501 | } |
| 5502 | |
| 5503 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5504 | Py_ssize_t size, |
| 5505 | PyObject *mapping, |
| 5506 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5507 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5508 | /* output object */ |
| 5509 | PyObject *res = NULL; |
| 5510 | /* pointers to the beginning and end+1 of input */ |
| 5511 | const Py_UNICODE *startp = p; |
| 5512 | const Py_UNICODE *endp = p + size; |
| 5513 | /* pointer into the output */ |
| 5514 | Py_UNICODE *str; |
| 5515 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5516 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5517 | char *reason = "character maps to <undefined>"; |
| 5518 | PyObject *errorHandler = NULL; |
| 5519 | PyObject *exc = NULL; |
| 5520 | /* the following variable is used for caching string comparisons |
| 5521 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5522 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5523 | int known_errorHandler = -1; |
| 5524 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5525 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5526 | PyErr_BadArgument(); |
| 5527 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5528 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5529 | |
| 5530 | /* allocate enough for a simple 1:1 translation without |
| 5531 | replacements, if we need more, we'll resize */ |
| 5532 | res = PyUnicode_FromUnicode(NULL, size); |
| 5533 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5534 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5535 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5536 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5537 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5539 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5540 | /* try to encode it */ |
| 5541 | PyObject *x = NULL; |
| 5542 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5543 | Py_XDECREF(x); |
| 5544 | goto onError; |
| 5545 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5546 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5547 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5548 | ++p; |
| 5549 | else { /* untranslatable character */ |
| 5550 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5551 | Py_ssize_t repsize; |
| 5552 | Py_ssize_t newpos; |
| 5553 | Py_UNICODE *uni2; |
| 5554 | /* startpos for collecting untranslatable chars */ |
| 5555 | const Py_UNICODE *collstart = p; |
| 5556 | const Py_UNICODE *collend = p+1; |
| 5557 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5558 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5559 | /* find all untranslatable characters */ |
| 5560 | while (collend < endp) { |
| 5561 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5562 | goto onError; |
| 5563 | Py_XDECREF(x); |
| 5564 | if (x!=Py_None) |
| 5565 | break; |
| 5566 | ++collend; |
| 5567 | } |
| 5568 | /* cache callback name lookup |
| 5569 | * (if not done yet, i.e. it's the first error) */ |
| 5570 | if (known_errorHandler==-1) { |
| 5571 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5572 | known_errorHandler = 1; |
| 5573 | else if (!strcmp(errors, "replace")) |
| 5574 | known_errorHandler = 2; |
| 5575 | else if (!strcmp(errors, "ignore")) |
| 5576 | known_errorHandler = 3; |
| 5577 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5578 | known_errorHandler = 4; |
| 5579 | else |
| 5580 | known_errorHandler = 0; |
| 5581 | } |
| 5582 | switch (known_errorHandler) { |
| 5583 | case 1: /* strict */ |
| 5584 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5585 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5586 | case 2: /* replace */ |
| 5587 | /* No need to check for space, this is a 1:1 replacement */ |
| 5588 | for (coll = collstart; coll<collend; ++coll) |
| 5589 | *str++ = '?'; |
| 5590 | /* fall through */ |
| 5591 | case 3: /* ignore */ |
| 5592 | p = collend; |
| 5593 | break; |
| 5594 | case 4: /* xmlcharrefreplace */ |
| 5595 | /* generate replacement (temporarily (mis)uses p) */ |
| 5596 | for (p = collstart; p < collend; ++p) { |
| 5597 | char buffer[2+29+1+1]; |
| 5598 | char *cp; |
| 5599 | sprintf(buffer, "&#%d;", (int)*p); |
| 5600 | if (charmaptranslate_makespace(&res, &str, |
| 5601 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5602 | goto onError; |
| 5603 | for (cp = buffer; *cp; ++cp) |
| 5604 | *str++ = *cp; |
| 5605 | } |
| 5606 | p = collend; |
| 5607 | break; |
| 5608 | default: |
| 5609 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5610 | reason, startp, size, &exc, |
| 5611 | collstart-startp, collend-startp, &newpos); |
| 5612 | if (repunicode == NULL) |
| 5613 | goto onError; |
| 5614 | /* generate replacement */ |
| 5615 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5616 | if (charmaptranslate_makespace(&res, &str, |
| 5617 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5618 | Py_DECREF(repunicode); |
| 5619 | goto onError; |
| 5620 | } |
| 5621 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5622 | *str++ = *uni2; |
| 5623 | p = startp + newpos; |
| 5624 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5625 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5626 | } |
| 5627 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5628 | /* Resize if we allocated to much */ |
| 5629 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5630 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5631 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5632 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5633 | } |
| 5634 | Py_XDECREF(exc); |
| 5635 | Py_XDECREF(errorHandler); |
| 5636 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5637 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5638 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5639 | Py_XDECREF(res); |
| 5640 | Py_XDECREF(exc); |
| 5641 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | return NULL; |
| 5643 | } |
| 5644 | |
| 5645 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5646 | PyObject *mapping, |
| 5647 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5648 | { |
| 5649 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5650 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5651 | str = PyUnicode_FromObject(str); |
| 5652 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5653 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5654 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5655 | PyUnicode_GET_SIZE(str), |
| 5656 | mapping, |
| 5657 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | Py_DECREF(str); |
| 5659 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5660 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5661 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | Py_XDECREF(str); |
| 5663 | return NULL; |
| 5664 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5665 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5666 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5667 | |
| 5668 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5669 | Py_ssize_t length, |
| 5670 | char *output, |
| 5671 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5672 | { |
| 5673 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5674 | PyObject *errorHandler = NULL; |
| 5675 | PyObject *exc = NULL; |
| 5676 | const char *encoding = "decimal"; |
| 5677 | const char *reason = "invalid decimal Unicode string"; |
| 5678 | /* the following variable is used for caching string comparisons |
| 5679 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5680 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5681 | |
| 5682 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5683 | PyErr_BadArgument(); |
| 5684 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | p = s; |
| 5688 | end = s + length; |
| 5689 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5690 | register Py_UNICODE ch = *p; |
| 5691 | int decimal; |
| 5692 | PyObject *repunicode; |
| 5693 | Py_ssize_t repsize; |
| 5694 | Py_ssize_t newpos; |
| 5695 | Py_UNICODE *uni2; |
| 5696 | Py_UNICODE *collstart; |
| 5697 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5698 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5699 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5700 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5701 | ++p; |
| 5702 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5703 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5704 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5705 | if (decimal >= 0) { |
| 5706 | *output++ = '0' + decimal; |
| 5707 | ++p; |
| 5708 | continue; |
| 5709 | } |
| 5710 | if (0 < ch && ch < 256) { |
| 5711 | *output++ = (char)ch; |
| 5712 | ++p; |
| 5713 | continue; |
| 5714 | } |
| 5715 | /* All other characters are considered unencodable */ |
| 5716 | collstart = p; |
| 5717 | collend = p+1; |
| 5718 | while (collend < end) { |
| 5719 | if ((0 < *collend && *collend < 256) || |
| 5720 | !Py_UNICODE_ISSPACE(*collend) || |
| 5721 | Py_UNICODE_TODECIMAL(*collend)) |
| 5722 | break; |
| 5723 | } |
| 5724 | /* cache callback name lookup |
| 5725 | * (if not done yet, i.e. it's the first error) */ |
| 5726 | if (known_errorHandler==-1) { |
| 5727 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5728 | known_errorHandler = 1; |
| 5729 | else if (!strcmp(errors, "replace")) |
| 5730 | known_errorHandler = 2; |
| 5731 | else if (!strcmp(errors, "ignore")) |
| 5732 | known_errorHandler = 3; |
| 5733 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5734 | known_errorHandler = 4; |
| 5735 | else |
| 5736 | known_errorHandler = 0; |
| 5737 | } |
| 5738 | switch (known_errorHandler) { |
| 5739 | case 1: /* strict */ |
| 5740 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5741 | goto onError; |
| 5742 | case 2: /* replace */ |
| 5743 | for (p = collstart; p < collend; ++p) |
| 5744 | *output++ = '?'; |
| 5745 | /* fall through */ |
| 5746 | case 3: /* ignore */ |
| 5747 | p = collend; |
| 5748 | break; |
| 5749 | case 4: /* xmlcharrefreplace */ |
| 5750 | /* generate replacement (temporarily (mis)uses p) */ |
| 5751 | for (p = collstart; p < collend; ++p) |
| 5752 | output += sprintf(output, "&#%d;", (int)*p); |
| 5753 | p = collend; |
| 5754 | break; |
| 5755 | default: |
| 5756 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5757 | encoding, reason, s, length, &exc, |
| 5758 | collstart-s, collend-s, &newpos); |
| 5759 | if (repunicode == NULL) |
| 5760 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5761 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5762 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5763 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 5764 | Py_DECREF(repunicode); |
| 5765 | goto onError; |
| 5766 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5767 | /* generate replacement */ |
| 5768 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5769 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5770 | Py_UNICODE ch = *uni2; |
| 5771 | if (Py_UNICODE_ISSPACE(ch)) |
| 5772 | *output++ = ' '; |
| 5773 | else { |
| 5774 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5775 | if (decimal >= 0) |
| 5776 | *output++ = '0' + decimal; |
| 5777 | else if (0 < ch && ch < 256) |
| 5778 | *output++ = (char)ch; |
| 5779 | else { |
| 5780 | Py_DECREF(repunicode); |
| 5781 | raise_encode_exception(&exc, encoding, |
| 5782 | s, length, collstart-s, collend-s, reason); |
| 5783 | goto onError; |
| 5784 | } |
| 5785 | } |
| 5786 | } |
| 5787 | p = s + newpos; |
| 5788 | Py_DECREF(repunicode); |
| 5789 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5790 | } |
| 5791 | /* 0-terminate the output string */ |
| 5792 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5793 | Py_XDECREF(exc); |
| 5794 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5795 | return 0; |
| 5796 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5797 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5798 | Py_XDECREF(exc); |
| 5799 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5800 | return -1; |
| 5801 | } |
| 5802 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5803 | /* --- Helpers ------------------------------------------------------------ */ |
| 5804 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5805 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5806 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5807 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5808 | /* Include _ParseTupleFinds from find.h */ |
| 5809 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5810 | #include "stringlib/find.h" |
| 5811 | #include "stringlib/partition.h" |
| 5812 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5813 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 5814 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5815 | #include "stringlib/localeutil.h" |
| 5816 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5817 | /* helper macro to fixup start/end slice values */ |
| 5818 | #define FIX_START_END(obj) \ |
| 5819 | if (start < 0) \ |
| 5820 | start += (obj)->length; \ |
| 5821 | if (start < 0) \ |
| 5822 | start = 0; \ |
| 5823 | if (end > (obj)->length) \ |
| 5824 | end = (obj)->length; \ |
| 5825 | if (end < 0) \ |
| 5826 | end += (obj)->length; \ |
| 5827 | if (end < 0) \ |
| 5828 | end = 0; |
| 5829 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5830 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5831 | PyObject *substr, |
| 5832 | Py_ssize_t start, |
| 5833 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5834 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5835 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5836 | PyUnicodeObject* str_obj; |
| 5837 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5838 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5839 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5840 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5841 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5842 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5843 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5844 | Py_DECREF(str_obj); |
| 5845 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5846 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5847 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5848 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5849 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5850 | result = stringlib_count( |
| 5851 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5852 | ); |
| 5853 | |
| 5854 | Py_DECREF(sub_obj); |
| 5855 | Py_DECREF(str_obj); |
| 5856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5857 | return result; |
| 5858 | } |
| 5859 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5860 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5861 | PyObject *sub, |
| 5862 | Py_ssize_t start, |
| 5863 | Py_ssize_t end, |
| 5864 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5866 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5867 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5868 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5869 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5870 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5871 | sub = PyUnicode_FromObject(sub); |
| 5872 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5873 | Py_DECREF(str); |
| 5874 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5875 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5876 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5877 | if (direction > 0) |
| 5878 | result = stringlib_find_slice( |
| 5879 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5880 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5881 | start, end |
| 5882 | ); |
| 5883 | else |
| 5884 | result = stringlib_rfind_slice( |
| 5885 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5886 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5887 | start, end |
| 5888 | ); |
| 5889 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5891 | Py_DECREF(sub); |
| 5892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | return result; |
| 5894 | } |
| 5895 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5896 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5898 | PyUnicodeObject *substring, |
| 5899 | Py_ssize_t start, |
| 5900 | Py_ssize_t end, |
| 5901 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5902 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | if (substring->length == 0) |
| 5904 | return 1; |
| 5905 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5906 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | |
| 5908 | end -= substring->length; |
| 5909 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5910 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | |
| 5912 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5913 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5914 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5915 | } else { |
| 5916 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5917 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
| 5920 | return 0; |
| 5921 | } |
| 5922 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5923 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5924 | PyObject *substr, |
| 5925 | Py_ssize_t start, |
| 5926 | Py_ssize_t end, |
| 5927 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5929 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5930 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5931 | str = PyUnicode_FromObject(str); |
| 5932 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5933 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | substr = PyUnicode_FromObject(substr); |
| 5935 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5936 | Py_DECREF(str); |
| 5937 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5939 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5940 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5941 | (PyUnicodeObject *)substr, |
| 5942 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5943 | Py_DECREF(str); |
| 5944 | Py_DECREF(substr); |
| 5945 | return result; |
| 5946 | } |
| 5947 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5948 | /* Apply fixfct filter to the Unicode object self and return a |
| 5949 | reference to the modified object */ |
| 5950 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5951 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5952 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5953 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | { |
| 5955 | |
| 5956 | PyUnicodeObject *u; |
| 5957 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5958 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5960 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5961 | |
| 5962 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5963 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5964 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5965 | /* fixfct should return TRUE if it modified the buffer. If |
| 5966 | FALSE, return a reference to the original buffer instead |
| 5967 | (to save space, not time) */ |
| 5968 | Py_INCREF(self); |
| 5969 | Py_DECREF(u); |
| 5970 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5971 | } |
| 5972 | return (PyObject*) u; |
| 5973 | } |
| 5974 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5975 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | int fixupper(PyUnicodeObject *self) |
| 5977 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5978 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5979 | Py_UNICODE *s = self->str; |
| 5980 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5981 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5982 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5983 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5984 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5985 | ch = Py_UNICODE_TOUPPER(*s); |
| 5986 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5988 | *s = ch; |
| 5989 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5990 | s++; |
| 5991 | } |
| 5992 | |
| 5993 | return status; |
| 5994 | } |
| 5995 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5996 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | int fixlower(PyUnicodeObject *self) |
| 5998 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5999 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | Py_UNICODE *s = self->str; |
| 6001 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6004 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6005 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6006 | ch = Py_UNICODE_TOLOWER(*s); |
| 6007 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6009 | *s = ch; |
| 6010 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | s++; |
| 6012 | } |
| 6013 | |
| 6014 | return status; |
| 6015 | } |
| 6016 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6017 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6018 | int fixswapcase(PyUnicodeObject *self) |
| 6019 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6020 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | Py_UNICODE *s = self->str; |
| 6022 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6023 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | while (len-- > 0) { |
| 6025 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6026 | *s = Py_UNICODE_TOLOWER(*s); |
| 6027 | status = 1; |
| 6028 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6029 | *s = Py_UNICODE_TOUPPER(*s); |
| 6030 | status = 1; |
| 6031 | } |
| 6032 | s++; |
| 6033 | } |
| 6034 | |
| 6035 | return status; |
| 6036 | } |
| 6037 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6038 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | int fixcapitalize(PyUnicodeObject *self) |
| 6040 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6041 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6042 | Py_UNICODE *s = self->str; |
| 6043 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6044 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6045 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6046 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6047 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6048 | *s = Py_UNICODE_TOUPPER(*s); |
| 6049 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6050 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6051 | s++; |
| 6052 | while (--len > 0) { |
| 6053 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6054 | *s = Py_UNICODE_TOLOWER(*s); |
| 6055 | status = 1; |
| 6056 | } |
| 6057 | s++; |
| 6058 | } |
| 6059 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6060 | } |
| 6061 | |
| 6062 | static |
| 6063 | int fixtitle(PyUnicodeObject *self) |
| 6064 | { |
| 6065 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6066 | register Py_UNICODE *e; |
| 6067 | int previous_is_cased; |
| 6068 | |
| 6069 | /* Shortcut for single character strings */ |
| 6070 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6071 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6072 | if (*p != ch) { |
| 6073 | *p = ch; |
| 6074 | return 1; |
| 6075 | } |
| 6076 | else |
| 6077 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6079 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | e = p + PyUnicode_GET_SIZE(self); |
| 6081 | previous_is_cased = 0; |
| 6082 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6083 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6084 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6085 | if (previous_is_cased) |
| 6086 | *p = Py_UNICODE_TOLOWER(ch); |
| 6087 | else |
| 6088 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6089 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6090 | if (Py_UNICODE_ISLOWER(ch) || |
| 6091 | Py_UNICODE_ISUPPER(ch) || |
| 6092 | Py_UNICODE_ISTITLE(ch)) |
| 6093 | previous_is_cased = 1; |
| 6094 | else |
| 6095 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | } |
| 6097 | return 1; |
| 6098 | } |
| 6099 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6100 | PyObject * |
| 6101 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6103 | const Py_UNICODE blank = ' '; |
| 6104 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6105 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6106 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6107 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6108 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6109 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6110 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6111 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6112 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6113 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6114 | fseq = PySequence_Fast(seq, ""); |
| 6115 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6116 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6119 | /* NOTE: the following code can't call back into Python code, |
| 6120 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6121 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6122 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6123 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6124 | /* If empty sequence, return u"". */ |
| 6125 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6126 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6127 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6128 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6129 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6130 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6131 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6132 | item = items[0]; |
| 6133 | if (PyUnicode_CheckExact(item)) { |
| 6134 | Py_INCREF(item); |
| 6135 | res = (PyUnicodeObject *)item; |
| 6136 | goto Done; |
| 6137 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6138 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6139 | else { |
| 6140 | /* Set up sep and seplen */ |
| 6141 | if (separator == NULL) { |
| 6142 | sep = ␣ |
| 6143 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6144 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6145 | else { |
| 6146 | if (!PyUnicode_Check(separator)) { |
| 6147 | PyErr_Format(PyExc_TypeError, |
| 6148 | "separator: expected str instance," |
| 6149 | " %.80s found", |
| 6150 | Py_TYPE(separator)->tp_name); |
| 6151 | goto onError; |
| 6152 | } |
| 6153 | sep = PyUnicode_AS_UNICODE(separator); |
| 6154 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6155 | } |
| 6156 | } |
| 6157 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6158 | /* There are at least two things to join, or else we have a subclass |
| 6159 | * of str in the sequence. |
| 6160 | * Do a pre-pass to figure out the total amount of space we'll |
| 6161 | * need (sz), and see whether all argument are strings. |
| 6162 | */ |
| 6163 | sz = 0; |
| 6164 | for (i = 0; i < seqlen; i++) { |
| 6165 | const Py_ssize_t old_sz = sz; |
| 6166 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | if (!PyUnicode_Check(item)) { |
| 6168 | PyErr_Format(PyExc_TypeError, |
| 6169 | "sequence item %zd: expected str instance," |
| 6170 | " %.80s found", |
| 6171 | i, Py_TYPE(item)->tp_name); |
| 6172 | goto onError; |
| 6173 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6174 | sz += PyUnicode_GET_SIZE(item); |
| 6175 | if (i != 0) |
| 6176 | sz += seplen; |
| 6177 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6178 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6179 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6180 | goto onError; |
| 6181 | } |
| 6182 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6183 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6184 | res = _PyUnicode_New(sz); |
| 6185 | if (res == NULL) |
| 6186 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6187 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6188 | /* Catenate everything. */ |
| 6189 | res_p = PyUnicode_AS_UNICODE(res); |
| 6190 | for (i = 0; i < seqlen; ++i) { |
| 6191 | Py_ssize_t itemlen; |
| 6192 | item = items[i]; |
| 6193 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6194 | /* Copy item, and maybe the separator. */ |
| 6195 | if (i) { |
| 6196 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6197 | res_p += seplen; |
| 6198 | } |
| 6199 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6200 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6201 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6202 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6203 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6204 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6205 | return (PyObject *)res; |
| 6206 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6207 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6208 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6209 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6210 | return NULL; |
| 6211 | } |
| 6212 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6213 | static |
| 6214 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6215 | Py_ssize_t left, |
| 6216 | Py_ssize_t right, |
| 6217 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | { |
| 6219 | PyUnicodeObject *u; |
| 6220 | |
| 6221 | if (left < 0) |
| 6222 | left = 0; |
| 6223 | if (right < 0) |
| 6224 | right = 0; |
| 6225 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6226 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6227 | Py_INCREF(self); |
| 6228 | return self; |
| 6229 | } |
| 6230 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6231 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6232 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6233 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6234 | return NULL; |
| 6235 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6236 | u = _PyUnicode_New(left + self->length + right); |
| 6237 | if (u) { |
| 6238 | if (left) |
| 6239 | Py_UNICODE_FILL(u->str, fill, left); |
| 6240 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6241 | if (right) |
| 6242 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6243 | } |
| 6244 | |
| 6245 | return u; |
| 6246 | } |
| 6247 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6248 | #define SPLIT_APPEND(data, left, right) \ |
| 6249 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 6250 | if (!str) \ |
| 6251 | goto onError; \ |
| 6252 | if (PyList_Append(list, str)) { \ |
| 6253 | Py_DECREF(str); \ |
| 6254 | goto onError; \ |
| 6255 | } \ |
| 6256 | else \ |
| 6257 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6258 | |
| 6259 | static |
| 6260 | PyObject *split_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6261 | PyObject *list, |
| 6262 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6263 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6264 | register Py_ssize_t i; |
| 6265 | register Py_ssize_t j; |
| 6266 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6267 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6268 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6269 | |
| 6270 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6271 | /* find a token */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6272 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6273 | i++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6274 | j = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6275 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
| 6276 | i++; |
| 6277 | if (j < i) { |
| 6278 | if (maxcount-- <= 0) |
| 6279 | break; |
| 6280 | SPLIT_APPEND(buf, j, i); |
| 6281 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
| 6282 | i++; |
| 6283 | j = i; |
| 6284 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | } |
| 6286 | if (j < len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6287 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6288 | } |
| 6289 | return list; |
| 6290 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6291 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6292 | Py_DECREF(list); |
| 6293 | return NULL; |
| 6294 | } |
| 6295 | |
| 6296 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6298 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6299 | register Py_ssize_t i; |
| 6300 | register Py_ssize_t j; |
| 6301 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6302 | PyObject *list; |
| 6303 | PyObject *str; |
| 6304 | Py_UNICODE *data; |
| 6305 | |
| 6306 | string = PyUnicode_FromObject(string); |
| 6307 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6308 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6309 | data = PyUnicode_AS_UNICODE(string); |
| 6310 | len = PyUnicode_GET_SIZE(string); |
| 6311 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6312 | list = PyList_New(0); |
| 6313 | if (!list) |
| 6314 | goto onError; |
| 6315 | |
| 6316 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6317 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6318 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6319 | /* Find a line and append it */ |
| 6320 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
| 6321 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6322 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6323 | /* Skip the line break reading CRLF as one line break */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6324 | eol = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6325 | if (i < len) { |
| 6326 | if (data[i] == '\r' && i + 1 < len && |
| 6327 | data[i+1] == '\n') |
| 6328 | i += 2; |
| 6329 | else |
| 6330 | i++; |
| 6331 | if (keepends) |
| 6332 | eol = i; |
| 6333 | } |
| 6334 | SPLIT_APPEND(data, j, eol); |
| 6335 | j = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6336 | } |
| 6337 | if (j < len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6338 | SPLIT_APPEND(data, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | Py_DECREF(string); |
| 6342 | return list; |
| 6343 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6344 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6345 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6346 | Py_DECREF(string); |
| 6347 | return NULL; |
| 6348 | } |
| 6349 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6350 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6351 | PyObject *split_char(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6352 | PyObject *list, |
| 6353 | Py_UNICODE ch, |
| 6354 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6355 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6356 | register Py_ssize_t i; |
| 6357 | register Py_ssize_t j; |
| 6358 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6359 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6360 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6361 | |
| 6362 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6363 | if (buf[i] == ch) { |
| 6364 | if (maxcount-- <= 0) |
| 6365 | break; |
| 6366 | SPLIT_APPEND(buf, j, i); |
| 6367 | i = j = i + 1; |
| 6368 | } else |
| 6369 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6370 | } |
| 6371 | if (j <= len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6372 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | } |
| 6374 | return list; |
| 6375 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6376 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | Py_DECREF(list); |
| 6378 | return NULL; |
| 6379 | } |
| 6380 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6381 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6382 | PyObject *split_substring(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6383 | PyObject *list, |
| 6384 | PyUnicodeObject *substring, |
| 6385 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6387 | register Py_ssize_t i; |
| 6388 | register Py_ssize_t j; |
| 6389 | Py_ssize_t len = self->length; |
| 6390 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | PyObject *str; |
| 6392 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 6393 | for (i = j = 0; i <= len - sublen; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6394 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6395 | if (maxcount-- <= 0) |
| 6396 | break; |
| 6397 | SPLIT_APPEND(self->str, j, i); |
| 6398 | i = j = i + sublen; |
| 6399 | } else |
| 6400 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6401 | } |
| 6402 | if (j <= len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6403 | SPLIT_APPEND(self->str, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | } |
| 6405 | return list; |
| 6406 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6407 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | Py_DECREF(list); |
| 6409 | return NULL; |
| 6410 | } |
| 6411 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6412 | static |
| 6413 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6414 | PyObject *list, |
| 6415 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6416 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6417 | register Py_ssize_t i; |
| 6418 | register Py_ssize_t j; |
| 6419 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6420 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6421 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6422 | |
| 6423 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6424 | /* find a token */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6425 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6426 | i--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6427 | j = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6428 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
| 6429 | i--; |
| 6430 | if (j > i) { |
| 6431 | if (maxcount-- <= 0) |
| 6432 | break; |
| 6433 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6434 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
| 6435 | i--; |
| 6436 | j = i; |
| 6437 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6438 | } |
| 6439 | if (j >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6440 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6441 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6442 | if (PyList_Reverse(list) < 0) |
| 6443 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6444 | return list; |
| 6445 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6446 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6447 | Py_DECREF(list); |
| 6448 | return NULL; |
| 6449 | } |
| 6450 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6451 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6452 | PyObject *rsplit_char(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6453 | PyObject *list, |
| 6454 | Py_UNICODE ch, |
| 6455 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6456 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6457 | register Py_ssize_t i; |
| 6458 | register Py_ssize_t j; |
| 6459 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6460 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6461 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6462 | |
| 6463 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6464 | if (buf[i] == ch) { |
| 6465 | if (maxcount-- <= 0) |
| 6466 | break; |
| 6467 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6468 | j = i = i - 1; |
| 6469 | } else |
| 6470 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6471 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 6472 | if (j >= -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6473 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6474 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6475 | if (PyList_Reverse(list) < 0) |
| 6476 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6477 | return list; |
| 6478 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6479 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6480 | Py_DECREF(list); |
| 6481 | return NULL; |
| 6482 | } |
| 6483 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6484 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6485 | PyObject *rsplit_substring(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6486 | PyObject *list, |
| 6487 | PyUnicodeObject *substring, |
| 6488 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6489 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6490 | register Py_ssize_t i; |
| 6491 | register Py_ssize_t j; |
| 6492 | Py_ssize_t len = self->length; |
| 6493 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6494 | PyObject *str; |
| 6495 | |
| 6496 | for (i = len - sublen, j = len; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6498 | if (maxcount-- <= 0) |
| 6499 | break; |
| 6500 | SPLIT_APPEND(self->str, i + sublen, j); |
| 6501 | j = i; |
| 6502 | i -= sublen; |
| 6503 | } else |
| 6504 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6505 | } |
| 6506 | if (j >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6507 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6508 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6509 | if (PyList_Reverse(list) < 0) |
| 6510 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6511 | return list; |
| 6512 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6513 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6514 | Py_DECREF(list); |
| 6515 | return NULL; |
| 6516 | } |
| 6517 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6518 | #undef SPLIT_APPEND |
| 6519 | |
| 6520 | static |
| 6521 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6522 | PyUnicodeObject *substring, |
| 6523 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6524 | { |
| 6525 | PyObject *list; |
| 6526 | |
| 6527 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6528 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6529 | |
| 6530 | list = PyList_New(0); |
| 6531 | if (!list) |
| 6532 | return NULL; |
| 6533 | |
| 6534 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6535 | return split_whitespace(self,list,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6536 | |
| 6537 | else if (substring->length == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6538 | return split_char(self,list,substring->str[0],maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 | |
| 6540 | else if (substring->length == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6541 | Py_DECREF(list); |
| 6542 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6543 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6544 | } |
| 6545 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6546 | return split_substring(self,list,substring,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6547 | } |
| 6548 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6549 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6550 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6551 | PyUnicodeObject *substring, |
| 6552 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6553 | { |
| 6554 | PyObject *list; |
| 6555 | |
| 6556 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6557 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6558 | |
| 6559 | list = PyList_New(0); |
| 6560 | if (!list) |
| 6561 | return NULL; |
| 6562 | |
| 6563 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6564 | return rsplit_whitespace(self,list,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6565 | |
| 6566 | else if (substring->length == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6567 | return rsplit_char(self,list,substring->str[0],maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6568 | |
| 6569 | else if (substring->length == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6570 | Py_DECREF(list); |
| 6571 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6572 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6573 | } |
| 6574 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6575 | return rsplit_substring(self,list,substring,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6576 | } |
| 6577 | |
| 6578 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6580 | PyUnicodeObject *str1, |
| 6581 | PyUnicodeObject *str2, |
| 6582 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | { |
| 6584 | PyUnicodeObject *u; |
| 6585 | |
| 6586 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6587 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6588 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6589 | if (str1->length == str2->length) { |
| 6590 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6591 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6592 | if (str1->length == 1) { |
| 6593 | /* replace characters */ |
| 6594 | Py_UNICODE u1, u2; |
| 6595 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6596 | goto nothing; |
| 6597 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6598 | if (!u) |
| 6599 | return NULL; |
| 6600 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6601 | u1 = str1->str[0]; |
| 6602 | u2 = str2->str[0]; |
| 6603 | for (i = 0; i < u->length; i++) |
| 6604 | if (u->str[i] == u1) { |
| 6605 | if (--maxcount < 0) |
| 6606 | break; |
| 6607 | u->str[i] = u2; |
| 6608 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6609 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6610 | i = fastsearch( |
| 6611 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6612 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6613 | if (i < 0) |
| 6614 | goto nothing; |
| 6615 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6616 | if (!u) |
| 6617 | return NULL; |
| 6618 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6619 | while (i <= self->length - str1->length) |
| 6620 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6621 | if (--maxcount < 0) |
| 6622 | break; |
| 6623 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6624 | i += str1->length; |
| 6625 | } else |
| 6626 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6628 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6629 | |
| 6630 | Py_ssize_t n, i, j, e; |
| 6631 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6632 | Py_UNICODE *p; |
| 6633 | |
| 6634 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6635 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | if (n > maxcount) |
| 6637 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6638 | if (n == 0) |
| 6639 | goto nothing; |
| 6640 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6641 | delta = (str2->length - str1->length); |
| 6642 | if (delta == 0) { |
| 6643 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6645 | product = n * (str2->length - str1->length); |
| 6646 | if ((product / (str2->length - str1->length)) != n) { |
| 6647 | PyErr_SetString(PyExc_OverflowError, |
| 6648 | "replace string is too long"); |
| 6649 | return NULL; |
| 6650 | } |
| 6651 | new_size = self->length + product; |
| 6652 | if (new_size < 0) { |
| 6653 | PyErr_SetString(PyExc_OverflowError, |
| 6654 | "replace string is too long"); |
| 6655 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | } |
| 6657 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6658 | u = _PyUnicode_New(new_size); |
| 6659 | if (!u) |
| 6660 | return NULL; |
| 6661 | i = 0; |
| 6662 | p = u->str; |
| 6663 | e = self->length - str1->length; |
| 6664 | if (str1->length > 0) { |
| 6665 | while (n-- > 0) { |
| 6666 | /* look for next match */ |
| 6667 | j = i; |
| 6668 | while (j <= e) { |
| 6669 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6670 | break; |
| 6671 | j++; |
| 6672 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6673 | if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6674 | if (j > e) |
| 6675 | break; |
| 6676 | /* copy unchanged part [i:j] */ |
| 6677 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6678 | p += j - i; |
| 6679 | } |
| 6680 | /* copy substitution string */ |
| 6681 | if (str2->length > 0) { |
| 6682 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6683 | p += str2->length; |
| 6684 | } |
| 6685 | i = j + str1->length; |
| 6686 | } |
| 6687 | if (i < self->length) |
| 6688 | /* copy tail [i:] */ |
| 6689 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6690 | } else { |
| 6691 | /* interleave */ |
| 6692 | while (n > 0) { |
| 6693 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6694 | p += str2->length; |
| 6695 | if (--n <= 0) |
| 6696 | break; |
| 6697 | *p++ = self->str[i++]; |
| 6698 | } |
| 6699 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6700 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6701 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6702 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6703 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6704 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6705 | /* nothing to replace; return original string (when possible) */ |
| 6706 | if (PyUnicode_CheckExact(self)) { |
| 6707 | Py_INCREF(self); |
| 6708 | return (PyObject *) self; |
| 6709 | } |
| 6710 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6714 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6715 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6716 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | \n\ |
| 6718 | 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] | 6719 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6720 | |
| 6721 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6722 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6724 | return fixup(self, fixtitle); |
| 6725 | } |
| 6726 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6727 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6728 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | \n\ |
| 6730 | 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] | 6731 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6732 | |
| 6733 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6734 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6736 | return fixup(self, fixcapitalize); |
| 6737 | } |
| 6738 | |
| 6739 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6740 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6741 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6742 | \n\ |
| 6743 | 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] | 6744 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6745 | |
| 6746 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6747 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6748 | { |
| 6749 | PyObject *list; |
| 6750 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6751 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6752 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6753 | /* Split into words */ |
| 6754 | list = split(self, NULL, -1); |
| 6755 | if (!list) |
| 6756 | return NULL; |
| 6757 | |
| 6758 | /* Capitalize each word */ |
| 6759 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6760 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6761 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6762 | if (item == NULL) |
| 6763 | goto onError; |
| 6764 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6765 | PyList_SET_ITEM(list, i, item); |
| 6766 | } |
| 6767 | |
| 6768 | /* Join the words to form a new string */ |
| 6769 | item = PyUnicode_Join(NULL, list); |
| 6770 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6771 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6772 | Py_DECREF(list); |
| 6773 | return (PyObject *)item; |
| 6774 | } |
| 6775 | #endif |
| 6776 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6777 | /* Argument converter. Coerces to a single unicode character */ |
| 6778 | |
| 6779 | static int |
| 6780 | convert_uc(PyObject *obj, void *addr) |
| 6781 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6782 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6783 | PyObject *uniobj; |
| 6784 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6785 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6786 | uniobj = PyUnicode_FromObject(obj); |
| 6787 | if (uniobj == NULL) { |
| 6788 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6789 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6790 | return 0; |
| 6791 | } |
| 6792 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6793 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6794 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6795 | Py_DECREF(uniobj); |
| 6796 | return 0; |
| 6797 | } |
| 6798 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6799 | *fillcharloc = unistr[0]; |
| 6800 | Py_DECREF(uniobj); |
| 6801 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6802 | } |
| 6803 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6804 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6805 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6806 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6807 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6808 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6809 | |
| 6810 | static PyObject * |
| 6811 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6812 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6813 | Py_ssize_t marg, left; |
| 6814 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6815 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6817 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6818 | return NULL; |
| 6819 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6820 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6821 | Py_INCREF(self); |
| 6822 | return (PyObject*) self; |
| 6823 | } |
| 6824 | |
| 6825 | marg = width - self->length; |
| 6826 | left = marg / 2 + (marg & width & 1); |
| 6827 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6828 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6829 | } |
| 6830 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6831 | #if 0 |
| 6832 | |
| 6833 | /* This code should go into some future Unicode collation support |
| 6834 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6835 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6836 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6837 | /* speedy UTF-16 code point order comparison */ |
| 6838 | /* gleaned from: */ |
| 6839 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6840 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6841 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6842 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6843 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6844 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6845 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6846 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6847 | }; |
| 6848 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | static int |
| 6850 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6851 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6852 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | Py_UNICODE *s1 = str1->str; |
| 6855 | Py_UNICODE *s2 = str2->str; |
| 6856 | |
| 6857 | len1 = str1->length; |
| 6858 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6860 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6861 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6862 | |
| 6863 | c1 = *s1++; |
| 6864 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6865 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6866 | if (c1 > (1<<11) * 26) |
| 6867 | c1 += utf16Fixup[c1>>11]; |
| 6868 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6869 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6870 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6871 | |
| 6872 | if (c1 != c2) |
| 6873 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6874 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6875 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6876 | } |
| 6877 | |
| 6878 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6879 | } |
| 6880 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6881 | #else |
| 6882 | |
| 6883 | static int |
| 6884 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6885 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6886 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6887 | |
| 6888 | Py_UNICODE *s1 = str1->str; |
| 6889 | Py_UNICODE *s2 = str2->str; |
| 6890 | |
| 6891 | len1 = str1->length; |
| 6892 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6893 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6894 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6895 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6896 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6897 | c1 = *s1++; |
| 6898 | c2 = *s2++; |
| 6899 | |
| 6900 | if (c1 != c2) |
| 6901 | return (c1 < c2) ? -1 : 1; |
| 6902 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6903 | len1--; len2--; |
| 6904 | } |
| 6905 | |
| 6906 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6907 | } |
| 6908 | |
| 6909 | #endif |
| 6910 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6911 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6912 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6913 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6914 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6915 | return unicode_compare((PyUnicodeObject *)left, |
| 6916 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6917 | PyErr_Format(PyExc_TypeError, |
| 6918 | "Can't compare %.100s and %.100s", |
| 6919 | left->ob_type->tp_name, |
| 6920 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6921 | return -1; |
| 6922 | } |
| 6923 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6924 | int |
| 6925 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6926 | { |
| 6927 | int i; |
| 6928 | Py_UNICODE *id; |
| 6929 | assert(PyUnicode_Check(uni)); |
| 6930 | id = PyUnicode_AS_UNICODE(uni); |
| 6931 | /* Compare Unicode string and source character set string */ |
| 6932 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6933 | if (id[i] != str[i]) |
| 6934 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6935 | if (id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6936 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6937 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6938 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6939 | return 0; |
| 6940 | } |
| 6941 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6942 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6943 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6944 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6945 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6946 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6947 | PyObject *right, |
| 6948 | int op) |
| 6949 | { |
| 6950 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6951 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6952 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6953 | PyObject *v; |
| 6954 | if (((PyUnicodeObject *) left)->length != |
| 6955 | ((PyUnicodeObject *) right)->length) { |
| 6956 | if (op == Py_EQ) { |
| 6957 | Py_INCREF(Py_False); |
| 6958 | return Py_False; |
| 6959 | } |
| 6960 | if (op == Py_NE) { |
| 6961 | Py_INCREF(Py_True); |
| 6962 | return Py_True; |
| 6963 | } |
| 6964 | } |
| 6965 | if (left == right) |
| 6966 | result = 0; |
| 6967 | else |
| 6968 | result = unicode_compare((PyUnicodeObject *)left, |
| 6969 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6970 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6971 | /* Convert the return value to a Boolean */ |
| 6972 | switch (op) { |
| 6973 | case Py_EQ: |
| 6974 | v = TEST_COND(result == 0); |
| 6975 | break; |
| 6976 | case Py_NE: |
| 6977 | v = TEST_COND(result != 0); |
| 6978 | break; |
| 6979 | case Py_LE: |
| 6980 | v = TEST_COND(result <= 0); |
| 6981 | break; |
| 6982 | case Py_GE: |
| 6983 | v = TEST_COND(result >= 0); |
| 6984 | break; |
| 6985 | case Py_LT: |
| 6986 | v = TEST_COND(result == -1); |
| 6987 | break; |
| 6988 | case Py_GT: |
| 6989 | v = TEST_COND(result == 1); |
| 6990 | break; |
| 6991 | default: |
| 6992 | PyErr_BadArgument(); |
| 6993 | return NULL; |
| 6994 | } |
| 6995 | Py_INCREF(v); |
| 6996 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6997 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6998 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6999 | Py_INCREF(Py_NotImplemented); |
| 7000 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7001 | } |
| 7002 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7003 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7004 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7005 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7006 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7007 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7008 | |
| 7009 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7010 | sub = PyUnicode_FromObject(element); |
| 7011 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7012 | PyErr_Format(PyExc_TypeError, |
| 7013 | "'in <string>' requires string as left operand, not %s", |
| 7014 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7015 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7016 | } |
| 7017 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7018 | str = PyUnicode_FromObject(container); |
| 7019 | if (!str) { |
| 7020 | Py_DECREF(sub); |
| 7021 | return -1; |
| 7022 | } |
| 7023 | |
| 7024 | result = stringlib_contains_obj(str, sub); |
| 7025 | |
| 7026 | Py_DECREF(str); |
| 7027 | Py_DECREF(sub); |
| 7028 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7029 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7030 | } |
| 7031 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7032 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7033 | |
| 7034 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7035 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7036 | { |
| 7037 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7038 | |
| 7039 | /* Coerce the two arguments */ |
| 7040 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7041 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7042 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7043 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7044 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7045 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7046 | |
| 7047 | /* Shortcuts */ |
| 7048 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7049 | Py_DECREF(v); |
| 7050 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | } |
| 7052 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7053 | Py_DECREF(u); |
| 7054 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7055 | } |
| 7056 | |
| 7057 | /* Concat the two Unicode strings */ |
| 7058 | w = _PyUnicode_New(u->length + v->length); |
| 7059 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7060 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7061 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7062 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7063 | |
| 7064 | Py_DECREF(u); |
| 7065 | Py_DECREF(v); |
| 7066 | return (PyObject *)w; |
| 7067 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7068 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7069 | Py_XDECREF(u); |
| 7070 | Py_XDECREF(v); |
| 7071 | return NULL; |
| 7072 | } |
| 7073 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7074 | void |
| 7075 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7076 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7077 | PyObject *new; |
| 7078 | if (*pleft == NULL) |
| 7079 | return; |
| 7080 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7081 | Py_DECREF(*pleft); |
| 7082 | *pleft = NULL; |
| 7083 | return; |
| 7084 | } |
| 7085 | new = PyUnicode_Concat(*pleft, right); |
| 7086 | Py_DECREF(*pleft); |
| 7087 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7088 | } |
| 7089 | |
| 7090 | void |
| 7091 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7092 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7093 | PyUnicode_Append(pleft, right); |
| 7094 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7097 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7098 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7099 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7100 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7101 | 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] | 7102 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7103 | |
| 7104 | static PyObject * |
| 7105 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7106 | { |
| 7107 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7108 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7109 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7110 | PyObject *result; |
| 7111 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7112 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7113 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7114 | return NULL; |
| 7115 | |
| 7116 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7117 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7118 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7119 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7120 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7121 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7123 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7124 | stringlib_count(self->str + start, end - start, |
| 7125 | substring->str, substring->length) |
| 7126 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7127 | |
| 7128 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7129 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | return result; |
| 7131 | } |
| 7132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7133 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7134 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7135 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7136 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7137 | 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] | 7138 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7139 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7140 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7141 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7142 | |
| 7143 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7144 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7145 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7146 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7147 | char *encoding = NULL; |
| 7148 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7149 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7150 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7151 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7152 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7154 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7155 | if (v == NULL) |
| 7156 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7157 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7158 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7159 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7160 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7161 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7162 | Py_DECREF(v); |
| 7163 | return NULL; |
| 7164 | } |
| 7165 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7166 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7167 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7168 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7171 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7172 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7173 | \n\ |
| 7174 | 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] | 7175 | 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] | 7176 | |
| 7177 | static PyObject* |
| 7178 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7179 | { |
| 7180 | Py_UNICODE *e; |
| 7181 | Py_UNICODE *p; |
| 7182 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7183 | Py_UNICODE *qe; |
| 7184 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7185 | PyUnicodeObject *u; |
| 7186 | int tabsize = 8; |
| 7187 | |
| 7188 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7189 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7190 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7191 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7192 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7193 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7194 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7195 | for (p = self->str; p < e; p++) |
| 7196 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7197 | if (tabsize > 0) { |
| 7198 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7199 | if (j > PY_SSIZE_T_MAX - incr) |
| 7200 | goto overflow1; |
| 7201 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7202 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7203 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7204 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7205 | if (j > PY_SSIZE_T_MAX - 1) |
| 7206 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7207 | j++; |
| 7208 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7209 | if (i > PY_SSIZE_T_MAX - j) |
| 7210 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7211 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7212 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7213 | } |
| 7214 | } |
| 7215 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7216 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7217 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7219 | /* Second pass: create output string and fill it */ |
| 7220 | u = _PyUnicode_New(i + j); |
| 7221 | if (!u) |
| 7222 | return NULL; |
| 7223 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7224 | j = 0; /* same as in first pass */ |
| 7225 | q = u->str; /* next output char */ |
| 7226 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7227 | |
| 7228 | for (p = self->str; p < e; p++) |
| 7229 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7230 | if (tabsize > 0) { |
| 7231 | i = tabsize - (j % tabsize); |
| 7232 | j += i; |
| 7233 | while (i--) { |
| 7234 | if (q >= qe) |
| 7235 | goto overflow2; |
| 7236 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7237 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7238 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7239 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7240 | else { |
| 7241 | if (q >= qe) |
| 7242 | goto overflow2; |
| 7243 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7244 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7245 | if (*p == '\n' || *p == '\r') |
| 7246 | j = 0; |
| 7247 | } |
| 7248 | |
| 7249 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7250 | |
| 7251 | overflow2: |
| 7252 | Py_DECREF(u); |
| 7253 | overflow1: |
| 7254 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7255 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7256 | } |
| 7257 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7258 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7259 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7260 | \n\ |
| 7261 | 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] | 7262 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7263 | arguments start and end are interpreted as in slice notation.\n\ |
| 7264 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7265 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7266 | |
| 7267 | static PyObject * |
| 7268 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7269 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7270 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7271 | Py_ssize_t start; |
| 7272 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7273 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7274 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7275 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7276 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7277 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7278 | result = stringlib_find_slice( |
| 7279 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7280 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7281 | start, end |
| 7282 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7283 | |
| 7284 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7285 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7286 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7287 | } |
| 7288 | |
| 7289 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7290 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7291 | { |
| 7292 | if (index < 0 || index >= self->length) { |
| 7293 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7294 | return NULL; |
| 7295 | } |
| 7296 | |
| 7297 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7298 | } |
| 7299 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7300 | /* Believe it or not, this produces the same value for ASCII strings |
| 7301 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7302 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7303 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7304 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7305 | Py_ssize_t len; |
| 7306 | Py_UNICODE *p; |
| 7307 | long x; |
| 7308 | |
| 7309 | if (self->hash != -1) |
| 7310 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7311 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7312 | p = self->str; |
| 7313 | x = *p << 7; |
| 7314 | while (--len >= 0) |
| 7315 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7316 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7317 | if (x == -1) |
| 7318 | x = -2; |
| 7319 | self->hash = x; |
| 7320 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7321 | } |
| 7322 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7323 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7324 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7325 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7326 | 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] | 7327 | |
| 7328 | static PyObject * |
| 7329 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7330 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7331 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7332 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7333 | Py_ssize_t start; |
| 7334 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7335 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7336 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7337 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7338 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7339 | result = stringlib_find_slice( |
| 7340 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7341 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7342 | start, end |
| 7343 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7344 | |
| 7345 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7347 | if (result < 0) { |
| 7348 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7349 | return NULL; |
| 7350 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7351 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7352 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7353 | } |
| 7354 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7355 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7356 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7358 | 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] | 7359 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7360 | |
| 7361 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7362 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7363 | { |
| 7364 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7365 | register const Py_UNICODE *e; |
| 7366 | int cased; |
| 7367 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7368 | /* Shortcut for single character strings */ |
| 7369 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7370 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7371 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7372 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7373 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7374 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7375 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7376 | e = p + PyUnicode_GET_SIZE(self); |
| 7377 | cased = 0; |
| 7378 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7379 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7380 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7381 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7382 | return PyBool_FromLong(0); |
| 7383 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7384 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7385 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7386 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7387 | } |
| 7388 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7389 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7390 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7391 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7392 | 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] | 7393 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7394 | |
| 7395 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7396 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7397 | { |
| 7398 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7399 | register const Py_UNICODE *e; |
| 7400 | int cased; |
| 7401 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7402 | /* Shortcut for single character strings */ |
| 7403 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7404 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7405 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7406 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7407 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7408 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7410 | e = p + PyUnicode_GET_SIZE(self); |
| 7411 | cased = 0; |
| 7412 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7413 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7414 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7415 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7416 | return PyBool_FromLong(0); |
| 7417 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7418 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7419 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7420 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7421 | } |
| 7422 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7423 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7424 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7426 | Return True if S is a titlecased string and there is at least one\n\ |
| 7427 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7428 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7429 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7430 | |
| 7431 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7432 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7433 | { |
| 7434 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7435 | register const Py_UNICODE *e; |
| 7436 | int cased, previous_is_cased; |
| 7437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7438 | /* Shortcut for single character strings */ |
| 7439 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7440 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7441 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7443 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7444 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7445 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7447 | e = p + PyUnicode_GET_SIZE(self); |
| 7448 | cased = 0; |
| 7449 | previous_is_cased = 0; |
| 7450 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7451 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7452 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7453 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7454 | if (previous_is_cased) |
| 7455 | return PyBool_FromLong(0); |
| 7456 | previous_is_cased = 1; |
| 7457 | cased = 1; |
| 7458 | } |
| 7459 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7460 | if (!previous_is_cased) |
| 7461 | return PyBool_FromLong(0); |
| 7462 | previous_is_cased = 1; |
| 7463 | cased = 1; |
| 7464 | } |
| 7465 | else |
| 7466 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7468 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7469 | } |
| 7470 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7471 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7472 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7473 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7474 | Return True if all characters in S are whitespace\n\ |
| 7475 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | |
| 7477 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7478 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | { |
| 7480 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7481 | register const Py_UNICODE *e; |
| 7482 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | /* Shortcut for single character strings */ |
| 7484 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7485 | Py_UNICODE_ISSPACE(*p)) |
| 7486 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7487 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7488 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7489 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7490 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7492 | e = p + PyUnicode_GET_SIZE(self); |
| 7493 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7494 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7495 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7497 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7498 | } |
| 7499 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7500 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7501 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7502 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7503 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7504 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7505 | |
| 7506 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7507 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7508 | { |
| 7509 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7510 | register const Py_UNICODE *e; |
| 7511 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +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 | Py_UNICODE_ISALPHA(*p)) |
| 7515 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7516 | |
| 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 | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7520 | |
| 7521 | e = p + PyUnicode_GET_SIZE(self); |
| 7522 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7523 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7524 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7525 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7526 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7527 | } |
| 7528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7529 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7531 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7532 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7533 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7534 | |
| 7535 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7536 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7537 | { |
| 7538 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7539 | register const Py_UNICODE *e; |
| 7540 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7541 | /* Shortcut for single character strings */ |
| 7542 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7543 | Py_UNICODE_ISALNUM(*p)) |
| 7544 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7545 | |
| 7546 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7547 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7548 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7549 | |
| 7550 | e = p + PyUnicode_GET_SIZE(self); |
| 7551 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7552 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7553 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7554 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7555 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7556 | } |
| 7557 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7558 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7559 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7560 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7561 | 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] | 7562 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7563 | |
| 7564 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7565 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7566 | { |
| 7567 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7568 | register const Py_UNICODE *e; |
| 7569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7570 | /* Shortcut for single character strings */ |
| 7571 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7572 | Py_UNICODE_ISDECIMAL(*p)) |
| 7573 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7574 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7575 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7576 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7577 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7579 | e = p + PyUnicode_GET_SIZE(self); |
| 7580 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7581 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7582 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7583 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7584 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7585 | } |
| 7586 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7587 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7588 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7589 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7590 | Return True if all characters in S are digits\n\ |
| 7591 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7592 | |
| 7593 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7594 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7595 | { |
| 7596 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7597 | register const Py_UNICODE *e; |
| 7598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | /* Shortcut for single character strings */ |
| 7600 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7601 | Py_UNICODE_ISDIGIT(*p)) |
| 7602 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7603 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7604 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7605 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7606 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7608 | e = p + PyUnicode_GET_SIZE(self); |
| 7609 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7610 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7611 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7612 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7613 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7614 | } |
| 7615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7616 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7617 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7618 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7619 | 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] | 7620 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7621 | |
| 7622 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7623 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7624 | { |
| 7625 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7626 | register const Py_UNICODE *e; |
| 7627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7628 | /* Shortcut for single character strings */ |
| 7629 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7630 | Py_UNICODE_ISNUMERIC(*p)) |
| 7631 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7632 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7633 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7634 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7635 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7636 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7637 | e = p + PyUnicode_GET_SIZE(self); |
| 7638 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7639 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7640 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7641 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7642 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7643 | } |
| 7644 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7645 | int |
| 7646 | PyUnicode_IsIdentifier(PyObject *self) |
| 7647 | { |
| 7648 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7649 | register const Py_UNICODE *e; |
| 7650 | |
| 7651 | /* Special case for empty strings */ |
| 7652 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7653 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7654 | |
| 7655 | /* PEP 3131 says that the first character must be in |
| 7656 | XID_Start and subsequent characters in XID_Continue, |
| 7657 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7658 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7659 | letters, digits, underscore). However, given the current |
| 7660 | definition of XID_Start and XID_Continue, it is sufficient |
| 7661 | to check just for these, except that _ must be allowed |
| 7662 | as starting an identifier. */ |
| 7663 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7664 | return 0; |
| 7665 | |
| 7666 | e = p + PyUnicode_GET_SIZE(self); |
| 7667 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7668 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7669 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7670 | } |
| 7671 | return 1; |
| 7672 | } |
| 7673 | |
| 7674 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7675 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7676 | \n\ |
| 7677 | Return True if S is a valid identifier according\n\ |
| 7678 | to the language definition."); |
| 7679 | |
| 7680 | static PyObject* |
| 7681 | unicode_isidentifier(PyObject *self) |
| 7682 | { |
| 7683 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7684 | } |
| 7685 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7686 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7687 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7688 | \n\ |
| 7689 | Return True if all characters in S are considered\n\ |
| 7690 | printable in repr() or S is empty, False otherwise."); |
| 7691 | |
| 7692 | static PyObject* |
| 7693 | unicode_isprintable(PyObject *self) |
| 7694 | { |
| 7695 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7696 | register const Py_UNICODE *e; |
| 7697 | |
| 7698 | /* Shortcut for single character strings */ |
| 7699 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7700 | Py_RETURN_TRUE; |
| 7701 | } |
| 7702 | |
| 7703 | e = p + PyUnicode_GET_SIZE(self); |
| 7704 | for (; p < e; p++) { |
| 7705 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7706 | Py_RETURN_FALSE; |
| 7707 | } |
| 7708 | } |
| 7709 | Py_RETURN_TRUE; |
| 7710 | } |
| 7711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7712 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7713 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7714 | \n\ |
| 7715 | 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] | 7716 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | |
| 7718 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7719 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7720 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7721 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 | } |
| 7723 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7724 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7725 | unicode_length(PyUnicodeObject *self) |
| 7726 | { |
| 7727 | return self->length; |
| 7728 | } |
| 7729 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7730 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7731 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7732 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7733 | 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] | 7734 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7735 | |
| 7736 | static PyObject * |
| 7737 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7738 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7739 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7740 | Py_UNICODE fillchar = ' '; |
| 7741 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7742 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7743 | return NULL; |
| 7744 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7745 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7746 | Py_INCREF(self); |
| 7747 | return (PyObject*) self; |
| 7748 | } |
| 7749 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7750 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7751 | } |
| 7752 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7753 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7754 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7755 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7756 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7757 | |
| 7758 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7759 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7760 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | return fixup(self, fixlower); |
| 7762 | } |
| 7763 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7764 | #define LEFTSTRIP 0 |
| 7765 | #define RIGHTSTRIP 1 |
| 7766 | #define BOTHSTRIP 2 |
| 7767 | |
| 7768 | /* Arrays indexed by above */ |
| 7769 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7770 | |
| 7771 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7772 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7773 | /* externally visible for str.strip(unicode) */ |
| 7774 | PyObject * |
| 7775 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7776 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7777 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7778 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7779 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7780 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7781 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7782 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7783 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7784 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7785 | i = 0; |
| 7786 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7787 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7788 | i++; |
| 7789 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7790 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7791 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7792 | j = len; |
| 7793 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7794 | do { |
| 7795 | j--; |
| 7796 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7797 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7798 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7799 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7800 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7801 | Py_INCREF(self); |
| 7802 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7803 | } |
| 7804 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7805 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7806 | } |
| 7807 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | |
| 7809 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7810 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7811 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7812 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7813 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7814 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7815 | i = 0; |
| 7816 | if (striptype != RIGHTSTRIP) { |
| 7817 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7818 | i++; |
| 7819 | } |
| 7820 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7821 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7822 | j = len; |
| 7823 | if (striptype != LEFTSTRIP) { |
| 7824 | do { |
| 7825 | j--; |
| 7826 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7827 | j++; |
| 7828 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7829 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7830 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7831 | Py_INCREF(self); |
| 7832 | return (PyObject*)self; |
| 7833 | } |
| 7834 | else |
| 7835 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7836 | } |
| 7837 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7838 | |
| 7839 | static PyObject * |
| 7840 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7841 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7842 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7843 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7844 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7845 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7846 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7847 | if (sep != NULL && sep != Py_None) { |
| 7848 | if (PyUnicode_Check(sep)) |
| 7849 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7850 | else { |
| 7851 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7852 | "%s arg must be None or str", |
| 7853 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7854 | return NULL; |
| 7855 | } |
| 7856 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7857 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7858 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7859 | } |
| 7860 | |
| 7861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7862 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7863 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7864 | \n\ |
| 7865 | Return a copy of the string S with leading and trailing\n\ |
| 7866 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7867 | 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] | 7868 | |
| 7869 | static PyObject * |
| 7870 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7871 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7872 | if (PyTuple_GET_SIZE(args) == 0) |
| 7873 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7874 | else |
| 7875 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7876 | } |
| 7877 | |
| 7878 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7879 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7880 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7881 | \n\ |
| 7882 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7883 | 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] | 7884 | |
| 7885 | static PyObject * |
| 7886 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7887 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7888 | if (PyTuple_GET_SIZE(args) == 0) |
| 7889 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7890 | else |
| 7891 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7892 | } |
| 7893 | |
| 7894 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7895 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7896 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7897 | \n\ |
| 7898 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7899 | 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] | 7900 | |
| 7901 | static PyObject * |
| 7902 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7903 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7904 | if (PyTuple_GET_SIZE(args) == 0) |
| 7905 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7906 | else |
| 7907 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7908 | } |
| 7909 | |
| 7910 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7911 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7912 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7913 | { |
| 7914 | PyUnicodeObject *u; |
| 7915 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7916 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7917 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7918 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7919 | if (len < 1) { |
| 7920 | Py_INCREF(unicode_empty); |
| 7921 | return (PyObject *)unicode_empty; |
| 7922 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7923 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7924 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7925 | /* no repeat, return original string */ |
| 7926 | Py_INCREF(str); |
| 7927 | return (PyObject*) str; |
| 7928 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7929 | |
| 7930 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7931 | * needed doesn't overflow size_t |
| 7932 | */ |
| 7933 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7934 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7935 | PyErr_SetString(PyExc_OverflowError, |
| 7936 | "repeated string is too long"); |
| 7937 | return NULL; |
| 7938 | } |
| 7939 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7940 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7941 | PyErr_SetString(PyExc_OverflowError, |
| 7942 | "repeated string is too long"); |
| 7943 | return NULL; |
| 7944 | } |
| 7945 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7946 | if (!u) |
| 7947 | return NULL; |
| 7948 | |
| 7949 | p = u->str; |
| 7950 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7951 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7952 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7953 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7954 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7955 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7956 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7957 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7958 | Py_UNICODE_COPY(p+done, p, n); |
| 7959 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7960 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7961 | } |
| 7962 | |
| 7963 | return (PyObject*) u; |
| 7964 | } |
| 7965 | |
| 7966 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7967 | PyObject *subobj, |
| 7968 | PyObject *replobj, |
| 7969 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7970 | { |
| 7971 | PyObject *self; |
| 7972 | PyObject *str1; |
| 7973 | PyObject *str2; |
| 7974 | PyObject *result; |
| 7975 | |
| 7976 | self = PyUnicode_FromObject(obj); |
| 7977 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7978 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7979 | str1 = PyUnicode_FromObject(subobj); |
| 7980 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7981 | Py_DECREF(self); |
| 7982 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7983 | } |
| 7984 | str2 = PyUnicode_FromObject(replobj); |
| 7985 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7986 | Py_DECREF(self); |
| 7987 | Py_DECREF(str1); |
| 7988 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7989 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7990 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7991 | (PyUnicodeObject *)str1, |
| 7992 | (PyUnicodeObject *)str2, |
| 7993 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7994 | Py_DECREF(self); |
| 7995 | Py_DECREF(str1); |
| 7996 | Py_DECREF(str2); |
| 7997 | return result; |
| 7998 | } |
| 7999 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8000 | PyDoc_STRVAR(replace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8001 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8002 | \n\ |
| 8003 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8004 | old replaced by new. If the optional argument count is\n\ |
| 8005 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8006 | |
| 8007 | static PyObject* |
| 8008 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8009 | { |
| 8010 | PyUnicodeObject *str1; |
| 8011 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8012 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8013 | PyObject *result; |
| 8014 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8015 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8016 | return NULL; |
| 8017 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8018 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8019 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8020 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8021 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8022 | Py_DECREF(str1); |
| 8023 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8024 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8025 | |
| 8026 | result = replace(self, str1, str2, maxcount); |
| 8027 | |
| 8028 | Py_DECREF(str1); |
| 8029 | Py_DECREF(str2); |
| 8030 | return result; |
| 8031 | } |
| 8032 | |
| 8033 | static |
| 8034 | PyObject *unicode_repr(PyObject *unicode) |
| 8035 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8036 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8037 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8038 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8039 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8040 | |
| 8041 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8042 | better to choose a different scheme. Perhaps scan the |
| 8043 | first N-chars of the string and allocate based on that size. |
| 8044 | */ |
| 8045 | /* Initial allocation is based on the longest-possible unichr |
| 8046 | escape. |
| 8047 | |
| 8048 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8049 | unichr, so in this case it's the longest unichr escape. In |
| 8050 | narrow (UTF-16) builds this is five chars per source unichr |
| 8051 | since there are two unichrs in the surrogate pair, so in narrow |
| 8052 | (UTF-16) builds it's not the longest unichr escape. |
| 8053 | |
| 8054 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8055 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8056 | escape. |
| 8057 | */ |
| 8058 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8059 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8060 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8061 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8062 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8063 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8064 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8065 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8066 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8067 | if (repr == NULL) |
| 8068 | return NULL; |
| 8069 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8070 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8071 | |
| 8072 | /* Add quote */ |
| 8073 | *p++ = (findchar(s, size, '\'') && |
| 8074 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8075 | while (size-- > 0) { |
| 8076 | Py_UNICODE ch = *s++; |
| 8077 | |
| 8078 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8079 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8080 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8081 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8082 | continue; |
| 8083 | } |
| 8084 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8085 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8086 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8087 | *p++ = '\\'; |
| 8088 | *p++ = 't'; |
| 8089 | } |
| 8090 | else if (ch == '\n') { |
| 8091 | *p++ = '\\'; |
| 8092 | *p++ = 'n'; |
| 8093 | } |
| 8094 | else if (ch == '\r') { |
| 8095 | *p++ = '\\'; |
| 8096 | *p++ = 'r'; |
| 8097 | } |
| 8098 | |
| 8099 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8100 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8101 | *p++ = '\\'; |
| 8102 | *p++ = 'x'; |
| 8103 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8104 | *p++ = hexdigits[ch & 0x000F]; |
| 8105 | } |
| 8106 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8107 | /* Copy ASCII characters as-is */ |
| 8108 | else if (ch < 0x7F) { |
| 8109 | *p++ = ch; |
| 8110 | } |
| 8111 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8112 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8113 | else { |
| 8114 | Py_UCS4 ucs = ch; |
| 8115 | |
| 8116 | #ifndef Py_UNICODE_WIDE |
| 8117 | Py_UNICODE ch2 = 0; |
| 8118 | /* Get code point from surrogate pair */ |
| 8119 | if (size > 0) { |
| 8120 | ch2 = *s; |
| 8121 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8123 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8124 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8125 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8126 | size--; |
| 8127 | } |
| 8128 | } |
| 8129 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8130 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8131 | (categories Z* and C* except ASCII space) |
| 8132 | */ |
| 8133 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8134 | /* Map 8-bit characters to '\xhh' */ |
| 8135 | if (ucs <= 0xff) { |
| 8136 | *p++ = '\\'; |
| 8137 | *p++ = 'x'; |
| 8138 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8139 | *p++ = hexdigits[ch & 0x000F]; |
| 8140 | } |
| 8141 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8142 | else if (ucs >= 0x10000) { |
| 8143 | *p++ = '\\'; |
| 8144 | *p++ = 'U'; |
| 8145 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8146 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8147 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8148 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8149 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8150 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8151 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8152 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8153 | } |
| 8154 | /* Map 16-bit characters to '\uxxxx' */ |
| 8155 | else { |
| 8156 | *p++ = '\\'; |
| 8157 | *p++ = 'u'; |
| 8158 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8159 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8160 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8161 | *p++ = hexdigits[ucs & 0x000F]; |
| 8162 | } |
| 8163 | } |
| 8164 | /* Copy characters as-is */ |
| 8165 | else { |
| 8166 | *p++ = ch; |
| 8167 | #ifndef Py_UNICODE_WIDE |
| 8168 | if (ucs >= 0x10000) |
| 8169 | *p++ = ch2; |
| 8170 | #endif |
| 8171 | } |
| 8172 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8173 | } |
| 8174 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8175 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8176 | |
| 8177 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8178 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8179 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8180 | } |
| 8181 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8182 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8183 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8184 | \n\ |
| 8185 | 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] | 8186 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | arguments start and end are interpreted as in slice notation.\n\ |
| 8188 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8189 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8190 | |
| 8191 | static PyObject * |
| 8192 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8193 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8194 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8195 | Py_ssize_t start; |
| 8196 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8197 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8198 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8199 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8200 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8201 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8202 | result = stringlib_rfind_slice( |
| 8203 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8204 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8205 | start, end |
| 8206 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8207 | |
| 8208 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8209 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8210 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8211 | } |
| 8212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8213 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8214 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8215 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8216 | 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] | 8217 | |
| 8218 | static PyObject * |
| 8219 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8220 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8221 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8222 | Py_ssize_t start; |
| 8223 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8224 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8225 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8226 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8227 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8228 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8229 | result = stringlib_rfind_slice( |
| 8230 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8231 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8232 | start, end |
| 8233 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8234 | |
| 8235 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8236 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8237 | if (result < 0) { |
| 8238 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8239 | return NULL; |
| 8240 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8241 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8242 | } |
| 8243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8244 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8245 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8246 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8247 | 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] | 8248 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8249 | |
| 8250 | static PyObject * |
| 8251 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8252 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8253 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8254 | Py_UNICODE fillchar = ' '; |
| 8255 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8256 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8257 | return NULL; |
| 8258 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8259 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8260 | Py_INCREF(self); |
| 8261 | return (PyObject*) self; |
| 8262 | } |
| 8263 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8264 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8265 | } |
| 8266 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8267 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8268 | PyObject *sep, |
| 8269 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8270 | { |
| 8271 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8272 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8273 | s = PyUnicode_FromObject(s); |
| 8274 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8275 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | if (sep != NULL) { |
| 8277 | sep = PyUnicode_FromObject(sep); |
| 8278 | if (sep == NULL) { |
| 8279 | Py_DECREF(s); |
| 8280 | return NULL; |
| 8281 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8282 | } |
| 8283 | |
| 8284 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8285 | |
| 8286 | Py_DECREF(s); |
| 8287 | Py_XDECREF(sep); |
| 8288 | return result; |
| 8289 | } |
| 8290 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8291 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8292 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8293 | \n\ |
| 8294 | Return a list of the words in S, using sep as the\n\ |
| 8295 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8296 | 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] | 8297 | whitespace string is a separator and empty strings are\n\ |
| 8298 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8299 | |
| 8300 | static PyObject* |
| 8301 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8302 | { |
| 8303 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8304 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8305 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8306 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8307 | return NULL; |
| 8308 | |
| 8309 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8310 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8311 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8312 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8313 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8314 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8315 | } |
| 8316 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8317 | PyObject * |
| 8318 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8319 | { |
| 8320 | PyObject* str_obj; |
| 8321 | PyObject* sep_obj; |
| 8322 | PyObject* out; |
| 8323 | |
| 8324 | str_obj = PyUnicode_FromObject(str_in); |
| 8325 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8326 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8327 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8328 | if (!sep_obj) { |
| 8329 | Py_DECREF(str_obj); |
| 8330 | return NULL; |
| 8331 | } |
| 8332 | |
| 8333 | out = stringlib_partition( |
| 8334 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8335 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8336 | ); |
| 8337 | |
| 8338 | Py_DECREF(sep_obj); |
| 8339 | Py_DECREF(str_obj); |
| 8340 | |
| 8341 | return out; |
| 8342 | } |
| 8343 | |
| 8344 | |
| 8345 | PyObject * |
| 8346 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8347 | { |
| 8348 | PyObject* str_obj; |
| 8349 | PyObject* sep_obj; |
| 8350 | PyObject* out; |
| 8351 | |
| 8352 | str_obj = PyUnicode_FromObject(str_in); |
| 8353 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8354 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8355 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8356 | if (!sep_obj) { |
| 8357 | Py_DECREF(str_obj); |
| 8358 | return NULL; |
| 8359 | } |
| 8360 | |
| 8361 | out = stringlib_rpartition( |
| 8362 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8363 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8364 | ); |
| 8365 | |
| 8366 | Py_DECREF(sep_obj); |
| 8367 | Py_DECREF(str_obj); |
| 8368 | |
| 8369 | return out; |
| 8370 | } |
| 8371 | |
| 8372 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8373 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8374 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8375 | 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] | 8376 | 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] | 8377 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8378 | |
| 8379 | static PyObject* |
| 8380 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8381 | { |
| 8382 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8383 | } |
| 8384 | |
| 8385 | PyDoc_STRVAR(rpartition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8386 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8387 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8388 | 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] | 8389 | 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] | 8390 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8391 | |
| 8392 | static PyObject* |
| 8393 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8394 | { |
| 8395 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8396 | } |
| 8397 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8398 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8399 | PyObject *sep, |
| 8400 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8401 | { |
| 8402 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8403 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8404 | s = PyUnicode_FromObject(s); |
| 8405 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8406 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8407 | if (sep != NULL) { |
| 8408 | sep = PyUnicode_FromObject(sep); |
| 8409 | if (sep == NULL) { |
| 8410 | Py_DECREF(s); |
| 8411 | return NULL; |
| 8412 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8413 | } |
| 8414 | |
| 8415 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8416 | |
| 8417 | Py_DECREF(s); |
| 8418 | Py_XDECREF(sep); |
| 8419 | return result; |
| 8420 | } |
| 8421 | |
| 8422 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8423 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8424 | \n\ |
| 8425 | Return a list of the words in S, using sep as the\n\ |
| 8426 | delimiter string, starting at the end of the string and\n\ |
| 8427 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8428 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8429 | is a separator."); |
| 8430 | |
| 8431 | static PyObject* |
| 8432 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8433 | { |
| 8434 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8435 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8436 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8437 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8438 | return NULL; |
| 8439 | |
| 8440 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8441 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8442 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8443 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8444 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8445 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8446 | } |
| 8447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8448 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8450 | \n\ |
| 8451 | 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] | 8452 | 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] | 8453 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8454 | |
| 8455 | static PyObject* |
| 8456 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8457 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8458 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8460 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8461 | return NULL; |
| 8462 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8463 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8464 | } |
| 8465 | |
| 8466 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8467 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8468 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8469 | if (PyUnicode_CheckExact(self)) { |
| 8470 | Py_INCREF(self); |
| 8471 | return self; |
| 8472 | } else |
| 8473 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8474 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8475 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8476 | } |
| 8477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8478 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8479 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8480 | \n\ |
| 8481 | 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] | 8482 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8483 | |
| 8484 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8485 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8486 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8487 | return fixup(self, fixswapcase); |
| 8488 | } |
| 8489 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8490 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8491 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8492 | \n\ |
| 8493 | Return a translation table usable for str.translate().\n\ |
| 8494 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8495 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8496 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8497 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8498 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8499 | character at the same position in y. If there is a third argument, it\n\ |
| 8500 | must be a string, whose characters will be mapped to None in the result."); |
| 8501 | |
| 8502 | static PyObject* |
| 8503 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8504 | { |
| 8505 | PyObject *x, *y = NULL, *z = NULL; |
| 8506 | PyObject *new = NULL, *key, *value; |
| 8507 | Py_ssize_t i = 0; |
| 8508 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8509 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8510 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8511 | return NULL; |
| 8512 | new = PyDict_New(); |
| 8513 | if (!new) |
| 8514 | return NULL; |
| 8515 | if (y != NULL) { |
| 8516 | /* x must be a string too, of equal length */ |
| 8517 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8518 | if (!PyUnicode_Check(x)) { |
| 8519 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8520 | "be a string if there is a second argument"); |
| 8521 | goto err; |
| 8522 | } |
| 8523 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8524 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8525 | "arguments must have equal length"); |
| 8526 | goto err; |
| 8527 | } |
| 8528 | /* create entries for translating chars in x to those in y */ |
| 8529 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8530 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8531 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8532 | if (!key || !value) |
| 8533 | goto err; |
| 8534 | res = PyDict_SetItem(new, key, value); |
| 8535 | Py_DECREF(key); |
| 8536 | Py_DECREF(value); |
| 8537 | if (res < 0) |
| 8538 | goto err; |
| 8539 | } |
| 8540 | /* create entries for deleting chars in z */ |
| 8541 | if (z != NULL) { |
| 8542 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8543 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8544 | if (!key) |
| 8545 | goto err; |
| 8546 | res = PyDict_SetItem(new, key, Py_None); |
| 8547 | Py_DECREF(key); |
| 8548 | if (res < 0) |
| 8549 | goto err; |
| 8550 | } |
| 8551 | } |
| 8552 | } else { |
| 8553 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8554 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8555 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8556 | "to maketrans it must be a dict"); |
| 8557 | goto err; |
| 8558 | } |
| 8559 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8560 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8561 | if (PyUnicode_Check(key)) { |
| 8562 | /* convert string keys to integer keys */ |
| 8563 | PyObject *newkey; |
| 8564 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8565 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8566 | "table must be of length 1"); |
| 8567 | goto err; |
| 8568 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8569 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8570 | if (!newkey) |
| 8571 | goto err; |
| 8572 | res = PyDict_SetItem(new, newkey, value); |
| 8573 | Py_DECREF(newkey); |
| 8574 | if (res < 0) |
| 8575 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8576 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8577 | /* just keep integer keys */ |
| 8578 | if (PyDict_SetItem(new, key, value) < 0) |
| 8579 | goto err; |
| 8580 | } else { |
| 8581 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8582 | "be strings or integers"); |
| 8583 | goto err; |
| 8584 | } |
| 8585 | } |
| 8586 | } |
| 8587 | return new; |
| 8588 | err: |
| 8589 | Py_DECREF(new); |
| 8590 | return NULL; |
| 8591 | } |
| 8592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8593 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8594 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8595 | \n\ |
| 8596 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8597 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8598 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8599 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8600 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8601 | |
| 8602 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8603 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8604 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8605 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8606 | } |
| 8607 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8608 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8609 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8610 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8611 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8612 | |
| 8613 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8614 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8615 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8616 | return fixup(self, fixupper); |
| 8617 | } |
| 8618 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8619 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8620 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8621 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8622 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8623 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8624 | |
| 8625 | static PyObject * |
| 8626 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8627 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8628 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8629 | PyUnicodeObject *u; |
| 8630 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8631 | Py_ssize_t width; |
| 8632 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | return NULL; |
| 8634 | |
| 8635 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8636 | if (PyUnicode_CheckExact(self)) { |
| 8637 | Py_INCREF(self); |
| 8638 | return (PyObject*) self; |
| 8639 | } |
| 8640 | else |
| 8641 | return PyUnicode_FromUnicode( |
| 8642 | PyUnicode_AS_UNICODE(self), |
| 8643 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8644 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8645 | } |
| 8646 | |
| 8647 | fill = width - self->length; |
| 8648 | |
| 8649 | u = pad(self, fill, 0, '0'); |
| 8650 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8651 | if (u == NULL) |
| 8652 | return NULL; |
| 8653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8654 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8655 | /* move sign to beginning of string */ |
| 8656 | u->str[0] = u->str[fill]; |
| 8657 | u->str[fill] = '0'; |
| 8658 | } |
| 8659 | |
| 8660 | return (PyObject*) u; |
| 8661 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8662 | |
| 8663 | #if 0 |
| 8664 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8665 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8666 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8667 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8668 | } |
| 8669 | #endif |
| 8670 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8671 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8672 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8673 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8674 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8675 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8676 | With optional end, stop comparing S at that position.\n\ |
| 8677 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8678 | |
| 8679 | static PyObject * |
| 8680 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8681 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8682 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8683 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8684 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8685 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8686 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8687 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8688 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8689 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8690 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8691 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8692 | if (PyTuple_Check(subobj)) { |
| 8693 | Py_ssize_t i; |
| 8694 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8695 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8696 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8697 | if (substring == NULL) |
| 8698 | return NULL; |
| 8699 | result = tailmatch(self, substring, start, end, -1); |
| 8700 | Py_DECREF(substring); |
| 8701 | if (result) { |
| 8702 | Py_RETURN_TRUE; |
| 8703 | } |
| 8704 | } |
| 8705 | /* nothing matched */ |
| 8706 | Py_RETURN_FALSE; |
| 8707 | } |
| 8708 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8709 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8710 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8711 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8712 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8713 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8714 | } |
| 8715 | |
| 8716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8717 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8718 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8719 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8720 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8721 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8722 | With optional end, stop comparing S at that position.\n\ |
| 8723 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8724 | |
| 8725 | static PyObject * |
| 8726 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8727 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8729 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8730 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8731 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8732 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8733 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8734 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8735 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8736 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8737 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8738 | if (PyTuple_Check(subobj)) { |
| 8739 | Py_ssize_t i; |
| 8740 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8741 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8742 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8743 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8744 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8745 | result = tailmatch(self, substring, start, end, +1); |
| 8746 | Py_DECREF(substring); |
| 8747 | if (result) { |
| 8748 | Py_RETURN_TRUE; |
| 8749 | } |
| 8750 | } |
| 8751 | Py_RETURN_FALSE; |
| 8752 | } |
| 8753 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8754 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8755 | return NULL; |
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 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8758 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8759 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8760 | } |
| 8761 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8762 | #include "stringlib/string_format.h" |
| 8763 | |
| 8764 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8765 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8766 | \n\ |
| 8767 | "); |
| 8768 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8769 | static PyObject * |
| 8770 | unicode__format__(PyObject* self, PyObject* args) |
| 8771 | { |
| 8772 | PyObject *format_spec; |
| 8773 | |
| 8774 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8775 | return NULL; |
| 8776 | |
| 8777 | return _PyUnicode_FormatAdvanced(self, |
| 8778 | PyUnicode_AS_UNICODE(format_spec), |
| 8779 | PyUnicode_GET_SIZE(format_spec)); |
| 8780 | } |
| 8781 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8782 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8783 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8784 | \n\ |
| 8785 | "); |
| 8786 | |
| 8787 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8788 | unicode__sizeof__(PyUnicodeObject *v) |
| 8789 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8790 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8791 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8792 | } |
| 8793 | |
| 8794 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8795 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8796 | |
| 8797 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8798 | unicode_getnewargs(PyUnicodeObject *v) |
| 8799 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8800 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8801 | } |
| 8802 | |
| 8803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8804 | static PyMethodDef unicode_methods[] = { |
| 8805 | |
| 8806 | /* Order is according to common usage: often used methods should |
| 8807 | appear first, since lookup is done sequentially. */ |
| 8808 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8809 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8810 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8811 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8812 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8813 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8814 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8815 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8816 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8817 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8818 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8819 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8820 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8821 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8822 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8823 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8824 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8825 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8826 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8827 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8828 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8829 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8830 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8831 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8832 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8833 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8834 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8835 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8836 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8837 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8838 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8839 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8840 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8841 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8842 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8843 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8844 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8845 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8846 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8847 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8848 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8849 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8850 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8851 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8852 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8853 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8854 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8855 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8856 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8857 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8858 | #endif |
| 8859 | |
| 8860 | #if 0 |
| 8861 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8862 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8863 | #endif |
| 8864 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8865 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8866 | {NULL, NULL} |
| 8867 | }; |
| 8868 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8869 | static PyObject * |
| 8870 | unicode_mod(PyObject *v, PyObject *w) |
| 8871 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8872 | if (!PyUnicode_Check(v)) { |
| 8873 | Py_INCREF(Py_NotImplemented); |
| 8874 | return Py_NotImplemented; |
| 8875 | } |
| 8876 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8877 | } |
| 8878 | |
| 8879 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8880 | 0, /*nb_add*/ |
| 8881 | 0, /*nb_subtract*/ |
| 8882 | 0, /*nb_multiply*/ |
| 8883 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8884 | }; |
| 8885 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8886 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8887 | (lenfunc) unicode_length, /* sq_length */ |
| 8888 | PyUnicode_Concat, /* sq_concat */ |
| 8889 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8890 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8891 | 0, /* sq_slice */ |
| 8892 | 0, /* sq_ass_item */ |
| 8893 | 0, /* sq_ass_slice */ |
| 8894 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8895 | }; |
| 8896 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8897 | static PyObject* |
| 8898 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8899 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8900 | if (PyIndex_Check(item)) { |
| 8901 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8902 | if (i == -1 && PyErr_Occurred()) |
| 8903 | return NULL; |
| 8904 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8905 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8906 | return unicode_getitem(self, i); |
| 8907 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8908 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8909 | Py_UNICODE* source_buf; |
| 8910 | Py_UNICODE* result_buf; |
| 8911 | PyObject* result; |
| 8912 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8913 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8914 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8915 | return NULL; |
| 8916 | } |
| 8917 | |
| 8918 | if (slicelength <= 0) { |
| 8919 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8920 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8921 | PyUnicode_CheckExact(self)) { |
| 8922 | Py_INCREF(self); |
| 8923 | return (PyObject *)self; |
| 8924 | } else if (step == 1) { |
| 8925 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8926 | } else { |
| 8927 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8928 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8929 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8930 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8931 | if (result_buf == NULL) |
| 8932 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8933 | |
| 8934 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8935 | result_buf[i] = source_buf[cur]; |
| 8936 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8937 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8938 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8939 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8940 | return result; |
| 8941 | } |
| 8942 | } else { |
| 8943 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8944 | return NULL; |
| 8945 | } |
| 8946 | } |
| 8947 | |
| 8948 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8949 | (lenfunc)unicode_length, /* mp_length */ |
| 8950 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8951 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8952 | }; |
| 8953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8954 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8955 | /* Helpers for PyUnicode_Format() */ |
| 8956 | |
| 8957 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8958 | 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] | 8959 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8960 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8961 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8962 | (*p_argidx)++; |
| 8963 | if (arglen < 0) |
| 8964 | return args; |
| 8965 | else |
| 8966 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8967 | } |
| 8968 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8969 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8970 | return NULL; |
| 8971 | } |
| 8972 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8973 | /* 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] | 8974 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8975 | static PyObject * |
| 8976 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8977 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8978 | char *p; |
| 8979 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8980 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8981 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8982 | x = PyFloat_AsDouble(v); |
| 8983 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8984 | return NULL; |
| 8985 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8986 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8987 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8988 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8989 | p = PyOS_double_to_string(x, type, prec, |
| 8990 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8991 | if (p == NULL) |
| 8992 | return NULL; |
| 8993 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8994 | PyMem_Free(p); |
| 8995 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8996 | } |
| 8997 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8998 | static PyObject* |
| 8999 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9000 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9001 | char *buf; |
| 9002 | int len; |
| 9003 | PyObject *str; /* temporary string object. */ |
| 9004 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9005 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9006 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9007 | if (!str) |
| 9008 | return NULL; |
| 9009 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9010 | Py_DECREF(str); |
| 9011 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9012 | } |
| 9013 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9014 | static int |
| 9015 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9016 | size_t buflen, |
| 9017 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9018 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9019 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9020 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9021 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9022 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9023 | buf[1] = '\0'; |
| 9024 | return 1; |
| 9025 | } |
| 9026 | #ifndef Py_UNICODE_WIDE |
| 9027 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9028 | /* Decode a valid surrogate pair */ |
| 9029 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9030 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9031 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9032 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9033 | buf[0] = c0; |
| 9034 | buf[1] = c1; |
| 9035 | buf[2] = '\0'; |
| 9036 | return 2; |
| 9037 | } |
| 9038 | } |
| 9039 | #endif |
| 9040 | goto onError; |
| 9041 | } |
| 9042 | else { |
| 9043 | /* Integer input truncated to a character */ |
| 9044 | long x; |
| 9045 | x = PyLong_AsLong(v); |
| 9046 | if (x == -1 && PyErr_Occurred()) |
| 9047 | goto onError; |
| 9048 | |
| 9049 | if (x < 0 || x > 0x10ffff) { |
| 9050 | PyErr_SetString(PyExc_OverflowError, |
| 9051 | "%c arg not in range(0x110000)"); |
| 9052 | return -1; |
| 9053 | } |
| 9054 | |
| 9055 | #ifndef Py_UNICODE_WIDE |
| 9056 | if (x > 0xffff) { |
| 9057 | x -= 0x10000; |
| 9058 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9059 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9060 | return 2; |
| 9061 | } |
| 9062 | #endif |
| 9063 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9064 | buf[1] = '\0'; |
| 9065 | return 1; |
| 9066 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9067 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9068 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9069 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9070 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9071 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9072 | } |
| 9073 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9074 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9075 | 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] | 9076 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9077 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9078 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9079 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9080 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9081 | { |
| 9082 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9083 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9084 | int args_owned = 0; |
| 9085 | PyUnicodeObject *result = NULL; |
| 9086 | PyObject *dict = NULL; |
| 9087 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9089 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9090 | PyErr_BadInternalCall(); |
| 9091 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9092 | } |
| 9093 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9094 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9095 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9096 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9097 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9098 | |
| 9099 | reslen = rescnt = fmtcnt + 100; |
| 9100 | result = _PyUnicode_New(reslen); |
| 9101 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9102 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9103 | res = PyUnicode_AS_UNICODE(result); |
| 9104 | |
| 9105 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9106 | arglen = PyTuple_Size(args); |
| 9107 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9108 | } |
| 9109 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9110 | arglen = -1; |
| 9111 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9112 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9113 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9114 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9115 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9116 | |
| 9117 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9118 | if (*fmt != '%') { |
| 9119 | if (--rescnt < 0) { |
| 9120 | rescnt = fmtcnt + 100; |
| 9121 | reslen += rescnt; |
| 9122 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9123 | goto onError; |
| 9124 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9125 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9126 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9127 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9128 | } |
| 9129 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9130 | /* Got a format specifier */ |
| 9131 | int flags = 0; |
| 9132 | Py_ssize_t width = -1; |
| 9133 | int prec = -1; |
| 9134 | Py_UNICODE c = '\0'; |
| 9135 | Py_UNICODE fill; |
| 9136 | int isnumok; |
| 9137 | PyObject *v = NULL; |
| 9138 | PyObject *temp = NULL; |
| 9139 | Py_UNICODE *pbuf; |
| 9140 | Py_UNICODE sign; |
| 9141 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9142 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9143 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9144 | fmt++; |
| 9145 | if (*fmt == '(') { |
| 9146 | Py_UNICODE *keystart; |
| 9147 | Py_ssize_t keylen; |
| 9148 | PyObject *key; |
| 9149 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9150 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9151 | if (dict == NULL) { |
| 9152 | PyErr_SetString(PyExc_TypeError, |
| 9153 | "format requires a mapping"); |
| 9154 | goto onError; |
| 9155 | } |
| 9156 | ++fmt; |
| 9157 | --fmtcnt; |
| 9158 | keystart = fmt; |
| 9159 | /* Skip over balanced parentheses */ |
| 9160 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9161 | if (*fmt == ')') |
| 9162 | --pcount; |
| 9163 | else if (*fmt == '(') |
| 9164 | ++pcount; |
| 9165 | fmt++; |
| 9166 | } |
| 9167 | keylen = fmt - keystart - 1; |
| 9168 | if (fmtcnt < 0 || pcount > 0) { |
| 9169 | PyErr_SetString(PyExc_ValueError, |
| 9170 | "incomplete format key"); |
| 9171 | goto onError; |
| 9172 | } |
| 9173 | #if 0 |
| 9174 | /* keys are converted to strings using UTF-8 and |
| 9175 | then looked up since Python uses strings to hold |
| 9176 | variables names etc. in its namespaces and we |
| 9177 | wouldn't want to break common idioms. */ |
| 9178 | key = PyUnicode_EncodeUTF8(keystart, |
| 9179 | keylen, |
| 9180 | NULL); |
| 9181 | #else |
| 9182 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9183 | #endif |
| 9184 | if (key == NULL) |
| 9185 | goto onError; |
| 9186 | if (args_owned) { |
| 9187 | Py_DECREF(args); |
| 9188 | args_owned = 0; |
| 9189 | } |
| 9190 | args = PyObject_GetItem(dict, key); |
| 9191 | Py_DECREF(key); |
| 9192 | if (args == NULL) { |
| 9193 | goto onError; |
| 9194 | } |
| 9195 | args_owned = 1; |
| 9196 | arglen = -1; |
| 9197 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9198 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9199 | while (--fmtcnt >= 0) { |
| 9200 | switch (c = *fmt++) { |
| 9201 | case '-': flags |= F_LJUST; continue; |
| 9202 | case '+': flags |= F_SIGN; continue; |
| 9203 | case ' ': flags |= F_BLANK; continue; |
| 9204 | case '#': flags |= F_ALT; continue; |
| 9205 | case '0': flags |= F_ZERO; continue; |
| 9206 | } |
| 9207 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9208 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9209 | if (c == '*') { |
| 9210 | v = getnextarg(args, arglen, &argidx); |
| 9211 | if (v == NULL) |
| 9212 | goto onError; |
| 9213 | if (!PyLong_Check(v)) { |
| 9214 | PyErr_SetString(PyExc_TypeError, |
| 9215 | "* wants int"); |
| 9216 | goto onError; |
| 9217 | } |
| 9218 | width = PyLong_AsLong(v); |
| 9219 | if (width == -1 && PyErr_Occurred()) |
| 9220 | goto onError; |
| 9221 | if (width < 0) { |
| 9222 | flags |= F_LJUST; |
| 9223 | width = -width; |
| 9224 | } |
| 9225 | if (--fmtcnt >= 0) |
| 9226 | c = *fmt++; |
| 9227 | } |
| 9228 | else if (c >= '0' && c <= '9') { |
| 9229 | width = c - '0'; |
| 9230 | while (--fmtcnt >= 0) { |
| 9231 | c = *fmt++; |
| 9232 | if (c < '0' || c > '9') |
| 9233 | break; |
| 9234 | if ((width*10) / 10 != width) { |
| 9235 | PyErr_SetString(PyExc_ValueError, |
| 9236 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9237 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9238 | } |
| 9239 | width = width*10 + (c - '0'); |
| 9240 | } |
| 9241 | } |
| 9242 | if (c == '.') { |
| 9243 | prec = 0; |
| 9244 | if (--fmtcnt >= 0) |
| 9245 | c = *fmt++; |
| 9246 | if (c == '*') { |
| 9247 | v = getnextarg(args, arglen, &argidx); |
| 9248 | if (v == NULL) |
| 9249 | goto onError; |
| 9250 | if (!PyLong_Check(v)) { |
| 9251 | PyErr_SetString(PyExc_TypeError, |
| 9252 | "* wants int"); |
| 9253 | goto onError; |
| 9254 | } |
| 9255 | prec = PyLong_AsLong(v); |
| 9256 | if (prec == -1 && PyErr_Occurred()) |
| 9257 | goto onError; |
| 9258 | if (prec < 0) |
| 9259 | prec = 0; |
| 9260 | if (--fmtcnt >= 0) |
| 9261 | c = *fmt++; |
| 9262 | } |
| 9263 | else if (c >= '0' && c <= '9') { |
| 9264 | prec = c - '0'; |
| 9265 | while (--fmtcnt >= 0) { |
| 9266 | c = Py_CHARMASK(*fmt++); |
| 9267 | if (c < '0' || c > '9') |
| 9268 | break; |
| 9269 | if ((prec*10) / 10 != prec) { |
| 9270 | PyErr_SetString(PyExc_ValueError, |
| 9271 | "prec too big"); |
| 9272 | goto onError; |
| 9273 | } |
| 9274 | prec = prec*10 + (c - '0'); |
| 9275 | } |
| 9276 | } |
| 9277 | } /* prec */ |
| 9278 | if (fmtcnt >= 0) { |
| 9279 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9280 | if (--fmtcnt >= 0) |
| 9281 | c = *fmt++; |
| 9282 | } |
| 9283 | } |
| 9284 | if (fmtcnt < 0) { |
| 9285 | PyErr_SetString(PyExc_ValueError, |
| 9286 | "incomplete format"); |
| 9287 | goto onError; |
| 9288 | } |
| 9289 | if (c != '%') { |
| 9290 | v = getnextarg(args, arglen, &argidx); |
| 9291 | if (v == NULL) |
| 9292 | goto onError; |
| 9293 | } |
| 9294 | sign = 0; |
| 9295 | fill = ' '; |
| 9296 | switch (c) { |
| 9297 | |
| 9298 | case '%': |
| 9299 | pbuf = formatbuf; |
| 9300 | /* presume that buffer length is at least 1 */ |
| 9301 | pbuf[0] = '%'; |
| 9302 | len = 1; |
| 9303 | break; |
| 9304 | |
| 9305 | case 's': |
| 9306 | case 'r': |
| 9307 | case 'a': |
| 9308 | if (PyUnicode_Check(v) && c == 's') { |
| 9309 | temp = v; |
| 9310 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9311 | } |
| 9312 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9313 | if (c == 's') |
| 9314 | temp = PyObject_Str(v); |
| 9315 | else if (c == 'r') |
| 9316 | temp = PyObject_Repr(v); |
| 9317 | else |
| 9318 | temp = PyObject_ASCII(v); |
| 9319 | if (temp == NULL) |
| 9320 | goto onError; |
| 9321 | if (PyUnicode_Check(temp)) |
| 9322 | /* nothing to do */; |
| 9323 | else { |
| 9324 | Py_DECREF(temp); |
| 9325 | PyErr_SetString(PyExc_TypeError, |
| 9326 | "%s argument has non-string str()"); |
| 9327 | goto onError; |
| 9328 | } |
| 9329 | } |
| 9330 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9331 | len = PyUnicode_GET_SIZE(temp); |
| 9332 | if (prec >= 0 && len > prec) |
| 9333 | len = prec; |
| 9334 | break; |
| 9335 | |
| 9336 | case 'i': |
| 9337 | case 'd': |
| 9338 | case 'u': |
| 9339 | case 'o': |
| 9340 | case 'x': |
| 9341 | case 'X': |
| 9342 | if (c == 'i') |
| 9343 | c = 'd'; |
| 9344 | isnumok = 0; |
| 9345 | if (PyNumber_Check(v)) { |
| 9346 | PyObject *iobj=NULL; |
| 9347 | |
| 9348 | if (PyLong_Check(v)) { |
| 9349 | iobj = v; |
| 9350 | Py_INCREF(iobj); |
| 9351 | } |
| 9352 | else { |
| 9353 | iobj = PyNumber_Long(v); |
| 9354 | } |
| 9355 | if (iobj!=NULL) { |
| 9356 | if (PyLong_Check(iobj)) { |
| 9357 | isnumok = 1; |
| 9358 | temp = formatlong(iobj, flags, prec, c); |
| 9359 | Py_DECREF(iobj); |
| 9360 | if (!temp) |
| 9361 | goto onError; |
| 9362 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9363 | len = PyUnicode_GET_SIZE(temp); |
| 9364 | sign = 1; |
| 9365 | } |
| 9366 | else { |
| 9367 | Py_DECREF(iobj); |
| 9368 | } |
| 9369 | } |
| 9370 | } |
| 9371 | if (!isnumok) { |
| 9372 | PyErr_Format(PyExc_TypeError, |
| 9373 | "%%%c format: a number is required, " |
| 9374 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9375 | goto onError; |
| 9376 | } |
| 9377 | if (flags & F_ZERO) |
| 9378 | fill = '0'; |
| 9379 | break; |
| 9380 | |
| 9381 | case 'e': |
| 9382 | case 'E': |
| 9383 | case 'f': |
| 9384 | case 'F': |
| 9385 | case 'g': |
| 9386 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9387 | temp = formatfloat(v, flags, prec, c); |
| 9388 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9389 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9390 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9391 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9392 | sign = 1; |
| 9393 | if (flags & F_ZERO) |
| 9394 | fill = '0'; |
| 9395 | break; |
| 9396 | |
| 9397 | case 'c': |
| 9398 | pbuf = formatbuf; |
| 9399 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9400 | if (len < 0) |
| 9401 | goto onError; |
| 9402 | break; |
| 9403 | |
| 9404 | default: |
| 9405 | PyErr_Format(PyExc_ValueError, |
| 9406 | "unsupported format character '%c' (0x%x) " |
| 9407 | "at index %zd", |
| 9408 | (31<=c && c<=126) ? (char)c : '?', |
| 9409 | (int)c, |
| 9410 | (Py_ssize_t)(fmt - 1 - |
| 9411 | PyUnicode_AS_UNICODE(uformat))); |
| 9412 | goto onError; |
| 9413 | } |
| 9414 | if (sign) { |
| 9415 | if (*pbuf == '-' || *pbuf == '+') { |
| 9416 | sign = *pbuf++; |
| 9417 | len--; |
| 9418 | } |
| 9419 | else if (flags & F_SIGN) |
| 9420 | sign = '+'; |
| 9421 | else if (flags & F_BLANK) |
| 9422 | sign = ' '; |
| 9423 | else |
| 9424 | sign = 0; |
| 9425 | } |
| 9426 | if (width < len) |
| 9427 | width = len; |
| 9428 | if (rescnt - (sign != 0) < width) { |
| 9429 | reslen -= rescnt; |
| 9430 | rescnt = width + fmtcnt + 100; |
| 9431 | reslen += rescnt; |
| 9432 | if (reslen < 0) { |
| 9433 | Py_XDECREF(temp); |
| 9434 | PyErr_NoMemory(); |
| 9435 | goto onError; |
| 9436 | } |
| 9437 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9438 | Py_XDECREF(temp); |
| 9439 | goto onError; |
| 9440 | } |
| 9441 | res = PyUnicode_AS_UNICODE(result) |
| 9442 | + reslen - rescnt; |
| 9443 | } |
| 9444 | if (sign) { |
| 9445 | if (fill != ' ') |
| 9446 | *res++ = sign; |
| 9447 | rescnt--; |
| 9448 | if (width > len) |
| 9449 | width--; |
| 9450 | } |
| 9451 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9452 | assert(pbuf[0] == '0'); |
| 9453 | assert(pbuf[1] == c); |
| 9454 | if (fill != ' ') { |
| 9455 | *res++ = *pbuf++; |
| 9456 | *res++ = *pbuf++; |
| 9457 | } |
| 9458 | rescnt -= 2; |
| 9459 | width -= 2; |
| 9460 | if (width < 0) |
| 9461 | width = 0; |
| 9462 | len -= 2; |
| 9463 | } |
| 9464 | if (width > len && !(flags & F_LJUST)) { |
| 9465 | do { |
| 9466 | --rescnt; |
| 9467 | *res++ = fill; |
| 9468 | } while (--width > len); |
| 9469 | } |
| 9470 | if (fill == ' ') { |
| 9471 | if (sign) |
| 9472 | *res++ = sign; |
| 9473 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9474 | assert(pbuf[0] == '0'); |
| 9475 | assert(pbuf[1] == c); |
| 9476 | *res++ = *pbuf++; |
| 9477 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9478 | } |
| 9479 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9480 | Py_UNICODE_COPY(res, pbuf, len); |
| 9481 | res += len; |
| 9482 | rescnt -= len; |
| 9483 | while (--width >= len) { |
| 9484 | --rescnt; |
| 9485 | *res++ = ' '; |
| 9486 | } |
| 9487 | if (dict && (argidx < arglen) && c != '%') { |
| 9488 | PyErr_SetString(PyExc_TypeError, |
| 9489 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9490 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9491 | goto onError; |
| 9492 | } |
| 9493 | Py_XDECREF(temp); |
| 9494 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9495 | } /* until end */ |
| 9496 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9497 | PyErr_SetString(PyExc_TypeError, |
| 9498 | "not all arguments converted during string formatting"); |
| 9499 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9500 | } |
| 9501 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9502 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9503 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9504 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9505 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9506 | } |
| 9507 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9508 | return (PyObject *)result; |
| 9509 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9510 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9511 | Py_XDECREF(result); |
| 9512 | Py_DECREF(uformat); |
| 9513 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9514 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9515 | } |
| 9516 | return NULL; |
| 9517 | } |
| 9518 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9519 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9520 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9521 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9522 | static PyObject * |
| 9523 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9524 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9525 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9526 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9527 | char *encoding = NULL; |
| 9528 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9529 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9530 | if (type != &PyUnicode_Type) |
| 9531 | return unicode_subtype_new(type, args, kwds); |
| 9532 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9533 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9534 | return NULL; |
| 9535 | if (x == NULL) |
| 9536 | return (PyObject *)_PyUnicode_New(0); |
| 9537 | if (encoding == NULL && errors == NULL) |
| 9538 | return PyObject_Str(x); |
| 9539 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9540 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9541 | } |
| 9542 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9543 | static PyObject * |
| 9544 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9545 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9546 | PyUnicodeObject *tmp, *pnew; |
| 9547 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9548 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9549 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9550 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9551 | if (tmp == NULL) |
| 9552 | return NULL; |
| 9553 | assert(PyUnicode_Check(tmp)); |
| 9554 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9555 | if (pnew == NULL) { |
| 9556 | Py_DECREF(tmp); |
| 9557 | return NULL; |
| 9558 | } |
| 9559 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9560 | if (pnew->str == NULL) { |
| 9561 | _Py_ForgetReference((PyObject *)pnew); |
| 9562 | PyObject_Del(pnew); |
| 9563 | Py_DECREF(tmp); |
| 9564 | return PyErr_NoMemory(); |
| 9565 | } |
| 9566 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9567 | pnew->length = n; |
| 9568 | pnew->hash = tmp->hash; |
| 9569 | Py_DECREF(tmp); |
| 9570 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9571 | } |
| 9572 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9573 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9574 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9575 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9576 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9577 | encoding defaults to the current default string encoding.\n\ |
| 9578 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9579 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9580 | static PyObject *unicode_iter(PyObject *seq); |
| 9581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9582 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9583 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9584 | "str", /* tp_name */ |
| 9585 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9586 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9587 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9588 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9589 | 0, /* tp_print */ |
| 9590 | 0, /* tp_getattr */ |
| 9591 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9592 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9593 | unicode_repr, /* tp_repr */ |
| 9594 | &unicode_as_number, /* tp_as_number */ |
| 9595 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9596 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9597 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9598 | 0, /* tp_call*/ |
| 9599 | (reprfunc) unicode_str, /* tp_str */ |
| 9600 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9601 | 0, /* tp_setattro */ |
| 9602 | 0, /* tp_as_buffer */ |
| 9603 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9604 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9605 | unicode_doc, /* tp_doc */ |
| 9606 | 0, /* tp_traverse */ |
| 9607 | 0, /* tp_clear */ |
| 9608 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9609 | 0, /* tp_weaklistoffset */ |
| 9610 | unicode_iter, /* tp_iter */ |
| 9611 | 0, /* tp_iternext */ |
| 9612 | unicode_methods, /* tp_methods */ |
| 9613 | 0, /* tp_members */ |
| 9614 | 0, /* tp_getset */ |
| 9615 | &PyBaseObject_Type, /* tp_base */ |
| 9616 | 0, /* tp_dict */ |
| 9617 | 0, /* tp_descr_get */ |
| 9618 | 0, /* tp_descr_set */ |
| 9619 | 0, /* tp_dictoffset */ |
| 9620 | 0, /* tp_init */ |
| 9621 | 0, /* tp_alloc */ |
| 9622 | unicode_new, /* tp_new */ |
| 9623 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9624 | }; |
| 9625 | |
| 9626 | /* Initialize the Unicode implementation */ |
| 9627 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9628 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9629 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9630 | int i; |
| 9631 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9632 | /* XXX - move this array to unicodectype.c ? */ |
| 9633 | Py_UNICODE linebreak[] = { |
| 9634 | 0x000A, /* LINE FEED */ |
| 9635 | 0x000D, /* CARRIAGE RETURN */ |
| 9636 | 0x001C, /* FILE SEPARATOR */ |
| 9637 | 0x001D, /* GROUP SEPARATOR */ |
| 9638 | 0x001E, /* RECORD SEPARATOR */ |
| 9639 | 0x0085, /* NEXT LINE */ |
| 9640 | 0x2028, /* LINE SEPARATOR */ |
| 9641 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9642 | }; |
| 9643 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9644 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9645 | free_list = NULL; |
| 9646 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9647 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9648 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9649 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9650 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9651 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9652 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9653 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9654 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9655 | |
| 9656 | /* initialize the linebreak bloom filter */ |
| 9657 | bloom_linebreak = make_bloom_mask( |
| 9658 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9659 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9660 | |
| 9661 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9662 | } |
| 9663 | |
| 9664 | /* Finalize the Unicode implementation */ |
| 9665 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9666 | int |
| 9667 | PyUnicode_ClearFreeList(void) |
| 9668 | { |
| 9669 | int freelist_size = numfree; |
| 9670 | PyUnicodeObject *u; |
| 9671 | |
| 9672 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9673 | PyUnicodeObject *v = u; |
| 9674 | u = *(PyUnicodeObject **)u; |
| 9675 | if (v->str) |
| 9676 | PyObject_DEL(v->str); |
| 9677 | Py_XDECREF(v->defenc); |
| 9678 | PyObject_Del(v); |
| 9679 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9680 | } |
| 9681 | free_list = NULL; |
| 9682 | assert(numfree == 0); |
| 9683 | return freelist_size; |
| 9684 | } |
| 9685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9686 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9687 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9688 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9689 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9690 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9691 | Py_XDECREF(unicode_empty); |
| 9692 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9693 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9694 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9695 | if (unicode_latin1[i]) { |
| 9696 | Py_DECREF(unicode_latin1[i]); |
| 9697 | unicode_latin1[i] = NULL; |
| 9698 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9699 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9700 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9701 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9702 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9703 | void |
| 9704 | PyUnicode_InternInPlace(PyObject **p) |
| 9705 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9706 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9707 | PyObject *t; |
| 9708 | if (s == NULL || !PyUnicode_Check(s)) |
| 9709 | Py_FatalError( |
| 9710 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9711 | /* If it's a subclass, we don't really know what putting |
| 9712 | it in the interned dict might do. */ |
| 9713 | if (!PyUnicode_CheckExact(s)) |
| 9714 | return; |
| 9715 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9716 | return; |
| 9717 | if (interned == NULL) { |
| 9718 | interned = PyDict_New(); |
| 9719 | if (interned == NULL) { |
| 9720 | PyErr_Clear(); /* Don't leave an exception */ |
| 9721 | return; |
| 9722 | } |
| 9723 | } |
| 9724 | /* It might be that the GetItem call fails even |
| 9725 | though the key is present in the dictionary, |
| 9726 | namely when this happens during a stack overflow. */ |
| 9727 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9728 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9729 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9730 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9731 | if (t) { |
| 9732 | Py_INCREF(t); |
| 9733 | Py_DECREF(*p); |
| 9734 | *p = t; |
| 9735 | return; |
| 9736 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9737 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9738 | PyThreadState_GET()->recursion_critical = 1; |
| 9739 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9740 | PyErr_Clear(); |
| 9741 | PyThreadState_GET()->recursion_critical = 0; |
| 9742 | return; |
| 9743 | } |
| 9744 | PyThreadState_GET()->recursion_critical = 0; |
| 9745 | /* The two references in interned are not counted by refcnt. |
| 9746 | The deallocator will take care of this */ |
| 9747 | Py_REFCNT(s) -= 2; |
| 9748 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9749 | } |
| 9750 | |
| 9751 | void |
| 9752 | PyUnicode_InternImmortal(PyObject **p) |
| 9753 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9754 | PyUnicode_InternInPlace(p); |
| 9755 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9756 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9757 | Py_INCREF(*p); |
| 9758 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9759 | } |
| 9760 | |
| 9761 | PyObject * |
| 9762 | PyUnicode_InternFromString(const char *cp) |
| 9763 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9764 | PyObject *s = PyUnicode_FromString(cp); |
| 9765 | if (s == NULL) |
| 9766 | return NULL; |
| 9767 | PyUnicode_InternInPlace(&s); |
| 9768 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9769 | } |
| 9770 | |
| 9771 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9772 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9773 | PyObject *keys; |
| 9774 | PyUnicodeObject *s; |
| 9775 | Py_ssize_t i, n; |
| 9776 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9777 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9778 | if (interned == NULL || !PyDict_Check(interned)) |
| 9779 | return; |
| 9780 | keys = PyDict_Keys(interned); |
| 9781 | if (keys == NULL || !PyList_Check(keys)) { |
| 9782 | PyErr_Clear(); |
| 9783 | return; |
| 9784 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9785 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9786 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9787 | detector, interned unicode strings are not forcibly deallocated; |
| 9788 | rather, we give them their stolen references back, and then clear |
| 9789 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9790 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9791 | n = PyList_GET_SIZE(keys); |
| 9792 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9793 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9794 | for (i = 0; i < n; i++) { |
| 9795 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9796 | switch (s->state) { |
| 9797 | case SSTATE_NOT_INTERNED: |
| 9798 | /* XXX Shouldn't happen */ |
| 9799 | break; |
| 9800 | case SSTATE_INTERNED_IMMORTAL: |
| 9801 | Py_REFCNT(s) += 1; |
| 9802 | immortal_size += s->length; |
| 9803 | break; |
| 9804 | case SSTATE_INTERNED_MORTAL: |
| 9805 | Py_REFCNT(s) += 2; |
| 9806 | mortal_size += s->length; |
| 9807 | break; |
| 9808 | default: |
| 9809 | Py_FatalError("Inconsistent interned string state."); |
| 9810 | } |
| 9811 | s->state = SSTATE_NOT_INTERNED; |
| 9812 | } |
| 9813 | fprintf(stderr, "total size of all interned strings: " |
| 9814 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9815 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9816 | Py_DECREF(keys); |
| 9817 | PyDict_Clear(interned); |
| 9818 | Py_DECREF(interned); |
| 9819 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9820 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9821 | |
| 9822 | |
| 9823 | /********************* Unicode Iterator **************************/ |
| 9824 | |
| 9825 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9826 | PyObject_HEAD |
| 9827 | Py_ssize_t it_index; |
| 9828 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9829 | } unicodeiterobject; |
| 9830 | |
| 9831 | static void |
| 9832 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9833 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9834 | _PyObject_GC_UNTRACK(it); |
| 9835 | Py_XDECREF(it->it_seq); |
| 9836 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9837 | } |
| 9838 | |
| 9839 | static int |
| 9840 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9841 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9842 | Py_VISIT(it->it_seq); |
| 9843 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9844 | } |
| 9845 | |
| 9846 | static PyObject * |
| 9847 | unicodeiter_next(unicodeiterobject *it) |
| 9848 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9849 | PyUnicodeObject *seq; |
| 9850 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9851 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9852 | assert(it != NULL); |
| 9853 | seq = it->it_seq; |
| 9854 | if (seq == NULL) |
| 9855 | return NULL; |
| 9856 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9857 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9858 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9859 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9860 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9861 | if (item != NULL) |
| 9862 | ++it->it_index; |
| 9863 | return item; |
| 9864 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9865 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9866 | Py_DECREF(seq); |
| 9867 | it->it_seq = NULL; |
| 9868 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9869 | } |
| 9870 | |
| 9871 | static PyObject * |
| 9872 | unicodeiter_len(unicodeiterobject *it) |
| 9873 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9874 | Py_ssize_t len = 0; |
| 9875 | if (it->it_seq) |
| 9876 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9877 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9878 | } |
| 9879 | |
| 9880 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9881 | |
| 9882 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9883 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9884 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9885 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9886 | }; |
| 9887 | |
| 9888 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9889 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9890 | "str_iterator", /* tp_name */ |
| 9891 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9892 | 0, /* tp_itemsize */ |
| 9893 | /* methods */ |
| 9894 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9895 | 0, /* tp_print */ |
| 9896 | 0, /* tp_getattr */ |
| 9897 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9898 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9899 | 0, /* tp_repr */ |
| 9900 | 0, /* tp_as_number */ |
| 9901 | 0, /* tp_as_sequence */ |
| 9902 | 0, /* tp_as_mapping */ |
| 9903 | 0, /* tp_hash */ |
| 9904 | 0, /* tp_call */ |
| 9905 | 0, /* tp_str */ |
| 9906 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9907 | 0, /* tp_setattro */ |
| 9908 | 0, /* tp_as_buffer */ |
| 9909 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9910 | 0, /* tp_doc */ |
| 9911 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9912 | 0, /* tp_clear */ |
| 9913 | 0, /* tp_richcompare */ |
| 9914 | 0, /* tp_weaklistoffset */ |
| 9915 | PyObject_SelfIter, /* tp_iter */ |
| 9916 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9917 | unicodeiter_methods, /* tp_methods */ |
| 9918 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9919 | }; |
| 9920 | |
| 9921 | static PyObject * |
| 9922 | unicode_iter(PyObject *seq) |
| 9923 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9924 | unicodeiterobject *it; |
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 | if (!PyUnicode_Check(seq)) { |
| 9927 | PyErr_BadInternalCall(); |
| 9928 | return NULL; |
| 9929 | } |
| 9930 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9931 | if (it == NULL) |
| 9932 | return NULL; |
| 9933 | it->it_index = 0; |
| 9934 | Py_INCREF(seq); |
| 9935 | it->it_seq = (PyUnicodeObject *)seq; |
| 9936 | _PyObject_GC_TRACK(it); |
| 9937 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9938 | } |
| 9939 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9940 | size_t |
| 9941 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9942 | { |
| 9943 | int res = 0; |
| 9944 | while(*u++) |
| 9945 | res++; |
| 9946 | return res; |
| 9947 | } |
| 9948 | |
| 9949 | Py_UNICODE* |
| 9950 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9951 | { |
| 9952 | Py_UNICODE *u = s1; |
| 9953 | while ((*u++ = *s2++)); |
| 9954 | return s1; |
| 9955 | } |
| 9956 | |
| 9957 | Py_UNICODE* |
| 9958 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9959 | { |
| 9960 | Py_UNICODE *u = s1; |
| 9961 | while ((*u++ = *s2++)) |
| 9962 | if (n-- == 0) |
| 9963 | break; |
| 9964 | return s1; |
| 9965 | } |
| 9966 | |
| 9967 | int |
| 9968 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9969 | { |
| 9970 | while (*s1 && *s2 && *s1 == *s2) |
| 9971 | s1++, s2++; |
| 9972 | if (*s1 && *s2) |
| 9973 | return (*s1 < *s2) ? -1 : +1; |
| 9974 | if (*s1) |
| 9975 | return 1; |
| 9976 | if (*s2) |
| 9977 | return -1; |
| 9978 | return 0; |
| 9979 | } |
| 9980 | |
| 9981 | Py_UNICODE* |
| 9982 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9983 | { |
| 9984 | const Py_UNICODE *p; |
| 9985 | for (p = s; *p; p++) |
| 9986 | if (*p == c) |
| 9987 | return (Py_UNICODE*)p; |
| 9988 | return NULL; |
| 9989 | } |
| 9990 | |
| 9991 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9992 | #ifdef __cplusplus |
| 9993 | } |
| 9994 | #endif |
| 9995 | |
| 9996 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9997 | /* |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9998 | Local variables: |
| 9999 | c-basic-offset: 4 |
| 10000 | indent-tabs-mode: nil |
| 10001 | End: |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 10002 | */ |