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 | |
| 157 | /* Same for linebreaks */ |
| 158 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 159 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 160 | /* 0x000A, * LINE FEED */ |
| 161 | /* 0x000D, * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 162 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 163 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 164 | /* 0x001C, * FILE SEPARATOR */ |
| 165 | /* 0x001D, * GROUP SEPARATOR */ |
| 166 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 167 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 169 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 172 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 184 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 185 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 186 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 187 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 188 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 189 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 190 | /* This is actually an illegal character, so it should |
| 191 | not be passed to unichr. */ |
| 192 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 193 | #endif |
| 194 | } |
| 195 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 196 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 197 | |
| 198 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 199 | to keep things simple, we use a single bitmask, using the least 5 |
| 200 | bits from each unicode characters as the bit index. */ |
| 201 | |
| 202 | /* the linebreak mask is set up by Unicode_Init below */ |
| 203 | |
| 204 | #define BLOOM_MASK unsigned long |
| 205 | |
| 206 | static BLOOM_MASK bloom_linebreak; |
| 207 | |
| 208 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 209 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 210 | #define BLOOM_LINEBREAK(ch) \ |
| 211 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 212 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 213 | |
| 214 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 215 | { |
| 216 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 217 | |
| 218 | long mask; |
| 219 | Py_ssize_t i; |
| 220 | |
| 221 | mask = 0; |
| 222 | for (i = 0; i < len; i++) |
| 223 | mask |= (1 << (ptr[i] & 0x1F)); |
| 224 | |
| 225 | return mask; |
| 226 | } |
| 227 | |
| 228 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 229 | { |
| 230 | Py_ssize_t i; |
| 231 | |
| 232 | for (i = 0; i < setlen; i++) |
| 233 | if (set[i] == chr) |
| 234 | return 1; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 239 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 240 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | /* --- Unicode Object ----------------------------------------------------- */ |
| 243 | |
| 244 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 245 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 246 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | { |
| 248 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 250 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 252 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 254 | /* Resizing shared object (unicode_empty or single character |
| 255 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 256 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 257 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 258 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 259 | (unicode->length == 1 && |
| 260 | unicode->str[0] < 256U && |
| 261 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 263 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | return -1; |
| 265 | } |
| 266 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 267 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 268 | The overallocation is also used by fastsearch, which assumes that it's |
| 269 | safe to look at str[length] (without making any assumptions about what |
| 270 | it contains). */ |
| 271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 272 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 273 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 274 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 275 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 276 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | PyErr_NoMemory(); |
| 278 | return -1; |
| 279 | } |
| 280 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 281 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 282 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 283 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 285 | if (unicode->defenc) { |
| 286 | Py_DECREF(unicode->defenc); |
| 287 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | } |
| 289 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* We allocate one more byte to make sure the string is |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 295 | Ux0000 terminated; some code (e.g. new_identifier) |
| 296 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | |
| 298 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 299 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 300 | |
| 301 | */ |
| 302 | |
| 303 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 304 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | { |
| 306 | register PyUnicodeObject *unicode; |
| 307 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 308 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | if (length == 0 && unicode_empty != NULL) { |
| 310 | Py_INCREF(unicode_empty); |
| 311 | return unicode_empty; |
| 312 | } |
| 313 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 314 | /* Ensure we won't overflow the size. */ |
| 315 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 316 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 317 | } |
| 318 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 319 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 320 | if (free_list) { |
| 321 | unicode = free_list; |
| 322 | free_list = *(PyUnicodeObject **)unicode; |
| 323 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 324 | if (unicode->str) { |
| 325 | /* Keep-Alive optimization: we only upsize the buffer, |
| 326 | never downsize it. */ |
| 327 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 328 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 329 | PyObject_DEL(unicode->str); |
| 330 | unicode->str = NULL; |
| 331 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 332 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 333 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 334 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 335 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 336 | } |
| 337 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 338 | } |
| 339 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 340 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 341 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 342 | if (unicode == NULL) |
| 343 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 344 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 345 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 348 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 349 | PyErr_NoMemory(); |
| 350 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 351 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 352 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 353 | * the caller fails before initializing str -- unicode_resize() |
| 354 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 355 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 356 | * We don't want unicode_resize to read uninitialized memory in |
| 357 | * that case. |
| 358 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 359 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 361 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 362 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 363 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 364 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 365 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 366 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 367 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 368 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 369 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 370 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 371 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 372 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 376 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 377 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 378 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 379 | case SSTATE_NOT_INTERNED: |
| 380 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 381 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 382 | case SSTATE_INTERNED_MORTAL: |
| 383 | /* revive dead object temporarily for DelItem */ |
| 384 | Py_REFCNT(unicode) = 3; |
| 385 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 386 | Py_FatalError( |
| 387 | "deletion of interned string failed"); |
| 388 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 390 | case SSTATE_INTERNED_IMMORTAL: |
| 391 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 392 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 393 | default: |
| 394 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 397 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 398 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 399 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 400 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 401 | PyObject_DEL(unicode->str); |
| 402 | unicode->str = NULL; |
| 403 | unicode->length = 0; |
| 404 | } |
| 405 | if (unicode->defenc) { |
| 406 | Py_DECREF(unicode->defenc); |
| 407 | unicode->defenc = NULL; |
| 408 | } |
| 409 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 410 | *(PyUnicodeObject **)unicode = free_list; |
| 411 | free_list = unicode; |
| 412 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 413 | } |
| 414 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 415 | PyObject_DEL(unicode->str); |
| 416 | Py_XDECREF(unicode->defenc); |
| 417 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 421 | static |
| 422 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 423 | { |
| 424 | register PyUnicodeObject *v; |
| 425 | |
| 426 | /* Argument checks */ |
| 427 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 428 | PyErr_BadInternalCall(); |
| 429 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 430 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 431 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 432 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
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 | } |
| 436 | |
| 437 | /* Resizing unicode_empty and single character objects is not |
| 438 | possible since these are being shared. We simply return a fresh |
| 439 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 440 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 441 | (v == unicode_empty || v->length == 1)) { |
| 442 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 443 | if (w == NULL) |
| 444 | return -1; |
| 445 | Py_UNICODE_COPY(w->str, v->str, |
| 446 | length < v->length ? length : v->length); |
| 447 | Py_DECREF(*unicode); |
| 448 | *unicode = w; |
| 449 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 453 | objects, since we can modify them in-place. */ |
| 454 | return unicode_resize(v, length); |
| 455 | } |
| 456 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 457 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 458 | { |
| 459 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 460 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 462 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 463 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 464 | { |
| 465 | PyUnicodeObject *unicode; |
| 466 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 467 | /* If the Unicode data is known at construction time, we can apply |
| 468 | some optimizations which share commonly used objects. */ |
| 469 | if (u != NULL) { |
| 470 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 471 | /* Optimization for empty strings */ |
| 472 | if (size == 0 && unicode_empty != NULL) { |
| 473 | Py_INCREF(unicode_empty); |
| 474 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 475 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 476 | |
| 477 | /* Single character Unicode objects in the Latin-1 range are |
| 478 | shared when using this constructor */ |
| 479 | if (size == 1 && *u < 256) { |
| 480 | unicode = unicode_latin1[*u]; |
| 481 | if (!unicode) { |
| 482 | unicode = _PyUnicode_New(1); |
| 483 | if (!unicode) |
| 484 | return NULL; |
| 485 | unicode->str[0] = *u; |
| 486 | unicode_latin1[*u] = unicode; |
| 487 | } |
| 488 | Py_INCREF(unicode); |
| 489 | return (PyObject *)unicode; |
| 490 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 491 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 493 | unicode = _PyUnicode_New(size); |
| 494 | if (!unicode) |
| 495 | return NULL; |
| 496 | |
| 497 | /* Copy the Unicode data into the new object */ |
| 498 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 499 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 500 | |
| 501 | return (PyObject *)unicode; |
| 502 | } |
| 503 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 504 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 505 | { |
| 506 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 507 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 508 | if (size < 0) { |
| 509 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 510 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 511 | return NULL; |
| 512 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 513 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 514 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 515 | some optimizations which share commonly used objects. |
| 516 | Also, this means the input must be UTF-8, so fall back to the |
| 517 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 518 | if (u != NULL) { |
| 519 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 520 | /* Optimization for empty strings */ |
| 521 | if (size == 0 && unicode_empty != NULL) { |
| 522 | Py_INCREF(unicode_empty); |
| 523 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 524 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 525 | |
| 526 | /* Single characters are shared when using this constructor. |
| 527 | Restrict to ASCII, since the input must be UTF-8. */ |
| 528 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 529 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 530 | if (!unicode) { |
| 531 | unicode = _PyUnicode_New(1); |
| 532 | if (!unicode) |
| 533 | return NULL; |
| 534 | unicode->str[0] = Py_CHARMASK(*u); |
| 535 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 536 | } |
| 537 | Py_INCREF(unicode); |
| 538 | return (PyObject *)unicode; |
| 539 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 540 | |
| 541 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 544 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 545 | if (!unicode) |
| 546 | return NULL; |
| 547 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 548 | return (PyObject *)unicode; |
| 549 | } |
| 550 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 551 | PyObject *PyUnicode_FromString(const char *u) |
| 552 | { |
| 553 | size_t size = strlen(u); |
| 554 | if (size > PY_SSIZE_T_MAX) { |
| 555 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | return PyUnicode_FromStringAndSize(u, size); |
| 560 | } |
| 561 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 562 | #ifdef HAVE_WCHAR_H |
| 563 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 564 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 565 | # define CONVERT_WCHAR_TO_SURROGATES |
| 566 | #endif |
| 567 | |
| 568 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 569 | |
| 570 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 571 | to convert from UTF32 to UTF16. */ |
| 572 | |
| 573 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 574 | Py_ssize_t size) |
| 575 | { |
| 576 | PyUnicodeObject *unicode; |
| 577 | register Py_ssize_t i; |
| 578 | Py_ssize_t alloc; |
| 579 | const wchar_t *orig_w; |
| 580 | |
| 581 | if (w == NULL) { |
| 582 | if (size == 0) |
| 583 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 584 | PyErr_BadInternalCall(); |
| 585 | return NULL; |
| 586 | } |
| 587 | |
| 588 | if (size == -1) { |
| 589 | size = wcslen(w); |
| 590 | } |
| 591 | |
| 592 | alloc = size; |
| 593 | orig_w = w; |
| 594 | for (i = size; i > 0; i--) { |
| 595 | if (*w > 0xFFFF) |
| 596 | alloc++; |
| 597 | w++; |
| 598 | } |
| 599 | w = orig_w; |
| 600 | unicode = _PyUnicode_New(alloc); |
| 601 | if (!unicode) |
| 602 | return NULL; |
| 603 | |
| 604 | /* Copy the wchar_t data into the new object */ |
| 605 | { |
| 606 | register Py_UNICODE *u; |
| 607 | u = PyUnicode_AS_UNICODE(unicode); |
| 608 | for (i = size; i > 0; i--) { |
| 609 | if (*w > 0xFFFF) { |
| 610 | wchar_t ordinal = *w++; |
| 611 | ordinal -= 0x10000; |
| 612 | *u++ = 0xD800 | (ordinal >> 10); |
| 613 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 614 | } |
| 615 | else |
| 616 | *u++ = *w++; |
| 617 | } |
| 618 | } |
| 619 | return (PyObject *)unicode; |
| 620 | } |
| 621 | |
| 622 | #else |
| 623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 624 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 625 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 626 | { |
| 627 | PyUnicodeObject *unicode; |
| 628 | |
| 629 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 630 | if (size == 0) |
| 631 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 632 | PyErr_BadInternalCall(); |
| 633 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 636 | if (size == -1) { |
| 637 | size = wcslen(w); |
| 638 | } |
| 639 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 640 | unicode = _PyUnicode_New(size); |
| 641 | if (!unicode) |
| 642 | return NULL; |
| 643 | |
| 644 | /* Copy the wchar_t data into the new object */ |
| 645 | #ifdef HAVE_USABLE_WCHAR_T |
| 646 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 647 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 648 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 649 | register Py_UNICODE *u; |
| 650 | register Py_ssize_t i; |
| 651 | u = PyUnicode_AS_UNICODE(unicode); |
| 652 | for (i = size; i > 0; i--) |
| 653 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 654 | } |
| 655 | #endif |
| 656 | |
| 657 | return (PyObject *)unicode; |
| 658 | } |
| 659 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 660 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 661 | |
| 662 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 663 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 664 | static void |
| 665 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 666 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 667 | *fmt++ = '%'; |
| 668 | if (width) { |
| 669 | if (zeropad) |
| 670 | *fmt++ = '0'; |
| 671 | fmt += sprintf(fmt, "%d", width); |
| 672 | } |
| 673 | if (precision) |
| 674 | fmt += sprintf(fmt, ".%d", precision); |
| 675 | if (longflag) |
| 676 | *fmt++ = 'l'; |
| 677 | else if (size_tflag) { |
| 678 | char *f = PY_FORMAT_SIZE_T; |
| 679 | while (*f) |
| 680 | *fmt++ = *f++; |
| 681 | } |
| 682 | *fmt++ = c; |
| 683 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 686 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 687 | |
| 688 | PyObject * |
| 689 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 690 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 691 | va_list count; |
| 692 | Py_ssize_t callcount = 0; |
| 693 | PyObject **callresults = NULL; |
| 694 | PyObject **callresult = NULL; |
| 695 | Py_ssize_t n = 0; |
| 696 | int width = 0; |
| 697 | int precision = 0; |
| 698 | int zeropad; |
| 699 | const char* f; |
| 700 | Py_UNICODE *s; |
| 701 | PyObject *string; |
| 702 | /* used by sprintf */ |
| 703 | char buffer[21]; |
| 704 | /* use abuffer instead of buffer, if we need more space |
| 705 | * (which can happen if there's a format specifier with width). */ |
| 706 | char *abuffer = NULL; |
| 707 | char *realbuffer; |
| 708 | Py_ssize_t abuffersize = 0; |
| 709 | char fmt[60]; /* should be enough for %0width.precisionld */ |
| 710 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 711 | |
| 712 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 713 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 714 | #else |
| 715 | #ifdef __va_copy |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 716 | __va_copy(count, vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 717 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 718 | count = vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 719 | #endif |
| 720 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 721 | /* step 1: count the number of %S/%R/%A format specifications |
| 722 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII() for |
| 723 | * these objects once during step 3 and put the result in |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 724 | an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 725 | for (f = format; *f; f++) { |
| 726 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A')) |
| 727 | ++callcount; |
| 728 | } |
| 729 | /* step 2: allocate memory for the results of |
| 730 | * PyObject_Str()/PyObject_Repr() calls */ |
| 731 | if (callcount) { |
| 732 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 733 | if (!callresults) { |
| 734 | PyErr_NoMemory(); |
| 735 | return NULL; |
| 736 | } |
| 737 | callresult = callresults; |
| 738 | } |
| 739 | /* step 3: figure out how large a buffer we need */ |
| 740 | for (f = format; *f; f++) { |
| 741 | if (*f == '%') { |
| 742 | const char* p = f; |
| 743 | width = 0; |
| 744 | while (ISDIGIT((unsigned)*f)) |
| 745 | width = (width*10) + *f++ - '0'; |
| 746 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 747 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 748 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 749 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 750 | * they don't affect the amount of space we reserve. |
| 751 | */ |
| 752 | if ((*f == 'l' || *f == 'z') && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 753 | (f[1] == 'd' || f[1] == 'u')) |
| 754 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 755 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 756 | switch (*f) { |
| 757 | case 'c': |
| 758 | (void)va_arg(count, int); |
| 759 | /* fall through... */ |
| 760 | case '%': |
| 761 | n++; |
| 762 | break; |
| 763 | case 'd': case 'u': case 'i': case 'x': |
| 764 | (void) va_arg(count, int); |
| 765 | /* 20 bytes is enough to hold a 64-bit |
| 766 | integer. Decimal takes the most space. |
| 767 | This isn't enough for octal. |
| 768 | If a width is specified we need more |
| 769 | (which we allocate later). */ |
| 770 | if (width < 20) |
| 771 | width = 20; |
| 772 | n += width; |
| 773 | if (abuffersize < width) |
| 774 | abuffersize = width; |
| 775 | break; |
| 776 | case 's': |
| 777 | { |
| 778 | /* UTF-8 */ |
| 779 | unsigned char*s; |
| 780 | s = va_arg(count, unsigned char*); |
| 781 | while (*s) { |
| 782 | if (*s < 128) { |
| 783 | n++; s++; |
| 784 | } else if (*s < 0xc0) { |
| 785 | /* invalid UTF-8 */ |
| 786 | n++; s++; |
| 787 | } else if (*s < 0xc0) { |
| 788 | n++; |
| 789 | s++; if(!*s)break; |
| 790 | s++; |
| 791 | } else if (*s < 0xe0) { |
| 792 | n++; |
| 793 | s++; if(!*s)break; |
| 794 | s++; if(!*s)break; |
| 795 | s++; |
| 796 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 797 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 798 | n++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 799 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 800 | n+=2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 801 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 802 | s++; if(!*s)break; |
| 803 | s++; if(!*s)break; |
| 804 | s++; if(!*s)break; |
| 805 | s++; |
| 806 | } |
| 807 | } |
| 808 | break; |
| 809 | } |
| 810 | case 'U': |
| 811 | { |
| 812 | PyObject *obj = va_arg(count, PyObject *); |
| 813 | assert(obj && PyUnicode_Check(obj)); |
| 814 | n += PyUnicode_GET_SIZE(obj); |
| 815 | break; |
| 816 | } |
| 817 | case 'V': |
| 818 | { |
| 819 | PyObject *obj = va_arg(count, PyObject *); |
| 820 | const char *str = va_arg(count, const char *); |
| 821 | assert(obj || str); |
| 822 | assert(!obj || PyUnicode_Check(obj)); |
| 823 | if (obj) |
| 824 | n += PyUnicode_GET_SIZE(obj); |
| 825 | else |
| 826 | n += strlen(str); |
| 827 | break; |
| 828 | } |
| 829 | case 'S': |
| 830 | { |
| 831 | PyObject *obj = va_arg(count, PyObject *); |
| 832 | PyObject *str; |
| 833 | assert(obj); |
| 834 | str = PyObject_Str(obj); |
| 835 | if (!str) |
| 836 | goto fail; |
| 837 | n += PyUnicode_GET_SIZE(str); |
| 838 | /* Remember the str and switch to the next slot */ |
| 839 | *callresult++ = str; |
| 840 | break; |
| 841 | } |
| 842 | case 'R': |
| 843 | { |
| 844 | PyObject *obj = va_arg(count, PyObject *); |
| 845 | PyObject *repr; |
| 846 | assert(obj); |
| 847 | repr = PyObject_Repr(obj); |
| 848 | if (!repr) |
| 849 | goto fail; |
| 850 | n += PyUnicode_GET_SIZE(repr); |
| 851 | /* Remember the repr and switch to the next slot */ |
| 852 | *callresult++ = repr; |
| 853 | break; |
| 854 | } |
| 855 | case 'A': |
| 856 | { |
| 857 | PyObject *obj = va_arg(count, PyObject *); |
| 858 | PyObject *ascii; |
| 859 | assert(obj); |
| 860 | ascii = PyObject_ASCII(obj); |
| 861 | if (!ascii) |
| 862 | goto fail; |
| 863 | n += PyUnicode_GET_SIZE(ascii); |
| 864 | /* Remember the repr and switch to the next slot */ |
| 865 | *callresult++ = ascii; |
| 866 | break; |
| 867 | } |
| 868 | case 'p': |
| 869 | (void) va_arg(count, int); |
| 870 | /* maximum 64-bit pointer representation: |
| 871 | * 0xffffffffffffffff |
| 872 | * so 19 characters is enough. |
| 873 | * XXX I count 18 -- what's the extra for? |
| 874 | */ |
| 875 | n += 19; |
| 876 | break; |
| 877 | default: |
| 878 | /* if we stumble upon an unknown |
| 879 | formatting code, copy the rest of |
| 880 | the format string to the output |
| 881 | string. (we cannot just skip the |
| 882 | code, since there's no way to know |
| 883 | what's in the argument list) */ |
| 884 | n += strlen(p); |
| 885 | goto expand; |
| 886 | } |
| 887 | } else |
| 888 | n++; |
| 889 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 890 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 891 | if (abuffersize > 20) { |
| 892 | abuffer = PyObject_Malloc(abuffersize); |
| 893 | if (!abuffer) { |
| 894 | PyErr_NoMemory(); |
| 895 | goto fail; |
| 896 | } |
| 897 | realbuffer = abuffer; |
| 898 | } |
| 899 | else |
| 900 | realbuffer = buffer; |
| 901 | /* step 4: fill the buffer */ |
| 902 | /* Since we've analyzed how much space we need for the worst case, |
| 903 | we don't have to resize the string. |
| 904 | There can be no errors beyond this point. */ |
| 905 | string = PyUnicode_FromUnicode(NULL, n); |
| 906 | if (!string) |
| 907 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 908 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 909 | s = PyUnicode_AS_UNICODE(string); |
| 910 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 911 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 912 | for (f = format; *f; f++) { |
| 913 | if (*f == '%') { |
| 914 | const char* p = f++; |
| 915 | int longflag = 0; |
| 916 | int size_tflag = 0; |
| 917 | zeropad = (*f == '0'); |
| 918 | /* parse the width.precision part */ |
| 919 | width = 0; |
| 920 | while (ISDIGIT((unsigned)*f)) |
| 921 | width = (width*10) + *f++ - '0'; |
| 922 | precision = 0; |
| 923 | if (*f == '.') { |
| 924 | f++; |
| 925 | while (ISDIGIT((unsigned)*f)) |
| 926 | precision = (precision*10) + *f++ - '0'; |
| 927 | } |
| 928 | /* handle the long flag, but only for %ld and %lu. |
| 929 | others can be added when necessary. */ |
| 930 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 931 | longflag = 1; |
| 932 | ++f; |
| 933 | } |
| 934 | /* handle the size_t flag. */ |
| 935 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 936 | size_tflag = 1; |
| 937 | ++f; |
| 938 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 939 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 940 | switch (*f) { |
| 941 | case 'c': |
| 942 | *s++ = va_arg(vargs, int); |
| 943 | break; |
| 944 | case 'd': |
| 945 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
| 946 | if (longflag) |
| 947 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
| 948 | else if (size_tflag) |
| 949 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 950 | else |
| 951 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 952 | appendstring(realbuffer); |
| 953 | break; |
| 954 | case 'u': |
| 955 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
| 956 | if (longflag) |
| 957 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
| 958 | else if (size_tflag) |
| 959 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 960 | else |
| 961 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 962 | appendstring(realbuffer); |
| 963 | break; |
| 964 | case 'i': |
| 965 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 966 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 967 | appendstring(realbuffer); |
| 968 | break; |
| 969 | case 'x': |
| 970 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 971 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 972 | appendstring(realbuffer); |
| 973 | break; |
| 974 | case 's': |
| 975 | { |
| 976 | /* Parameter must be UTF-8 encoded. |
| 977 | In case of encoding errors, use |
| 978 | the replacement character. */ |
| 979 | PyObject *u; |
| 980 | p = va_arg(vargs, char*); |
| 981 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 982 | "replace"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 983 | if (!u) |
| 984 | goto fail; |
| 985 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 986 | PyUnicode_GET_SIZE(u)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 987 | s += PyUnicode_GET_SIZE(u); |
| 988 | Py_DECREF(u); |
| 989 | break; |
| 990 | } |
| 991 | case 'U': |
| 992 | { |
| 993 | PyObject *obj = va_arg(vargs, PyObject *); |
| 994 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 995 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 996 | s += size; |
| 997 | break; |
| 998 | } |
| 999 | case 'V': |
| 1000 | { |
| 1001 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1002 | const char *str = va_arg(vargs, const char *); |
| 1003 | if (obj) { |
| 1004 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1005 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1006 | s += size; |
| 1007 | } else { |
| 1008 | appendstring(str); |
| 1009 | } |
| 1010 | break; |
| 1011 | } |
| 1012 | case 'S': |
| 1013 | case 'R': |
| 1014 | { |
| 1015 | Py_UNICODE *ucopy; |
| 1016 | Py_ssize_t usize; |
| 1017 | Py_ssize_t upos; |
| 1018 | /* unused, since we already have the result */ |
| 1019 | (void) va_arg(vargs, PyObject *); |
| 1020 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1021 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1022 | for (upos = 0; upos<usize;) |
| 1023 | *s++ = ucopy[upos++]; |
| 1024 | /* We're done with the unicode()/repr() => forget it */ |
| 1025 | Py_DECREF(*callresult); |
| 1026 | /* switch to next unicode()/repr() result */ |
| 1027 | ++callresult; |
| 1028 | break; |
| 1029 | } |
| 1030 | case 'p': |
| 1031 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1032 | /* %p is ill-defined: ensure leading 0x. */ |
| 1033 | if (buffer[1] == 'X') |
| 1034 | buffer[1] = 'x'; |
| 1035 | else if (buffer[1] != 'x') { |
| 1036 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1037 | buffer[0] = '0'; |
| 1038 | buffer[1] = 'x'; |
| 1039 | } |
| 1040 | appendstring(buffer); |
| 1041 | break; |
| 1042 | case '%': |
| 1043 | *s++ = '%'; |
| 1044 | break; |
| 1045 | default: |
| 1046 | appendstring(p); |
| 1047 | goto end; |
| 1048 | } |
| 1049 | } else |
| 1050 | *s++ = *f; |
| 1051 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1052 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1053 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1054 | if (callresults) |
| 1055 | PyObject_Free(callresults); |
| 1056 | if (abuffer) |
| 1057 | PyObject_Free(abuffer); |
| 1058 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1059 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1060 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1061 | if (callresults) { |
| 1062 | PyObject **callresult2 = callresults; |
| 1063 | while (callresult2 < callresult) { |
| 1064 | Py_DECREF(*callresult2); |
| 1065 | ++callresult2; |
| 1066 | } |
| 1067 | PyObject_Free(callresults); |
| 1068 | } |
| 1069 | if (abuffer) |
| 1070 | PyObject_Free(abuffer); |
| 1071 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | #undef appendstring |
| 1075 | |
| 1076 | PyObject * |
| 1077 | PyUnicode_FromFormat(const char *format, ...) |
| 1078 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1079 | PyObject* ret; |
| 1080 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1081 | |
| 1082 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1083 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1084 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1085 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1086 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1087 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1088 | va_end(vargs); |
| 1089 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1092 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1093 | wchar_t *w, |
| 1094 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1095 | { |
| 1096 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1097 | PyErr_BadInternalCall(); |
| 1098 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1099 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1100 | |
| 1101 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1102 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1103 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1104 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1105 | #ifdef HAVE_USABLE_WCHAR_T |
| 1106 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1107 | #else |
| 1108 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1109 | register Py_UNICODE *u; |
| 1110 | register Py_ssize_t i; |
| 1111 | u = PyUnicode_AS_UNICODE(unicode); |
| 1112 | for (i = size; i > 0; i--) |
| 1113 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1114 | } |
| 1115 | #endif |
| 1116 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1117 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1118 | return PyUnicode_GET_SIZE(unicode); |
| 1119 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1120 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | #endif |
| 1124 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1125 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1126 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1127 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1128 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1129 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1130 | PyErr_SetString(PyExc_ValueError, |
| 1131 | "chr() arg not in range(0x110000)"); |
| 1132 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1133 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1134 | |
| 1135 | #ifndef Py_UNICODE_WIDE |
| 1136 | if (ordinal > 0xffff) { |
| 1137 | ordinal -= 0x10000; |
| 1138 | s[0] = 0xD800 | (ordinal >> 10); |
| 1139 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1140 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1141 | } |
| 1142 | #endif |
| 1143 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1144 | s[0] = (Py_UNICODE)ordinal; |
| 1145 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1148 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1149 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1150 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1151 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1152 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1153 | Py_INCREF(obj); |
| 1154 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1155 | } |
| 1156 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1157 | /* For a Unicode subtype that's not a Unicode object, |
| 1158 | return a true Unicode object with the same data. */ |
| 1159 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1160 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1161 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1162 | PyErr_Format(PyExc_TypeError, |
| 1163 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1164 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1165 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1169 | const char *encoding, |
| 1170 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1171 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1172 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1173 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1174 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1176 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1177 | PyErr_BadInternalCall(); |
| 1178 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1179 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1180 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1181 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1182 | PyErr_SetString(PyExc_TypeError, |
| 1183 | "decoding str is not supported"); |
| 1184 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1185 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1186 | |
| 1187 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1188 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1189 | s = PyBytes_AS_STRING(obj); |
| 1190 | len = PyBytes_GET_SIZE(obj); |
| 1191 | } |
| 1192 | else if (PyByteArray_Check(obj)) { |
| 1193 | s = PyByteArray_AS_STRING(obj); |
| 1194 | len = PyByteArray_GET_SIZE(obj); |
| 1195 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1196 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1197 | /* Overwrite the error message with something more useful in |
| 1198 | case of a TypeError. */ |
| 1199 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1200 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1201 | "coercing to str: need string or buffer, " |
| 1202 | "%.80s found", |
| 1203 | Py_TYPE(obj)->tp_name); |
| 1204 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1205 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1206 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1207 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1208 | if (len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1209 | Py_INCREF(unicode_empty); |
| 1210 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1211 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1212 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1213 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1214 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1215 | return v; |
| 1216 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1217 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1218 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1222 | Py_ssize_t size, |
| 1223 | const char *encoding, |
| 1224 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1225 | { |
| 1226 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1227 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1228 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1229 | char *l; |
| 1230 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1231 | |
| 1232 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1233 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1234 | |
| 1235 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1236 | catch e.g. UTF_8 */ |
| 1237 | e = encoding; |
| 1238 | l = lower; |
| 1239 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1240 | if (ISUPPER(*e)) { |
| 1241 | *l++ = TOLOWER(*e++); |
| 1242 | } |
| 1243 | else if (*e == '_') { |
| 1244 | *l++ = '-'; |
| 1245 | e++; |
| 1246 | } |
| 1247 | else { |
| 1248 | *l++ = *e++; |
| 1249 | } |
| 1250 | } |
| 1251 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1252 | |
| 1253 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1254 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1255 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1256 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1257 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1258 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1259 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1260 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1261 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1262 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1263 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1264 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1265 | else if (strcmp(lower, "utf-16") == 0) |
| 1266 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1267 | else if (strcmp(lower, "utf-32") == 0) |
| 1268 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1269 | |
| 1270 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1271 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1272 | 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] | 1273 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1274 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1275 | if (buffer == NULL) |
| 1276 | goto onError; |
| 1277 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1278 | if (unicode == NULL) |
| 1279 | goto onError; |
| 1280 | if (!PyUnicode_Check(unicode)) { |
| 1281 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1282 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1283 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1284 | Py_DECREF(unicode); |
| 1285 | goto onError; |
| 1286 | } |
| 1287 | Py_DECREF(buffer); |
| 1288 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1289 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1290 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1291 | Py_XDECREF(buffer); |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1295 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1296 | const char *encoding, |
| 1297 | const char *errors) |
| 1298 | { |
| 1299 | PyObject *v; |
| 1300 | |
| 1301 | if (!PyUnicode_Check(unicode)) { |
| 1302 | PyErr_BadArgument(); |
| 1303 | goto onError; |
| 1304 | } |
| 1305 | |
| 1306 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1307 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1308 | |
| 1309 | /* Decode via the codec registry */ |
| 1310 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1311 | if (v == NULL) |
| 1312 | goto onError; |
| 1313 | return v; |
| 1314 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1315 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1316 | return NULL; |
| 1317 | } |
| 1318 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1319 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1320 | const char *encoding, |
| 1321 | const char *errors) |
| 1322 | { |
| 1323 | PyObject *v; |
| 1324 | |
| 1325 | if (!PyUnicode_Check(unicode)) { |
| 1326 | PyErr_BadArgument(); |
| 1327 | goto onError; |
| 1328 | } |
| 1329 | |
| 1330 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1331 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1332 | |
| 1333 | /* Decode via the codec registry */ |
| 1334 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1335 | if (v == NULL) |
| 1336 | goto onError; |
| 1337 | if (!PyUnicode_Check(v)) { |
| 1338 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1339 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1340 | Py_TYPE(v)->tp_name); |
| 1341 | Py_DECREF(v); |
| 1342 | goto onError; |
| 1343 | } |
| 1344 | return v; |
| 1345 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1346 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1347 | return NULL; |
| 1348 | } |
| 1349 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1350 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1351 | Py_ssize_t size, |
| 1352 | const char *encoding, |
| 1353 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1354 | { |
| 1355 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1356 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1357 | unicode = PyUnicode_FromUnicode(s, size); |
| 1358 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1359 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1360 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1361 | Py_DECREF(unicode); |
| 1362 | return v; |
| 1363 | } |
| 1364 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1365 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1366 | const char *encoding, |
| 1367 | const char *errors) |
| 1368 | { |
| 1369 | PyObject *v; |
| 1370 | |
| 1371 | if (!PyUnicode_Check(unicode)) { |
| 1372 | PyErr_BadArgument(); |
| 1373 | goto onError; |
| 1374 | } |
| 1375 | |
| 1376 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1377 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1378 | |
| 1379 | /* Encode via the codec registry */ |
| 1380 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1381 | if (v == NULL) |
| 1382 | goto onError; |
| 1383 | return v; |
| 1384 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1385 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1386 | return NULL; |
| 1387 | } |
| 1388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1389 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1390 | const char *encoding, |
| 1391 | const char *errors) |
| 1392 | { |
| 1393 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1394 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1395 | if (!PyUnicode_Check(unicode)) { |
| 1396 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1397 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1398 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1399 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1400 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1401 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1402 | |
| 1403 | /* Shortcuts for common default encodings */ |
| 1404 | if (errors == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1405 | if (strcmp(encoding, "utf-8") == 0) |
| 1406 | return PyUnicode_AsUTF8String(unicode); |
| 1407 | else if (strcmp(encoding, "latin-1") == 0) |
| 1408 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1409 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1410 | else if (strcmp(encoding, "mbcs") == 0) |
| 1411 | return PyUnicode_AsMBCSString(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1412 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1413 | else if (strcmp(encoding, "ascii") == 0) |
| 1414 | return PyUnicode_AsASCIIString(unicode); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1415 | /* During bootstrap, we may need to find the encodings |
| 1416 | package, to load the file system encoding, and require the |
| 1417 | file system encoding in order to load the encodings |
| 1418 | package. |
| 1419 | |
| 1420 | Break out of this dependency by assuming that the path to |
| 1421 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1422 | instead, if the file system encoding is the locale's |
| 1423 | encoding. */ |
| 1424 | else if (Py_FileSystemDefaultEncoding && |
| 1425 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1426 | !PyThreadState_GET()->interp->codecs_initialized) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1427 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1428 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1429 | |
| 1430 | /* Encode via the codec registry */ |
| 1431 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1432 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1433 | return NULL; |
| 1434 | |
| 1435 | /* The normal path */ |
| 1436 | if (PyBytes_Check(v)) |
| 1437 | return v; |
| 1438 | |
| 1439 | /* 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] | 1440 | if (PyByteArray_Check(v)) { |
| 1441 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1442 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1443 | PyOS_snprintf(msg, sizeof(msg), |
| 1444 | "encoder %s returned buffer instead of bytes", |
| 1445 | encoding); |
| 1446 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1447 | Py_DECREF(v); |
| 1448 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1449 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1450 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1451 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1452 | Py_DECREF(v); |
| 1453 | return b; |
| 1454 | } |
| 1455 | |
| 1456 | PyErr_Format(PyExc_TypeError, |
| 1457 | "encoder did not return a bytes object (type=%.400s)", |
| 1458 | Py_TYPE(v)->tp_name); |
| 1459 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1460 | return NULL; |
| 1461 | } |
| 1462 | |
| 1463 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1464 | const char *encoding, |
| 1465 | const char *errors) |
| 1466 | { |
| 1467 | PyObject *v; |
| 1468 | |
| 1469 | if (!PyUnicode_Check(unicode)) { |
| 1470 | PyErr_BadArgument(); |
| 1471 | goto onError; |
| 1472 | } |
| 1473 | |
| 1474 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1475 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1476 | |
| 1477 | /* Encode via the codec registry */ |
| 1478 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1479 | if (v == NULL) |
| 1480 | goto onError; |
| 1481 | if (!PyUnicode_Check(v)) { |
| 1482 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1483 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1484 | Py_TYPE(v)->tp_name); |
| 1485 | Py_DECREF(v); |
| 1486 | goto onError; |
| 1487 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1488 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1489 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1490 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1491 | return NULL; |
| 1492 | } |
| 1493 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1494 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1495 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1496 | { |
| 1497 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1498 | if (v) |
| 1499 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1500 | if (errors != NULL) |
| 1501 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1502 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1503 | PyUnicode_GET_SIZE(unicode), |
| 1504 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1505 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1506 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1507 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1508 | return v; |
| 1509 | } |
| 1510 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1511 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1512 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1513 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1514 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1515 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1516 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1517 | PyObject* |
| 1518 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1519 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1520 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1521 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1522 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1523 | bootstrapping process where the codecs aren't ready yet. |
| 1524 | */ |
| 1525 | if (Py_FileSystemDefaultEncoding) { |
| 1526 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1527 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1528 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1529 | } |
| 1530 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1531 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1532 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1533 | } |
| 1534 | #endif |
| 1535 | return PyUnicode_Decode(s, size, |
| 1536 | Py_FileSystemDefaultEncoding, |
| 1537 | "replace"); |
| 1538 | } |
| 1539 | else { |
| 1540 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1541 | } |
| 1542 | } |
| 1543 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1544 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1545 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1546 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1547 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1548 | if (!PyUnicode_Check(unicode)) { |
| 1549 | PyErr_BadArgument(); |
| 1550 | return NULL; |
| 1551 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1552 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1553 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1554 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1555 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1556 | *psize = PyBytes_GET_SIZE(bytes); |
| 1557 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1561 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1562 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1563 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1566 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1567 | { |
| 1568 | if (!PyUnicode_Check(unicode)) { |
| 1569 | PyErr_BadArgument(); |
| 1570 | goto onError; |
| 1571 | } |
| 1572 | return PyUnicode_AS_UNICODE(unicode); |
| 1573 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1574 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1575 | return NULL; |
| 1576 | } |
| 1577 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1578 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1579 | { |
| 1580 | if (!PyUnicode_Check(unicode)) { |
| 1581 | PyErr_BadArgument(); |
| 1582 | goto onError; |
| 1583 | } |
| 1584 | return PyUnicode_GET_SIZE(unicode); |
| 1585 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1586 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1587 | return -1; |
| 1588 | } |
| 1589 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1590 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1591 | { |
| 1592 | return unicode_default_encoding; |
| 1593 | } |
| 1594 | |
| 1595 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1596 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1597 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1598 | PyErr_Format(PyExc_ValueError, |
| 1599 | "Can only set default encoding to %s", |
| 1600 | unicode_default_encoding); |
| 1601 | return -1; |
| 1602 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1603 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1606 | /* error handling callback helper: |
| 1607 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1608 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1609 | and adjust various state variables. |
| 1610 | return 0 on success, -1 on error |
| 1611 | */ |
| 1612 | |
| 1613 | static |
| 1614 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1615 | const char *encoding, const char *reason, |
| 1616 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1617 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1618 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1619 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1620 | 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] | 1621 | |
| 1622 | PyObject *restuple = NULL; |
| 1623 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1624 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1625 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1626 | Py_ssize_t requiredsize; |
| 1627 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1628 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1629 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1630 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1631 | int res = -1; |
| 1632 | |
| 1633 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1634 | *errorHandler = PyCodec_LookupError(errors); |
| 1635 | if (*errorHandler == NULL) |
| 1636 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1640 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1641 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
| 1642 | if (*exceptionObject == NULL) |
| 1643 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1644 | } |
| 1645 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1646 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1647 | goto onError; |
| 1648 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1649 | goto onError; |
| 1650 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1651 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1655 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1656 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1657 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1658 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1659 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1660 | } |
| 1661 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1662 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1663 | |
| 1664 | /* Copy back the bytes variables, which might have been modified by the |
| 1665 | callback */ |
| 1666 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1667 | if (!inputobj) |
| 1668 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1669 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1670 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1671 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1672 | *input = PyBytes_AS_STRING(inputobj); |
| 1673 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1674 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1675 | /* we can DECREF safely, as the exception has another reference, |
| 1676 | so the object won't go away. */ |
| 1677 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1678 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1679 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1680 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1681 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1682 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1683 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1684 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1685 | |
| 1686 | /* need more space? (at least enough for what we |
| 1687 | have+the replacement+the rest of the string (starting |
| 1688 | at the new input position), so we won't have to check space |
| 1689 | when there are no errors in the rest of the string) */ |
| 1690 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1691 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1692 | requiredsize = *outpos + repsize + insize-newpos; |
| 1693 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1694 | if (requiredsize<2*outsize) |
| 1695 | requiredsize = 2*outsize; |
| 1696 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1697 | goto onError; |
| 1698 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1699 | } |
| 1700 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1701 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1702 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1703 | *outptr += repsize; |
| 1704 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1705 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1706 | /* we made it! */ |
| 1707 | res = 0; |
| 1708 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1709 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1710 | Py_XDECREF(restuple); |
| 1711 | return res; |
| 1712 | } |
| 1713 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1714 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1715 | |
| 1716 | /* see RFC2152 for details */ |
| 1717 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1718 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1719 | char utf7_special[128] = { |
| 1720 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1721 | encoded: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1722 | 0 - not special |
| 1723 | 1 - special |
| 1724 | 2 - whitespace (optional) |
| 1725 | 3 - RFC2152 Set O (optional) */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1726 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1727 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1728 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1729 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1730 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1731 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1732 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1733 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1734 | |
| 1735 | }; |
| 1736 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1737 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1738 | warnings about the comparison always being false; since |
| 1739 | utf7_special[0] is 1, we can safely make that one comparison |
| 1740 | true */ |
| 1741 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1742 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1743 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1744 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1745 | (encodeO && (utf7_special[(c)] == 3))) |
| 1746 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1747 | #define B64(n) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1748 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1749 | #define B64CHAR(c) \ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1750 | (ISALNUM(c) || (c) == '+' || (c) == '/') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1751 | #define UB64(c) \ |
| 1752 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1753 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1754 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1755 | #define ENCODE(out, ch, bits) \ |
| 1756 | while (bits >= 6) { \ |
| 1757 | *out++ = B64(ch >> (bits-6)); \ |
| 1758 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1761 | #define DECODE(out, ch, bits, surrogate) \ |
| 1762 | while (bits >= 16) { \ |
| 1763 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1764 | bits -= 16; \ |
| 1765 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1766 | /* We have already generated an error for the high surrogate \ |
| 1767 | so let's not bother seeing if the low surrogate is correct or not */ \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1768 | surrogate = 0; \ |
| 1769 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1770 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1771 | it in a 16-bit character */ \ |
| 1772 | surrogate = 1; \ |
| 1773 | errmsg = "code pairs are not supported"; \ |
| 1774 | goto utf7Error; \ |
| 1775 | } else { \ |
| 1776 | *out++ = outCh; \ |
| 1777 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1778 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1779 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1780 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1781 | Py_ssize_t size, |
| 1782 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1783 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1784 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1785 | } |
| 1786 | |
| 1787 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1788 | Py_ssize_t size, |
| 1789 | const char *errors, |
| 1790 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1791 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1792 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1793 | Py_ssize_t startinpos; |
| 1794 | Py_ssize_t endinpos; |
| 1795 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1796 | const char *e; |
| 1797 | PyUnicodeObject *unicode; |
| 1798 | Py_UNICODE *p; |
| 1799 | const char *errmsg = ""; |
| 1800 | int inShift = 0; |
| 1801 | unsigned int bitsleft = 0; |
| 1802 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1803 | int surrogate = 0; |
| 1804 | PyObject *errorHandler = NULL; |
| 1805 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1806 | |
| 1807 | unicode = _PyUnicode_New(size); |
| 1808 | if (!unicode) |
| 1809 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1810 | if (size == 0) { |
| 1811 | if (consumed) |
| 1812 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1813 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1814 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1815 | |
| 1816 | p = unicode->str; |
| 1817 | e = s + size; |
| 1818 | |
| 1819 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1820 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1821 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1822 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1823 | |
| 1824 | if (inShift) { |
| 1825 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1826 | inShift = 0; |
| 1827 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1828 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1829 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1830 | if (bitsleft >= 6) { |
| 1831 | /* The shift sequence has a partial character in it. If |
| 1832 | bitsleft < 6 then we could just classify it as padding |
| 1833 | but that is not the case here */ |
| 1834 | |
| 1835 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1836 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1837 | } |
| 1838 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1839 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1840 | here so indicate the potential of a misencoded character. */ |
| 1841 | |
| 1842 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1843 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1844 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1845 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | if (ch == '-') { |
| 1849 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1850 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1851 | inShift = 1; |
| 1852 | } |
| 1853 | } else if (SPECIAL(ch,0,0)) { |
| 1854 | errmsg = "unexpected special character"; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1855 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1856 | } else { |
| 1857 | *p++ = ch; |
| 1858 | } |
| 1859 | } else { |
| 1860 | charsleft = (charsleft << 6) | UB64(ch); |
| 1861 | bitsleft += 6; |
| 1862 | s++; |
| 1863 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1864 | } |
| 1865 | } |
| 1866 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1867 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1868 | s++; |
| 1869 | if (s < e && *s == '-') { |
| 1870 | s++; |
| 1871 | *p++ = '+'; |
| 1872 | } else |
| 1873 | { |
| 1874 | inShift = 1; |
| 1875 | bitsleft = 0; |
| 1876 | } |
| 1877 | } |
| 1878 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1879 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1880 | errmsg = "unexpected special character"; |
| 1881 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1882 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1883 | } |
| 1884 | else { |
| 1885 | *p++ = ch; |
| 1886 | s++; |
| 1887 | } |
| 1888 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1889 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1890 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1891 | endinpos = s-starts; |
| 1892 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1893 | errors, &errorHandler, |
| 1894 | "utf7", errmsg, |
| 1895 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 1896 | &unicode, &outpos, &p)) |
| 1897 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1900 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1901 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1902 | endinpos = size; |
| 1903 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1904 | errors, &errorHandler, |
| 1905 | "utf7", "unterminated shift sequence", |
| 1906 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 1907 | &unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1908 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1909 | if (s < e) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1910 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1911 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1912 | if (consumed) { |
| 1913 | if(inShift) |
| 1914 | *consumed = startinpos; |
| 1915 | else |
| 1916 | *consumed = s-starts; |
| 1917 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1918 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1919 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1920 | goto onError; |
| 1921 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1922 | Py_XDECREF(errorHandler); |
| 1923 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1924 | return (PyObject *)unicode; |
| 1925 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1926 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1927 | Py_XDECREF(errorHandler); |
| 1928 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1929 | Py_DECREF(unicode); |
| 1930 | return NULL; |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1935 | Py_ssize_t size, |
| 1936 | int encodeSetO, |
| 1937 | int encodeWhiteSpace, |
| 1938 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1939 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1940 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1941 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1942 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1943 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1944 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1945 | unsigned int bitsleft = 0; |
| 1946 | unsigned long charsleft = 0; |
| 1947 | char * out; |
| 1948 | char * start; |
| 1949 | |
| 1950 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1951 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1952 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 1953 | if (cbAllocated / 5 != size) |
| 1954 | return PyErr_NoMemory(); |
| 1955 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1956 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1957 | if (v == NULL) |
| 1958 | return NULL; |
| 1959 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1960 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1961 | for (;i < size; ++i) { |
| 1962 | Py_UNICODE ch = s[i]; |
| 1963 | |
| 1964 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1965 | if (ch == '+') { |
| 1966 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1967 | *out++ = '-'; |
| 1968 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1969 | charsleft = ch; |
| 1970 | bitsleft = 16; |
| 1971 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1972 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1973 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1974 | } else { |
| 1975 | *out++ = (char) ch; |
| 1976 | } |
| 1977 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1978 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1979 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1980 | charsleft = 0; |
| 1981 | bitsleft = 0; |
| 1982 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1983 | so no '-' is required, except if the character is itself a '-' */ |
| 1984 | if (B64CHAR(ch) || ch == '-') { |
| 1985 | *out++ = '-'; |
| 1986 | } |
| 1987 | inShift = 0; |
| 1988 | *out++ = (char) ch; |
| 1989 | } else { |
| 1990 | bitsleft += 16; |
| 1991 | charsleft = (charsleft << 16) | ch; |
| 1992 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1993 | |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 1994 | /* If the next character is special then we don't need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1995 | the shift sequence. If the next character is not a BASE64 character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1996 | or '-' then the shift sequence will be terminated implicitly and we |
| 1997 | don't have to insert a '-'. */ |
| 1998 | |
| 1999 | if (bitsleft == 0) { |
| 2000 | if (i + 1 < size) { |
| 2001 | Py_UNICODE ch2 = s[i+1]; |
| 2002 | |
| 2003 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2004 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2005 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 2006 | *out++ = '-'; |
| 2007 | inShift = 0; |
| 2008 | } else { |
| 2009 | inShift = 0; |
| 2010 | } |
| 2011 | |
| 2012 | } |
| 2013 | else { |
| 2014 | *out++ = '-'; |
| 2015 | inShift = 0; |
| 2016 | } |
| 2017 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2018 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2019 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2020 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2021 | if (bitsleft) { |
| 2022 | *out++= B64(charsleft << (6-bitsleft) ); |
| 2023 | *out++ = '-'; |
| 2024 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2025 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2026 | return NULL; |
| 2027 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | #undef SPECIAL |
| 2031 | #undef B64 |
| 2032 | #undef B64CHAR |
| 2033 | #undef UB64 |
| 2034 | #undef ENCODE |
| 2035 | #undef DECODE |
| 2036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2037 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2038 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2039 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2040 | char utf8_code_length[256] = { |
| 2041 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 2042 | illegal prefix. see RFC 2279 for details */ |
| 2043 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2044 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2045 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2046 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2047 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2048 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2049 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2050 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2051 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2052 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2053 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2054 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2055 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2056 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2057 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2058 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 2059 | }; |
| 2060 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2061 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2062 | Py_ssize_t size, |
| 2063 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2064 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2065 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2066 | } |
| 2067 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2068 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2069 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2070 | |
| 2071 | /* Mask to quickly check whether a C 'long' contains a |
| 2072 | non-ASCII, UTF8-encoded char. */ |
| 2073 | #if (SIZEOF_LONG == 8) |
| 2074 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2075 | #elif (SIZEOF_LONG == 4) |
| 2076 | # define ASCII_CHAR_MASK 0x80808080L |
| 2077 | #else |
| 2078 | # error C 'long' size should be either 4 or 8! |
| 2079 | #endif |
| 2080 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2081 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2082 | Py_ssize_t size, |
| 2083 | const char *errors, |
| 2084 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2085 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2086 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2087 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2088 | Py_ssize_t startinpos; |
| 2089 | Py_ssize_t endinpos; |
| 2090 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2091 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2092 | PyUnicodeObject *unicode; |
| 2093 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2094 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2095 | PyObject *errorHandler = NULL; |
| 2096 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2097 | |
| 2098 | /* Note: size will always be longer than the resulting Unicode |
| 2099 | character count */ |
| 2100 | unicode = _PyUnicode_New(size); |
| 2101 | if (!unicode) |
| 2102 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2103 | if (size == 0) { |
| 2104 | if (consumed) |
| 2105 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2106 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2107 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2108 | |
| 2109 | /* Unpack UTF-8 encoded data */ |
| 2110 | p = unicode->str; |
| 2111 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2112 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2113 | |
| 2114 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2115 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2116 | |
| 2117 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2118 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2119 | input will consist of an overwhelming majority of ASCII |
| 2120 | characters, we try to optimize for this case by checking |
| 2121 | as many characters as a C 'long' can contain. |
| 2122 | First, check if we can do an aligned read, as most CPUs have |
| 2123 | a penalty for unaligned reads. |
| 2124 | */ |
| 2125 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2126 | /* Help register allocation */ |
| 2127 | register const char *_s = s; |
| 2128 | register Py_UNICODE *_p = p; |
| 2129 | while (_s < aligned_end) { |
| 2130 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2131 | and do a fast unrolled copy if it only contains ASCII |
| 2132 | characters. */ |
| 2133 | unsigned long data = *(unsigned long *) _s; |
| 2134 | if (data & ASCII_CHAR_MASK) |
| 2135 | break; |
| 2136 | _p[0] = (unsigned char) _s[0]; |
| 2137 | _p[1] = (unsigned char) _s[1]; |
| 2138 | _p[2] = (unsigned char) _s[2]; |
| 2139 | _p[3] = (unsigned char) _s[3]; |
| 2140 | #if (SIZEOF_LONG == 8) |
| 2141 | _p[4] = (unsigned char) _s[4]; |
| 2142 | _p[5] = (unsigned char) _s[5]; |
| 2143 | _p[6] = (unsigned char) _s[6]; |
| 2144 | _p[7] = (unsigned char) _s[7]; |
| 2145 | #endif |
| 2146 | _s += SIZEOF_LONG; |
| 2147 | _p += SIZEOF_LONG; |
| 2148 | } |
| 2149 | s = _s; |
| 2150 | p = _p; |
| 2151 | if (s == e) |
| 2152 | break; |
| 2153 | ch = (unsigned char)*s; |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2158 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2159 | s++; |
| 2160 | continue; |
| 2161 | } |
| 2162 | |
| 2163 | n = utf8_code_length[ch]; |
| 2164 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2165 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2166 | if (consumed) |
| 2167 | break; |
| 2168 | else { |
| 2169 | errmsg = "unexpected end of data"; |
| 2170 | startinpos = s-starts; |
| 2171 | endinpos = size; |
| 2172 | goto utf8Error; |
| 2173 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2174 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2175 | |
| 2176 | switch (n) { |
| 2177 | |
| 2178 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2179 | errmsg = "unexpected code byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2180 | startinpos = s-starts; |
| 2181 | endinpos = startinpos+1; |
| 2182 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2183 | |
| 2184 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2185 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2186 | startinpos = s-starts; |
| 2187 | endinpos = startinpos+1; |
| 2188 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2189 | |
| 2190 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2191 | if ((s[1] & 0xc0) != 0x80) { |
| 2192 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2193 | startinpos = s-starts; |
| 2194 | endinpos = startinpos+2; |
| 2195 | goto utf8Error; |
| 2196 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2197 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2198 | if (ch < 0x80) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2199 | startinpos = s-starts; |
| 2200 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2201 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2202 | goto utf8Error; |
| 2203 | } |
| 2204 | else |
| 2205 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | |
| 2208 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2209 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2210 | (s[2] & 0xc0) != 0x80) { |
| 2211 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2212 | startinpos = s-starts; |
| 2213 | endinpos = startinpos+3; |
| 2214 | goto utf8Error; |
| 2215 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2216 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2217 | if (ch < 0x0800) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2218 | /* Note: UTF-8 encodings of surrogates are considered |
| 2219 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2220 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2221 | XXX For wide builds (UCS-4) we should probably try |
| 2222 | to recombine the surrogates into a single code |
| 2223 | unit. |
| 2224 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2225 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2226 | startinpos = s-starts; |
| 2227 | endinpos = startinpos+3; |
| 2228 | goto utf8Error; |
| 2229 | } |
| 2230 | else |
| 2231 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2232 | break; |
| 2233 | |
| 2234 | case 4: |
| 2235 | if ((s[1] & 0xc0) != 0x80 || |
| 2236 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2237 | (s[3] & 0xc0) != 0x80) { |
| 2238 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2239 | startinpos = s-starts; |
| 2240 | endinpos = startinpos+4; |
| 2241 | goto utf8Error; |
| 2242 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2243 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2244 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2245 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2246 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2247 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2248 | || (ch > 0x10ffff)) /* maximum value allowed for |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2249 | UTF-16 */ |
| 2250 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2251 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2252 | startinpos = s-starts; |
| 2253 | endinpos = startinpos+4; |
| 2254 | goto utf8Error; |
| 2255 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2256 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2257 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2258 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2259 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2260 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2261 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2262 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2263 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2264 | /* high surrogate = top 10 bits added to D800 */ |
| 2265 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2266 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2267 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2268 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2269 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2270 | break; |
| 2271 | |
| 2272 | default: |
| 2273 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2274 | errmsg = "unsupported Unicode code range"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2275 | startinpos = s-starts; |
| 2276 | endinpos = startinpos+n; |
| 2277 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2278 | } |
| 2279 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2280 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2281 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2282 | utf8Error: |
| 2283 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2284 | if (unicode_decode_call_errorhandler( |
| 2285 | errors, &errorHandler, |
| 2286 | "utf8", errmsg, |
| 2287 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2288 | &unicode, &outpos, &p)) |
| 2289 | goto onError; |
| 2290 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2292 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2293 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2294 | |
| 2295 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2296 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 | goto onError; |
| 2298 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2299 | Py_XDECREF(errorHandler); |
| 2300 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2301 | return (PyObject *)unicode; |
| 2302 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2303 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2304 | Py_XDECREF(errorHandler); |
| 2305 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2306 | Py_DECREF(unicode); |
| 2307 | return NULL; |
| 2308 | } |
| 2309 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2310 | #undef ASCII_CHAR_MASK |
| 2311 | |
| 2312 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2313 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2314 | and allocate exactly as much space needed at the end. Else allocate the |
| 2315 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2316 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2317 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2318 | PyObject * |
| 2319 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2320 | Py_ssize_t size, |
| 2321 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2322 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2323 | #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] | 2324 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2325 | Py_ssize_t i; /* index into s of next input byte */ |
| 2326 | PyObject *result; /* result string object */ |
| 2327 | char *p; /* next free byte in output buffer */ |
| 2328 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2329 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2330 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2331 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2332 | assert(s != NULL); |
| 2333 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2334 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2335 | if (size <= MAX_SHORT_UNICHARS) { |
| 2336 | /* Write into the stack buffer; nallocated can't overflow. |
| 2337 | * At the end, we'll allocate exactly as much heap space as it |
| 2338 | * turns out we need. |
| 2339 | */ |
| 2340 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2341 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2342 | p = stackbuf; |
| 2343 | } |
| 2344 | else { |
| 2345 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2346 | nallocated = size * 4; |
| 2347 | if (nallocated / 4 != size) /* overflow! */ |
| 2348 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2349 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2350 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2351 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2352 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2353 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2354 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2355 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2356 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2357 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2358 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2359 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2360 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2361 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2362 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2363 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2364 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2365 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2366 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2367 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2368 | /* Encode UCS2 Unicode ordinals */ |
| 2369 | if (ch < 0x10000) { |
| 2370 | /* Special case: check for high surrogate */ |
| 2371 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2372 | Py_UCS4 ch2 = s[i]; |
| 2373 | /* Check for low surrogate and combine the two to |
| 2374 | form a UCS4 value */ |
| 2375 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2376 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2377 | i++; |
| 2378 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2379 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2380 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2381 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2382 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2383 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2384 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2385 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2386 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2387 | encodeUCS4: |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2388 | /* Encode UCS4 Unicode ordinals */ |
| 2389 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2390 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2391 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2392 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2393 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2394 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2395 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2396 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2397 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2398 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2399 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2400 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2401 | } |
| 2402 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2403 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2404 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2405 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2406 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2407 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2408 | return result; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2409 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2410 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2413 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2414 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2415 | if (!PyUnicode_Check(unicode)) { |
| 2416 | PyErr_BadArgument(); |
| 2417 | return NULL; |
| 2418 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2419 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2420 | PyUnicode_GET_SIZE(unicode), |
| 2421 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2424 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2425 | |
| 2426 | PyObject * |
| 2427 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2428 | Py_ssize_t size, |
| 2429 | const char *errors, |
| 2430 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2431 | { |
| 2432 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2433 | } |
| 2434 | |
| 2435 | PyObject * |
| 2436 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2437 | Py_ssize_t size, |
| 2438 | const char *errors, |
| 2439 | int *byteorder, |
| 2440 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2441 | { |
| 2442 | const char *starts = s; |
| 2443 | Py_ssize_t startinpos; |
| 2444 | Py_ssize_t endinpos; |
| 2445 | Py_ssize_t outpos; |
| 2446 | PyUnicodeObject *unicode; |
| 2447 | Py_UNICODE *p; |
| 2448 | #ifndef Py_UNICODE_WIDE |
| 2449 | int i, pairs; |
| 2450 | #else |
| 2451 | const int pairs = 0; |
| 2452 | #endif |
| 2453 | const unsigned char *q, *e; |
| 2454 | int bo = 0; /* assume native ordering by default */ |
| 2455 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2456 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2457 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2458 | int iorder[] = {0, 1, 2, 3}; |
| 2459 | #else |
| 2460 | int iorder[] = {3, 2, 1, 0}; |
| 2461 | #endif |
| 2462 | PyObject *errorHandler = NULL; |
| 2463 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2464 | /* On narrow builds we split characters outside the BMP into two |
| 2465 | codepoints => count how much extra space we need. */ |
| 2466 | #ifndef Py_UNICODE_WIDE |
| 2467 | for (i = pairs = 0; i < size/4; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2468 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2469 | pairs++; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2470 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2471 | |
| 2472 | /* This might be one to much, because of a BOM */ |
| 2473 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2474 | if (!unicode) |
| 2475 | return NULL; |
| 2476 | if (size == 0) |
| 2477 | return (PyObject *)unicode; |
| 2478 | |
| 2479 | /* Unpack UTF-32 encoded data */ |
| 2480 | p = unicode->str; |
| 2481 | q = (unsigned char *)s; |
| 2482 | e = q + size; |
| 2483 | |
| 2484 | if (byteorder) |
| 2485 | bo = *byteorder; |
| 2486 | |
| 2487 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2488 | byte order setting accordingly. In native mode, the leading BOM |
| 2489 | mark is skipped, in all other modes, it is copied to the output |
| 2490 | stream as-is (giving a ZWNBSP character). */ |
| 2491 | if (bo == 0) { |
| 2492 | if (size >= 4) { |
| 2493 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2494 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2495 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2496 | if (bom == 0x0000FEFF) { |
| 2497 | q += 4; |
| 2498 | bo = -1; |
| 2499 | } |
| 2500 | else if (bom == 0xFFFE0000) { |
| 2501 | q += 4; |
| 2502 | bo = 1; |
| 2503 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2504 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2505 | if (bom == 0x0000FEFF) { |
| 2506 | q += 4; |
| 2507 | bo = 1; |
| 2508 | } |
| 2509 | else if (bom == 0xFFFE0000) { |
| 2510 | q += 4; |
| 2511 | bo = -1; |
| 2512 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2513 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2514 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | if (bo == -1) { |
| 2518 | /* force LE */ |
| 2519 | iorder[0] = 0; |
| 2520 | iorder[1] = 1; |
| 2521 | iorder[2] = 2; |
| 2522 | iorder[3] = 3; |
| 2523 | } |
| 2524 | else if (bo == 1) { |
| 2525 | /* force BE */ |
| 2526 | iorder[0] = 3; |
| 2527 | iorder[1] = 2; |
| 2528 | iorder[2] = 1; |
| 2529 | iorder[3] = 0; |
| 2530 | } |
| 2531 | |
| 2532 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2533 | Py_UCS4 ch; |
| 2534 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2535 | if (e-q<4) { |
| 2536 | if (consumed) |
| 2537 | break; |
| 2538 | errmsg = "truncated data"; |
| 2539 | startinpos = ((const char *)q)-starts; |
| 2540 | endinpos = ((const char *)e)-starts; |
| 2541 | goto utf32Error; |
| 2542 | /* The remaining input chars are ignored if the callback |
| 2543 | chooses to skip the input */ |
| 2544 | } |
| 2545 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2546 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2547 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2548 | if (ch >= 0x110000) |
| 2549 | { |
| 2550 | errmsg = "codepoint not in range(0x110000)"; |
| 2551 | startinpos = ((const char *)q)-starts; |
| 2552 | endinpos = startinpos+4; |
| 2553 | goto utf32Error; |
| 2554 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2555 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2556 | if (ch >= 0x10000) |
| 2557 | { |
| 2558 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2559 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2560 | } |
| 2561 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2562 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2563 | *p++ = ch; |
| 2564 | q += 4; |
| 2565 | continue; |
| 2566 | utf32Error: |
| 2567 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2568 | if (unicode_decode_call_errorhandler( |
| 2569 | errors, &errorHandler, |
| 2570 | "utf32", errmsg, |
| 2571 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2572 | &unicode, &outpos, &p)) |
| 2573 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | if (byteorder) |
| 2577 | *byteorder = bo; |
| 2578 | |
| 2579 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2580 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2581 | |
| 2582 | /* Adjust length */ |
| 2583 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2584 | goto onError; |
| 2585 | |
| 2586 | Py_XDECREF(errorHandler); |
| 2587 | Py_XDECREF(exc); |
| 2588 | return (PyObject *)unicode; |
| 2589 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2590 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2591 | Py_DECREF(unicode); |
| 2592 | Py_XDECREF(errorHandler); |
| 2593 | Py_XDECREF(exc); |
| 2594 | return NULL; |
| 2595 | } |
| 2596 | |
| 2597 | PyObject * |
| 2598 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2599 | Py_ssize_t size, |
| 2600 | const char *errors, |
| 2601 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2602 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2603 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2604 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2605 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2606 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2607 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2608 | #else |
| 2609 | const int pairs = 0; |
| 2610 | #endif |
| 2611 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2612 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2613 | int iorder[] = {0, 1, 2, 3}; |
| 2614 | #else |
| 2615 | int iorder[] = {3, 2, 1, 0}; |
| 2616 | #endif |
| 2617 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2618 | #define STORECHAR(CH) \ |
| 2619 | do { \ |
| 2620 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2621 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2622 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2623 | p[iorder[0]] = (CH) & 0xff; \ |
| 2624 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2625 | } while(0) |
| 2626 | |
| 2627 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2628 | so we need less space. */ |
| 2629 | #ifndef Py_UNICODE_WIDE |
| 2630 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2631 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2632 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2633 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2634 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2635 | nsize = (size - pairs + (byteorder == 0)); |
| 2636 | bytesize = nsize * 4; |
| 2637 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2638 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2639 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2640 | if (v == NULL) |
| 2641 | return NULL; |
| 2642 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2643 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2644 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2645 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2646 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2647 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2648 | |
| 2649 | if (byteorder == -1) { |
| 2650 | /* force LE */ |
| 2651 | iorder[0] = 0; |
| 2652 | iorder[1] = 1; |
| 2653 | iorder[2] = 2; |
| 2654 | iorder[3] = 3; |
| 2655 | } |
| 2656 | else if (byteorder == 1) { |
| 2657 | /* force BE */ |
| 2658 | iorder[0] = 3; |
| 2659 | iorder[1] = 2; |
| 2660 | iorder[2] = 1; |
| 2661 | iorder[3] = 0; |
| 2662 | } |
| 2663 | |
| 2664 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2665 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2666 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2667 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2668 | Py_UCS4 ch2 = *s; |
| 2669 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2670 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2671 | s++; |
| 2672 | size--; |
| 2673 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2674 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2675 | #endif |
| 2676 | STORECHAR(ch); |
| 2677 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2678 | |
| 2679 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2680 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2681 | #undef STORECHAR |
| 2682 | } |
| 2683 | |
| 2684 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2685 | { |
| 2686 | if (!PyUnicode_Check(unicode)) { |
| 2687 | PyErr_BadArgument(); |
| 2688 | return NULL; |
| 2689 | } |
| 2690 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2691 | PyUnicode_GET_SIZE(unicode), |
| 2692 | NULL, |
| 2693 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2696 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2697 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2698 | PyObject * |
| 2699 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2700 | Py_ssize_t size, |
| 2701 | const char *errors, |
| 2702 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2703 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2704 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2705 | } |
| 2706 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2707 | /* Two masks for fast checking of whether a C 'long' may contain |
| 2708 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 2709 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 2710 | rare in most input. |
| 2711 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 2712 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2713 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2714 | #if (SIZEOF_LONG == 8) |
| 2715 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 2716 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 2717 | #elif (SIZEOF_LONG == 4) |
| 2718 | # define FAST_CHAR_MASK 0x80008000L |
| 2719 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 2720 | #else |
| 2721 | # error C 'long' size should be either 4 or 8! |
| 2722 | #endif |
| 2723 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2724 | PyObject * |
| 2725 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2726 | Py_ssize_t size, |
| 2727 | const char *errors, |
| 2728 | int *byteorder, |
| 2729 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2730 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2731 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2732 | Py_ssize_t startinpos; |
| 2733 | Py_ssize_t endinpos; |
| 2734 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2735 | PyUnicodeObject *unicode; |
| 2736 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2737 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2738 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2739 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2740 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2741 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2742 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2743 | int ihi = 1, ilo = 0; |
| 2744 | #else |
| 2745 | int ihi = 0, ilo = 1; |
| 2746 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2747 | PyObject *errorHandler = NULL; |
| 2748 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2749 | |
| 2750 | /* Note: size will always be longer than the resulting Unicode |
| 2751 | character count */ |
| 2752 | unicode = _PyUnicode_New(size); |
| 2753 | if (!unicode) |
| 2754 | return NULL; |
| 2755 | if (size == 0) |
| 2756 | return (PyObject *)unicode; |
| 2757 | |
| 2758 | /* Unpack UTF-16 encoded data */ |
| 2759 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2760 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2761 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2762 | |
| 2763 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2764 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2765 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2766 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2767 | byte order setting accordingly. In native mode, the leading BOM |
| 2768 | mark is skipped, in all other modes, it is copied to the output |
| 2769 | stream as-is (giving a ZWNBSP character). */ |
| 2770 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2771 | if (size >= 2) { |
| 2772 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2773 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2774 | if (bom == 0xFEFF) { |
| 2775 | q += 2; |
| 2776 | bo = -1; |
| 2777 | } |
| 2778 | else if (bom == 0xFFFE) { |
| 2779 | q += 2; |
| 2780 | bo = 1; |
| 2781 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2782 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2783 | if (bom == 0xFEFF) { |
| 2784 | q += 2; |
| 2785 | bo = 1; |
| 2786 | } |
| 2787 | else if (bom == 0xFFFE) { |
| 2788 | q += 2; |
| 2789 | bo = -1; |
| 2790 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2791 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2792 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2793 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2794 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2795 | if (bo == -1) { |
| 2796 | /* force LE */ |
| 2797 | ihi = 1; |
| 2798 | ilo = 0; |
| 2799 | } |
| 2800 | else if (bo == 1) { |
| 2801 | /* force BE */ |
| 2802 | ihi = 0; |
| 2803 | ilo = 1; |
| 2804 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2805 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2806 | native_ordering = ilo < ihi; |
| 2807 | #else |
| 2808 | native_ordering = ilo > ihi; |
| 2809 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2810 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2811 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2812 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2813 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2814 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 2815 | reads are more expensive, better to defer to another iteration. */ |
| 2816 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 2817 | /* Fast path for runs of non-surrogate chars. */ |
| 2818 | register const unsigned char *_q = q; |
| 2819 | Py_UNICODE *_p = p; |
| 2820 | if (native_ordering) { |
| 2821 | /* Native ordering is simple: as long as the input cannot |
| 2822 | possibly contain a surrogate char, do an unrolled copy |
| 2823 | of several 16-bit code points to the target object. |
| 2824 | The non-surrogate check is done on several input bytes |
| 2825 | at a time (as many as a C 'long' can contain). */ |
| 2826 | while (_q < aligned_end) { |
| 2827 | unsigned long data = * (unsigned long *) _q; |
| 2828 | if (data & FAST_CHAR_MASK) |
| 2829 | break; |
| 2830 | _p[0] = ((unsigned short *) _q)[0]; |
| 2831 | _p[1] = ((unsigned short *) _q)[1]; |
| 2832 | #if (SIZEOF_LONG == 8) |
| 2833 | _p[2] = ((unsigned short *) _q)[2]; |
| 2834 | _p[3] = ((unsigned short *) _q)[3]; |
| 2835 | #endif |
| 2836 | _q += SIZEOF_LONG; |
| 2837 | _p += SIZEOF_LONG / 2; |
| 2838 | } |
| 2839 | } |
| 2840 | else { |
| 2841 | /* Byteswapped ordering is similar, but we must decompose |
| 2842 | the copy bytewise, and take care of zero'ing out the |
| 2843 | upper bytes if the target object is in 32-bit units |
| 2844 | (that is, in UCS-4 builds). */ |
| 2845 | while (_q < aligned_end) { |
| 2846 | unsigned long data = * (unsigned long *) _q; |
| 2847 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 2848 | break; |
| 2849 | /* Zero upper bytes in UCS-4 builds */ |
| 2850 | #if (Py_UNICODE_SIZE > 2) |
| 2851 | _p[0] = 0; |
| 2852 | _p[1] = 0; |
| 2853 | #if (SIZEOF_LONG == 8) |
| 2854 | _p[2] = 0; |
| 2855 | _p[3] = 0; |
| 2856 | #endif |
| 2857 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 2858 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 2859 | fill the two last bytes of each 4-byte unit. */ |
| 2860 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 2861 | # define OFF 2 |
| 2862 | #else |
| 2863 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2864 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 2865 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 2866 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 2867 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 2868 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 2869 | #if (SIZEOF_LONG == 8) |
| 2870 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 2871 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 2872 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 2873 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 2874 | #endif |
| 2875 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2876 | _q += SIZEOF_LONG; |
| 2877 | _p += SIZEOF_LONG / 2; |
| 2878 | } |
| 2879 | } |
| 2880 | p = _p; |
| 2881 | q = _q; |
| 2882 | if (q >= e) |
| 2883 | break; |
| 2884 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2885 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2886 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2887 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2888 | |
| 2889 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2890 | *p++ = ch; |
| 2891 | continue; |
| 2892 | } |
| 2893 | |
| 2894 | /* UTF-16 code pair: */ |
| 2895 | if (q > e) { |
| 2896 | errmsg = "unexpected end of data"; |
| 2897 | startinpos = (((const char *)q) - 2) - starts; |
| 2898 | endinpos = ((const char *)e) + 1 - starts; |
| 2899 | goto utf16Error; |
| 2900 | } |
| 2901 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 2902 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2903 | q += 2; |
| 2904 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2905 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2906 | *p++ = ch; |
| 2907 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2908 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2909 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2910 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2911 | continue; |
| 2912 | } |
| 2913 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2914 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2915 | startinpos = (((const char *)q)-4)-starts; |
| 2916 | endinpos = startinpos+2; |
| 2917 | goto utf16Error; |
| 2918 | } |
| 2919 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2920 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2921 | errmsg = "illegal encoding"; |
| 2922 | startinpos = (((const char *)q)-2)-starts; |
| 2923 | endinpos = startinpos+2; |
| 2924 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2925 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2926 | utf16Error: |
| 2927 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 2928 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2929 | errors, |
| 2930 | &errorHandler, |
| 2931 | "utf16", errmsg, |
| 2932 | &starts, |
| 2933 | (const char **)&e, |
| 2934 | &startinpos, |
| 2935 | &endinpos, |
| 2936 | &exc, |
| 2937 | (const char **)&q, |
| 2938 | &unicode, |
| 2939 | &outpos, |
| 2940 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2941 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2942 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2943 | /* remaining byte at the end? (size should be even) */ |
| 2944 | if (e == q) { |
| 2945 | if (!consumed) { |
| 2946 | errmsg = "truncated data"; |
| 2947 | startinpos = ((const char *)q) - starts; |
| 2948 | endinpos = ((const char *)e) + 1 - starts; |
| 2949 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 2950 | if (unicode_decode_call_errorhandler( |
| 2951 | errors, |
| 2952 | &errorHandler, |
| 2953 | "utf16", errmsg, |
| 2954 | &starts, |
| 2955 | (const char **)&e, |
| 2956 | &startinpos, |
| 2957 | &endinpos, |
| 2958 | &exc, |
| 2959 | (const char **)&q, |
| 2960 | &unicode, |
| 2961 | &outpos, |
| 2962 | &p)) |
| 2963 | goto onError; |
| 2964 | /* The remaining input chars are ignored if the callback |
| 2965 | chooses to skip the input */ |
| 2966 | } |
| 2967 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2968 | |
| 2969 | if (byteorder) |
| 2970 | *byteorder = bo; |
| 2971 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2972 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2973 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2974 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2975 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2976 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2977 | goto onError; |
| 2978 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2979 | Py_XDECREF(errorHandler); |
| 2980 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2981 | return (PyObject *)unicode; |
| 2982 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2983 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2984 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2985 | Py_XDECREF(errorHandler); |
| 2986 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2987 | return NULL; |
| 2988 | } |
| 2989 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2990 | #undef FAST_CHAR_MASK |
| 2991 | #undef SWAPPED_FAST_CHAR_MASK |
| 2992 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2993 | PyObject * |
| 2994 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2995 | Py_ssize_t size, |
| 2996 | const char *errors, |
| 2997 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2999 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3000 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3001 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3002 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3003 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3004 | #else |
| 3005 | const int pairs = 0; |
| 3006 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3007 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3008 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3009 | int ihi = 1, ilo = 0; |
| 3010 | #else |
| 3011 | int ihi = 0, ilo = 1; |
| 3012 | #endif |
| 3013 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3014 | #define STORECHAR(CH) \ |
| 3015 | do { \ |
| 3016 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3017 | p[ilo] = (CH) & 0xff; \ |
| 3018 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3019 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3020 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3021 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3022 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3023 | if (s[i] >= 0x10000) |
| 3024 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3025 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3026 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3027 | if (size > PY_SSIZE_T_MAX || |
| 3028 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3029 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3030 | nsize = size + pairs + (byteorder == 0); |
| 3031 | bytesize = nsize * 2; |
| 3032 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3033 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3034 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3035 | if (v == NULL) |
| 3036 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3038 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3040 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3041 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3042 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3043 | |
| 3044 | if (byteorder == -1) { |
| 3045 | /* force LE */ |
| 3046 | ihi = 1; |
| 3047 | ilo = 0; |
| 3048 | } |
| 3049 | else if (byteorder == 1) { |
| 3050 | /* force BE */ |
| 3051 | ihi = 0; |
| 3052 | ilo = 1; |
| 3053 | } |
| 3054 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3055 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3056 | Py_UNICODE ch = *s++; |
| 3057 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3058 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3059 | if (ch >= 0x10000) { |
| 3060 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3061 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3062 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3063 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3064 | STORECHAR(ch); |
| 3065 | if (ch2) |
| 3066 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3067 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3068 | |
| 3069 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3070 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3071 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3075 | { |
| 3076 | if (!PyUnicode_Check(unicode)) { |
| 3077 | PyErr_BadArgument(); |
| 3078 | return NULL; |
| 3079 | } |
| 3080 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3081 | PyUnicode_GET_SIZE(unicode), |
| 3082 | NULL, |
| 3083 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3087 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3088 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3089 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3091 | Py_ssize_t size, |
| 3092 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3093 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3094 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3095 | Py_ssize_t startinpos; |
| 3096 | Py_ssize_t endinpos; |
| 3097 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3098 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3099 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3100 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3101 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3102 | char* message; |
| 3103 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3104 | PyObject *errorHandler = NULL; |
| 3105 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3106 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3107 | /* Escaped strings will always be longer than the resulting |
| 3108 | 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] | 3109 | length after conversion to the true value. |
| 3110 | (but if the error callback returns a long replacement string |
| 3111 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3112 | v = _PyUnicode_New(size); |
| 3113 | if (v == NULL) |
| 3114 | goto onError; |
| 3115 | if (size == 0) |
| 3116 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3117 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3118 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3119 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3120 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3121 | while (s < end) { |
| 3122 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3123 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3124 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3125 | |
| 3126 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3127 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3128 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3129 | continue; |
| 3130 | } |
| 3131 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3132 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3133 | /* \ - Escapes */ |
| 3134 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3135 | c = *s++; |
| 3136 | if (s > end) |
| 3137 | c = '\0'; /* Invalid after \ */ |
| 3138 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3139 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3140 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3141 | case '\n': break; |
| 3142 | case '\\': *p++ = '\\'; break; |
| 3143 | case '\'': *p++ = '\''; break; |
| 3144 | case '\"': *p++ = '\"'; break; |
| 3145 | case 'b': *p++ = '\b'; break; |
| 3146 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3147 | case 't': *p++ = '\t'; break; |
| 3148 | case 'n': *p++ = '\n'; break; |
| 3149 | case 'r': *p++ = '\r'; break; |
| 3150 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3151 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3152 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3153 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3154 | case '0': case '1': case '2': case '3': |
| 3155 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3156 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3157 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3158 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3159 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3160 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3161 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3162 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3163 | break; |
| 3164 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3165 | /* hex escapes */ |
| 3166 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3167 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3168 | digits = 2; |
| 3169 | message = "truncated \\xXX escape"; |
| 3170 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3171 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3172 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3173 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3174 | digits = 4; |
| 3175 | message = "truncated \\uXXXX escape"; |
| 3176 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3177 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3178 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3179 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3180 | digits = 8; |
| 3181 | message = "truncated \\UXXXXXXXX escape"; |
| 3182 | hexescape: |
| 3183 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3184 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3185 | if (s+digits>end) { |
| 3186 | endinpos = size; |
| 3187 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3188 | errors, &errorHandler, |
| 3189 | "unicodeescape", "end of string in escape sequence", |
| 3190 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3191 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3192 | goto onError; |
| 3193 | goto nextByte; |
| 3194 | } |
| 3195 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3196 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3197 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3198 | endinpos = (s+i+1)-starts; |
| 3199 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3200 | errors, &errorHandler, |
| 3201 | "unicodeescape", message, |
| 3202 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3203 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3204 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3205 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3206 | } |
| 3207 | chr = (chr<<4) & ~0xF; |
| 3208 | if (c >= '0' && c <= '9') |
| 3209 | chr += c - '0'; |
| 3210 | else if (c >= 'a' && c <= 'f') |
| 3211 | chr += 10 + c - 'a'; |
| 3212 | else |
| 3213 | chr += 10 + c - 'A'; |
| 3214 | } |
| 3215 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3216 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3217 | /* _decoding_error will have already written into the |
| 3218 | target buffer. */ |
| 3219 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3220 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3221 | /* when we get here, chr is a 32-bit unicode character */ |
| 3222 | if (chr <= 0xffff) |
| 3223 | /* UCS-2 character */ |
| 3224 | *p++ = (Py_UNICODE) chr; |
| 3225 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3226 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3227 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3228 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3229 | *p++ = chr; |
| 3230 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3231 | chr -= 0x10000L; |
| 3232 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3233 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3234 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3235 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3236 | endinpos = s-starts; |
| 3237 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3238 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3239 | errors, &errorHandler, |
| 3240 | "unicodeescape", "illegal Unicode character", |
| 3241 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3242 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3243 | goto onError; |
| 3244 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3245 | break; |
| 3246 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3247 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3248 | case 'N': |
| 3249 | message = "malformed \\N character escape"; |
| 3250 | if (ucnhash_CAPI == NULL) { |
| 3251 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3252 | PyObject *m, *api; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 3253 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3254 | if (m == NULL) |
| 3255 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3256 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3257 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3258 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3259 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3260 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3261 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3262 | if (ucnhash_CAPI == NULL) |
| 3263 | goto ucnhashError; |
| 3264 | } |
| 3265 | if (*s == '{') { |
| 3266 | const char *start = s+1; |
| 3267 | /* look for the closing brace */ |
| 3268 | while (*s != '}' && s < end) |
| 3269 | s++; |
| 3270 | if (s > start && s < end && *s == '}') { |
| 3271 | /* found a name. look it up in the unicode database */ |
| 3272 | message = "unknown Unicode character name"; |
| 3273 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3274 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3275 | goto store; |
| 3276 | } |
| 3277 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3278 | endinpos = s-starts; |
| 3279 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3280 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3281 | errors, &errorHandler, |
| 3282 | "unicodeescape", message, |
| 3283 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3284 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3285 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3286 | break; |
| 3287 | |
| 3288 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3289 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3290 | message = "\\ at end of string"; |
| 3291 | s--; |
| 3292 | endinpos = s-starts; |
| 3293 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3294 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3295 | errors, &errorHandler, |
| 3296 | "unicodeescape", message, |
| 3297 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3298 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3299 | goto onError; |
| 3300 | } |
| 3301 | else { |
| 3302 | *p++ = '\\'; |
| 3303 | *p++ = (unsigned char)s[-1]; |
| 3304 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3305 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3306 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3307 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3308 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3309 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3310 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3311 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3312 | Py_XDECREF(errorHandler); |
| 3313 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3314 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3315 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3316 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3317 | PyErr_SetString( |
| 3318 | PyExc_UnicodeError, |
| 3319 | "\\N escapes not supported (can't load unicodedata module)" |
| 3320 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3321 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3322 | Py_XDECREF(errorHandler); |
| 3323 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3324 | return NULL; |
| 3325 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3326 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3327 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3328 | Py_XDECREF(errorHandler); |
| 3329 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3330 | return NULL; |
| 3331 | } |
| 3332 | |
| 3333 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3334 | |
| 3335 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3336 | appropriate. |
| 3337 | |
| 3338 | */ |
| 3339 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3340 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3341 | Py_ssize_t size, |
| 3342 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3343 | { |
| 3344 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3345 | |
| 3346 | while (size-- > 0) { |
| 3347 | if (*s == ch) |
| 3348 | return s; |
| 3349 | s++; |
| 3350 | } |
| 3351 | |
| 3352 | return NULL; |
| 3353 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3354 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3355 | static const char *hexdigits = "0123456789abcdef"; |
| 3356 | |
| 3357 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3358 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3359 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3360 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3361 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3362 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3363 | #ifdef Py_UNICODE_WIDE |
| 3364 | const Py_ssize_t expandsize = 10; |
| 3365 | #else |
| 3366 | const Py_ssize_t expandsize = 6; |
| 3367 | #endif |
| 3368 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3369 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3370 | better to choose a different scheme. Perhaps scan the |
| 3371 | first N-chars of the string and allocate based on that size. |
| 3372 | */ |
| 3373 | /* Initial allocation is based on the longest-possible unichr |
| 3374 | escape. |
| 3375 | |
| 3376 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3377 | unichr, so in this case it's the longest unichr escape. In |
| 3378 | narrow (UTF-16) builds this is five chars per source unichr |
| 3379 | since there are two unichrs in the surrogate pair, so in narrow |
| 3380 | (UTF-16) builds it's not the longest unichr escape. |
| 3381 | |
| 3382 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3383 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3384 | escape. |
| 3385 | */ |
| 3386 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3387 | if (size == 0) |
| 3388 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3389 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3390 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3391 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3392 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3393 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3394 | 2 |
| 3395 | + expandsize*size |
| 3396 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3397 | if (repr == NULL) |
| 3398 | return NULL; |
| 3399 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3400 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3401 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3402 | while (size-- > 0) { |
| 3403 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3404 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3405 | /* Escape backslashes */ |
| 3406 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3407 | *p++ = '\\'; |
| 3408 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3409 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3410 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3411 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3412 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3413 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3414 | else if (ch >= 0x10000) { |
| 3415 | *p++ = '\\'; |
| 3416 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3417 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3418 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3419 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3420 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3421 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3422 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3423 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3424 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3425 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3426 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3427 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3428 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3429 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3430 | Py_UNICODE ch2; |
| 3431 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3432 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3433 | ch2 = *s++; |
| 3434 | size--; |
| 3435 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3436 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3437 | *p++ = '\\'; |
| 3438 | *p++ = 'U'; |
| 3439 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3440 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3441 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3442 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3443 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3444 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3445 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3446 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3447 | continue; |
| 3448 | } |
| 3449 | /* Fall through: isolated surrogates are copied as-is */ |
| 3450 | s--; |
| 3451 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3452 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3453 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3455 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3456 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3457 | *p++ = '\\'; |
| 3458 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3459 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3460 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3461 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3462 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3463 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3464 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3465 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3466 | else if (ch == '\t') { |
| 3467 | *p++ = '\\'; |
| 3468 | *p++ = 't'; |
| 3469 | } |
| 3470 | else if (ch == '\n') { |
| 3471 | *p++ = '\\'; |
| 3472 | *p++ = 'n'; |
| 3473 | } |
| 3474 | else if (ch == '\r') { |
| 3475 | *p++ = '\\'; |
| 3476 | *p++ = 'r'; |
| 3477 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3478 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3479 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3480 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3481 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3482 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3483 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3484 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3485 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3486 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3487 | /* Copy everything else as-is */ |
| 3488 | else |
| 3489 | *p++ = (char) ch; |
| 3490 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3491 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3492 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3493 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3494 | return NULL; |
| 3495 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3498 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3499 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3500 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3501 | if (!PyUnicode_Check(unicode)) { |
| 3502 | PyErr_BadArgument(); |
| 3503 | return NULL; |
| 3504 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3505 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3506 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3507 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3511 | |
| 3512 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3513 | Py_ssize_t size, |
| 3514 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3515 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3516 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3517 | Py_ssize_t startinpos; |
| 3518 | Py_ssize_t endinpos; |
| 3519 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3520 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3521 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3522 | const char *end; |
| 3523 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3524 | PyObject *errorHandler = NULL; |
| 3525 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3526 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3527 | /* Escaped strings will always be longer than the resulting |
| 3528 | 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] | 3529 | length after conversion to the true value. (But decoding error |
| 3530 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3531 | v = _PyUnicode_New(size); |
| 3532 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3533 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3534 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3535 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3536 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3537 | end = s + size; |
| 3538 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3539 | unsigned char c; |
| 3540 | Py_UCS4 x; |
| 3541 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3542 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3543 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3544 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3545 | if (*s != '\\') { |
| 3546 | *p++ = (unsigned char)*s++; |
| 3547 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3548 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3549 | startinpos = s-starts; |
| 3550 | |
| 3551 | /* \u-escapes are only interpreted iff the number of leading |
| 3552 | backslashes if odd */ |
| 3553 | bs = s; |
| 3554 | for (;s < end;) { |
| 3555 | if (*s != '\\') |
| 3556 | break; |
| 3557 | *p++ = (unsigned char)*s++; |
| 3558 | } |
| 3559 | if (((s - bs) & 1) == 0 || |
| 3560 | s >= end || |
| 3561 | (*s != 'u' && *s != 'U')) { |
| 3562 | continue; |
| 3563 | } |
| 3564 | p--; |
| 3565 | count = *s=='u' ? 4 : 8; |
| 3566 | s++; |
| 3567 | |
| 3568 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3569 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3570 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3571 | c = (unsigned char)*s; |
| 3572 | if (!ISXDIGIT(c)) { |
| 3573 | endinpos = s-starts; |
| 3574 | if (unicode_decode_call_errorhandler( |
| 3575 | errors, &errorHandler, |
| 3576 | "rawunicodeescape", "truncated \\uXXXX", |
| 3577 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3578 | &v, &outpos, &p)) |
| 3579 | goto onError; |
| 3580 | goto nextByte; |
| 3581 | } |
| 3582 | x = (x<<4) & ~0xF; |
| 3583 | if (c >= '0' && c <= '9') |
| 3584 | x += c - '0'; |
| 3585 | else if (c >= 'a' && c <= 'f') |
| 3586 | x += 10 + c - 'a'; |
| 3587 | else |
| 3588 | x += 10 + c - 'A'; |
| 3589 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3590 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3591 | /* UCS-2 character */ |
| 3592 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3593 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3594 | /* UCS-4 character. Either store directly, or as |
| 3595 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3596 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3597 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3598 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3599 | x -= 0x10000L; |
| 3600 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3601 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3602 | #endif |
| 3603 | } else { |
| 3604 | endinpos = s-starts; |
| 3605 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3606 | if (unicode_decode_call_errorhandler( |
| 3607 | errors, &errorHandler, |
| 3608 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3609 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3610 | &v, &outpos, &p)) |
| 3611 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3612 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3613 | nextByte: |
| 3614 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3615 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3616 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3617 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3618 | Py_XDECREF(errorHandler); |
| 3619 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3620 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3621 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3622 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3623 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3624 | Py_XDECREF(errorHandler); |
| 3625 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3626 | return NULL; |
| 3627 | } |
| 3628 | |
| 3629 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3630 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3631 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3632 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3633 | char *p; |
| 3634 | char *q; |
| 3635 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3636 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3637 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3638 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3639 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3640 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3641 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3642 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3643 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3644 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3645 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3646 | if (repr == NULL) |
| 3647 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3648 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3649 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3650 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3651 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3652 | while (size-- > 0) { |
| 3653 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3654 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3655 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3656 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3657 | *p++ = '\\'; |
| 3658 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3659 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3660 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3661 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3662 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3663 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3664 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3665 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3666 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3667 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3668 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3669 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3670 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3671 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3672 | Py_UNICODE ch2; |
| 3673 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3674 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3675 | ch2 = *s++; |
| 3676 | size--; |
| 3677 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3678 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3679 | *p++ = '\\'; |
| 3680 | *p++ = 'U'; |
| 3681 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3682 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3683 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3684 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3685 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3686 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3687 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3688 | *p++ = hexdigits[ucs & 0xf]; |
| 3689 | continue; |
| 3690 | } |
| 3691 | /* Fall through: isolated surrogates are copied as-is */ |
| 3692 | s--; |
| 3693 | size++; |
| 3694 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3695 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3696 | /* Map 16-bit characters to '\uxxxx' */ |
| 3697 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3698 | *p++ = '\\'; |
| 3699 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3700 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3701 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3702 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3703 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3704 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3705 | /* Copy everything else as-is */ |
| 3706 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3707 | *p++ = (char) ch; |
| 3708 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3709 | size = p - q; |
| 3710 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3711 | assert(size > 0); |
| 3712 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3713 | return NULL; |
| 3714 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
| 3717 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3718 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3719 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3720 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3721 | PyErr_BadArgument(); |
| 3722 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3723 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3724 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3725 | PyUnicode_GET_SIZE(unicode)); |
| 3726 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3727 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3730 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3731 | |
| 3732 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3733 | Py_ssize_t size, |
| 3734 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3735 | { |
| 3736 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3737 | Py_ssize_t startinpos; |
| 3738 | Py_ssize_t endinpos; |
| 3739 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3740 | PyUnicodeObject *v; |
| 3741 | Py_UNICODE *p; |
| 3742 | const char *end; |
| 3743 | const char *reason; |
| 3744 | PyObject *errorHandler = NULL; |
| 3745 | PyObject *exc = NULL; |
| 3746 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3747 | #ifdef Py_UNICODE_WIDE |
| 3748 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3749 | #endif |
| 3750 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3751 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3752 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3753 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3754 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3755 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3756 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3757 | p = PyUnicode_AS_UNICODE(v); |
| 3758 | end = s + size; |
| 3759 | |
| 3760 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3761 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3762 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3763 | some malformed UCS-4 data. */ |
| 3764 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3765 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3766 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3767 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3768 | end-s < Py_UNICODE_SIZE |
| 3769 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3770 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3771 | startinpos = s - starts; |
| 3772 | if (end-s < Py_UNICODE_SIZE) { |
| 3773 | endinpos = end-starts; |
| 3774 | reason = "truncated input"; |
| 3775 | } |
| 3776 | else { |
| 3777 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3778 | reason = "illegal code point (> 0x10FFFF)"; |
| 3779 | } |
| 3780 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3781 | if (unicode_decode_call_errorhandler( |
| 3782 | errors, &errorHandler, |
| 3783 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3784 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3785 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3786 | goto onError; |
| 3787 | } |
| 3788 | } |
| 3789 | else { |
| 3790 | p++; |
| 3791 | s += Py_UNICODE_SIZE; |
| 3792 | } |
| 3793 | } |
| 3794 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3795 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3796 | goto onError; |
| 3797 | Py_XDECREF(errorHandler); |
| 3798 | Py_XDECREF(exc); |
| 3799 | return (PyObject *)v; |
| 3800 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3801 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3802 | Py_XDECREF(v); |
| 3803 | Py_XDECREF(errorHandler); |
| 3804 | Py_XDECREF(exc); |
| 3805 | return NULL; |
| 3806 | } |
| 3807 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3808 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3809 | |
| 3810 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3811 | Py_ssize_t size, |
| 3812 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3813 | { |
| 3814 | PyUnicodeObject *v; |
| 3815 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3816 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3817 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3818 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3819 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3820 | Py_UNICODE r = *(unsigned char*)s; |
| 3821 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3824 | v = _PyUnicode_New(size); |
| 3825 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3826 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3827 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3828 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3829 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3830 | e = s + size; |
| 3831 | /* Unrolling the copy makes it much faster by reducing the looping |
| 3832 | overhead. This is similar to what many memcpy() implementations do. */ |
| 3833 | unrolled_end = e - 4; |
| 3834 | while (s < unrolled_end) { |
| 3835 | p[0] = (unsigned char) s[0]; |
| 3836 | p[1] = (unsigned char) s[1]; |
| 3837 | p[2] = (unsigned char) s[2]; |
| 3838 | p[3] = (unsigned char) s[3]; |
| 3839 | s += 4; |
| 3840 | p += 4; |
| 3841 | } |
| 3842 | while (s < e) |
| 3843 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3844 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3845 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3846 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3847 | Py_XDECREF(v); |
| 3848 | return NULL; |
| 3849 | } |
| 3850 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3851 | /* create or adjust a UnicodeEncodeError */ |
| 3852 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3853 | const char *encoding, |
| 3854 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3855 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3856 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3857 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3858 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3859 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3860 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3861 | } |
| 3862 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3863 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3864 | goto onError; |
| 3865 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3866 | goto onError; |
| 3867 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3868 | goto onError; |
| 3869 | return; |
| 3870 | onError: |
| 3871 | Py_DECREF(*exceptionObject); |
| 3872 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3873 | } |
| 3874 | } |
| 3875 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3876 | /* raises a UnicodeEncodeError */ |
| 3877 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3878 | const char *encoding, |
| 3879 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3880 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3881 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3882 | { |
| 3883 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3884 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3885 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3886 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | /* error handling callback helper: |
| 3890 | build arguments, call the callback and check the arguments, |
| 3891 | put the result into newpos and return the replacement string, which |
| 3892 | has to be freed by the caller */ |
| 3893 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3894 | PyObject **errorHandler, |
| 3895 | const char *encoding, const char *reason, |
| 3896 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3897 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3898 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3899 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3900 | static char *argparse = "O!n;encoding error handler must return (str, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3901 | |
| 3902 | PyObject *restuple; |
| 3903 | PyObject *resunicode; |
| 3904 | |
| 3905 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3906 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3907 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3908 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3912 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3913 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3914 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3915 | |
| 3916 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3917 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3918 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3919 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3920 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3921 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3922 | Py_DECREF(restuple); |
| 3923 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3924 | } |
| 3925 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3926 | &resunicode, newpos)) { |
| 3927 | Py_DECREF(restuple); |
| 3928 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3929 | } |
| 3930 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3931 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3932 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3933 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 3934 | Py_DECREF(restuple); |
| 3935 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3936 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3937 | Py_INCREF(resunicode); |
| 3938 | Py_DECREF(restuple); |
| 3939 | return resunicode; |
| 3940 | } |
| 3941 | |
| 3942 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3943 | Py_ssize_t size, |
| 3944 | const char *errors, |
| 3945 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3946 | { |
| 3947 | /* output object */ |
| 3948 | PyObject *res; |
| 3949 | /* pointers to the beginning and end+1 of input */ |
| 3950 | const Py_UNICODE *startp = p; |
| 3951 | const Py_UNICODE *endp = p + size; |
| 3952 | /* pointer to the beginning of the unencodable characters */ |
| 3953 | /* const Py_UNICODE *badp = NULL; */ |
| 3954 | /* pointer into the output */ |
| 3955 | char *str; |
| 3956 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3957 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3958 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3959 | 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] | 3960 | PyObject *errorHandler = NULL; |
| 3961 | PyObject *exc = NULL; |
| 3962 | /* the following variable is used for caching string comparisons |
| 3963 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3964 | int known_errorHandler = -1; |
| 3965 | |
| 3966 | /* allocate enough for a simple encoding without |
| 3967 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3968 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3969 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3970 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3971 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3972 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3973 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3974 | ressize = size; |
| 3975 | |
| 3976 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3977 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3978 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3979 | /* can we encode this? */ |
| 3980 | if (c<limit) { |
| 3981 | /* no overflow check, because we know that the space is enough */ |
| 3982 | *str++ = (char)c; |
| 3983 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3984 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3985 | else { |
| 3986 | Py_ssize_t unicodepos = p-startp; |
| 3987 | Py_ssize_t requiredsize; |
| 3988 | PyObject *repunicode; |
| 3989 | Py_ssize_t repsize; |
| 3990 | Py_ssize_t newpos; |
| 3991 | Py_ssize_t respos; |
| 3992 | Py_UNICODE *uni2; |
| 3993 | /* startpos for collecting unencodable chars */ |
| 3994 | const Py_UNICODE *collstart = p; |
| 3995 | const Py_UNICODE *collend = p; |
| 3996 | /* find all unecodable characters */ |
| 3997 | while ((collend < endp) && ((*collend)>=limit)) |
| 3998 | ++collend; |
| 3999 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4000 | if (known_errorHandler==-1) { |
| 4001 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4002 | known_errorHandler = 1; |
| 4003 | else if (!strcmp(errors, "replace")) |
| 4004 | known_errorHandler = 2; |
| 4005 | else if (!strcmp(errors, "ignore")) |
| 4006 | known_errorHandler = 3; |
| 4007 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4008 | known_errorHandler = 4; |
| 4009 | else |
| 4010 | known_errorHandler = 0; |
| 4011 | } |
| 4012 | switch (known_errorHandler) { |
| 4013 | case 1: /* strict */ |
| 4014 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4015 | goto onError; |
| 4016 | case 2: /* replace */ |
| 4017 | while (collstart++<collend) |
| 4018 | *str++ = '?'; /* fall through */ |
| 4019 | case 3: /* ignore */ |
| 4020 | p = collend; |
| 4021 | break; |
| 4022 | case 4: /* xmlcharrefreplace */ |
| 4023 | respos = str - PyBytes_AS_STRING(res); |
| 4024 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4025 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4026 | if (*p<10) |
| 4027 | repsize += 2+1+1; |
| 4028 | else if (*p<100) |
| 4029 | repsize += 2+2+1; |
| 4030 | else if (*p<1000) |
| 4031 | repsize += 2+3+1; |
| 4032 | else if (*p<10000) |
| 4033 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4034 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4035 | else |
| 4036 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4037 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4038 | else if (*p<100000) |
| 4039 | repsize += 2+5+1; |
| 4040 | else if (*p<1000000) |
| 4041 | repsize += 2+6+1; |
| 4042 | else |
| 4043 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4044 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4045 | } |
| 4046 | requiredsize = respos+repsize+(endp-collend); |
| 4047 | if (requiredsize > ressize) { |
| 4048 | if (requiredsize<2*ressize) |
| 4049 | requiredsize = 2*ressize; |
| 4050 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4051 | goto onError; |
| 4052 | str = PyBytes_AS_STRING(res) + respos; |
| 4053 | ressize = requiredsize; |
| 4054 | } |
| 4055 | /* generate replacement (temporarily (mis)uses p) */ |
| 4056 | for (p = collstart; p < collend; ++p) { |
| 4057 | str += sprintf(str, "&#%d;", (int)*p); |
| 4058 | } |
| 4059 | p = collend; |
| 4060 | break; |
| 4061 | default: |
| 4062 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4063 | encoding, reason, startp, size, &exc, |
| 4064 | collstart-startp, collend-startp, &newpos); |
| 4065 | if (repunicode == NULL) |
| 4066 | goto onError; |
| 4067 | /* need more space? (at least enough for what we |
| 4068 | have+the replacement+the rest of the string, so |
| 4069 | we won't have to check space for encodable characters) */ |
| 4070 | respos = str - PyBytes_AS_STRING(res); |
| 4071 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4072 | requiredsize = respos+repsize+(endp-collend); |
| 4073 | if (requiredsize > ressize) { |
| 4074 | if (requiredsize<2*ressize) |
| 4075 | requiredsize = 2*ressize; |
| 4076 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4077 | Py_DECREF(repunicode); |
| 4078 | goto onError; |
| 4079 | } |
| 4080 | str = PyBytes_AS_STRING(res) + respos; |
| 4081 | ressize = requiredsize; |
| 4082 | } |
| 4083 | /* check if there is anything unencodable in the replacement |
| 4084 | and copy it to the output */ |
| 4085 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4086 | c = *uni2; |
| 4087 | if (c >= limit) { |
| 4088 | raise_encode_exception(&exc, encoding, startp, size, |
| 4089 | unicodepos, unicodepos+1, reason); |
| 4090 | Py_DECREF(repunicode); |
| 4091 | goto onError; |
| 4092 | } |
| 4093 | *str = (char)c; |
| 4094 | } |
| 4095 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4096 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4097 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4098 | } |
| 4099 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4100 | /* Resize if we allocated to much */ |
| 4101 | size = str - PyBytes_AS_STRING(res); |
| 4102 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4103 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4104 | if (_PyBytes_Resize(&res, size) < 0) |
| 4105 | goto onError; |
| 4106 | } |
| 4107 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4108 | Py_XDECREF(errorHandler); |
| 4109 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4110 | return res; |
| 4111 | |
| 4112 | onError: |
| 4113 | Py_XDECREF(res); |
| 4114 | Py_XDECREF(errorHandler); |
| 4115 | Py_XDECREF(exc); |
| 4116 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4119 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4120 | Py_ssize_t size, |
| 4121 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4122 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4123 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4127 | { |
| 4128 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4129 | PyErr_BadArgument(); |
| 4130 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4131 | } |
| 4132 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4133 | PyUnicode_GET_SIZE(unicode), |
| 4134 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4138 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4139 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4140 | Py_ssize_t size, |
| 4141 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4142 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4143 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4144 | PyUnicodeObject *v; |
| 4145 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4146 | Py_ssize_t startinpos; |
| 4147 | Py_ssize_t endinpos; |
| 4148 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4149 | const char *e; |
| 4150 | PyObject *errorHandler = NULL; |
| 4151 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4152 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4153 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4154 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4155 | Py_UNICODE r = *(unsigned char*)s; |
| 4156 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4157 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4159 | v = _PyUnicode_New(size); |
| 4160 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4161 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4162 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4163 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4164 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4165 | e = s + size; |
| 4166 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4167 | register unsigned char c = (unsigned char)*s; |
| 4168 | if (c < 128) { |
| 4169 | *p++ = c; |
| 4170 | ++s; |
| 4171 | } |
| 4172 | else { |
| 4173 | startinpos = s-starts; |
| 4174 | endinpos = startinpos + 1; |
| 4175 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4176 | if (unicode_decode_call_errorhandler( |
| 4177 | errors, &errorHandler, |
| 4178 | "ascii", "ordinal not in range(128)", |
| 4179 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4180 | &v, &outpos, &p)) |
| 4181 | goto onError; |
| 4182 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4183 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4184 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4185 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4186 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4187 | Py_XDECREF(errorHandler); |
| 4188 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4189 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4190 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4191 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4192 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4193 | Py_XDECREF(errorHandler); |
| 4194 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4195 | return NULL; |
| 4196 | } |
| 4197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4198 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4199 | Py_ssize_t size, |
| 4200 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4201 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4202 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4206 | { |
| 4207 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4208 | PyErr_BadArgument(); |
| 4209 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4210 | } |
| 4211 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4212 | PyUnicode_GET_SIZE(unicode), |
| 4213 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4216 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4217 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4218 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4219 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4220 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4221 | #define NEED_RETRY |
| 4222 | #endif |
| 4223 | |
| 4224 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4225 | a) it assumes an incomplete character consists of a single byte, and |
| 4226 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4227 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4228 | |
| 4229 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4230 | { |
| 4231 | const char *curr = s + offset; |
| 4232 | |
| 4233 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4234 | const char *prev = CharPrev(s, curr); |
| 4235 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4236 | } |
| 4237 | return 0; |
| 4238 | } |
| 4239 | |
| 4240 | /* |
| 4241 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4242 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4243 | */ |
| 4244 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4245 | const char *s, /* MBCS string */ |
| 4246 | int size, /* sizeof MBCS string */ |
| 4247 | int final) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4248 | { |
| 4249 | Py_UNICODE *p; |
| 4250 | Py_ssize_t n = 0; |
| 4251 | int usize = 0; |
| 4252 | |
| 4253 | assert(size >= 0); |
| 4254 | |
| 4255 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4256 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4257 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4258 | |
| 4259 | /* First get the size of the result */ |
| 4260 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4261 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4262 | if (usize == 0) { |
| 4263 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4264 | return -1; |
| 4265 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
| 4268 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4269 | /* Create unicode object */ |
| 4270 | *v = _PyUnicode_New(usize); |
| 4271 | if (*v == NULL) |
| 4272 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4273 | } |
| 4274 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4275 | /* Extend unicode object */ |
| 4276 | n = PyUnicode_GET_SIZE(*v); |
| 4277 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4278 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | /* Do the conversion */ |
| 4282 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4283 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4284 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4285 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4286 | return -1; |
| 4287 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | return size; |
| 4291 | } |
| 4292 | |
| 4293 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4294 | Py_ssize_t size, |
| 4295 | const char *errors, |
| 4296 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4297 | { |
| 4298 | PyUnicodeObject *v = NULL; |
| 4299 | int done; |
| 4300 | |
| 4301 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4302 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4303 | |
| 4304 | #ifdef NEED_RETRY |
| 4305 | retry: |
| 4306 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4307 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4308 | else |
| 4309 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4310 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4311 | |
| 4312 | if (done < 0) { |
| 4313 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4314 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4318 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4319 | |
| 4320 | #ifdef NEED_RETRY |
| 4321 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4322 | s += done; |
| 4323 | size -= done; |
| 4324 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4325 | } |
| 4326 | #endif |
| 4327 | |
| 4328 | return (PyObject *)v; |
| 4329 | } |
| 4330 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4331 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4332 | Py_ssize_t size, |
| 4333 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4334 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4335 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4336 | } |
| 4337 | |
| 4338 | /* |
| 4339 | * Convert unicode into string object (MBCS). |
| 4340 | * Returns 0 if succeed, -1 otherwise. |
| 4341 | */ |
| 4342 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4343 | const Py_UNICODE *p, /* unicode */ |
| 4344 | int size) /* size of unicode */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4345 | { |
| 4346 | int mbcssize = 0; |
| 4347 | Py_ssize_t n = 0; |
| 4348 | |
| 4349 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4350 | |
| 4351 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4352 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4353 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4354 | if (mbcssize == 0) { |
| 4355 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4356 | return -1; |
| 4357 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4360 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4361 | /* Create string object */ |
| 4362 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4363 | if (*repr == NULL) |
| 4364 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4365 | } |
| 4366 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4367 | /* Extend string object */ |
| 4368 | n = PyBytes_Size(*repr); |
| 4369 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4370 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | /* Do the conversion */ |
| 4374 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4375 | char *s = PyBytes_AS_STRING(*repr) + n; |
| 4376 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4377 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4378 | return -1; |
| 4379 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4386 | Py_ssize_t size, |
| 4387 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4388 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4389 | PyObject *repr = NULL; |
| 4390 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4391 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4392 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4393 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4394 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4395 | ret = encode_mbcs(&repr, p, INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4396 | else |
| 4397 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4398 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4399 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4400 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4401 | Py_XDECREF(repr); |
| 4402 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4403 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4404 | |
| 4405 | #ifdef NEED_RETRY |
| 4406 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4407 | p += INT_MAX; |
| 4408 | size -= INT_MAX; |
| 4409 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4410 | } |
| 4411 | #endif |
| 4412 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4413 | return repr; |
| 4414 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4415 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4416 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4417 | { |
| 4418 | if (!PyUnicode_Check(unicode)) { |
| 4419 | PyErr_BadArgument(); |
| 4420 | return NULL; |
| 4421 | } |
| 4422 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4423 | PyUnicode_GET_SIZE(unicode), |
| 4424 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4427 | #undef NEED_RETRY |
| 4428 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4429 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4430 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4431 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4432 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4433 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4434 | Py_ssize_t size, |
| 4435 | PyObject *mapping, |
| 4436 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4437 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4438 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4439 | Py_ssize_t startinpos; |
| 4440 | Py_ssize_t endinpos; |
| 4441 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4442 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4443 | PyUnicodeObject *v; |
| 4444 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4445 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4446 | PyObject *errorHandler = NULL; |
| 4447 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4448 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4449 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4451 | /* Default to Latin-1 */ |
| 4452 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4453 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4454 | |
| 4455 | v = _PyUnicode_New(size); |
| 4456 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4457 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4458 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4459 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4460 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4461 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4462 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4463 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4464 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4465 | while (s < e) { |
| 4466 | unsigned char ch = *s; |
| 4467 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4468 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4469 | if (ch < maplen) |
| 4470 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4471 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4472 | if (x == 0xfffe) { |
| 4473 | /* undefined mapping */ |
| 4474 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4475 | startinpos = s-starts; |
| 4476 | endinpos = startinpos+1; |
| 4477 | if (unicode_decode_call_errorhandler( |
| 4478 | errors, &errorHandler, |
| 4479 | "charmap", "character maps to <undefined>", |
| 4480 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4481 | &v, &outpos, &p)) { |
| 4482 | goto onError; |
| 4483 | } |
| 4484 | continue; |
| 4485 | } |
| 4486 | *p++ = x; |
| 4487 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4488 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4489 | } |
| 4490 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4491 | while (s < e) { |
| 4492 | unsigned char ch = *s; |
| 4493 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4494 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4495 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4496 | w = PyLong_FromLong((long)ch); |
| 4497 | if (w == NULL) |
| 4498 | goto onError; |
| 4499 | x = PyObject_GetItem(mapping, w); |
| 4500 | Py_DECREF(w); |
| 4501 | if (x == NULL) { |
| 4502 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4503 | /* No mapping found means: mapping is undefined. */ |
| 4504 | PyErr_Clear(); |
| 4505 | x = Py_None; |
| 4506 | Py_INCREF(x); |
| 4507 | } else |
| 4508 | goto onError; |
| 4509 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4510 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4511 | /* Apply mapping */ |
| 4512 | if (PyLong_Check(x)) { |
| 4513 | long value = PyLong_AS_LONG(x); |
| 4514 | if (value < 0 || value > 65535) { |
| 4515 | PyErr_SetString(PyExc_TypeError, |
| 4516 | "character mapping must be in range(65536)"); |
| 4517 | Py_DECREF(x); |
| 4518 | goto onError; |
| 4519 | } |
| 4520 | *p++ = (Py_UNICODE)value; |
| 4521 | } |
| 4522 | else if (x == Py_None) { |
| 4523 | /* undefined mapping */ |
| 4524 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4525 | startinpos = s-starts; |
| 4526 | endinpos = startinpos+1; |
| 4527 | if (unicode_decode_call_errorhandler( |
| 4528 | errors, &errorHandler, |
| 4529 | "charmap", "character maps to <undefined>", |
| 4530 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4531 | &v, &outpos, &p)) { |
| 4532 | Py_DECREF(x); |
| 4533 | goto onError; |
| 4534 | } |
| 4535 | Py_DECREF(x); |
| 4536 | continue; |
| 4537 | } |
| 4538 | else if (PyUnicode_Check(x)) { |
| 4539 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4540 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4541 | if (targetsize == 1) |
| 4542 | /* 1-1 mapping */ |
| 4543 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4544 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4545 | else if (targetsize > 1) { |
| 4546 | /* 1-n mapping */ |
| 4547 | if (targetsize > extrachars) { |
| 4548 | /* resize first */ |
| 4549 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4550 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4551 | (targetsize << 2); |
| 4552 | extrachars += needed; |
| 4553 | /* XXX overflow detection missing */ |
| 4554 | if (_PyUnicode_Resize(&v, |
| 4555 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4556 | Py_DECREF(x); |
| 4557 | goto onError; |
| 4558 | } |
| 4559 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4560 | } |
| 4561 | Py_UNICODE_COPY(p, |
| 4562 | PyUnicode_AS_UNICODE(x), |
| 4563 | targetsize); |
| 4564 | p += targetsize; |
| 4565 | extrachars -= targetsize; |
| 4566 | } |
| 4567 | /* 1-0 mapping: skip the character */ |
| 4568 | } |
| 4569 | else { |
| 4570 | /* wrong return value */ |
| 4571 | PyErr_SetString(PyExc_TypeError, |
| 4572 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4573 | Py_DECREF(x); |
| 4574 | goto onError; |
| 4575 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4576 | Py_DECREF(x); |
| 4577 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4578 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4579 | } |
| 4580 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4581 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4582 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4583 | Py_XDECREF(errorHandler); |
| 4584 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4585 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4586 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4587 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4588 | Py_XDECREF(errorHandler); |
| 4589 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4590 | Py_XDECREF(v); |
| 4591 | return NULL; |
| 4592 | } |
| 4593 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4594 | /* Charmap encoding: the lookup table */ |
| 4595 | |
| 4596 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4597 | PyObject_HEAD |
| 4598 | unsigned char level1[32]; |
| 4599 | int count2, count3; |
| 4600 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4601 | }; |
| 4602 | |
| 4603 | static PyObject* |
| 4604 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4605 | { |
| 4606 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4607 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4608 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4612 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4613 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4614 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4615 | }; |
| 4616 | |
| 4617 | static void |
| 4618 | encoding_map_dealloc(PyObject* o) |
| 4619 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4620 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4624 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4625 | "EncodingMap", /*tp_name*/ |
| 4626 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4627 | 0, /*tp_itemsize*/ |
| 4628 | /* methods */ |
| 4629 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4630 | 0, /*tp_print*/ |
| 4631 | 0, /*tp_getattr*/ |
| 4632 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4633 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4634 | 0, /*tp_repr*/ |
| 4635 | 0, /*tp_as_number*/ |
| 4636 | 0, /*tp_as_sequence*/ |
| 4637 | 0, /*tp_as_mapping*/ |
| 4638 | 0, /*tp_hash*/ |
| 4639 | 0, /*tp_call*/ |
| 4640 | 0, /*tp_str*/ |
| 4641 | 0, /*tp_getattro*/ |
| 4642 | 0, /*tp_setattro*/ |
| 4643 | 0, /*tp_as_buffer*/ |
| 4644 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4645 | 0, /*tp_doc*/ |
| 4646 | 0, /*tp_traverse*/ |
| 4647 | 0, /*tp_clear*/ |
| 4648 | 0, /*tp_richcompare*/ |
| 4649 | 0, /*tp_weaklistoffset*/ |
| 4650 | 0, /*tp_iter*/ |
| 4651 | 0, /*tp_iternext*/ |
| 4652 | encoding_map_methods, /*tp_methods*/ |
| 4653 | 0, /*tp_members*/ |
| 4654 | 0, /*tp_getset*/ |
| 4655 | 0, /*tp_base*/ |
| 4656 | 0, /*tp_dict*/ |
| 4657 | 0, /*tp_descr_get*/ |
| 4658 | 0, /*tp_descr_set*/ |
| 4659 | 0, /*tp_dictoffset*/ |
| 4660 | 0, /*tp_init*/ |
| 4661 | 0, /*tp_alloc*/ |
| 4662 | 0, /*tp_new*/ |
| 4663 | 0, /*tp_free*/ |
| 4664 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4665 | }; |
| 4666 | |
| 4667 | PyObject* |
| 4668 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4669 | { |
| 4670 | Py_UNICODE *decode; |
| 4671 | PyObject *result; |
| 4672 | struct encoding_map *mresult; |
| 4673 | int i; |
| 4674 | int need_dict = 0; |
| 4675 | unsigned char level1[32]; |
| 4676 | unsigned char level2[512]; |
| 4677 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4678 | int count2 = 0, count3 = 0; |
| 4679 | |
| 4680 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4681 | PyErr_BadArgument(); |
| 4682 | return NULL; |
| 4683 | } |
| 4684 | decode = PyUnicode_AS_UNICODE(string); |
| 4685 | memset(level1, 0xFF, sizeof level1); |
| 4686 | memset(level2, 0xFF, sizeof level2); |
| 4687 | |
| 4688 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4689 | or if there are non-BMP characters, we need to use |
| 4690 | a mapping dictionary. */ |
| 4691 | if (decode[0] != 0) |
| 4692 | need_dict = 1; |
| 4693 | for (i = 1; i < 256; i++) { |
| 4694 | int l1, l2; |
| 4695 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4696 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4697 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4698 | #endif |
| 4699 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4700 | need_dict = 1; |
| 4701 | break; |
| 4702 | } |
| 4703 | if (decode[i] == 0xFFFE) |
| 4704 | /* unmapped character */ |
| 4705 | continue; |
| 4706 | l1 = decode[i] >> 11; |
| 4707 | l2 = decode[i] >> 7; |
| 4708 | if (level1[l1] == 0xFF) |
| 4709 | level1[l1] = count2++; |
| 4710 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4711 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4715 | need_dict = 1; |
| 4716 | |
| 4717 | if (need_dict) { |
| 4718 | PyObject *result = PyDict_New(); |
| 4719 | PyObject *key, *value; |
| 4720 | if (!result) |
| 4721 | return NULL; |
| 4722 | for (i = 0; i < 256; i++) { |
| 4723 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4724 | key = PyLong_FromLong(decode[i]); |
| 4725 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4726 | if (!key || !value) |
| 4727 | goto failed1; |
| 4728 | if (PyDict_SetItem(result, key, value) == -1) |
| 4729 | goto failed1; |
| 4730 | Py_DECREF(key); |
| 4731 | Py_DECREF(value); |
| 4732 | } |
| 4733 | return result; |
| 4734 | failed1: |
| 4735 | Py_XDECREF(key); |
| 4736 | Py_XDECREF(value); |
| 4737 | Py_DECREF(result); |
| 4738 | return NULL; |
| 4739 | } |
| 4740 | |
| 4741 | /* Create a three-level trie */ |
| 4742 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4743 | 16*count2 + 128*count3 - 1); |
| 4744 | if (!result) |
| 4745 | return PyErr_NoMemory(); |
| 4746 | PyObject_Init(result, &EncodingMapType); |
| 4747 | mresult = (struct encoding_map*)result; |
| 4748 | mresult->count2 = count2; |
| 4749 | mresult->count3 = count3; |
| 4750 | mlevel1 = mresult->level1; |
| 4751 | mlevel2 = mresult->level23; |
| 4752 | mlevel3 = mresult->level23 + 16*count2; |
| 4753 | memcpy(mlevel1, level1, 32); |
| 4754 | memset(mlevel2, 0xFF, 16*count2); |
| 4755 | memset(mlevel3, 0, 128*count3); |
| 4756 | count3 = 0; |
| 4757 | for (i = 1; i < 256; i++) { |
| 4758 | int o1, o2, o3, i2, i3; |
| 4759 | if (decode[i] == 0xFFFE) |
| 4760 | /* unmapped character */ |
| 4761 | continue; |
| 4762 | o1 = decode[i]>>11; |
| 4763 | o2 = (decode[i]>>7) & 0xF; |
| 4764 | i2 = 16*mlevel1[o1] + o2; |
| 4765 | if (mlevel2[i2] == 0xFF) |
| 4766 | mlevel2[i2] = count3++; |
| 4767 | o3 = decode[i] & 0x7F; |
| 4768 | i3 = 128*mlevel2[i2] + o3; |
| 4769 | mlevel3[i3] = i; |
| 4770 | } |
| 4771 | return result; |
| 4772 | } |
| 4773 | |
| 4774 | static int |
| 4775 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4776 | { |
| 4777 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4778 | int l1 = c>>11; |
| 4779 | int l2 = (c>>7) & 0xF; |
| 4780 | int l3 = c & 0x7F; |
| 4781 | int i; |
| 4782 | |
| 4783 | #ifdef Py_UNICODE_WIDE |
| 4784 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4785 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4786 | } |
| 4787 | #endif |
| 4788 | if (c == 0) |
| 4789 | return 0; |
| 4790 | /* level 1*/ |
| 4791 | i = map->level1[l1]; |
| 4792 | if (i == 0xFF) { |
| 4793 | return -1; |
| 4794 | } |
| 4795 | /* level 2*/ |
| 4796 | i = map->level23[16*i+l2]; |
| 4797 | if (i == 0xFF) { |
| 4798 | return -1; |
| 4799 | } |
| 4800 | /* level 3 */ |
| 4801 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4802 | if (i == 0) { |
| 4803 | return -1; |
| 4804 | } |
| 4805 | return i; |
| 4806 | } |
| 4807 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4808 | /* Lookup the character ch in the mapping. If the character |
| 4809 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4810 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4811 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4812 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4813 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4814 | PyObject *x; |
| 4815 | |
| 4816 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4817 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4818 | x = PyObject_GetItem(mapping, w); |
| 4819 | Py_DECREF(w); |
| 4820 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4821 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4822 | /* No mapping found means: mapping is undefined. */ |
| 4823 | PyErr_Clear(); |
| 4824 | x = Py_None; |
| 4825 | Py_INCREF(x); |
| 4826 | return x; |
| 4827 | } else |
| 4828 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4829 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4830 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4831 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4832 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4833 | long value = PyLong_AS_LONG(x); |
| 4834 | if (value < 0 || value > 255) { |
| 4835 | PyErr_SetString(PyExc_TypeError, |
| 4836 | "character mapping must be in range(256)"); |
| 4837 | Py_DECREF(x); |
| 4838 | return NULL; |
| 4839 | } |
| 4840 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4842 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4843 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4844 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4845 | /* wrong return value */ |
| 4846 | PyErr_Format(PyExc_TypeError, |
| 4847 | "character mapping must return integer, bytes or None, not %.400s", |
| 4848 | x->ob_type->tp_name); |
| 4849 | Py_DECREF(x); |
| 4850 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4851 | } |
| 4852 | } |
| 4853 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4854 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4855 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4856 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4857 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 4858 | /* exponentially overallocate to minimize reallocations */ |
| 4859 | if (requiredsize < 2*outsize) |
| 4860 | requiredsize = 2*outsize; |
| 4861 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 4862 | return -1; |
| 4863 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4866 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4867 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4868 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4869 | /* 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] | 4870 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4871 | space is available. Return a new reference to the object that |
| 4872 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4873 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4874 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4875 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4876 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4877 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4878 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4879 | PyObject *rep; |
| 4880 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4881 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4882 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4883 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4884 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4885 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4886 | if (res == -1) |
| 4887 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4888 | if (outsize<requiredsize) |
| 4889 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 4890 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4891 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4892 | outstart[(*outpos)++] = (char)res; |
| 4893 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4897 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4898 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4899 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4900 | Py_DECREF(rep); |
| 4901 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4902 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4903 | if (PyLong_Check(rep)) { |
| 4904 | Py_ssize_t requiredsize = *outpos+1; |
| 4905 | if (outsize<requiredsize) |
| 4906 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 4907 | Py_DECREF(rep); |
| 4908 | return enc_EXCEPTION; |
| 4909 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4910 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4911 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4912 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4913 | else { |
| 4914 | const char *repchars = PyBytes_AS_STRING(rep); |
| 4915 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 4916 | Py_ssize_t requiredsize = *outpos+repsize; |
| 4917 | if (outsize<requiredsize) |
| 4918 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 4919 | Py_DECREF(rep); |
| 4920 | return enc_EXCEPTION; |
| 4921 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4922 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4923 | memcpy(outstart + *outpos, repchars, repsize); |
| 4924 | *outpos += repsize; |
| 4925 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4926 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4927 | Py_DECREF(rep); |
| 4928 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | /* handle an error in PyUnicode_EncodeCharmap |
| 4932 | Return 0 on success, -1 on error */ |
| 4933 | static |
| 4934 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4935 | 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] | 4936 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4937 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4938 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4939 | { |
| 4940 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4941 | Py_ssize_t repsize; |
| 4942 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4943 | Py_UNICODE *uni2; |
| 4944 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4945 | Py_ssize_t collstartpos = *inpos; |
| 4946 | Py_ssize_t collendpos = *inpos+1; |
| 4947 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4948 | char *encoding = "charmap"; |
| 4949 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4950 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4951 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4952 | /* find all unencodable characters */ |
| 4953 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4954 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4955 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4956 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4957 | if (res != -1) |
| 4958 | break; |
| 4959 | ++collendpos; |
| 4960 | continue; |
| 4961 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4962 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4963 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4964 | if (rep==NULL) |
| 4965 | return -1; |
| 4966 | else if (rep!=Py_None) { |
| 4967 | Py_DECREF(rep); |
| 4968 | break; |
| 4969 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4970 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4971 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4972 | } |
| 4973 | /* cache callback name lookup |
| 4974 | * (if not done yet, i.e. it's the first error) */ |
| 4975 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4976 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4977 | *known_errorHandler = 1; |
| 4978 | else if (!strcmp(errors, "replace")) |
| 4979 | *known_errorHandler = 2; |
| 4980 | else if (!strcmp(errors, "ignore")) |
| 4981 | *known_errorHandler = 3; |
| 4982 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4983 | *known_errorHandler = 4; |
| 4984 | else |
| 4985 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4986 | } |
| 4987 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4988 | case 1: /* strict */ |
| 4989 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4990 | return -1; |
| 4991 | case 2: /* replace */ |
| 4992 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4993 | x = charmapencode_output('?', mapping, res, respos); |
| 4994 | if (x==enc_EXCEPTION) { |
| 4995 | return -1; |
| 4996 | } |
| 4997 | else if (x==enc_FAILED) { |
| 4998 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4999 | return -1; |
| 5000 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5001 | } |
| 5002 | /* fall through */ |
| 5003 | case 3: /* ignore */ |
| 5004 | *inpos = collendpos; |
| 5005 | break; |
| 5006 | case 4: /* xmlcharrefreplace */ |
| 5007 | /* generate replacement (temporarily (mis)uses p) */ |
| 5008 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5009 | char buffer[2+29+1+1]; |
| 5010 | char *cp; |
| 5011 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5012 | for (cp = buffer; *cp; ++cp) { |
| 5013 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5014 | if (x==enc_EXCEPTION) |
| 5015 | return -1; |
| 5016 | else if (x==enc_FAILED) { |
| 5017 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5018 | return -1; |
| 5019 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5020 | } |
| 5021 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5022 | *inpos = collendpos; |
| 5023 | break; |
| 5024 | default: |
| 5025 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5026 | encoding, reason, p, size, exceptionObject, |
| 5027 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5028 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5029 | return -1; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5030 | /* generate replacement */ |
| 5031 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5032 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5033 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5034 | if (x==enc_EXCEPTION) { |
| 5035 | return -1; |
| 5036 | } |
| 5037 | else if (x==enc_FAILED) { |
| 5038 | Py_DECREF(repunicode); |
| 5039 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5040 | return -1; |
| 5041 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5042 | } |
| 5043 | *inpos = newpos; |
| 5044 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5045 | } |
| 5046 | return 0; |
| 5047 | } |
| 5048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5049 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5050 | Py_ssize_t size, |
| 5051 | PyObject *mapping, |
| 5052 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5053 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5054 | /* output object */ |
| 5055 | PyObject *res = NULL; |
| 5056 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5057 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5058 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5059 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5060 | PyObject *errorHandler = NULL; |
| 5061 | PyObject *exc = NULL; |
| 5062 | /* the following variable is used for caching string comparisons |
| 5063 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5064 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5065 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5066 | |
| 5067 | /* Default to Latin-1 */ |
| 5068 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5069 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5070 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5071 | /* allocate enough for a simple encoding without |
| 5072 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5073 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5074 | if (res == NULL) |
| 5075 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5076 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5077 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5078 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5079 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5080 | /* try to encode it */ |
| 5081 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5082 | if (x==enc_EXCEPTION) /* error */ |
| 5083 | goto onError; |
| 5084 | if (x==enc_FAILED) { /* unencodable character */ |
| 5085 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5086 | &exc, |
| 5087 | &known_errorHandler, &errorHandler, errors, |
| 5088 | &res, &respos)) { |
| 5089 | goto onError; |
| 5090 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5091 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5092 | else |
| 5093 | /* done with this character => adjust input position */ |
| 5094 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5095 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5096 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5097 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5098 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5099 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5100 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5101 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5102 | Py_XDECREF(exc); |
| 5103 | Py_XDECREF(errorHandler); |
| 5104 | return res; |
| 5105 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5106 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5107 | Py_XDECREF(res); |
| 5108 | Py_XDECREF(exc); |
| 5109 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5110 | return NULL; |
| 5111 | } |
| 5112 | |
| 5113 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5114 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5115 | { |
| 5116 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5117 | PyErr_BadArgument(); |
| 5118 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | } |
| 5120 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5121 | PyUnicode_GET_SIZE(unicode), |
| 5122 | mapping, |
| 5123 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5126 | /* create or adjust a UnicodeTranslateError */ |
| 5127 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5128 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5129 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5130 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5131 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5132 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5133 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5134 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5135 | } |
| 5136 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5137 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5138 | goto onError; |
| 5139 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5140 | goto onError; |
| 5141 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5142 | goto onError; |
| 5143 | return; |
| 5144 | onError: |
| 5145 | Py_DECREF(*exceptionObject); |
| 5146 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | } |
| 5148 | } |
| 5149 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5150 | /* raises a UnicodeTranslateError */ |
| 5151 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5152 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5153 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5154 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5155 | { |
| 5156 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5157 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5158 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5159 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | /* error handling callback helper: |
| 5163 | build arguments, call the callback and check the arguments, |
| 5164 | put the result into newpos and return the replacement string, which |
| 5165 | has to be freed by the caller */ |
| 5166 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5167 | PyObject **errorHandler, |
| 5168 | const char *reason, |
| 5169 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5170 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5171 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5172 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5173 | 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] | 5174 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5175 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5176 | PyObject *restuple; |
| 5177 | PyObject *resunicode; |
| 5178 | |
| 5179 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5180 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5181 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5182 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5186 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5187 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5188 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5189 | |
| 5190 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5191 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5192 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5193 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5194 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5195 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5196 | Py_DECREF(restuple); |
| 5197 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5198 | } |
| 5199 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5200 | &resunicode, &i_newpos)) { |
| 5201 | Py_DECREF(restuple); |
| 5202 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5203 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5204 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5205 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5206 | else |
| 5207 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5208 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5209 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5210 | Py_DECREF(restuple); |
| 5211 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5212 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5213 | Py_INCREF(resunicode); |
| 5214 | Py_DECREF(restuple); |
| 5215 | return resunicode; |
| 5216 | } |
| 5217 | |
| 5218 | /* Lookup the character ch in the mapping and put the result in result, |
| 5219 | which must be decrefed by the caller. |
| 5220 | Return 0 on success, -1 on error */ |
| 5221 | static |
| 5222 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5223 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5224 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5225 | PyObject *x; |
| 5226 | |
| 5227 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5228 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5229 | x = PyObject_GetItem(mapping, w); |
| 5230 | Py_DECREF(w); |
| 5231 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5232 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5233 | /* No mapping found means: use 1:1 mapping. */ |
| 5234 | PyErr_Clear(); |
| 5235 | *result = NULL; |
| 5236 | return 0; |
| 5237 | } else |
| 5238 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5239 | } |
| 5240 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5241 | *result = x; |
| 5242 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5243 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5244 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5245 | long value = PyLong_AS_LONG(x); |
| 5246 | long max = PyUnicode_GetMax(); |
| 5247 | if (value < 0 || value > max) { |
| 5248 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5249 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5250 | Py_DECREF(x); |
| 5251 | return -1; |
| 5252 | } |
| 5253 | *result = x; |
| 5254 | return 0; |
| 5255 | } |
| 5256 | else if (PyUnicode_Check(x)) { |
| 5257 | *result = x; |
| 5258 | return 0; |
| 5259 | } |
| 5260 | else { |
| 5261 | /* wrong return value */ |
| 5262 | PyErr_SetString(PyExc_TypeError, |
| 5263 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5264 | Py_DECREF(x); |
| 5265 | return -1; |
| 5266 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5267 | } |
| 5268 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5269 | if not reallocate and adjust various state variables. |
| 5270 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5271 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5272 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5273 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5274 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5275 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5276 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5277 | /* remember old output position */ |
| 5278 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5279 | /* exponentially overallocate to minimize reallocations */ |
| 5280 | if (requiredsize < 2 * oldsize) |
| 5281 | requiredsize = 2 * oldsize; |
| 5282 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5283 | return -1; |
| 5284 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5285 | } |
| 5286 | return 0; |
| 5287 | } |
| 5288 | /* lookup the character, put the result in the output string and adjust |
| 5289 | various state variables. Return a new reference to the object that |
| 5290 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5291 | undefined (in which case no character was written). |
| 5292 | The called must decref result. |
| 5293 | Return 0 on success, -1 on error. */ |
| 5294 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5295 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5296 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5297 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5298 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5299 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5300 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5301 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5302 | /* not found => default to 1:1 mapping */ |
| 5303 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5304 | } |
| 5305 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5306 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5307 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5308 | /* no overflow check, because we know that the space is enough */ |
| 5309 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5310 | } |
| 5311 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5312 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5313 | if (repsize==1) { |
| 5314 | /* no overflow check, because we know that the space is enough */ |
| 5315 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5316 | } |
| 5317 | else if (repsize!=0) { |
| 5318 | /* more than one character */ |
| 5319 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5320 | (insize - (curinp-startinp)) + |
| 5321 | repsize - 1; |
| 5322 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5323 | return -1; |
| 5324 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5325 | *outp += repsize; |
| 5326 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5327 | } |
| 5328 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5329 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5330 | return 0; |
| 5331 | } |
| 5332 | |
| 5333 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5334 | Py_ssize_t size, |
| 5335 | PyObject *mapping, |
| 5336 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5337 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5338 | /* output object */ |
| 5339 | PyObject *res = NULL; |
| 5340 | /* pointers to the beginning and end+1 of input */ |
| 5341 | const Py_UNICODE *startp = p; |
| 5342 | const Py_UNICODE *endp = p + size; |
| 5343 | /* pointer into the output */ |
| 5344 | Py_UNICODE *str; |
| 5345 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5346 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5347 | char *reason = "character maps to <undefined>"; |
| 5348 | PyObject *errorHandler = NULL; |
| 5349 | PyObject *exc = NULL; |
| 5350 | /* the following variable is used for caching string comparisons |
| 5351 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5352 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5353 | int known_errorHandler = -1; |
| 5354 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5355 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5356 | PyErr_BadArgument(); |
| 5357 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5359 | |
| 5360 | /* allocate enough for a simple 1:1 translation without |
| 5361 | replacements, if we need more, we'll resize */ |
| 5362 | res = PyUnicode_FromUnicode(NULL, size); |
| 5363 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5364 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5365 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5366 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5367 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5369 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5370 | /* try to encode it */ |
| 5371 | PyObject *x = NULL; |
| 5372 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5373 | Py_XDECREF(x); |
| 5374 | goto onError; |
| 5375 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5376 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5377 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5378 | ++p; |
| 5379 | else { /* untranslatable character */ |
| 5380 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5381 | Py_ssize_t repsize; |
| 5382 | Py_ssize_t newpos; |
| 5383 | Py_UNICODE *uni2; |
| 5384 | /* startpos for collecting untranslatable chars */ |
| 5385 | const Py_UNICODE *collstart = p; |
| 5386 | const Py_UNICODE *collend = p+1; |
| 5387 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5388 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5389 | /* find all untranslatable characters */ |
| 5390 | while (collend < endp) { |
| 5391 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5392 | goto onError; |
| 5393 | Py_XDECREF(x); |
| 5394 | if (x!=Py_None) |
| 5395 | break; |
| 5396 | ++collend; |
| 5397 | } |
| 5398 | /* cache callback name lookup |
| 5399 | * (if not done yet, i.e. it's the first error) */ |
| 5400 | if (known_errorHandler==-1) { |
| 5401 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5402 | known_errorHandler = 1; |
| 5403 | else if (!strcmp(errors, "replace")) |
| 5404 | known_errorHandler = 2; |
| 5405 | else if (!strcmp(errors, "ignore")) |
| 5406 | known_errorHandler = 3; |
| 5407 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5408 | known_errorHandler = 4; |
| 5409 | else |
| 5410 | known_errorHandler = 0; |
| 5411 | } |
| 5412 | switch (known_errorHandler) { |
| 5413 | case 1: /* strict */ |
| 5414 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5415 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5416 | case 2: /* replace */ |
| 5417 | /* No need to check for space, this is a 1:1 replacement */ |
| 5418 | for (coll = collstart; coll<collend; ++coll) |
| 5419 | *str++ = '?'; |
| 5420 | /* fall through */ |
| 5421 | case 3: /* ignore */ |
| 5422 | p = collend; |
| 5423 | break; |
| 5424 | case 4: /* xmlcharrefreplace */ |
| 5425 | /* generate replacement (temporarily (mis)uses p) */ |
| 5426 | for (p = collstart; p < collend; ++p) { |
| 5427 | char buffer[2+29+1+1]; |
| 5428 | char *cp; |
| 5429 | sprintf(buffer, "&#%d;", (int)*p); |
| 5430 | if (charmaptranslate_makespace(&res, &str, |
| 5431 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5432 | goto onError; |
| 5433 | for (cp = buffer; *cp; ++cp) |
| 5434 | *str++ = *cp; |
| 5435 | } |
| 5436 | p = collend; |
| 5437 | break; |
| 5438 | default: |
| 5439 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5440 | reason, startp, size, &exc, |
| 5441 | collstart-startp, collend-startp, &newpos); |
| 5442 | if (repunicode == NULL) |
| 5443 | goto onError; |
| 5444 | /* generate replacement */ |
| 5445 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5446 | if (charmaptranslate_makespace(&res, &str, |
| 5447 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5448 | Py_DECREF(repunicode); |
| 5449 | goto onError; |
| 5450 | } |
| 5451 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5452 | *str++ = *uni2; |
| 5453 | p = startp + newpos; |
| 5454 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5455 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5456 | } |
| 5457 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5458 | /* Resize if we allocated to much */ |
| 5459 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5460 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5461 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5462 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5463 | } |
| 5464 | Py_XDECREF(exc); |
| 5465 | Py_XDECREF(errorHandler); |
| 5466 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5467 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5468 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5469 | Py_XDECREF(res); |
| 5470 | Py_XDECREF(exc); |
| 5471 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5472 | return NULL; |
| 5473 | } |
| 5474 | |
| 5475 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5476 | PyObject *mapping, |
| 5477 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5478 | { |
| 5479 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5480 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5481 | str = PyUnicode_FromObject(str); |
| 5482 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5483 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5485 | PyUnicode_GET_SIZE(str), |
| 5486 | mapping, |
| 5487 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5488 | Py_DECREF(str); |
| 5489 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5490 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5491 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | Py_XDECREF(str); |
| 5493 | return NULL; |
| 5494 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5495 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5496 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5497 | |
| 5498 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5499 | Py_ssize_t length, |
| 5500 | char *output, |
| 5501 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5502 | { |
| 5503 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5504 | PyObject *errorHandler = NULL; |
| 5505 | PyObject *exc = NULL; |
| 5506 | const char *encoding = "decimal"; |
| 5507 | const char *reason = "invalid decimal Unicode string"; |
| 5508 | /* the following variable is used for caching string comparisons |
| 5509 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5510 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5511 | |
| 5512 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5513 | PyErr_BadArgument(); |
| 5514 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | p = s; |
| 5518 | end = s + length; |
| 5519 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5520 | register Py_UNICODE ch = *p; |
| 5521 | int decimal; |
| 5522 | PyObject *repunicode; |
| 5523 | Py_ssize_t repsize; |
| 5524 | Py_ssize_t newpos; |
| 5525 | Py_UNICODE *uni2; |
| 5526 | Py_UNICODE *collstart; |
| 5527 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5529 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5530 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5531 | ++p; |
| 5532 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5533 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5534 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5535 | if (decimal >= 0) { |
| 5536 | *output++ = '0' + decimal; |
| 5537 | ++p; |
| 5538 | continue; |
| 5539 | } |
| 5540 | if (0 < ch && ch < 256) { |
| 5541 | *output++ = (char)ch; |
| 5542 | ++p; |
| 5543 | continue; |
| 5544 | } |
| 5545 | /* All other characters are considered unencodable */ |
| 5546 | collstart = p; |
| 5547 | collend = p+1; |
| 5548 | while (collend < end) { |
| 5549 | if ((0 < *collend && *collend < 256) || |
| 5550 | !Py_UNICODE_ISSPACE(*collend) || |
| 5551 | Py_UNICODE_TODECIMAL(*collend)) |
| 5552 | break; |
| 5553 | } |
| 5554 | /* cache callback name lookup |
| 5555 | * (if not done yet, i.e. it's the first error) */ |
| 5556 | if (known_errorHandler==-1) { |
| 5557 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5558 | known_errorHandler = 1; |
| 5559 | else if (!strcmp(errors, "replace")) |
| 5560 | known_errorHandler = 2; |
| 5561 | else if (!strcmp(errors, "ignore")) |
| 5562 | known_errorHandler = 3; |
| 5563 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5564 | known_errorHandler = 4; |
| 5565 | else |
| 5566 | known_errorHandler = 0; |
| 5567 | } |
| 5568 | switch (known_errorHandler) { |
| 5569 | case 1: /* strict */ |
| 5570 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5571 | goto onError; |
| 5572 | case 2: /* replace */ |
| 5573 | for (p = collstart; p < collend; ++p) |
| 5574 | *output++ = '?'; |
| 5575 | /* fall through */ |
| 5576 | case 3: /* ignore */ |
| 5577 | p = collend; |
| 5578 | break; |
| 5579 | case 4: /* xmlcharrefreplace */ |
| 5580 | /* generate replacement (temporarily (mis)uses p) */ |
| 5581 | for (p = collstart; p < collend; ++p) |
| 5582 | output += sprintf(output, "&#%d;", (int)*p); |
| 5583 | p = collend; |
| 5584 | break; |
| 5585 | default: |
| 5586 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5587 | encoding, reason, s, length, &exc, |
| 5588 | collstart-s, collend-s, &newpos); |
| 5589 | if (repunicode == NULL) |
| 5590 | goto onError; |
| 5591 | /* generate replacement */ |
| 5592 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5593 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5594 | Py_UNICODE ch = *uni2; |
| 5595 | if (Py_UNICODE_ISSPACE(ch)) |
| 5596 | *output++ = ' '; |
| 5597 | else { |
| 5598 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5599 | if (decimal >= 0) |
| 5600 | *output++ = '0' + decimal; |
| 5601 | else if (0 < ch && ch < 256) |
| 5602 | *output++ = (char)ch; |
| 5603 | else { |
| 5604 | Py_DECREF(repunicode); |
| 5605 | raise_encode_exception(&exc, encoding, |
| 5606 | s, length, collstart-s, collend-s, reason); |
| 5607 | goto onError; |
| 5608 | } |
| 5609 | } |
| 5610 | } |
| 5611 | p = s + newpos; |
| 5612 | Py_DECREF(repunicode); |
| 5613 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5614 | } |
| 5615 | /* 0-terminate the output string */ |
| 5616 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5617 | Py_XDECREF(exc); |
| 5618 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5619 | return 0; |
| 5620 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5621 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5622 | Py_XDECREF(exc); |
| 5623 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5624 | return -1; |
| 5625 | } |
| 5626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | /* --- Helpers ------------------------------------------------------------ */ |
| 5628 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5629 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5630 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5631 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5632 | /* Include _ParseTupleFinds from find.h */ |
| 5633 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5634 | #include "stringlib/find.h" |
| 5635 | #include "stringlib/partition.h" |
| 5636 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5637 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 5638 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5639 | #include "stringlib/localeutil.h" |
| 5640 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5641 | /* helper macro to fixup start/end slice values */ |
| 5642 | #define FIX_START_END(obj) \ |
| 5643 | if (start < 0) \ |
| 5644 | start += (obj)->length; \ |
| 5645 | if (start < 0) \ |
| 5646 | start = 0; \ |
| 5647 | if (end > (obj)->length) \ |
| 5648 | end = (obj)->length; \ |
| 5649 | if (end < 0) \ |
| 5650 | end += (obj)->length; \ |
| 5651 | if (end < 0) \ |
| 5652 | end = 0; |
| 5653 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5654 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5655 | PyObject *substr, |
| 5656 | Py_ssize_t start, |
| 5657 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5659 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5660 | PyUnicodeObject* str_obj; |
| 5661 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5662 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5663 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5664 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5665 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5666 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5667 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5668 | Py_DECREF(str_obj); |
| 5669 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5671 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5672 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5673 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5674 | result = stringlib_count( |
| 5675 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5676 | ); |
| 5677 | |
| 5678 | Py_DECREF(sub_obj); |
| 5679 | Py_DECREF(str_obj); |
| 5680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5681 | return result; |
| 5682 | } |
| 5683 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5684 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5685 | PyObject *sub, |
| 5686 | Py_ssize_t start, |
| 5687 | Py_ssize_t end, |
| 5688 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5690 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5691 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5692 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5693 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5694 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5695 | sub = PyUnicode_FromObject(sub); |
| 5696 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5697 | Py_DECREF(str); |
| 5698 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5699 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5700 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5701 | if (direction > 0) |
| 5702 | result = stringlib_find_slice( |
| 5703 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5704 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5705 | start, end |
| 5706 | ); |
| 5707 | else |
| 5708 | result = stringlib_rfind_slice( |
| 5709 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5710 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5711 | start, end |
| 5712 | ); |
| 5713 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5714 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5715 | Py_DECREF(sub); |
| 5716 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | return result; |
| 5718 | } |
| 5719 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5720 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5722 | PyUnicodeObject *substring, |
| 5723 | Py_ssize_t start, |
| 5724 | Py_ssize_t end, |
| 5725 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5726 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | if (substring->length == 0) |
| 5728 | return 1; |
| 5729 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5730 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5731 | |
| 5732 | end -= substring->length; |
| 5733 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5734 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | |
| 5736 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5737 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5738 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | } else { |
| 5740 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5741 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5742 | } |
| 5743 | |
| 5744 | return 0; |
| 5745 | } |
| 5746 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5747 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5748 | PyObject *substr, |
| 5749 | Py_ssize_t start, |
| 5750 | Py_ssize_t end, |
| 5751 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5752 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5753 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5754 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5755 | str = PyUnicode_FromObject(str); |
| 5756 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5757 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5758 | substr = PyUnicode_FromObject(substr); |
| 5759 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5760 | Py_DECREF(str); |
| 5761 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5762 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5763 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5765 | (PyUnicodeObject *)substr, |
| 5766 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5767 | Py_DECREF(str); |
| 5768 | Py_DECREF(substr); |
| 5769 | return result; |
| 5770 | } |
| 5771 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | /* Apply fixfct filter to the Unicode object self and return a |
| 5773 | reference to the modified object */ |
| 5774 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5775 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5776 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5777 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5778 | { |
| 5779 | |
| 5780 | PyUnicodeObject *u; |
| 5781 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5782 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5783 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5784 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5785 | |
| 5786 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5787 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5788 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5789 | /* fixfct should return TRUE if it modified the buffer. If |
| 5790 | FALSE, return a reference to the original buffer instead |
| 5791 | (to save space, not time) */ |
| 5792 | Py_INCREF(self); |
| 5793 | Py_DECREF(u); |
| 5794 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | } |
| 5796 | return (PyObject*) u; |
| 5797 | } |
| 5798 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5799 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5800 | int fixupper(PyUnicodeObject *self) |
| 5801 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5802 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5803 | Py_UNICODE *s = self->str; |
| 5804 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5806 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5807 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5808 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5809 | ch = Py_UNICODE_TOUPPER(*s); |
| 5810 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5812 | *s = ch; |
| 5813 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5814 | s++; |
| 5815 | } |
| 5816 | |
| 5817 | return status; |
| 5818 | } |
| 5819 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5820 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5821 | int fixlower(PyUnicodeObject *self) |
| 5822 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5823 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5824 | Py_UNICODE *s = self->str; |
| 5825 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5826 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5827 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5828 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5829 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5830 | ch = Py_UNICODE_TOLOWER(*s); |
| 5831 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5832 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5833 | *s = ch; |
| 5834 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5835 | s++; |
| 5836 | } |
| 5837 | |
| 5838 | return status; |
| 5839 | } |
| 5840 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5841 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5842 | int fixswapcase(PyUnicodeObject *self) |
| 5843 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5844 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5845 | Py_UNICODE *s = self->str; |
| 5846 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5848 | while (len-- > 0) { |
| 5849 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5850 | *s = Py_UNICODE_TOLOWER(*s); |
| 5851 | status = 1; |
| 5852 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5853 | *s = Py_UNICODE_TOUPPER(*s); |
| 5854 | status = 1; |
| 5855 | } |
| 5856 | s++; |
| 5857 | } |
| 5858 | |
| 5859 | return status; |
| 5860 | } |
| 5861 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5862 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5863 | int fixcapitalize(PyUnicodeObject *self) |
| 5864 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5865 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5866 | Py_UNICODE *s = self->str; |
| 5867 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5868 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5869 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5870 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5871 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5872 | *s = Py_UNICODE_TOUPPER(*s); |
| 5873 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5874 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5875 | s++; |
| 5876 | while (--len > 0) { |
| 5877 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5878 | *s = Py_UNICODE_TOLOWER(*s); |
| 5879 | status = 1; |
| 5880 | } |
| 5881 | s++; |
| 5882 | } |
| 5883 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | static |
| 5887 | int fixtitle(PyUnicodeObject *self) |
| 5888 | { |
| 5889 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5890 | register Py_UNICODE *e; |
| 5891 | int previous_is_cased; |
| 5892 | |
| 5893 | /* Shortcut for single character strings */ |
| 5894 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5895 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5896 | if (*p != ch) { |
| 5897 | *p = ch; |
| 5898 | return 1; |
| 5899 | } |
| 5900 | else |
| 5901 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5902 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5903 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5904 | e = p + PyUnicode_GET_SIZE(self); |
| 5905 | previous_is_cased = 0; |
| 5906 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5907 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5908 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5909 | if (previous_is_cased) |
| 5910 | *p = Py_UNICODE_TOLOWER(ch); |
| 5911 | else |
| 5912 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5913 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5914 | if (Py_UNICODE_ISLOWER(ch) || |
| 5915 | Py_UNICODE_ISUPPER(ch) || |
| 5916 | Py_UNICODE_ISTITLE(ch)) |
| 5917 | previous_is_cased = 1; |
| 5918 | else |
| 5919 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | } |
| 5921 | return 1; |
| 5922 | } |
| 5923 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5924 | PyObject * |
| 5925 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5927 | const Py_UNICODE blank = ' '; |
| 5928 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5929 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5930 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5931 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5932 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5933 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 5934 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5935 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5936 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5937 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5938 | fseq = PySequence_Fast(seq, ""); |
| 5939 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5940 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5943 | /* NOTE: the following code can't call back into Python code, |
| 5944 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5945 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5946 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5947 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5948 | /* If empty sequence, return u"". */ |
| 5949 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5950 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5951 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5952 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5953 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5954 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5955 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5956 | item = items[0]; |
| 5957 | if (PyUnicode_CheckExact(item)) { |
| 5958 | Py_INCREF(item); |
| 5959 | res = (PyUnicodeObject *)item; |
| 5960 | goto Done; |
| 5961 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5962 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5963 | else { |
| 5964 | /* Set up sep and seplen */ |
| 5965 | if (separator == NULL) { |
| 5966 | sep = ␣ |
| 5967 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5968 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5969 | else { |
| 5970 | if (!PyUnicode_Check(separator)) { |
| 5971 | PyErr_Format(PyExc_TypeError, |
| 5972 | "separator: expected str instance," |
| 5973 | " %.80s found", |
| 5974 | Py_TYPE(separator)->tp_name); |
| 5975 | goto onError; |
| 5976 | } |
| 5977 | sep = PyUnicode_AS_UNICODE(separator); |
| 5978 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5979 | } |
| 5980 | } |
| 5981 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5982 | /* There are at least two things to join, or else we have a subclass |
| 5983 | * of str in the sequence. |
| 5984 | * Do a pre-pass to figure out the total amount of space we'll |
| 5985 | * need (sz), and see whether all argument are strings. |
| 5986 | */ |
| 5987 | sz = 0; |
| 5988 | for (i = 0; i < seqlen; i++) { |
| 5989 | const Py_ssize_t old_sz = sz; |
| 5990 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5991 | if (!PyUnicode_Check(item)) { |
| 5992 | PyErr_Format(PyExc_TypeError, |
| 5993 | "sequence item %zd: expected str instance," |
| 5994 | " %.80s found", |
| 5995 | i, Py_TYPE(item)->tp_name); |
| 5996 | goto onError; |
| 5997 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5998 | sz += PyUnicode_GET_SIZE(item); |
| 5999 | if (i != 0) |
| 6000 | sz += seplen; |
| 6001 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6002 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6003 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6004 | goto onError; |
| 6005 | } |
| 6006 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6007 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6008 | res = _PyUnicode_New(sz); |
| 6009 | if (res == NULL) |
| 6010 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6011 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6012 | /* Catenate everything. */ |
| 6013 | res_p = PyUnicode_AS_UNICODE(res); |
| 6014 | for (i = 0; i < seqlen; ++i) { |
| 6015 | Py_ssize_t itemlen; |
| 6016 | item = items[i]; |
| 6017 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6018 | /* Copy item, and maybe the separator. */ |
| 6019 | if (i) { |
| 6020 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6021 | res_p += seplen; |
| 6022 | } |
| 6023 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6024 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6025 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6026 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6027 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6028 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | return (PyObject *)res; |
| 6030 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6031 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6032 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6033 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | return NULL; |
| 6035 | } |
| 6036 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6037 | static |
| 6038 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6039 | Py_ssize_t left, |
| 6040 | Py_ssize_t right, |
| 6041 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6042 | { |
| 6043 | PyUnicodeObject *u; |
| 6044 | |
| 6045 | if (left < 0) |
| 6046 | left = 0; |
| 6047 | if (right < 0) |
| 6048 | right = 0; |
| 6049 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6050 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | Py_INCREF(self); |
| 6052 | return self; |
| 6053 | } |
| 6054 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6055 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6056 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6057 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6058 | return NULL; |
| 6059 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6060 | u = _PyUnicode_New(left + self->length + right); |
| 6061 | if (u) { |
| 6062 | if (left) |
| 6063 | Py_UNICODE_FILL(u->str, fill, left); |
| 6064 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6065 | if (right) |
| 6066 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6067 | } |
| 6068 | |
| 6069 | return u; |
| 6070 | } |
| 6071 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6072 | #define SPLIT_APPEND(data, left, right) \ |
| 6073 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 6074 | if (!str) \ |
| 6075 | goto onError; \ |
| 6076 | if (PyList_Append(list, str)) { \ |
| 6077 | Py_DECREF(str); \ |
| 6078 | goto onError; \ |
| 6079 | } \ |
| 6080 | else \ |
| 6081 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | |
| 6083 | static |
| 6084 | PyObject *split_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6085 | PyObject *list, |
| 6086 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6087 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6088 | register Py_ssize_t i; |
| 6089 | register Py_ssize_t j; |
| 6090 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6091 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6092 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | |
| 6094 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6095 | /* find a token */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6096 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6097 | i++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6098 | j = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6099 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
| 6100 | i++; |
| 6101 | if (j < i) { |
| 6102 | if (maxcount-- <= 0) |
| 6103 | break; |
| 6104 | SPLIT_APPEND(buf, j, i); |
| 6105 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
| 6106 | i++; |
| 6107 | j = i; |
| 6108 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | } |
| 6110 | if (j < len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6111 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6112 | } |
| 6113 | return list; |
| 6114 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | Py_DECREF(list); |
| 6117 | return NULL; |
| 6118 | } |
| 6119 | |
| 6120 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6121 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6123 | register Py_ssize_t i; |
| 6124 | register Py_ssize_t j; |
| 6125 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | PyObject *list; |
| 6127 | PyObject *str; |
| 6128 | Py_UNICODE *data; |
| 6129 | |
| 6130 | string = PyUnicode_FromObject(string); |
| 6131 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6132 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | data = PyUnicode_AS_UNICODE(string); |
| 6134 | len = PyUnicode_GET_SIZE(string); |
| 6135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | list = PyList_New(0); |
| 6137 | if (!list) |
| 6138 | goto onError; |
| 6139 | |
| 6140 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6141 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6142 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6143 | /* Find a line and append it */ |
| 6144 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
| 6145 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6147 | /* Skip the line break reading CRLF as one line break */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6148 | eol = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6149 | if (i < len) { |
| 6150 | if (data[i] == '\r' && i + 1 < len && |
| 6151 | data[i+1] == '\n') |
| 6152 | i += 2; |
| 6153 | else |
| 6154 | i++; |
| 6155 | if (keepends) |
| 6156 | eol = i; |
| 6157 | } |
| 6158 | SPLIT_APPEND(data, j, eol); |
| 6159 | j = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6160 | } |
| 6161 | if (j < len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6162 | SPLIT_APPEND(data, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | Py_DECREF(string); |
| 6166 | return list; |
| 6167 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6168 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6169 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6170 | Py_DECREF(string); |
| 6171 | return NULL; |
| 6172 | } |
| 6173 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6174 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | PyObject *split_char(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6176 | PyObject *list, |
| 6177 | Py_UNICODE ch, |
| 6178 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6180 | register Py_ssize_t i; |
| 6181 | register Py_ssize_t j; |
| 6182 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6183 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6184 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6185 | |
| 6186 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6187 | if (buf[i] == ch) { |
| 6188 | if (maxcount-- <= 0) |
| 6189 | break; |
| 6190 | SPLIT_APPEND(buf, j, i); |
| 6191 | i = j = i + 1; |
| 6192 | } else |
| 6193 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | } |
| 6195 | if (j <= len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6196 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6197 | } |
| 6198 | return list; |
| 6199 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6200 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6201 | Py_DECREF(list); |
| 6202 | return NULL; |
| 6203 | } |
| 6204 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6205 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | PyObject *split_substring(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6207 | PyObject *list, |
| 6208 | PyUnicodeObject *substring, |
| 6209 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6210 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6211 | register Py_ssize_t i; |
| 6212 | register Py_ssize_t j; |
| 6213 | Py_ssize_t len = self->length; |
| 6214 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | PyObject *str; |
| 6216 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 6217 | for (i = j = 0; i <= len - sublen; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6218 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6219 | if (maxcount-- <= 0) |
| 6220 | break; |
| 6221 | SPLIT_APPEND(self->str, j, i); |
| 6222 | i = j = i + sublen; |
| 6223 | } else |
| 6224 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6225 | } |
| 6226 | if (j <= len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6227 | SPLIT_APPEND(self->str, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6228 | } |
| 6229 | return list; |
| 6230 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6231 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | Py_DECREF(list); |
| 6233 | return NULL; |
| 6234 | } |
| 6235 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6236 | static |
| 6237 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6238 | PyObject *list, |
| 6239 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6240 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6241 | register Py_ssize_t i; |
| 6242 | register Py_ssize_t j; |
| 6243 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6244 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6245 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6246 | |
| 6247 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6248 | /* find a token */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6249 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6250 | i--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6251 | j = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6252 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
| 6253 | i--; |
| 6254 | if (j > i) { |
| 6255 | if (maxcount-- <= 0) |
| 6256 | break; |
| 6257 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6258 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
| 6259 | i--; |
| 6260 | j = i; |
| 6261 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6262 | } |
| 6263 | if (j >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6264 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6265 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6266 | if (PyList_Reverse(list) < 0) |
| 6267 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6268 | return list; |
| 6269 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6270 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6271 | Py_DECREF(list); |
| 6272 | return NULL; |
| 6273 | } |
| 6274 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6275 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6276 | PyObject *rsplit_char(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | PyObject *list, |
| 6278 | Py_UNICODE ch, |
| 6279 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6280 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6281 | register Py_ssize_t i; |
| 6282 | register Py_ssize_t j; |
| 6283 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6284 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6285 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6286 | |
| 6287 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6288 | if (buf[i] == ch) { |
| 6289 | if (maxcount-- <= 0) |
| 6290 | break; |
| 6291 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6292 | j = i = i - 1; |
| 6293 | } else |
| 6294 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6295 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 6296 | if (j >= -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6298 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6299 | if (PyList_Reverse(list) < 0) |
| 6300 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6301 | return list; |
| 6302 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6303 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6304 | Py_DECREF(list); |
| 6305 | return NULL; |
| 6306 | } |
| 6307 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6308 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6309 | PyObject *rsplit_substring(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6310 | PyObject *list, |
| 6311 | PyUnicodeObject *substring, |
| 6312 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6313 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6314 | register Py_ssize_t i; |
| 6315 | register Py_ssize_t j; |
| 6316 | Py_ssize_t len = self->length; |
| 6317 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6318 | PyObject *str; |
| 6319 | |
| 6320 | for (i = len - sublen, j = len; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6321 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6322 | if (maxcount-- <= 0) |
| 6323 | break; |
| 6324 | SPLIT_APPEND(self->str, i + sublen, j); |
| 6325 | j = i; |
| 6326 | i -= sublen; |
| 6327 | } else |
| 6328 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6329 | } |
| 6330 | if (j >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6331 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6332 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6333 | if (PyList_Reverse(list) < 0) |
| 6334 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6335 | return list; |
| 6336 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6337 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6338 | Py_DECREF(list); |
| 6339 | return NULL; |
| 6340 | } |
| 6341 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | #undef SPLIT_APPEND |
| 6343 | |
| 6344 | static |
| 6345 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6346 | PyUnicodeObject *substring, |
| 6347 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6348 | { |
| 6349 | PyObject *list; |
| 6350 | |
| 6351 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6352 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | |
| 6354 | list = PyList_New(0); |
| 6355 | if (!list) |
| 6356 | return NULL; |
| 6357 | |
| 6358 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6359 | return split_whitespace(self,list,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6360 | |
| 6361 | else if (substring->length == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6362 | return split_char(self,list,substring->str[0],maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6363 | |
| 6364 | else if (substring->length == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6365 | Py_DECREF(list); |
| 6366 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6367 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6368 | } |
| 6369 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6370 | return split_substring(self,list,substring,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6373 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6374 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6375 | PyUnicodeObject *substring, |
| 6376 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6377 | { |
| 6378 | PyObject *list; |
| 6379 | |
| 6380 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6381 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6382 | |
| 6383 | list = PyList_New(0); |
| 6384 | if (!list) |
| 6385 | return NULL; |
| 6386 | |
| 6387 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6388 | return rsplit_whitespace(self,list,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6389 | |
| 6390 | else if (substring->length == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6391 | return rsplit_char(self,list,substring->str[0],maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6392 | |
| 6393 | else if (substring->length == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6394 | Py_DECREF(list); |
| 6395 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6396 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6397 | } |
| 6398 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6399 | return rsplit_substring(self,list,substring,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6403 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6404 | PyUnicodeObject *str1, |
| 6405 | PyUnicodeObject *str2, |
| 6406 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6407 | { |
| 6408 | PyUnicodeObject *u; |
| 6409 | |
| 6410 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6411 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6413 | if (str1->length == str2->length) { |
| 6414 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6415 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6416 | if (str1->length == 1) { |
| 6417 | /* replace characters */ |
| 6418 | Py_UNICODE u1, u2; |
| 6419 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6420 | goto nothing; |
| 6421 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6422 | if (!u) |
| 6423 | return NULL; |
| 6424 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6425 | u1 = str1->str[0]; |
| 6426 | u2 = str2->str[0]; |
| 6427 | for (i = 0; i < u->length; i++) |
| 6428 | if (u->str[i] == u1) { |
| 6429 | if (--maxcount < 0) |
| 6430 | break; |
| 6431 | u->str[i] = u2; |
| 6432 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6433 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6434 | i = fastsearch( |
| 6435 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6436 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6437 | if (i < 0) |
| 6438 | goto nothing; |
| 6439 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6440 | if (!u) |
| 6441 | return NULL; |
| 6442 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6443 | while (i <= self->length - str1->length) |
| 6444 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6445 | if (--maxcount < 0) |
| 6446 | break; |
| 6447 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6448 | i += str1->length; |
| 6449 | } else |
| 6450 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6451 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6453 | |
| 6454 | Py_ssize_t n, i, j, e; |
| 6455 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6456 | Py_UNICODE *p; |
| 6457 | |
| 6458 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6459 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 | if (n > maxcount) |
| 6461 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6462 | if (n == 0) |
| 6463 | goto nothing; |
| 6464 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6465 | delta = (str2->length - str1->length); |
| 6466 | if (delta == 0) { |
| 6467 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6468 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6469 | product = n * (str2->length - str1->length); |
| 6470 | if ((product / (str2->length - str1->length)) != n) { |
| 6471 | PyErr_SetString(PyExc_OverflowError, |
| 6472 | "replace string is too long"); |
| 6473 | return NULL; |
| 6474 | } |
| 6475 | new_size = self->length + product; |
| 6476 | if (new_size < 0) { |
| 6477 | PyErr_SetString(PyExc_OverflowError, |
| 6478 | "replace string is too long"); |
| 6479 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6480 | } |
| 6481 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6482 | u = _PyUnicode_New(new_size); |
| 6483 | if (!u) |
| 6484 | return NULL; |
| 6485 | i = 0; |
| 6486 | p = u->str; |
| 6487 | e = self->length - str1->length; |
| 6488 | if (str1->length > 0) { |
| 6489 | while (n-- > 0) { |
| 6490 | /* look for next match */ |
| 6491 | j = i; |
| 6492 | while (j <= e) { |
| 6493 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6494 | break; |
| 6495 | j++; |
| 6496 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6498 | if (j > e) |
| 6499 | break; |
| 6500 | /* copy unchanged part [i:j] */ |
| 6501 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6502 | p += j - i; |
| 6503 | } |
| 6504 | /* copy substitution string */ |
| 6505 | if (str2->length > 0) { |
| 6506 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6507 | p += str2->length; |
| 6508 | } |
| 6509 | i = j + str1->length; |
| 6510 | } |
| 6511 | if (i < self->length) |
| 6512 | /* copy tail [i:] */ |
| 6513 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6514 | } else { |
| 6515 | /* interleave */ |
| 6516 | while (n > 0) { |
| 6517 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6518 | p += str2->length; |
| 6519 | if (--n <= 0) |
| 6520 | break; |
| 6521 | *p++ = self->str[i++]; |
| 6522 | } |
| 6523 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6524 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6525 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6526 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6527 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6528 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6529 | /* nothing to replace; return original string (when possible) */ |
| 6530 | if (PyUnicode_CheckExact(self)) { |
| 6531 | Py_INCREF(self); |
| 6532 | return (PyObject *) self; |
| 6533 | } |
| 6534 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
| 6537 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6539 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6540 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6541 | \n\ |
| 6542 | 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] | 6543 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6544 | |
| 6545 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6546 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6547 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6548 | return fixup(self, fixtitle); |
| 6549 | } |
| 6550 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6551 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6552 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6553 | \n\ |
| 6554 | 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] | 6555 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | |
| 6557 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6558 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6559 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | return fixup(self, fixcapitalize); |
| 6561 | } |
| 6562 | |
| 6563 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6564 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6565 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6566 | \n\ |
| 6567 | 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] | 6568 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6569 | |
| 6570 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6571 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6572 | { |
| 6573 | PyObject *list; |
| 6574 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6575 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | /* Split into words */ |
| 6578 | list = split(self, NULL, -1); |
| 6579 | if (!list) |
| 6580 | return NULL; |
| 6581 | |
| 6582 | /* Capitalize each word */ |
| 6583 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6584 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6585 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | if (item == NULL) |
| 6587 | goto onError; |
| 6588 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6589 | PyList_SET_ITEM(list, i, item); |
| 6590 | } |
| 6591 | |
| 6592 | /* Join the words to form a new string */ |
| 6593 | item = PyUnicode_Join(NULL, list); |
| 6594 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6595 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6596 | Py_DECREF(list); |
| 6597 | return (PyObject *)item; |
| 6598 | } |
| 6599 | #endif |
| 6600 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6601 | /* Argument converter. Coerces to a single unicode character */ |
| 6602 | |
| 6603 | static int |
| 6604 | convert_uc(PyObject *obj, void *addr) |
| 6605 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6606 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6607 | PyObject *uniobj; |
| 6608 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6609 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6610 | uniobj = PyUnicode_FromObject(obj); |
| 6611 | if (uniobj == NULL) { |
| 6612 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6613 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6614 | return 0; |
| 6615 | } |
| 6616 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6617 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6618 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6619 | Py_DECREF(uniobj); |
| 6620 | return 0; |
| 6621 | } |
| 6622 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6623 | *fillcharloc = unistr[0]; |
| 6624 | Py_DECREF(uniobj); |
| 6625 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6628 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6629 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6630 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6631 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6632 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6633 | |
| 6634 | static PyObject * |
| 6635 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6636 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6637 | Py_ssize_t marg, left; |
| 6638 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6639 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6640 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6641 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6642 | return NULL; |
| 6643 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6644 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | Py_INCREF(self); |
| 6646 | return (PyObject*) self; |
| 6647 | } |
| 6648 | |
| 6649 | marg = width - self->length; |
| 6650 | left = marg / 2 + (marg & width & 1); |
| 6651 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6652 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | } |
| 6654 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6655 | #if 0 |
| 6656 | |
| 6657 | /* This code should go into some future Unicode collation support |
| 6658 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6659 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6660 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6661 | /* speedy UTF-16 code point order comparison */ |
| 6662 | /* gleaned from: */ |
| 6663 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6664 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6665 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6666 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6667 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6668 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6669 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6670 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6671 | }; |
| 6672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6673 | static int |
| 6674 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6675 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6676 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6677 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | Py_UNICODE *s1 = str1->str; |
| 6679 | Py_UNICODE *s2 = str2->str; |
| 6680 | |
| 6681 | len1 = str1->length; |
| 6682 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6683 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6685 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6686 | |
| 6687 | c1 = *s1++; |
| 6688 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6689 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6690 | if (c1 > (1<<11) * 26) |
| 6691 | c1 += utf16Fixup[c1>>11]; |
| 6692 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6693 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6694 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6695 | |
| 6696 | if (c1 != c2) |
| 6697 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6698 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6699 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6700 | } |
| 6701 | |
| 6702 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6703 | } |
| 6704 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6705 | #else |
| 6706 | |
| 6707 | static int |
| 6708 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6709 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6710 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6711 | |
| 6712 | Py_UNICODE *s1 = str1->str; |
| 6713 | Py_UNICODE *s2 = str2->str; |
| 6714 | |
| 6715 | len1 = str1->length; |
| 6716 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6717 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6718 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6719 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6720 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6721 | c1 = *s1++; |
| 6722 | c2 = *s2++; |
| 6723 | |
| 6724 | if (c1 != c2) |
| 6725 | return (c1 < c2) ? -1 : 1; |
| 6726 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6727 | len1--; len2--; |
| 6728 | } |
| 6729 | |
| 6730 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6731 | } |
| 6732 | |
| 6733 | #endif |
| 6734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6736 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6737 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6738 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6739 | return unicode_compare((PyUnicodeObject *)left, |
| 6740 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6741 | PyErr_Format(PyExc_TypeError, |
| 6742 | "Can't compare %.100s and %.100s", |
| 6743 | left->ob_type->tp_name, |
| 6744 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6745 | return -1; |
| 6746 | } |
| 6747 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6748 | int |
| 6749 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6750 | { |
| 6751 | int i; |
| 6752 | Py_UNICODE *id; |
| 6753 | assert(PyUnicode_Check(uni)); |
| 6754 | id = PyUnicode_AS_UNICODE(uni); |
| 6755 | /* Compare Unicode string and source character set string */ |
| 6756 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6757 | if (id[i] != str[i]) |
| 6758 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6759 | if (id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6760 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6761 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6762 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6763 | return 0; |
| 6764 | } |
| 6765 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6766 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6768 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6769 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6770 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6771 | PyObject *right, |
| 6772 | int op) |
| 6773 | { |
| 6774 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6775 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6776 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6777 | PyObject *v; |
| 6778 | if (((PyUnicodeObject *) left)->length != |
| 6779 | ((PyUnicodeObject *) right)->length) { |
| 6780 | if (op == Py_EQ) { |
| 6781 | Py_INCREF(Py_False); |
| 6782 | return Py_False; |
| 6783 | } |
| 6784 | if (op == Py_NE) { |
| 6785 | Py_INCREF(Py_True); |
| 6786 | return Py_True; |
| 6787 | } |
| 6788 | } |
| 6789 | if (left == right) |
| 6790 | result = 0; |
| 6791 | else |
| 6792 | result = unicode_compare((PyUnicodeObject *)left, |
| 6793 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6794 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6795 | /* Convert the return value to a Boolean */ |
| 6796 | switch (op) { |
| 6797 | case Py_EQ: |
| 6798 | v = TEST_COND(result == 0); |
| 6799 | break; |
| 6800 | case Py_NE: |
| 6801 | v = TEST_COND(result != 0); |
| 6802 | break; |
| 6803 | case Py_LE: |
| 6804 | v = TEST_COND(result <= 0); |
| 6805 | break; |
| 6806 | case Py_GE: |
| 6807 | v = TEST_COND(result >= 0); |
| 6808 | break; |
| 6809 | case Py_LT: |
| 6810 | v = TEST_COND(result == -1); |
| 6811 | break; |
| 6812 | case Py_GT: |
| 6813 | v = TEST_COND(result == 1); |
| 6814 | break; |
| 6815 | default: |
| 6816 | PyErr_BadArgument(); |
| 6817 | return NULL; |
| 6818 | } |
| 6819 | Py_INCREF(v); |
| 6820 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6821 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6822 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6823 | Py_INCREF(Py_NotImplemented); |
| 6824 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6825 | } |
| 6826 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6827 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6828 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6829 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6830 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6831 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6832 | |
| 6833 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6834 | sub = PyUnicode_FromObject(element); |
| 6835 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6836 | PyErr_Format(PyExc_TypeError, |
| 6837 | "'in <string>' requires string as left operand, not %s", |
| 6838 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6839 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6840 | } |
| 6841 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6842 | str = PyUnicode_FromObject(container); |
| 6843 | if (!str) { |
| 6844 | Py_DECREF(sub); |
| 6845 | return -1; |
| 6846 | } |
| 6847 | |
| 6848 | result = stringlib_contains_obj(str, sub); |
| 6849 | |
| 6850 | Py_DECREF(str); |
| 6851 | Py_DECREF(sub); |
| 6852 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6853 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6857 | |
| 6858 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6859 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6860 | { |
| 6861 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6862 | |
| 6863 | /* Coerce the two arguments */ |
| 6864 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6865 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6866 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6868 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6869 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | |
| 6871 | /* Shortcuts */ |
| 6872 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6873 | Py_DECREF(v); |
| 6874 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6875 | } |
| 6876 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6877 | Py_DECREF(u); |
| 6878 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6879 | } |
| 6880 | |
| 6881 | /* Concat the two Unicode strings */ |
| 6882 | w = _PyUnicode_New(u->length + v->length); |
| 6883 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6884 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6885 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6886 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6887 | |
| 6888 | Py_DECREF(u); |
| 6889 | Py_DECREF(v); |
| 6890 | return (PyObject *)w; |
| 6891 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6892 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6893 | Py_XDECREF(u); |
| 6894 | Py_XDECREF(v); |
| 6895 | return NULL; |
| 6896 | } |
| 6897 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6898 | void |
| 6899 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6900 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6901 | PyObject *new; |
| 6902 | if (*pleft == NULL) |
| 6903 | return; |
| 6904 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6905 | Py_DECREF(*pleft); |
| 6906 | *pleft = NULL; |
| 6907 | return; |
| 6908 | } |
| 6909 | new = PyUnicode_Concat(*pleft, right); |
| 6910 | Py_DECREF(*pleft); |
| 6911 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6912 | } |
| 6913 | |
| 6914 | void |
| 6915 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6916 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6917 | PyUnicode_Append(pleft, right); |
| 6918 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6919 | } |
| 6920 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6921 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6922 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6923 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6924 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6925 | 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] | 6926 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6927 | |
| 6928 | static PyObject * |
| 6929 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6930 | { |
| 6931 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6932 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6933 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | PyObject *result; |
| 6935 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6936 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6937 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | return NULL; |
| 6939 | |
| 6940 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6941 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6942 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6943 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6944 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6945 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6947 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6948 | stringlib_count(self->str + start, end - start, |
| 6949 | substring->str, substring->length) |
| 6950 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6951 | |
| 6952 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6954 | return result; |
| 6955 | } |
| 6956 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6957 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6958 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6959 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6960 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6961 | 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] | 6962 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6963 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6964 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6965 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6966 | |
| 6967 | static PyObject * |
| 6968 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6969 | { |
| 6970 | char *encoding = NULL; |
| 6971 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6972 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6973 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6974 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6975 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 6976 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6977 | if (v == NULL) |
| 6978 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6979 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6980 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6981 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6982 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6983 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6984 | Py_DECREF(v); |
| 6985 | return NULL; |
| 6986 | } |
| 6987 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6988 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6989 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6990 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6991 | } |
| 6992 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6993 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6994 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6995 | \n\ |
| 6996 | 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] | 6997 | 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] | 6998 | |
| 6999 | static PyObject* |
| 7000 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7001 | { |
| 7002 | Py_UNICODE *e; |
| 7003 | Py_UNICODE *p; |
| 7004 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7005 | Py_UNICODE *qe; |
| 7006 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7007 | PyUnicodeObject *u; |
| 7008 | int tabsize = 8; |
| 7009 | |
| 7010 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7011 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7012 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7013 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7014 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7015 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7016 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7017 | for (p = self->str; p < e; p++) |
| 7018 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7019 | if (tabsize > 0) { |
| 7020 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7021 | if (j > PY_SSIZE_T_MAX - incr) |
| 7022 | goto overflow1; |
| 7023 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7024 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7025 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7026 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7027 | if (j > PY_SSIZE_T_MAX - 1) |
| 7028 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7029 | j++; |
| 7030 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7031 | if (i > PY_SSIZE_T_MAX - j) |
| 7032 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7033 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7034 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7035 | } |
| 7036 | } |
| 7037 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7038 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7039 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7040 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7041 | /* Second pass: create output string and fill it */ |
| 7042 | u = _PyUnicode_New(i + j); |
| 7043 | if (!u) |
| 7044 | return NULL; |
| 7045 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7046 | j = 0; /* same as in first pass */ |
| 7047 | q = u->str; /* next output char */ |
| 7048 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7049 | |
| 7050 | for (p = self->str; p < e; p++) |
| 7051 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7052 | if (tabsize > 0) { |
| 7053 | i = tabsize - (j % tabsize); |
| 7054 | j += i; |
| 7055 | while (i--) { |
| 7056 | if (q >= qe) |
| 7057 | goto overflow2; |
| 7058 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7059 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7060 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7061 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7062 | else { |
| 7063 | if (q >= qe) |
| 7064 | goto overflow2; |
| 7065 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7066 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7067 | if (*p == '\n' || *p == '\r') |
| 7068 | j = 0; |
| 7069 | } |
| 7070 | |
| 7071 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7072 | |
| 7073 | overflow2: |
| 7074 | Py_DECREF(u); |
| 7075 | overflow1: |
| 7076 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7077 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7078 | } |
| 7079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7080 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7081 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7082 | \n\ |
| 7083 | 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] | 7084 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | arguments start and end are interpreted as in slice notation.\n\ |
| 7086 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7087 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | |
| 7089 | static PyObject * |
| 7090 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7091 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7092 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7093 | Py_ssize_t start; |
| 7094 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7095 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7096 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7097 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7098 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7099 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7100 | result = stringlib_find_slice( |
| 7101 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7102 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7103 | start, end |
| 7104 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7105 | |
| 7106 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7107 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7108 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7109 | } |
| 7110 | |
| 7111 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7112 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7113 | { |
| 7114 | if (index < 0 || index >= self->length) { |
| 7115 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7116 | return NULL; |
| 7117 | } |
| 7118 | |
| 7119 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7120 | } |
| 7121 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7122 | /* Believe it or not, this produces the same value for ASCII strings |
| 7123 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7124 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7125 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7126 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7127 | Py_ssize_t len; |
| 7128 | Py_UNICODE *p; |
| 7129 | long x; |
| 7130 | |
| 7131 | if (self->hash != -1) |
| 7132 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7133 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7134 | p = self->str; |
| 7135 | x = *p << 7; |
| 7136 | while (--len >= 0) |
| 7137 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7138 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7139 | if (x == -1) |
| 7140 | x = -2; |
| 7141 | self->hash = x; |
| 7142 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | } |
| 7144 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7145 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7146 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7147 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7148 | 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] | 7149 | |
| 7150 | static PyObject * |
| 7151 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7152 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7153 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7154 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7155 | Py_ssize_t start; |
| 7156 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7157 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7158 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7159 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7160 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7161 | result = stringlib_find_slice( |
| 7162 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7163 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7164 | start, end |
| 7165 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7166 | |
| 7167 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7169 | if (result < 0) { |
| 7170 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7171 | return NULL; |
| 7172 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7173 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7174 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7175 | } |
| 7176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7177 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7178 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7179 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7180 | 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] | 7181 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | |
| 7183 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7184 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7185 | { |
| 7186 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7187 | register const Py_UNICODE *e; |
| 7188 | int cased; |
| 7189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7190 | /* Shortcut for single character strings */ |
| 7191 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7192 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7193 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7194 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7195 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7196 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7198 | e = p + PyUnicode_GET_SIZE(self); |
| 7199 | cased = 0; |
| 7200 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7201 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7202 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7203 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7204 | return PyBool_FromLong(0); |
| 7205 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7206 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7207 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7208 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7209 | } |
| 7210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7211 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7212 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7213 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7214 | 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] | 7215 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7216 | |
| 7217 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7218 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7219 | { |
| 7220 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7221 | register const Py_UNICODE *e; |
| 7222 | int cased; |
| 7223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7224 | /* Shortcut for single character strings */ |
| 7225 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7226 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7227 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7228 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7229 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7230 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7232 | e = p + PyUnicode_GET_SIZE(self); |
| 7233 | cased = 0; |
| 7234 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7235 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7236 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7237 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7238 | return PyBool_FromLong(0); |
| 7239 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7240 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7241 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7242 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7243 | } |
| 7244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7245 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7246 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7247 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7248 | Return True if S is a titlecased string and there is at least one\n\ |
| 7249 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7250 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7251 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7252 | |
| 7253 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7254 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7255 | { |
| 7256 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7257 | register const Py_UNICODE *e; |
| 7258 | int cased, previous_is_cased; |
| 7259 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7260 | /* Shortcut for single character strings */ |
| 7261 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7262 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7263 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7264 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7265 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7266 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7267 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7269 | e = p + PyUnicode_GET_SIZE(self); |
| 7270 | cased = 0; |
| 7271 | previous_is_cased = 0; |
| 7272 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7273 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7274 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7275 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7276 | if (previous_is_cased) |
| 7277 | return PyBool_FromLong(0); |
| 7278 | previous_is_cased = 1; |
| 7279 | cased = 1; |
| 7280 | } |
| 7281 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7282 | if (!previous_is_cased) |
| 7283 | return PyBool_FromLong(0); |
| 7284 | previous_is_cased = 1; |
| 7285 | cased = 1; |
| 7286 | } |
| 7287 | else |
| 7288 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7289 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7290 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7291 | } |
| 7292 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7293 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7294 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7295 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7296 | Return True if all characters in S are whitespace\n\ |
| 7297 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | |
| 7299 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7300 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7301 | { |
| 7302 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7303 | register const Py_UNICODE *e; |
| 7304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7305 | /* Shortcut for single character strings */ |
| 7306 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7307 | Py_UNICODE_ISSPACE(*p)) |
| 7308 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7309 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7310 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7311 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7312 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7313 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7314 | e = p + PyUnicode_GET_SIZE(self); |
| 7315 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7316 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7317 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7318 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7319 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7320 | } |
| 7321 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7322 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7323 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7324 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7325 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7326 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7327 | |
| 7328 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7329 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7330 | { |
| 7331 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7332 | register const Py_UNICODE *e; |
| 7333 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7334 | /* Shortcut for single character strings */ |
| 7335 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7336 | Py_UNICODE_ISALPHA(*p)) |
| 7337 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7338 | |
| 7339 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7340 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7341 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7342 | |
| 7343 | e = p + PyUnicode_GET_SIZE(self); |
| 7344 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7345 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7346 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7347 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7348 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7349 | } |
| 7350 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7351 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7352 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7353 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7354 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7355 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7356 | |
| 7357 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7358 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7359 | { |
| 7360 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7361 | register const Py_UNICODE *e; |
| 7362 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7363 | /* Shortcut for single character strings */ |
| 7364 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7365 | Py_UNICODE_ISALNUM(*p)) |
| 7366 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7367 | |
| 7368 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7369 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7370 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7371 | |
| 7372 | e = p + PyUnicode_GET_SIZE(self); |
| 7373 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7374 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7375 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7376 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7377 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7378 | } |
| 7379 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7380 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7381 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7382 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7383 | 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] | 7384 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7385 | |
| 7386 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7387 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7388 | { |
| 7389 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7390 | register const Py_UNICODE *e; |
| 7391 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7392 | /* Shortcut for single character strings */ |
| 7393 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7394 | Py_UNICODE_ISDECIMAL(*p)) |
| 7395 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7396 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7397 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7398 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7399 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7401 | e = p + PyUnicode_GET_SIZE(self); |
| 7402 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7403 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7404 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7405 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7406 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7407 | } |
| 7408 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7409 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7410 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7412 | Return True if all characters in S are digits\n\ |
| 7413 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7414 | |
| 7415 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7416 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7417 | { |
| 7418 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7419 | register const Py_UNICODE *e; |
| 7420 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7421 | /* Shortcut for single character strings */ |
| 7422 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7423 | Py_UNICODE_ISDIGIT(*p)) |
| 7424 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7426 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7427 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7428 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7430 | e = p + PyUnicode_GET_SIZE(self); |
| 7431 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7432 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7433 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7434 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7435 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7436 | } |
| 7437 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7438 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7439 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7441 | 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] | 7442 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7443 | |
| 7444 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7445 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7446 | { |
| 7447 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7448 | register const Py_UNICODE *e; |
| 7449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7450 | /* Shortcut for single character strings */ |
| 7451 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7452 | Py_UNICODE_ISNUMERIC(*p)) |
| 7453 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7454 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7455 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7456 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7457 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7459 | e = p + PyUnicode_GET_SIZE(self); |
| 7460 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7461 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7462 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7463 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7464 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7465 | } |
| 7466 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7467 | int |
| 7468 | PyUnicode_IsIdentifier(PyObject *self) |
| 7469 | { |
| 7470 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7471 | register const Py_UNICODE *e; |
| 7472 | |
| 7473 | /* Special case for empty strings */ |
| 7474 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7475 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7476 | |
| 7477 | /* PEP 3131 says that the first character must be in |
| 7478 | XID_Start and subsequent characters in XID_Continue, |
| 7479 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7480 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7481 | letters, digits, underscore). However, given the current |
| 7482 | definition of XID_Start and XID_Continue, it is sufficient |
| 7483 | to check just for these, except that _ must be allowed |
| 7484 | as starting an identifier. */ |
| 7485 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7486 | return 0; |
| 7487 | |
| 7488 | e = p + PyUnicode_GET_SIZE(self); |
| 7489 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7490 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7491 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7492 | } |
| 7493 | return 1; |
| 7494 | } |
| 7495 | |
| 7496 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7497 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7498 | \n\ |
| 7499 | Return True if S is a valid identifier according\n\ |
| 7500 | to the language definition."); |
| 7501 | |
| 7502 | static PyObject* |
| 7503 | unicode_isidentifier(PyObject *self) |
| 7504 | { |
| 7505 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7506 | } |
| 7507 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7508 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7509 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7510 | \n\ |
| 7511 | Return True if all characters in S are considered\n\ |
| 7512 | printable in repr() or S is empty, False otherwise."); |
| 7513 | |
| 7514 | static PyObject* |
| 7515 | unicode_isprintable(PyObject *self) |
| 7516 | { |
| 7517 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7518 | register const Py_UNICODE *e; |
| 7519 | |
| 7520 | /* Shortcut for single character strings */ |
| 7521 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7522 | Py_RETURN_TRUE; |
| 7523 | } |
| 7524 | |
| 7525 | e = p + PyUnicode_GET_SIZE(self); |
| 7526 | for (; p < e; p++) { |
| 7527 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7528 | Py_RETURN_FALSE; |
| 7529 | } |
| 7530 | } |
| 7531 | Py_RETURN_TRUE; |
| 7532 | } |
| 7533 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7534 | PyDoc_STRVAR(join__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7535 | "S.join(sequence) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7536 | \n\ |
| 7537 | Return a string which is the concatenation of the strings in the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7538 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7539 | |
| 7540 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7541 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7542 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7543 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7544 | } |
| 7545 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7546 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7547 | unicode_length(PyUnicodeObject *self) |
| 7548 | { |
| 7549 | return self->length; |
| 7550 | } |
| 7551 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7552 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7553 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7554 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7555 | 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] | 7556 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7557 | |
| 7558 | static PyObject * |
| 7559 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7560 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7561 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7562 | Py_UNICODE fillchar = ' '; |
| 7563 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7564 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7565 | return NULL; |
| 7566 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7567 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7568 | Py_INCREF(self); |
| 7569 | return (PyObject*) self; |
| 7570 | } |
| 7571 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7572 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | } |
| 7574 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7575 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7576 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7577 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7578 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7579 | |
| 7580 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7581 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7582 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7583 | return fixup(self, fixlower); |
| 7584 | } |
| 7585 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7586 | #define LEFTSTRIP 0 |
| 7587 | #define RIGHTSTRIP 1 |
| 7588 | #define BOTHSTRIP 2 |
| 7589 | |
| 7590 | /* Arrays indexed by above */ |
| 7591 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7592 | |
| 7593 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7594 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7595 | /* externally visible for str.strip(unicode) */ |
| 7596 | PyObject * |
| 7597 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7598 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7599 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7600 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7601 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7602 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7603 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7604 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7605 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7606 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7607 | i = 0; |
| 7608 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7609 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7610 | i++; |
| 7611 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7612 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7613 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7614 | j = len; |
| 7615 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7616 | do { |
| 7617 | j--; |
| 7618 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7619 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7620 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7621 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7622 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7623 | Py_INCREF(self); |
| 7624 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7625 | } |
| 7626 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7627 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7628 | } |
| 7629 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7630 | |
| 7631 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7632 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7633 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7634 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7635 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7636 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7637 | i = 0; |
| 7638 | if (striptype != RIGHTSTRIP) { |
| 7639 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7640 | i++; |
| 7641 | } |
| 7642 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7643 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7644 | j = len; |
| 7645 | if (striptype != LEFTSTRIP) { |
| 7646 | do { |
| 7647 | j--; |
| 7648 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7649 | j++; |
| 7650 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7651 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7652 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7653 | Py_INCREF(self); |
| 7654 | return (PyObject*)self; |
| 7655 | } |
| 7656 | else |
| 7657 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7658 | } |
| 7659 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7660 | |
| 7661 | static PyObject * |
| 7662 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7663 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7664 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7665 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7666 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7667 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7668 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7669 | if (sep != NULL && sep != Py_None) { |
| 7670 | if (PyUnicode_Check(sep)) |
| 7671 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7672 | else { |
| 7673 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7674 | "%s arg must be None or str", |
| 7675 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7676 | return NULL; |
| 7677 | } |
| 7678 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7679 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7680 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7681 | } |
| 7682 | |
| 7683 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7684 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7685 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7686 | \n\ |
| 7687 | Return a copy of the string S with leading and trailing\n\ |
| 7688 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7689 | 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] | 7690 | |
| 7691 | static PyObject * |
| 7692 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7693 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7694 | if (PyTuple_GET_SIZE(args) == 0) |
| 7695 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7696 | else |
| 7697 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7698 | } |
| 7699 | |
| 7700 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7701 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7702 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7703 | \n\ |
| 7704 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7705 | 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] | 7706 | |
| 7707 | static PyObject * |
| 7708 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7709 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7710 | if (PyTuple_GET_SIZE(args) == 0) |
| 7711 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7712 | else |
| 7713 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7714 | } |
| 7715 | |
| 7716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7717 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7719 | \n\ |
| 7720 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7721 | 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] | 7722 | |
| 7723 | static PyObject * |
| 7724 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7725 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7726 | if (PyTuple_GET_SIZE(args) == 0) |
| 7727 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7728 | else |
| 7729 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7730 | } |
| 7731 | |
| 7732 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7733 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7734 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7735 | { |
| 7736 | PyUnicodeObject *u; |
| 7737 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7738 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7739 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7740 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7741 | if (len < 1) { |
| 7742 | Py_INCREF(unicode_empty); |
| 7743 | return (PyObject *)unicode_empty; |
| 7744 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7746 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7747 | /* no repeat, return original string */ |
| 7748 | Py_INCREF(str); |
| 7749 | return (PyObject*) str; |
| 7750 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7751 | |
| 7752 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7753 | * needed doesn't overflow size_t |
| 7754 | */ |
| 7755 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7756 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7757 | PyErr_SetString(PyExc_OverflowError, |
| 7758 | "repeated string is too long"); |
| 7759 | return NULL; |
| 7760 | } |
| 7761 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7762 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7763 | PyErr_SetString(PyExc_OverflowError, |
| 7764 | "repeated string is too long"); |
| 7765 | return NULL; |
| 7766 | } |
| 7767 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7768 | if (!u) |
| 7769 | return NULL; |
| 7770 | |
| 7771 | p = u->str; |
| 7772 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7773 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7774 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7775 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7776 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7777 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7778 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7779 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7780 | Py_UNICODE_COPY(p+done, p, n); |
| 7781 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7782 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7783 | } |
| 7784 | |
| 7785 | return (PyObject*) u; |
| 7786 | } |
| 7787 | |
| 7788 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7789 | PyObject *subobj, |
| 7790 | PyObject *replobj, |
| 7791 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7792 | { |
| 7793 | PyObject *self; |
| 7794 | PyObject *str1; |
| 7795 | PyObject *str2; |
| 7796 | PyObject *result; |
| 7797 | |
| 7798 | self = PyUnicode_FromObject(obj); |
| 7799 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7800 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7801 | str1 = PyUnicode_FromObject(subobj); |
| 7802 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7803 | Py_DECREF(self); |
| 7804 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7805 | } |
| 7806 | str2 = PyUnicode_FromObject(replobj); |
| 7807 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7808 | Py_DECREF(self); |
| 7809 | Py_DECREF(str1); |
| 7810 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7811 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7812 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7813 | (PyUnicodeObject *)str1, |
| 7814 | (PyUnicodeObject *)str2, |
| 7815 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7816 | Py_DECREF(self); |
| 7817 | Py_DECREF(str1); |
| 7818 | Py_DECREF(str2); |
| 7819 | return result; |
| 7820 | } |
| 7821 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7822 | PyDoc_STRVAR(replace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7823 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7824 | \n\ |
| 7825 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7826 | old replaced by new. If the optional argument count is\n\ |
| 7827 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 | |
| 7829 | static PyObject* |
| 7830 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7831 | { |
| 7832 | PyUnicodeObject *str1; |
| 7833 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7834 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7835 | PyObject *result; |
| 7836 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7837 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7838 | return NULL; |
| 7839 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7840 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7843 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7844 | Py_DECREF(str1); |
| 7845 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7846 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7847 | |
| 7848 | result = replace(self, str1, str2, maxcount); |
| 7849 | |
| 7850 | Py_DECREF(str1); |
| 7851 | Py_DECREF(str2); |
| 7852 | return result; |
| 7853 | } |
| 7854 | |
| 7855 | static |
| 7856 | PyObject *unicode_repr(PyObject *unicode) |
| 7857 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7858 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7859 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7860 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7861 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7862 | |
| 7863 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7864 | better to choose a different scheme. Perhaps scan the |
| 7865 | first N-chars of the string and allocate based on that size. |
| 7866 | */ |
| 7867 | /* Initial allocation is based on the longest-possible unichr |
| 7868 | escape. |
| 7869 | |
| 7870 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7871 | unichr, so in this case it's the longest unichr escape. In |
| 7872 | narrow (UTF-16) builds this is five chars per source unichr |
| 7873 | since there are two unichrs in the surrogate pair, so in narrow |
| 7874 | (UTF-16) builds it's not the longest unichr escape. |
| 7875 | |
| 7876 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7877 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7878 | escape. |
| 7879 | */ |
| 7880 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7881 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7882 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7883 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7884 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7885 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7886 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7887 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7888 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7889 | if (repr == NULL) |
| 7890 | return NULL; |
| 7891 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7892 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7893 | |
| 7894 | /* Add quote */ |
| 7895 | *p++ = (findchar(s, size, '\'') && |
| 7896 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7897 | while (size-- > 0) { |
| 7898 | Py_UNICODE ch = *s++; |
| 7899 | |
| 7900 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7901 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7902 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7903 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7904 | continue; |
| 7905 | } |
| 7906 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7907 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7908 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7909 | *p++ = '\\'; |
| 7910 | *p++ = 't'; |
| 7911 | } |
| 7912 | else if (ch == '\n') { |
| 7913 | *p++ = '\\'; |
| 7914 | *p++ = 'n'; |
| 7915 | } |
| 7916 | else if (ch == '\r') { |
| 7917 | *p++ = '\\'; |
| 7918 | *p++ = 'r'; |
| 7919 | } |
| 7920 | |
| 7921 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7922 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7923 | *p++ = '\\'; |
| 7924 | *p++ = 'x'; |
| 7925 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7926 | *p++ = hexdigits[ch & 0x000F]; |
| 7927 | } |
| 7928 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7929 | /* Copy ASCII characters as-is */ |
| 7930 | else if (ch < 0x7F) { |
| 7931 | *p++ = ch; |
| 7932 | } |
| 7933 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7934 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7935 | else { |
| 7936 | Py_UCS4 ucs = ch; |
| 7937 | |
| 7938 | #ifndef Py_UNICODE_WIDE |
| 7939 | Py_UNICODE ch2 = 0; |
| 7940 | /* Get code point from surrogate pair */ |
| 7941 | if (size > 0) { |
| 7942 | ch2 = *s; |
| 7943 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7944 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7945 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7946 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7947 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7948 | size--; |
| 7949 | } |
| 7950 | } |
| 7951 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7952 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7953 | (categories Z* and C* except ASCII space) |
| 7954 | */ |
| 7955 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 7956 | /* Map 8-bit characters to '\xhh' */ |
| 7957 | if (ucs <= 0xff) { |
| 7958 | *p++ = '\\'; |
| 7959 | *p++ = 'x'; |
| 7960 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7961 | *p++ = hexdigits[ch & 0x000F]; |
| 7962 | } |
| 7963 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7964 | else if (ucs >= 0x10000) { |
| 7965 | *p++ = '\\'; |
| 7966 | *p++ = 'U'; |
| 7967 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7968 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7969 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7970 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7971 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7972 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7973 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7974 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7975 | } |
| 7976 | /* Map 16-bit characters to '\uxxxx' */ |
| 7977 | else { |
| 7978 | *p++ = '\\'; |
| 7979 | *p++ = 'u'; |
| 7980 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 7981 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 7982 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 7983 | *p++ = hexdigits[ucs & 0x000F]; |
| 7984 | } |
| 7985 | } |
| 7986 | /* Copy characters as-is */ |
| 7987 | else { |
| 7988 | *p++ = ch; |
| 7989 | #ifndef Py_UNICODE_WIDE |
| 7990 | if (ucs >= 0x10000) |
| 7991 | *p++ = ch2; |
| 7992 | #endif |
| 7993 | } |
| 7994 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7995 | } |
| 7996 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7997 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7998 | |
| 7999 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8000 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8001 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8002 | } |
| 8003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8004 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8005 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8006 | \n\ |
| 8007 | 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] | 8008 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8009 | arguments start and end are interpreted as in slice notation.\n\ |
| 8010 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8011 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8012 | |
| 8013 | static PyObject * |
| 8014 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8015 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8016 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8017 | Py_ssize_t start; |
| 8018 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8019 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8020 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8021 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8022 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8023 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8024 | result = stringlib_rfind_slice( |
| 8025 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8026 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8027 | start, end |
| 8028 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8029 | |
| 8030 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8031 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8032 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | } |
| 8034 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8035 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8036 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8037 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8038 | 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] | 8039 | |
| 8040 | static PyObject * |
| 8041 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8042 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8043 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8044 | Py_ssize_t start; |
| 8045 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8046 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8047 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8048 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8049 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8050 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8051 | result = stringlib_rfind_slice( |
| 8052 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8053 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8054 | start, end |
| 8055 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8056 | |
| 8057 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8058 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8059 | if (result < 0) { |
| 8060 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8061 | return NULL; |
| 8062 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8063 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8064 | } |
| 8065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8066 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8067 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8068 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8069 | 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] | 8070 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8071 | |
| 8072 | static PyObject * |
| 8073 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8074 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8075 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8076 | Py_UNICODE fillchar = ' '; |
| 8077 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8078 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8079 | return NULL; |
| 8080 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8081 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8082 | Py_INCREF(self); |
| 8083 | return (PyObject*) self; |
| 8084 | } |
| 8085 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8086 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8087 | } |
| 8088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8090 | PyObject *sep, |
| 8091 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8092 | { |
| 8093 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8094 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8095 | s = PyUnicode_FromObject(s); |
| 8096 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8097 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8098 | if (sep != NULL) { |
| 8099 | sep = PyUnicode_FromObject(sep); |
| 8100 | if (sep == NULL) { |
| 8101 | Py_DECREF(s); |
| 8102 | return NULL; |
| 8103 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8104 | } |
| 8105 | |
| 8106 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8107 | |
| 8108 | Py_DECREF(s); |
| 8109 | Py_XDECREF(sep); |
| 8110 | return result; |
| 8111 | } |
| 8112 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8113 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8114 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8115 | \n\ |
| 8116 | Return a list of the words in S, using sep as the\n\ |
| 8117 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8118 | 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] | 8119 | whitespace string is a separator and empty strings are\n\ |
| 8120 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8121 | |
| 8122 | static PyObject* |
| 8123 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8124 | { |
| 8125 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8126 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8127 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8128 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8129 | return NULL; |
| 8130 | |
| 8131 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8133 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8134 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8135 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8136 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8139 | PyObject * |
| 8140 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8141 | { |
| 8142 | PyObject* str_obj; |
| 8143 | PyObject* sep_obj; |
| 8144 | PyObject* out; |
| 8145 | |
| 8146 | str_obj = PyUnicode_FromObject(str_in); |
| 8147 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8149 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8150 | if (!sep_obj) { |
| 8151 | Py_DECREF(str_obj); |
| 8152 | return NULL; |
| 8153 | } |
| 8154 | |
| 8155 | out = stringlib_partition( |
| 8156 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8157 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8158 | ); |
| 8159 | |
| 8160 | Py_DECREF(sep_obj); |
| 8161 | Py_DECREF(str_obj); |
| 8162 | |
| 8163 | return out; |
| 8164 | } |
| 8165 | |
| 8166 | |
| 8167 | PyObject * |
| 8168 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8169 | { |
| 8170 | PyObject* str_obj; |
| 8171 | PyObject* sep_obj; |
| 8172 | PyObject* out; |
| 8173 | |
| 8174 | str_obj = PyUnicode_FromObject(str_in); |
| 8175 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8176 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8177 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8178 | if (!sep_obj) { |
| 8179 | Py_DECREF(str_obj); |
| 8180 | return NULL; |
| 8181 | } |
| 8182 | |
| 8183 | out = stringlib_rpartition( |
| 8184 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8185 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8186 | ); |
| 8187 | |
| 8188 | Py_DECREF(sep_obj); |
| 8189 | Py_DECREF(str_obj); |
| 8190 | |
| 8191 | return out; |
| 8192 | } |
| 8193 | |
| 8194 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8195 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8196 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8197 | 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] | 8198 | 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] | 8199 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8200 | |
| 8201 | static PyObject* |
| 8202 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8203 | { |
| 8204 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8205 | } |
| 8206 | |
| 8207 | PyDoc_STRVAR(rpartition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8208 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8209 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8210 | 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] | 8211 | 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] | 8212 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8213 | |
| 8214 | static PyObject* |
| 8215 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8216 | { |
| 8217 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8218 | } |
| 8219 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8220 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8221 | PyObject *sep, |
| 8222 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8223 | { |
| 8224 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8225 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8226 | s = PyUnicode_FromObject(s); |
| 8227 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8228 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8229 | if (sep != NULL) { |
| 8230 | sep = PyUnicode_FromObject(sep); |
| 8231 | if (sep == NULL) { |
| 8232 | Py_DECREF(s); |
| 8233 | return NULL; |
| 8234 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8235 | } |
| 8236 | |
| 8237 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8238 | |
| 8239 | Py_DECREF(s); |
| 8240 | Py_XDECREF(sep); |
| 8241 | return result; |
| 8242 | } |
| 8243 | |
| 8244 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8245 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8246 | \n\ |
| 8247 | Return a list of the words in S, using sep as the\n\ |
| 8248 | delimiter string, starting at the end of the string and\n\ |
| 8249 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8250 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8251 | is a separator."); |
| 8252 | |
| 8253 | static PyObject* |
| 8254 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8255 | { |
| 8256 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8257 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8258 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8259 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8260 | return NULL; |
| 8261 | |
| 8262 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8263 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8264 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8265 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8266 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8267 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8268 | } |
| 8269 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8270 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | \n\ |
| 8273 | 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] | 8274 | 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] | 8275 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8276 | |
| 8277 | static PyObject* |
| 8278 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8279 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8280 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8281 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8282 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8283 | return NULL; |
| 8284 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8285 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8286 | } |
| 8287 | |
| 8288 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8289 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8290 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8291 | if (PyUnicode_CheckExact(self)) { |
| 8292 | Py_INCREF(self); |
| 8293 | return self; |
| 8294 | } else |
| 8295 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8296 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8297 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8298 | } |
| 8299 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8300 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8301 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8302 | \n\ |
| 8303 | 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] | 8304 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8305 | |
| 8306 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8307 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8308 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8309 | return fixup(self, fixswapcase); |
| 8310 | } |
| 8311 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8312 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8313 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8314 | \n\ |
| 8315 | Return a translation table usable for str.translate().\n\ |
| 8316 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8317 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8318 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8319 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8320 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8321 | character at the same position in y. If there is a third argument, it\n\ |
| 8322 | must be a string, whose characters will be mapped to None in the result."); |
| 8323 | |
| 8324 | static PyObject* |
| 8325 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8326 | { |
| 8327 | PyObject *x, *y = NULL, *z = NULL; |
| 8328 | PyObject *new = NULL, *key, *value; |
| 8329 | Py_ssize_t i = 0; |
| 8330 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8331 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8332 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8333 | return NULL; |
| 8334 | new = PyDict_New(); |
| 8335 | if (!new) |
| 8336 | return NULL; |
| 8337 | if (y != NULL) { |
| 8338 | /* x must be a string too, of equal length */ |
| 8339 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8340 | if (!PyUnicode_Check(x)) { |
| 8341 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8342 | "be a string if there is a second argument"); |
| 8343 | goto err; |
| 8344 | } |
| 8345 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8346 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8347 | "arguments must have equal length"); |
| 8348 | goto err; |
| 8349 | } |
| 8350 | /* create entries for translating chars in x to those in y */ |
| 8351 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8352 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8353 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8354 | if (!key || !value) |
| 8355 | goto err; |
| 8356 | res = PyDict_SetItem(new, key, value); |
| 8357 | Py_DECREF(key); |
| 8358 | Py_DECREF(value); |
| 8359 | if (res < 0) |
| 8360 | goto err; |
| 8361 | } |
| 8362 | /* create entries for deleting chars in z */ |
| 8363 | if (z != NULL) { |
| 8364 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8365 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8366 | if (!key) |
| 8367 | goto err; |
| 8368 | res = PyDict_SetItem(new, key, Py_None); |
| 8369 | Py_DECREF(key); |
| 8370 | if (res < 0) |
| 8371 | goto err; |
| 8372 | } |
| 8373 | } |
| 8374 | } else { |
| 8375 | /* x must be a dict */ |
| 8376 | if (!PyDict_Check(x)) { |
| 8377 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8378 | "to maketrans it must be a dict"); |
| 8379 | goto err; |
| 8380 | } |
| 8381 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8382 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8383 | if (PyUnicode_Check(key)) { |
| 8384 | /* convert string keys to integer keys */ |
| 8385 | PyObject *newkey; |
| 8386 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8387 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8388 | "table must be of length 1"); |
| 8389 | goto err; |
| 8390 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8391 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8392 | if (!newkey) |
| 8393 | goto err; |
| 8394 | res = PyDict_SetItem(new, newkey, value); |
| 8395 | Py_DECREF(newkey); |
| 8396 | if (res < 0) |
| 8397 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8398 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8399 | /* just keep integer keys */ |
| 8400 | if (PyDict_SetItem(new, key, value) < 0) |
| 8401 | goto err; |
| 8402 | } else { |
| 8403 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8404 | "be strings or integers"); |
| 8405 | goto err; |
| 8406 | } |
| 8407 | } |
| 8408 | } |
| 8409 | return new; |
| 8410 | err: |
| 8411 | Py_DECREF(new); |
| 8412 | return NULL; |
| 8413 | } |
| 8414 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8415 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8416 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8417 | \n\ |
| 8418 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8419 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8420 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8421 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8422 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | |
| 8424 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8425 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8426 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8427 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8428 | } |
| 8429 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8430 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8431 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8432 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8433 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8434 | |
| 8435 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8436 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8437 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8438 | return fixup(self, fixupper); |
| 8439 | } |
| 8440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8441 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8442 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8443 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8444 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8445 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8446 | |
| 8447 | static PyObject * |
| 8448 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8449 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8450 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8451 | PyUnicodeObject *u; |
| 8452 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8453 | Py_ssize_t width; |
| 8454 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8455 | return NULL; |
| 8456 | |
| 8457 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8458 | if (PyUnicode_CheckExact(self)) { |
| 8459 | Py_INCREF(self); |
| 8460 | return (PyObject*) self; |
| 8461 | } |
| 8462 | else |
| 8463 | return PyUnicode_FromUnicode( |
| 8464 | PyUnicode_AS_UNICODE(self), |
| 8465 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8466 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | } |
| 8468 | |
| 8469 | fill = width - self->length; |
| 8470 | |
| 8471 | u = pad(self, fill, 0, '0'); |
| 8472 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8473 | if (u == NULL) |
| 8474 | return NULL; |
| 8475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8476 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8477 | /* move sign to beginning of string */ |
| 8478 | u->str[0] = u->str[fill]; |
| 8479 | u->str[fill] = '0'; |
| 8480 | } |
| 8481 | |
| 8482 | return (PyObject*) u; |
| 8483 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8484 | |
| 8485 | #if 0 |
| 8486 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8487 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8488 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8489 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8490 | } |
| 8491 | #endif |
| 8492 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8493 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8494 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8495 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8496 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8497 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8498 | With optional end, stop comparing S at that position.\n\ |
| 8499 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8500 | |
| 8501 | static PyObject * |
| 8502 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8503 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8504 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8505 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8506 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8507 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8508 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8509 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8510 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8511 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8512 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8513 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8514 | if (PyTuple_Check(subobj)) { |
| 8515 | Py_ssize_t i; |
| 8516 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8517 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8518 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8519 | if (substring == NULL) |
| 8520 | return NULL; |
| 8521 | result = tailmatch(self, substring, start, end, -1); |
| 8522 | Py_DECREF(substring); |
| 8523 | if (result) { |
| 8524 | Py_RETURN_TRUE; |
| 8525 | } |
| 8526 | } |
| 8527 | /* nothing matched */ |
| 8528 | Py_RETURN_FALSE; |
| 8529 | } |
| 8530 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8531 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8533 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8534 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8535 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8536 | } |
| 8537 | |
| 8538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8539 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8540 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8541 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8542 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8543 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8544 | With optional end, stop comparing S at that position.\n\ |
| 8545 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8546 | |
| 8547 | static PyObject * |
| 8548 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8549 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8550 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8551 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8552 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8553 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8554 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8555 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8556 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8557 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8558 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8559 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8560 | if (PyTuple_Check(subobj)) { |
| 8561 | Py_ssize_t i; |
| 8562 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8563 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8564 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8565 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8566 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8567 | result = tailmatch(self, substring, start, end, +1); |
| 8568 | Py_DECREF(substring); |
| 8569 | if (result) { |
| 8570 | Py_RETURN_TRUE; |
| 8571 | } |
| 8572 | } |
| 8573 | Py_RETURN_FALSE; |
| 8574 | } |
| 8575 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8576 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8577 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8578 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8579 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8580 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8581 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8582 | } |
| 8583 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8584 | #include "stringlib/string_format.h" |
| 8585 | |
| 8586 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8587 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8588 | \n\ |
| 8589 | "); |
| 8590 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8591 | static PyObject * |
| 8592 | unicode__format__(PyObject* self, PyObject* args) |
| 8593 | { |
| 8594 | PyObject *format_spec; |
| 8595 | |
| 8596 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8597 | return NULL; |
| 8598 | |
| 8599 | return _PyUnicode_FormatAdvanced(self, |
| 8600 | PyUnicode_AS_UNICODE(format_spec), |
| 8601 | PyUnicode_GET_SIZE(format_spec)); |
| 8602 | } |
| 8603 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8604 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8605 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8606 | \n\ |
| 8607 | "); |
| 8608 | |
| 8609 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8610 | unicode__sizeof__(PyUnicodeObject *v) |
| 8611 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8612 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8613 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8614 | } |
| 8615 | |
| 8616 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8617 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8618 | |
| 8619 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8620 | unicode_getnewargs(PyUnicodeObject *v) |
| 8621 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8622 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8623 | } |
| 8624 | |
| 8625 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8626 | static PyMethodDef unicode_methods[] = { |
| 8627 | |
| 8628 | /* Order is according to common usage: often used methods should |
| 8629 | appear first, since lookup is done sequentially. */ |
| 8630 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8631 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 8632 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8633 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8634 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8635 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8636 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8637 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8638 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8639 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8640 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8641 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8642 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8643 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8644 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8645 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8646 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8647 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8648 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8649 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8650 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8651 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8652 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8653 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8654 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8655 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8656 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8657 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8658 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8659 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8660 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8661 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8662 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8663 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8664 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8665 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8666 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8667 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8668 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8669 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8670 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8671 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8672 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8673 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8674 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8675 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8676 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8677 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8678 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8679 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8680 | #endif |
| 8681 | |
| 8682 | #if 0 |
| 8683 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8684 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8685 | #endif |
| 8686 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8687 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8688 | {NULL, NULL} |
| 8689 | }; |
| 8690 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8691 | static PyObject * |
| 8692 | unicode_mod(PyObject *v, PyObject *w) |
| 8693 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8694 | if (!PyUnicode_Check(v)) { |
| 8695 | Py_INCREF(Py_NotImplemented); |
| 8696 | return Py_NotImplemented; |
| 8697 | } |
| 8698 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8699 | } |
| 8700 | |
| 8701 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8702 | 0, /*nb_add*/ |
| 8703 | 0, /*nb_subtract*/ |
| 8704 | 0, /*nb_multiply*/ |
| 8705 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8706 | }; |
| 8707 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8708 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8709 | (lenfunc) unicode_length, /* sq_length */ |
| 8710 | PyUnicode_Concat, /* sq_concat */ |
| 8711 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8712 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8713 | 0, /* sq_slice */ |
| 8714 | 0, /* sq_ass_item */ |
| 8715 | 0, /* sq_ass_slice */ |
| 8716 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | }; |
| 8718 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8719 | static PyObject* |
| 8720 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8721 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8722 | if (PyIndex_Check(item)) { |
| 8723 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8724 | if (i == -1 && PyErr_Occurred()) |
| 8725 | return NULL; |
| 8726 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8727 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8728 | return unicode_getitem(self, i); |
| 8729 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8730 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8731 | Py_UNICODE* source_buf; |
| 8732 | Py_UNICODE* result_buf; |
| 8733 | PyObject* result; |
| 8734 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8735 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8736 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8737 | return NULL; |
| 8738 | } |
| 8739 | |
| 8740 | if (slicelength <= 0) { |
| 8741 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8742 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8743 | PyUnicode_CheckExact(self)) { |
| 8744 | Py_INCREF(self); |
| 8745 | return (PyObject *)self; |
| 8746 | } else if (step == 1) { |
| 8747 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8748 | } else { |
| 8749 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8750 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8751 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8752 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8753 | if (result_buf == NULL) |
| 8754 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8755 | |
| 8756 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8757 | result_buf[i] = source_buf[cur]; |
| 8758 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8759 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8760 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8761 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8762 | return result; |
| 8763 | } |
| 8764 | } else { |
| 8765 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8766 | return NULL; |
| 8767 | } |
| 8768 | } |
| 8769 | |
| 8770 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8771 | (lenfunc)unicode_length, /* mp_length */ |
| 8772 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8773 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8774 | }; |
| 8775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8776 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8777 | /* Helpers for PyUnicode_Format() */ |
| 8778 | |
| 8779 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8780 | 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] | 8781 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8782 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8783 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8784 | (*p_argidx)++; |
| 8785 | if (arglen < 0) |
| 8786 | return args; |
| 8787 | else |
| 8788 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | } |
| 8790 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8791 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8792 | return NULL; |
| 8793 | } |
| 8794 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8795 | static void |
| 8796 | strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8797 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8798 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8799 | for (i = len - 1; i >= 0; i--) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8800 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8801 | } |
| 8802 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8803 | static int |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8804 | formatfloat(Py_UNICODE *buf, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8805 | size_t buflen, |
| 8806 | int flags, |
| 8807 | int prec, |
| 8808 | int type, |
| 8809 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8810 | { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8811 | /* eric.smith: To minimize disturbances in PyUnicode_Format (the |
| 8812 | only caller of this routine), I'm going to keep the existing |
| 8813 | API to this function. That means that we'll allocate memory and |
| 8814 | then copy back into the supplied buffer. But that's better than |
| 8815 | all of the changes that would be required in PyUnicode_Format |
| 8816 | because it does lots of memory management tricks. */ |
| 8817 | |
| 8818 | char* p = NULL; |
| 8819 | int result = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8820 | double x; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8821 | Py_ssize_t len; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8822 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8823 | x = PyFloat_AsDouble(v); |
| 8824 | if (x == -1.0 && PyErr_Occurred()) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8825 | goto done; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8826 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8827 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8828 | |
Mark Dickinson | 4feda2a | 2009-03-29 16:34:21 +0000 | [diff] [blame] | 8829 | /* make sure that the decimal representation of precision really does |
| 8830 | need at most 10 digits: platforms with sizeof(int) == 8 exist! */ |
| 8831 | if (prec > 0x7fffffffL) { |
| 8832 | PyErr_SetString(PyExc_OverflowError, |
| 8833 | "outrageously large precision " |
| 8834 | "for formatted float"); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8835 | goto done; |
Mark Dickinson | 4feda2a | 2009-03-29 16:34:21 +0000 | [diff] [blame] | 8836 | } |
| 8837 | |
Mark Dickinson | c8a608c | 2009-03-29 15:19:47 +0000 | [diff] [blame] | 8838 | if (type == 'f' && fabs(x) >= 1e50) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8839 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8840 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8841 | if (((type == 'g' || type == 'G') && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8842 | buflen <= (size_t)10 + (size_t)prec) || |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8843 | ((type == 'f' || type == 'F') && |
| 8844 | buflen <= (size_t)53 + (size_t)prec)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8845 | PyErr_SetString(PyExc_OverflowError, |
| 8846 | "formatted float is too long (precision too large?)"); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8847 | goto done; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8848 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8849 | |
| 8850 | p = PyOS_double_to_string(x, type, prec, |
| 8851 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
| 8852 | len = strlen(p); |
| 8853 | if (len+1 >= buflen) { |
| 8854 | /* Caller supplied buffer is not large enough. */ |
| 8855 | PyErr_NoMemory(); |
| 8856 | goto done; |
| 8857 | } |
| 8858 | strtounicode(buf, p, len); |
| 8859 | result = Py_SAFE_DOWNCAST(len, Py_ssize_t, int); |
| 8860 | |
| 8861 | done: |
| 8862 | PyMem_Free(p); |
| 8863 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8864 | } |
| 8865 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8866 | static PyObject* |
| 8867 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8868 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8869 | char *buf; |
| 8870 | int len; |
| 8871 | PyObject *str; /* temporary string object. */ |
| 8872 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8873 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8874 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 8875 | if (!str) |
| 8876 | return NULL; |
| 8877 | result = PyUnicode_FromStringAndSize(buf, len); |
| 8878 | Py_DECREF(str); |
| 8879 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8880 | } |
| 8881 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8882 | static int |
| 8883 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8884 | size_t buflen, |
| 8885 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8886 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8887 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8888 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8889 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 8890 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 8891 | buf[1] = '\0'; |
| 8892 | return 1; |
| 8893 | } |
| 8894 | #ifndef Py_UNICODE_WIDE |
| 8895 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 8896 | /* Decode a valid surrogate pair */ |
| 8897 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 8898 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 8899 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 8900 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 8901 | buf[0] = c0; |
| 8902 | buf[1] = c1; |
| 8903 | buf[2] = '\0'; |
| 8904 | return 2; |
| 8905 | } |
| 8906 | } |
| 8907 | #endif |
| 8908 | goto onError; |
| 8909 | } |
| 8910 | else { |
| 8911 | /* Integer input truncated to a character */ |
| 8912 | long x; |
| 8913 | x = PyLong_AsLong(v); |
| 8914 | if (x == -1 && PyErr_Occurred()) |
| 8915 | goto onError; |
| 8916 | |
| 8917 | if (x < 0 || x > 0x10ffff) { |
| 8918 | PyErr_SetString(PyExc_OverflowError, |
| 8919 | "%c arg not in range(0x110000)"); |
| 8920 | return -1; |
| 8921 | } |
| 8922 | |
| 8923 | #ifndef Py_UNICODE_WIDE |
| 8924 | if (x > 0xffff) { |
| 8925 | x -= 0x10000; |
| 8926 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 8927 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 8928 | return 2; |
| 8929 | } |
| 8930 | #endif |
| 8931 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8932 | buf[1] = '\0'; |
| 8933 | return 1; |
| 8934 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8935 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8936 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8937 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8938 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8939 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8940 | } |
| 8941 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8942 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8943 | |
| 8944 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8945 | chars are formatted. XXX This is a magic number. Each formatting |
| 8946 | routine does bounds checking to ensure no overflow, but a better |
| 8947 | solution may be to malloc a buffer of appropriate size for each |
| 8948 | format. For now, the current solution is sufficient. |
| 8949 | */ |
| 8950 | #define FORMATBUFLEN (size_t)120 |
| 8951 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8952 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8953 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8954 | { |
| 8955 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8956 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8957 | int args_owned = 0; |
| 8958 | PyUnicodeObject *result = NULL; |
| 8959 | PyObject *dict = NULL; |
| 8960 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8962 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8963 | PyErr_BadInternalCall(); |
| 8964 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8965 | } |
| 8966 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8967 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8968 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8969 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8970 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8971 | |
| 8972 | reslen = rescnt = fmtcnt + 100; |
| 8973 | result = _PyUnicode_New(reslen); |
| 8974 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8975 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8976 | res = PyUnicode_AS_UNICODE(result); |
| 8977 | |
| 8978 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8979 | arglen = PyTuple_Size(args); |
| 8980 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8981 | } |
| 8982 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8983 | arglen = -1; |
| 8984 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8985 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8986 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 8987 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8988 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8989 | |
| 8990 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8991 | if (*fmt != '%') { |
| 8992 | if (--rescnt < 0) { |
| 8993 | rescnt = fmtcnt + 100; |
| 8994 | reslen += rescnt; |
| 8995 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 8996 | goto onError; |
| 8997 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8998 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8999 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9000 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9001 | } |
| 9002 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9003 | /* Got a format specifier */ |
| 9004 | int flags = 0; |
| 9005 | Py_ssize_t width = -1; |
| 9006 | int prec = -1; |
| 9007 | Py_UNICODE c = '\0'; |
| 9008 | Py_UNICODE fill; |
| 9009 | int isnumok; |
| 9010 | PyObject *v = NULL; |
| 9011 | PyObject *temp = NULL; |
| 9012 | Py_UNICODE *pbuf; |
| 9013 | Py_UNICODE sign; |
| 9014 | Py_ssize_t len; |
| 9015 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9016 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9017 | fmt++; |
| 9018 | if (*fmt == '(') { |
| 9019 | Py_UNICODE *keystart; |
| 9020 | Py_ssize_t keylen; |
| 9021 | PyObject *key; |
| 9022 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9023 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9024 | if (dict == NULL) { |
| 9025 | PyErr_SetString(PyExc_TypeError, |
| 9026 | "format requires a mapping"); |
| 9027 | goto onError; |
| 9028 | } |
| 9029 | ++fmt; |
| 9030 | --fmtcnt; |
| 9031 | keystart = fmt; |
| 9032 | /* Skip over balanced parentheses */ |
| 9033 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9034 | if (*fmt == ')') |
| 9035 | --pcount; |
| 9036 | else if (*fmt == '(') |
| 9037 | ++pcount; |
| 9038 | fmt++; |
| 9039 | } |
| 9040 | keylen = fmt - keystart - 1; |
| 9041 | if (fmtcnt < 0 || pcount > 0) { |
| 9042 | PyErr_SetString(PyExc_ValueError, |
| 9043 | "incomplete format key"); |
| 9044 | goto onError; |
| 9045 | } |
| 9046 | #if 0 |
| 9047 | /* keys are converted to strings using UTF-8 and |
| 9048 | then looked up since Python uses strings to hold |
| 9049 | variables names etc. in its namespaces and we |
| 9050 | wouldn't want to break common idioms. */ |
| 9051 | key = PyUnicode_EncodeUTF8(keystart, |
| 9052 | keylen, |
| 9053 | NULL); |
| 9054 | #else |
| 9055 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9056 | #endif |
| 9057 | if (key == NULL) |
| 9058 | goto onError; |
| 9059 | if (args_owned) { |
| 9060 | Py_DECREF(args); |
| 9061 | args_owned = 0; |
| 9062 | } |
| 9063 | args = PyObject_GetItem(dict, key); |
| 9064 | Py_DECREF(key); |
| 9065 | if (args == NULL) { |
| 9066 | goto onError; |
| 9067 | } |
| 9068 | args_owned = 1; |
| 9069 | arglen = -1; |
| 9070 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9071 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9072 | while (--fmtcnt >= 0) { |
| 9073 | switch (c = *fmt++) { |
| 9074 | case '-': flags |= F_LJUST; continue; |
| 9075 | case '+': flags |= F_SIGN; continue; |
| 9076 | case ' ': flags |= F_BLANK; continue; |
| 9077 | case '#': flags |= F_ALT; continue; |
| 9078 | case '0': flags |= F_ZERO; continue; |
| 9079 | } |
| 9080 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9081 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9082 | if (c == '*') { |
| 9083 | v = getnextarg(args, arglen, &argidx); |
| 9084 | if (v == NULL) |
| 9085 | goto onError; |
| 9086 | if (!PyLong_Check(v)) { |
| 9087 | PyErr_SetString(PyExc_TypeError, |
| 9088 | "* wants int"); |
| 9089 | goto onError; |
| 9090 | } |
| 9091 | width = PyLong_AsLong(v); |
| 9092 | if (width == -1 && PyErr_Occurred()) |
| 9093 | goto onError; |
| 9094 | if (width < 0) { |
| 9095 | flags |= F_LJUST; |
| 9096 | width = -width; |
| 9097 | } |
| 9098 | if (--fmtcnt >= 0) |
| 9099 | c = *fmt++; |
| 9100 | } |
| 9101 | else if (c >= '0' && c <= '9') { |
| 9102 | width = c - '0'; |
| 9103 | while (--fmtcnt >= 0) { |
| 9104 | c = *fmt++; |
| 9105 | if (c < '0' || c > '9') |
| 9106 | break; |
| 9107 | if ((width*10) / 10 != width) { |
| 9108 | PyErr_SetString(PyExc_ValueError, |
| 9109 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9110 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9111 | } |
| 9112 | width = width*10 + (c - '0'); |
| 9113 | } |
| 9114 | } |
| 9115 | if (c == '.') { |
| 9116 | prec = 0; |
| 9117 | if (--fmtcnt >= 0) |
| 9118 | c = *fmt++; |
| 9119 | if (c == '*') { |
| 9120 | v = getnextarg(args, arglen, &argidx); |
| 9121 | if (v == NULL) |
| 9122 | goto onError; |
| 9123 | if (!PyLong_Check(v)) { |
| 9124 | PyErr_SetString(PyExc_TypeError, |
| 9125 | "* wants int"); |
| 9126 | goto onError; |
| 9127 | } |
| 9128 | prec = PyLong_AsLong(v); |
| 9129 | if (prec == -1 && PyErr_Occurred()) |
| 9130 | goto onError; |
| 9131 | if (prec < 0) |
| 9132 | prec = 0; |
| 9133 | if (--fmtcnt >= 0) |
| 9134 | c = *fmt++; |
| 9135 | } |
| 9136 | else if (c >= '0' && c <= '9') { |
| 9137 | prec = c - '0'; |
| 9138 | while (--fmtcnt >= 0) { |
| 9139 | c = Py_CHARMASK(*fmt++); |
| 9140 | if (c < '0' || c > '9') |
| 9141 | break; |
| 9142 | if ((prec*10) / 10 != prec) { |
| 9143 | PyErr_SetString(PyExc_ValueError, |
| 9144 | "prec too big"); |
| 9145 | goto onError; |
| 9146 | } |
| 9147 | prec = prec*10 + (c - '0'); |
| 9148 | } |
| 9149 | } |
| 9150 | } /* prec */ |
| 9151 | if (fmtcnt >= 0) { |
| 9152 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9153 | if (--fmtcnt >= 0) |
| 9154 | c = *fmt++; |
| 9155 | } |
| 9156 | } |
| 9157 | if (fmtcnt < 0) { |
| 9158 | PyErr_SetString(PyExc_ValueError, |
| 9159 | "incomplete format"); |
| 9160 | goto onError; |
| 9161 | } |
| 9162 | if (c != '%') { |
| 9163 | v = getnextarg(args, arglen, &argidx); |
| 9164 | if (v == NULL) |
| 9165 | goto onError; |
| 9166 | } |
| 9167 | sign = 0; |
| 9168 | fill = ' '; |
| 9169 | switch (c) { |
| 9170 | |
| 9171 | case '%': |
| 9172 | pbuf = formatbuf; |
| 9173 | /* presume that buffer length is at least 1 */ |
| 9174 | pbuf[0] = '%'; |
| 9175 | len = 1; |
| 9176 | break; |
| 9177 | |
| 9178 | case 's': |
| 9179 | case 'r': |
| 9180 | case 'a': |
| 9181 | if (PyUnicode_Check(v) && c == 's') { |
| 9182 | temp = v; |
| 9183 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9184 | } |
| 9185 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9186 | if (c == 's') |
| 9187 | temp = PyObject_Str(v); |
| 9188 | else if (c == 'r') |
| 9189 | temp = PyObject_Repr(v); |
| 9190 | else |
| 9191 | temp = PyObject_ASCII(v); |
| 9192 | if (temp == NULL) |
| 9193 | goto onError; |
| 9194 | if (PyUnicode_Check(temp)) |
| 9195 | /* nothing to do */; |
| 9196 | else { |
| 9197 | Py_DECREF(temp); |
| 9198 | PyErr_SetString(PyExc_TypeError, |
| 9199 | "%s argument has non-string str()"); |
| 9200 | goto onError; |
| 9201 | } |
| 9202 | } |
| 9203 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9204 | len = PyUnicode_GET_SIZE(temp); |
| 9205 | if (prec >= 0 && len > prec) |
| 9206 | len = prec; |
| 9207 | break; |
| 9208 | |
| 9209 | case 'i': |
| 9210 | case 'd': |
| 9211 | case 'u': |
| 9212 | case 'o': |
| 9213 | case 'x': |
| 9214 | case 'X': |
| 9215 | if (c == 'i') |
| 9216 | c = 'd'; |
| 9217 | isnumok = 0; |
| 9218 | if (PyNumber_Check(v)) { |
| 9219 | PyObject *iobj=NULL; |
| 9220 | |
| 9221 | if (PyLong_Check(v)) { |
| 9222 | iobj = v; |
| 9223 | Py_INCREF(iobj); |
| 9224 | } |
| 9225 | else { |
| 9226 | iobj = PyNumber_Long(v); |
| 9227 | } |
| 9228 | if (iobj!=NULL) { |
| 9229 | if (PyLong_Check(iobj)) { |
| 9230 | isnumok = 1; |
| 9231 | temp = formatlong(iobj, flags, prec, c); |
| 9232 | Py_DECREF(iobj); |
| 9233 | if (!temp) |
| 9234 | goto onError; |
| 9235 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9236 | len = PyUnicode_GET_SIZE(temp); |
| 9237 | sign = 1; |
| 9238 | } |
| 9239 | else { |
| 9240 | Py_DECREF(iobj); |
| 9241 | } |
| 9242 | } |
| 9243 | } |
| 9244 | if (!isnumok) { |
| 9245 | PyErr_Format(PyExc_TypeError, |
| 9246 | "%%%c format: a number is required, " |
| 9247 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9248 | goto onError; |
| 9249 | } |
| 9250 | if (flags & F_ZERO) |
| 9251 | fill = '0'; |
| 9252 | break; |
| 9253 | |
| 9254 | case 'e': |
| 9255 | case 'E': |
| 9256 | case 'f': |
| 9257 | case 'F': |
| 9258 | case 'g': |
| 9259 | case 'G': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9260 | pbuf = formatbuf; |
| 9261 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 9262 | flags, prec, c, v); |
| 9263 | if (len < 0) |
| 9264 | goto onError; |
| 9265 | sign = 1; |
| 9266 | if (flags & F_ZERO) |
| 9267 | fill = '0'; |
| 9268 | break; |
| 9269 | |
| 9270 | case 'c': |
| 9271 | pbuf = formatbuf; |
| 9272 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9273 | if (len < 0) |
| 9274 | goto onError; |
| 9275 | break; |
| 9276 | |
| 9277 | default: |
| 9278 | PyErr_Format(PyExc_ValueError, |
| 9279 | "unsupported format character '%c' (0x%x) " |
| 9280 | "at index %zd", |
| 9281 | (31<=c && c<=126) ? (char)c : '?', |
| 9282 | (int)c, |
| 9283 | (Py_ssize_t)(fmt - 1 - |
| 9284 | PyUnicode_AS_UNICODE(uformat))); |
| 9285 | goto onError; |
| 9286 | } |
| 9287 | if (sign) { |
| 9288 | if (*pbuf == '-' || *pbuf == '+') { |
| 9289 | sign = *pbuf++; |
| 9290 | len--; |
| 9291 | } |
| 9292 | else if (flags & F_SIGN) |
| 9293 | sign = '+'; |
| 9294 | else if (flags & F_BLANK) |
| 9295 | sign = ' '; |
| 9296 | else |
| 9297 | sign = 0; |
| 9298 | } |
| 9299 | if (width < len) |
| 9300 | width = len; |
| 9301 | if (rescnt - (sign != 0) < width) { |
| 9302 | reslen -= rescnt; |
| 9303 | rescnt = width + fmtcnt + 100; |
| 9304 | reslen += rescnt; |
| 9305 | if (reslen < 0) { |
| 9306 | Py_XDECREF(temp); |
| 9307 | PyErr_NoMemory(); |
| 9308 | goto onError; |
| 9309 | } |
| 9310 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9311 | Py_XDECREF(temp); |
| 9312 | goto onError; |
| 9313 | } |
| 9314 | res = PyUnicode_AS_UNICODE(result) |
| 9315 | + reslen - rescnt; |
| 9316 | } |
| 9317 | if (sign) { |
| 9318 | if (fill != ' ') |
| 9319 | *res++ = sign; |
| 9320 | rescnt--; |
| 9321 | if (width > len) |
| 9322 | width--; |
| 9323 | } |
| 9324 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9325 | assert(pbuf[0] == '0'); |
| 9326 | assert(pbuf[1] == c); |
| 9327 | if (fill != ' ') { |
| 9328 | *res++ = *pbuf++; |
| 9329 | *res++ = *pbuf++; |
| 9330 | } |
| 9331 | rescnt -= 2; |
| 9332 | width -= 2; |
| 9333 | if (width < 0) |
| 9334 | width = 0; |
| 9335 | len -= 2; |
| 9336 | } |
| 9337 | if (width > len && !(flags & F_LJUST)) { |
| 9338 | do { |
| 9339 | --rescnt; |
| 9340 | *res++ = fill; |
| 9341 | } while (--width > len); |
| 9342 | } |
| 9343 | if (fill == ' ') { |
| 9344 | if (sign) |
| 9345 | *res++ = sign; |
| 9346 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9347 | assert(pbuf[0] == '0'); |
| 9348 | assert(pbuf[1] == c); |
| 9349 | *res++ = *pbuf++; |
| 9350 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9351 | } |
| 9352 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9353 | Py_UNICODE_COPY(res, pbuf, len); |
| 9354 | res += len; |
| 9355 | rescnt -= len; |
| 9356 | while (--width >= len) { |
| 9357 | --rescnt; |
| 9358 | *res++ = ' '; |
| 9359 | } |
| 9360 | if (dict && (argidx < arglen) && c != '%') { |
| 9361 | PyErr_SetString(PyExc_TypeError, |
| 9362 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9363 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9364 | goto onError; |
| 9365 | } |
| 9366 | Py_XDECREF(temp); |
| 9367 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9368 | } /* until end */ |
| 9369 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9370 | PyErr_SetString(PyExc_TypeError, |
| 9371 | "not all arguments converted during string formatting"); |
| 9372 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9373 | } |
| 9374 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9375 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9376 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9377 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9378 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9379 | } |
| 9380 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9381 | return (PyObject *)result; |
| 9382 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9383 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9384 | Py_XDECREF(result); |
| 9385 | Py_DECREF(uformat); |
| 9386 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9387 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9388 | } |
| 9389 | return NULL; |
| 9390 | } |
| 9391 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9392 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9393 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9394 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9395 | static PyObject * |
| 9396 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9397 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9398 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9399 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9400 | char *encoding = NULL; |
| 9401 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9402 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9403 | if (type != &PyUnicode_Type) |
| 9404 | return unicode_subtype_new(type, args, kwds); |
| 9405 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9406 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9407 | return NULL; |
| 9408 | if (x == NULL) |
| 9409 | return (PyObject *)_PyUnicode_New(0); |
| 9410 | if (encoding == NULL && errors == NULL) |
| 9411 | return PyObject_Str(x); |
| 9412 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9413 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9414 | } |
| 9415 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9416 | static PyObject * |
| 9417 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9418 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9419 | PyUnicodeObject *tmp, *pnew; |
| 9420 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9421 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9422 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9423 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9424 | if (tmp == NULL) |
| 9425 | return NULL; |
| 9426 | assert(PyUnicode_Check(tmp)); |
| 9427 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9428 | if (pnew == NULL) { |
| 9429 | Py_DECREF(tmp); |
| 9430 | return NULL; |
| 9431 | } |
| 9432 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9433 | if (pnew->str == NULL) { |
| 9434 | _Py_ForgetReference((PyObject *)pnew); |
| 9435 | PyObject_Del(pnew); |
| 9436 | Py_DECREF(tmp); |
| 9437 | return PyErr_NoMemory(); |
| 9438 | } |
| 9439 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9440 | pnew->length = n; |
| 9441 | pnew->hash = tmp->hash; |
| 9442 | Py_DECREF(tmp); |
| 9443 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9444 | } |
| 9445 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9446 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9447 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9448 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9449 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9450 | encoding defaults to the current default string encoding.\n\ |
| 9451 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9452 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9453 | static PyObject *unicode_iter(PyObject *seq); |
| 9454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9455 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9456 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9457 | "str", /* tp_name */ |
| 9458 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9459 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9460 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9461 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9462 | 0, /* tp_print */ |
| 9463 | 0, /* tp_getattr */ |
| 9464 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9465 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9466 | unicode_repr, /* tp_repr */ |
| 9467 | &unicode_as_number, /* tp_as_number */ |
| 9468 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9469 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9470 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9471 | 0, /* tp_call*/ |
| 9472 | (reprfunc) unicode_str, /* tp_str */ |
| 9473 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9474 | 0, /* tp_setattro */ |
| 9475 | 0, /* tp_as_buffer */ |
| 9476 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9477 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9478 | unicode_doc, /* tp_doc */ |
| 9479 | 0, /* tp_traverse */ |
| 9480 | 0, /* tp_clear */ |
| 9481 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9482 | 0, /* tp_weaklistoffset */ |
| 9483 | unicode_iter, /* tp_iter */ |
| 9484 | 0, /* tp_iternext */ |
| 9485 | unicode_methods, /* tp_methods */ |
| 9486 | 0, /* tp_members */ |
| 9487 | 0, /* tp_getset */ |
| 9488 | &PyBaseObject_Type, /* tp_base */ |
| 9489 | 0, /* tp_dict */ |
| 9490 | 0, /* tp_descr_get */ |
| 9491 | 0, /* tp_descr_set */ |
| 9492 | 0, /* tp_dictoffset */ |
| 9493 | 0, /* tp_init */ |
| 9494 | 0, /* tp_alloc */ |
| 9495 | unicode_new, /* tp_new */ |
| 9496 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9497 | }; |
| 9498 | |
| 9499 | /* Initialize the Unicode implementation */ |
| 9500 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9501 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9502 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9503 | int i; |
| 9504 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9505 | /* XXX - move this array to unicodectype.c ? */ |
| 9506 | Py_UNICODE linebreak[] = { |
| 9507 | 0x000A, /* LINE FEED */ |
| 9508 | 0x000D, /* CARRIAGE RETURN */ |
| 9509 | 0x001C, /* FILE SEPARATOR */ |
| 9510 | 0x001D, /* GROUP SEPARATOR */ |
| 9511 | 0x001E, /* RECORD SEPARATOR */ |
| 9512 | 0x0085, /* NEXT LINE */ |
| 9513 | 0x2028, /* LINE SEPARATOR */ |
| 9514 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9515 | }; |
| 9516 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9517 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9518 | free_list = NULL; |
| 9519 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9520 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9521 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9522 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9523 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9524 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9525 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9526 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9527 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9528 | |
| 9529 | /* initialize the linebreak bloom filter */ |
| 9530 | bloom_linebreak = make_bloom_mask( |
| 9531 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9532 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9533 | |
| 9534 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9535 | } |
| 9536 | |
| 9537 | /* Finalize the Unicode implementation */ |
| 9538 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9539 | int |
| 9540 | PyUnicode_ClearFreeList(void) |
| 9541 | { |
| 9542 | int freelist_size = numfree; |
| 9543 | PyUnicodeObject *u; |
| 9544 | |
| 9545 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9546 | PyUnicodeObject *v = u; |
| 9547 | u = *(PyUnicodeObject **)u; |
| 9548 | if (v->str) |
| 9549 | PyObject_DEL(v->str); |
| 9550 | Py_XDECREF(v->defenc); |
| 9551 | PyObject_Del(v); |
| 9552 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9553 | } |
| 9554 | free_list = NULL; |
| 9555 | assert(numfree == 0); |
| 9556 | return freelist_size; |
| 9557 | } |
| 9558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9559 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9560 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9561 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9562 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9563 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9564 | Py_XDECREF(unicode_empty); |
| 9565 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9566 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9567 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9568 | if (unicode_latin1[i]) { |
| 9569 | Py_DECREF(unicode_latin1[i]); |
| 9570 | unicode_latin1[i] = NULL; |
| 9571 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9572 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9573 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9574 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9575 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9576 | void |
| 9577 | PyUnicode_InternInPlace(PyObject **p) |
| 9578 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9579 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9580 | PyObject *t; |
| 9581 | if (s == NULL || !PyUnicode_Check(s)) |
| 9582 | Py_FatalError( |
| 9583 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9584 | /* If it's a subclass, we don't really know what putting |
| 9585 | it in the interned dict might do. */ |
| 9586 | if (!PyUnicode_CheckExact(s)) |
| 9587 | return; |
| 9588 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9589 | return; |
| 9590 | if (interned == NULL) { |
| 9591 | interned = PyDict_New(); |
| 9592 | if (interned == NULL) { |
| 9593 | PyErr_Clear(); /* Don't leave an exception */ |
| 9594 | return; |
| 9595 | } |
| 9596 | } |
| 9597 | /* It might be that the GetItem call fails even |
| 9598 | though the key is present in the dictionary, |
| 9599 | namely when this happens during a stack overflow. */ |
| 9600 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9601 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9602 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9603 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9604 | if (t) { |
| 9605 | Py_INCREF(t); |
| 9606 | Py_DECREF(*p); |
| 9607 | *p = t; |
| 9608 | return; |
| 9609 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9610 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9611 | PyThreadState_GET()->recursion_critical = 1; |
| 9612 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9613 | PyErr_Clear(); |
| 9614 | PyThreadState_GET()->recursion_critical = 0; |
| 9615 | return; |
| 9616 | } |
| 9617 | PyThreadState_GET()->recursion_critical = 0; |
| 9618 | /* The two references in interned are not counted by refcnt. |
| 9619 | The deallocator will take care of this */ |
| 9620 | Py_REFCNT(s) -= 2; |
| 9621 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9622 | } |
| 9623 | |
| 9624 | void |
| 9625 | PyUnicode_InternImmortal(PyObject **p) |
| 9626 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9627 | PyUnicode_InternInPlace(p); |
| 9628 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9629 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9630 | Py_INCREF(*p); |
| 9631 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9632 | } |
| 9633 | |
| 9634 | PyObject * |
| 9635 | PyUnicode_InternFromString(const char *cp) |
| 9636 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9637 | PyObject *s = PyUnicode_FromString(cp); |
| 9638 | if (s == NULL) |
| 9639 | return NULL; |
| 9640 | PyUnicode_InternInPlace(&s); |
| 9641 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9642 | } |
| 9643 | |
| 9644 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9645 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9646 | PyObject *keys; |
| 9647 | PyUnicodeObject *s; |
| 9648 | Py_ssize_t i, n; |
| 9649 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9650 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9651 | if (interned == NULL || !PyDict_Check(interned)) |
| 9652 | return; |
| 9653 | keys = PyDict_Keys(interned); |
| 9654 | if (keys == NULL || !PyList_Check(keys)) { |
| 9655 | PyErr_Clear(); |
| 9656 | return; |
| 9657 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9658 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9659 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9660 | detector, interned unicode strings are not forcibly deallocated; |
| 9661 | rather, we give them their stolen references back, and then clear |
| 9662 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9663 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9664 | n = PyList_GET_SIZE(keys); |
| 9665 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9666 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9667 | for (i = 0; i < n; i++) { |
| 9668 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9669 | switch (s->state) { |
| 9670 | case SSTATE_NOT_INTERNED: |
| 9671 | /* XXX Shouldn't happen */ |
| 9672 | break; |
| 9673 | case SSTATE_INTERNED_IMMORTAL: |
| 9674 | Py_REFCNT(s) += 1; |
| 9675 | immortal_size += s->length; |
| 9676 | break; |
| 9677 | case SSTATE_INTERNED_MORTAL: |
| 9678 | Py_REFCNT(s) += 2; |
| 9679 | mortal_size += s->length; |
| 9680 | break; |
| 9681 | default: |
| 9682 | Py_FatalError("Inconsistent interned string state."); |
| 9683 | } |
| 9684 | s->state = SSTATE_NOT_INTERNED; |
| 9685 | } |
| 9686 | fprintf(stderr, "total size of all interned strings: " |
| 9687 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9688 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9689 | Py_DECREF(keys); |
| 9690 | PyDict_Clear(interned); |
| 9691 | Py_DECREF(interned); |
| 9692 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9693 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9694 | |
| 9695 | |
| 9696 | /********************* Unicode Iterator **************************/ |
| 9697 | |
| 9698 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9699 | PyObject_HEAD |
| 9700 | Py_ssize_t it_index; |
| 9701 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9702 | } unicodeiterobject; |
| 9703 | |
| 9704 | static void |
| 9705 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9706 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9707 | _PyObject_GC_UNTRACK(it); |
| 9708 | Py_XDECREF(it->it_seq); |
| 9709 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9710 | } |
| 9711 | |
| 9712 | static int |
| 9713 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9714 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9715 | Py_VISIT(it->it_seq); |
| 9716 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9717 | } |
| 9718 | |
| 9719 | static PyObject * |
| 9720 | unicodeiter_next(unicodeiterobject *it) |
| 9721 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9722 | PyUnicodeObject *seq; |
| 9723 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9724 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9725 | assert(it != NULL); |
| 9726 | seq = it->it_seq; |
| 9727 | if (seq == NULL) |
| 9728 | return NULL; |
| 9729 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9730 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9731 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9732 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9733 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9734 | if (item != NULL) |
| 9735 | ++it->it_index; |
| 9736 | return item; |
| 9737 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9738 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9739 | Py_DECREF(seq); |
| 9740 | it->it_seq = NULL; |
| 9741 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9742 | } |
| 9743 | |
| 9744 | static PyObject * |
| 9745 | unicodeiter_len(unicodeiterobject *it) |
| 9746 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9747 | Py_ssize_t len = 0; |
| 9748 | if (it->it_seq) |
| 9749 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9750 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9751 | } |
| 9752 | |
| 9753 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9754 | |
| 9755 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9756 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9757 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9758 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9759 | }; |
| 9760 | |
| 9761 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9762 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9763 | "str_iterator", /* tp_name */ |
| 9764 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9765 | 0, /* tp_itemsize */ |
| 9766 | /* methods */ |
| 9767 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9768 | 0, /* tp_print */ |
| 9769 | 0, /* tp_getattr */ |
| 9770 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9771 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9772 | 0, /* tp_repr */ |
| 9773 | 0, /* tp_as_number */ |
| 9774 | 0, /* tp_as_sequence */ |
| 9775 | 0, /* tp_as_mapping */ |
| 9776 | 0, /* tp_hash */ |
| 9777 | 0, /* tp_call */ |
| 9778 | 0, /* tp_str */ |
| 9779 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9780 | 0, /* tp_setattro */ |
| 9781 | 0, /* tp_as_buffer */ |
| 9782 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9783 | 0, /* tp_doc */ |
| 9784 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9785 | 0, /* tp_clear */ |
| 9786 | 0, /* tp_richcompare */ |
| 9787 | 0, /* tp_weaklistoffset */ |
| 9788 | PyObject_SelfIter, /* tp_iter */ |
| 9789 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9790 | unicodeiter_methods, /* tp_methods */ |
| 9791 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9792 | }; |
| 9793 | |
| 9794 | static PyObject * |
| 9795 | unicode_iter(PyObject *seq) |
| 9796 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9797 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9798 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9799 | if (!PyUnicode_Check(seq)) { |
| 9800 | PyErr_BadInternalCall(); |
| 9801 | return NULL; |
| 9802 | } |
| 9803 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9804 | if (it == NULL) |
| 9805 | return NULL; |
| 9806 | it->it_index = 0; |
| 9807 | Py_INCREF(seq); |
| 9808 | it->it_seq = (PyUnicodeObject *)seq; |
| 9809 | _PyObject_GC_TRACK(it); |
| 9810 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9811 | } |
| 9812 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9813 | size_t |
| 9814 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9815 | { |
| 9816 | int res = 0; |
| 9817 | while(*u++) |
| 9818 | res++; |
| 9819 | return res; |
| 9820 | } |
| 9821 | |
| 9822 | Py_UNICODE* |
| 9823 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9824 | { |
| 9825 | Py_UNICODE *u = s1; |
| 9826 | while ((*u++ = *s2++)); |
| 9827 | return s1; |
| 9828 | } |
| 9829 | |
| 9830 | Py_UNICODE* |
| 9831 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9832 | { |
| 9833 | Py_UNICODE *u = s1; |
| 9834 | while ((*u++ = *s2++)) |
| 9835 | if (n-- == 0) |
| 9836 | break; |
| 9837 | return s1; |
| 9838 | } |
| 9839 | |
| 9840 | int |
| 9841 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9842 | { |
| 9843 | while (*s1 && *s2 && *s1 == *s2) |
| 9844 | s1++, s2++; |
| 9845 | if (*s1 && *s2) |
| 9846 | return (*s1 < *s2) ? -1 : +1; |
| 9847 | if (*s1) |
| 9848 | return 1; |
| 9849 | if (*s2) |
| 9850 | return -1; |
| 9851 | return 0; |
| 9852 | } |
| 9853 | |
| 9854 | Py_UNICODE* |
| 9855 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9856 | { |
| 9857 | const Py_UNICODE *p; |
| 9858 | for (p = s; *p; p++) |
| 9859 | if (*p == c) |
| 9860 | return (Py_UNICODE*)p; |
| 9861 | return NULL; |
| 9862 | } |
| 9863 | |
| 9864 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9865 | #ifdef __cplusplus |
| 9866 | } |
| 9867 | #endif |
| 9868 | |
| 9869 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9870 | /* |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9871 | Local variables: |
| 9872 | c-basic-offset: 4 |
| 9873 | indent-tabs-mode: nil |
| 9874 | End: |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9875 | */ |