Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 46 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 47 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 49 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 50 | #include <windows.h> |
| 51 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Limit for the Unicode object free list */ |
| 54 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
| 57 | /* Limit for the Unicode object free list stay alive optimization. |
| 58 | |
| 59 | The implementation will keep allocated Unicode memory intact for |
| 60 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | malloc()-overhead) bytes of unused garbage. |
| 66 | |
| 67 | Setting the limit to 0 effectively turns the feature off. |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | Note: This is an experimental feature ! If you get core dumps when |
| 70 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | */ |
| 73 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Endianness switches; defaults to little endian */ |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | # define BYTEORDER_IS_BIG_ENDIAN |
| 80 | #else |
| 81 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 82 | #endif |
| 83 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ |
| 85 | |
| 86 | The globals are initialized by the _PyUnicode_Init() API and should |
| 87 | not be used before calling that API. |
| 88 | |
| 89 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 96 | /* This dictionary holds all interned unicode strings. Note that references |
| 97 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 98 | When the interned string reaches a refcnt of 0 the string deallocation |
| 99 | function will delete the reference from this dictionary. |
| 100 | |
| 101 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 102 | count of a string is: s->ob_refcnt + (s->state ? 2 : 0) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 103 | */ |
| 104 | static PyObject *interned; |
| 105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 106 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 107 | static PyUnicodeObject *free_list; |
| 108 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 109 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 110 | /* The empty Unicode object is shared to improve performance. */ |
| 111 | static PyUnicodeObject *unicode_empty; |
| 112 | |
| 113 | /* Single character Unicode strings in the Latin-1 range are being |
| 114 | shared as well. */ |
| 115 | static PyUnicodeObject *unicode_latin1[256]; |
| 116 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 117 | /* Default encoding to use and assume when NULL is passed as encoding |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 118 | parameter; it is fixed to "utf-8". Always use the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 119 | PyUnicode_GetDefaultEncoding() API to access this global. |
| 120 | |
Alexandre Vassalotti | 3d2fd7f | 2007-10-16 00:26:33 +0000 | [diff] [blame] | 121 | Don't forget to alter Py_FileSystemDefaultEncoding if you change the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 122 | hard coded default! |
| 123 | */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 124 | static const char unicode_default_encoding[] = "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 125 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 126 | /* Fast detection of the most frequent whitespace characters */ |
| 127 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 128 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 129 | /* case 0x0009: * HORIZONTAL TABULATION */ |
| 130 | /* case 0x000A: * LINE FEED */ |
| 131 | /* case 0x000B: * VERTICAL TABULATION */ |
| 132 | /* case 0x000C: * FORM FEED */ |
| 133 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 134 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 136 | /* case 0x001C: * FILE SEPARATOR */ |
| 137 | /* case 0x001D: * GROUP SEPARATOR */ |
| 138 | /* case 0x001E: * RECORD SEPARATOR */ |
| 139 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 140 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 141 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 142 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 146 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 147 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 149 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 150 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 151 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 153 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 154 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 157 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 158 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 159 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 160 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 161 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 162 | /* Same for linebreaks */ |
| 163 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 164 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 165 | /* 0x000A, * LINE FEED */ |
| 166 | /* 0x000D, * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 167 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 169 | /* 0x001C, * FILE SEPARATOR */ |
| 170 | /* 0x001D, * GROUP SEPARATOR */ |
| 171 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 172 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 177 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 184 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 185 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 189 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 190 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 191 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 192 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 193 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 194 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 195 | /* This is actually an illegal character, so it should |
| 196 | not be passed to unichr. */ |
| 197 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 198 | #endif |
| 199 | } |
| 200 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 201 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 202 | |
| 203 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 204 | to keep things simple, we use a single bitmask, using the least 5 |
| 205 | bits from each unicode characters as the bit index. */ |
| 206 | |
| 207 | /* the linebreak mask is set up by Unicode_Init below */ |
| 208 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 209 | #if LONG_BIT >= 128 |
| 210 | #define BLOOM_WIDTH 128 |
| 211 | #elif LONG_BIT >= 64 |
| 212 | #define BLOOM_WIDTH 64 |
| 213 | #elif LONG_BIT >= 32 |
| 214 | #define BLOOM_WIDTH 32 |
| 215 | #else |
| 216 | #error "LONG_BIT is smaller than 32" |
| 217 | #endif |
| 218 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 219 | #define BLOOM_MASK unsigned long |
| 220 | |
| 221 | static BLOOM_MASK bloom_linebreak; |
| 222 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 223 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 224 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 225 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 226 | #define BLOOM_LINEBREAK(ch) \ |
| 227 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 228 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 229 | |
| 230 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 231 | { |
| 232 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 233 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 234 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 235 | Py_ssize_t i; |
| 236 | |
| 237 | mask = 0; |
| 238 | for (i = 0; i < len; i++) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 239 | BLOOM_ADD(mask, ptr[i]); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 240 | |
| 241 | return mask; |
| 242 | } |
| 243 | |
| 244 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 245 | { |
| 246 | Py_ssize_t i; |
| 247 | |
| 248 | for (i = 0; i < setlen; i++) |
| 249 | if (set[i] == chr) |
| 250 | return 1; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 255 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 256 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 257 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 258 | /* --- Unicode Object ----------------------------------------------------- */ |
| 259 | |
| 260 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 261 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 262 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | { |
| 264 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 266 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 267 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 268 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 269 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 270 | /* Resizing shared object (unicode_empty or single character |
| 271 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 272 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 273 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 274 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 275 | (unicode->length == 1 && |
| 276 | unicode->str[0] < 256U && |
| 277 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 278 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 279 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 283 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 284 | The overallocation is also used by fastsearch, which assumes that it's |
| 285 | safe to look at str[length] (without making any assumptions about what |
| 286 | it contains). */ |
| 287 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 289 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 290 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 291 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 292 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 293 | PyErr_NoMemory(); |
| 294 | return -1; |
| 295 | } |
| 296 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 297 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 298 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 299 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 300 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 301 | if (unicode->defenc) { |
| 302 | Py_DECREF(unicode->defenc); |
| 303 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 304 | } |
| 305 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 306 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | /* 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] | 311 | Ux0000 terminated; some code (e.g. new_identifier) |
| 312 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 313 | |
| 314 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 315 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 316 | |
| 317 | */ |
| 318 | |
| 319 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 320 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 321 | { |
| 322 | register PyUnicodeObject *unicode; |
| 323 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 324 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 325 | if (length == 0 && unicode_empty != NULL) { |
| 326 | Py_INCREF(unicode_empty); |
| 327 | return unicode_empty; |
| 328 | } |
| 329 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 330 | /* Ensure we won't overflow the size. */ |
| 331 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 332 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 333 | } |
| 334 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 335 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 336 | if (free_list) { |
| 337 | unicode = free_list; |
| 338 | free_list = *(PyUnicodeObject **)unicode; |
| 339 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 340 | if (unicode->str) { |
| 341 | /* Keep-Alive optimization: we only upsize the buffer, |
| 342 | never downsize it. */ |
| 343 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 344 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 345 | PyObject_DEL(unicode->str); |
| 346 | unicode->str = NULL; |
| 347 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 348 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 349 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 350 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 351 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 352 | } |
| 353 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 354 | } |
| 355 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 356 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 357 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 358 | if (unicode == NULL) |
| 359 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 360 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 361 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 364 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 365 | PyErr_NoMemory(); |
| 366 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 367 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 368 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 369 | * the caller fails before initializing str -- unicode_resize() |
| 370 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 371 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 372 | * We don't want unicode_resize to read uninitialized memory in |
| 373 | * that case. |
| 374 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 375 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 376 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 377 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 378 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 379 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 380 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 381 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 382 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 383 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 384 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 385 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 386 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 387 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 388 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 392 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 393 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 394 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 395 | case SSTATE_NOT_INTERNED: |
| 396 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 397 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 398 | case SSTATE_INTERNED_MORTAL: |
| 399 | /* revive dead object temporarily for DelItem */ |
| 400 | Py_REFCNT(unicode) = 3; |
| 401 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 402 | Py_FatalError( |
| 403 | "deletion of interned string failed"); |
| 404 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 405 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 406 | case SSTATE_INTERNED_IMMORTAL: |
| 407 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 408 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 409 | default: |
| 410 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 413 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 414 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 415 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 416 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 417 | PyObject_DEL(unicode->str); |
| 418 | unicode->str = NULL; |
| 419 | unicode->length = 0; |
| 420 | } |
| 421 | if (unicode->defenc) { |
| 422 | Py_DECREF(unicode->defenc); |
| 423 | unicode->defenc = NULL; |
| 424 | } |
| 425 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 426 | *(PyUnicodeObject **)unicode = free_list; |
| 427 | free_list = unicode; |
| 428 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 429 | } |
| 430 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 431 | PyObject_DEL(unicode->str); |
| 432 | Py_XDECREF(unicode->defenc); |
| 433 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 437 | static |
| 438 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 439 | { |
| 440 | register PyUnicodeObject *v; |
| 441 | |
| 442 | /* Argument checks */ |
| 443 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 444 | PyErr_BadInternalCall(); |
| 445 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 446 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 447 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 448 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 449 | PyErr_BadInternalCall(); |
| 450 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* Resizing unicode_empty and single character objects is not |
| 454 | possible since these are being shared. We simply return a fresh |
| 455 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 456 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 457 | (v == unicode_empty || v->length == 1)) { |
| 458 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 459 | if (w == NULL) |
| 460 | return -1; |
| 461 | Py_UNICODE_COPY(w->str, v->str, |
| 462 | length < v->length ? length : v->length); |
| 463 | Py_DECREF(*unicode); |
| 464 | *unicode = w; |
| 465 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 469 | objects, since we can modify them in-place. */ |
| 470 | return unicode_resize(v, length); |
| 471 | } |
| 472 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 473 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 474 | { |
| 475 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 476 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 478 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 479 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 480 | { |
| 481 | PyUnicodeObject *unicode; |
| 482 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 483 | /* If the Unicode data is known at construction time, we can apply |
| 484 | some optimizations which share commonly used objects. */ |
| 485 | if (u != NULL) { |
| 486 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 487 | /* Optimization for empty strings */ |
| 488 | if (size == 0 && unicode_empty != NULL) { |
| 489 | Py_INCREF(unicode_empty); |
| 490 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 491 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 492 | |
| 493 | /* Single character Unicode objects in the Latin-1 range are |
| 494 | shared when using this constructor */ |
| 495 | if (size == 1 && *u < 256) { |
| 496 | unicode = unicode_latin1[*u]; |
| 497 | if (!unicode) { |
| 498 | unicode = _PyUnicode_New(1); |
| 499 | if (!unicode) |
| 500 | return NULL; |
| 501 | unicode->str[0] = *u; |
| 502 | unicode_latin1[*u] = unicode; |
| 503 | } |
| 504 | Py_INCREF(unicode); |
| 505 | return (PyObject *)unicode; |
| 506 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 507 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 508 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 509 | unicode = _PyUnicode_New(size); |
| 510 | if (!unicode) |
| 511 | return NULL; |
| 512 | |
| 513 | /* Copy the Unicode data into the new object */ |
| 514 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 515 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 516 | |
| 517 | return (PyObject *)unicode; |
| 518 | } |
| 519 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 520 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 521 | { |
| 522 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 523 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 524 | if (size < 0) { |
| 525 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 526 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 527 | return NULL; |
| 528 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 529 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 530 | /* 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] | 531 | some optimizations which share commonly used objects. |
| 532 | Also, this means the input must be UTF-8, so fall back to the |
| 533 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 534 | if (u != NULL) { |
| 535 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 536 | /* Optimization for empty strings */ |
| 537 | if (size == 0 && unicode_empty != NULL) { |
| 538 | Py_INCREF(unicode_empty); |
| 539 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 540 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 541 | |
| 542 | /* Single characters are shared when using this constructor. |
| 543 | Restrict to ASCII, since the input must be UTF-8. */ |
| 544 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 545 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 546 | if (!unicode) { |
| 547 | unicode = _PyUnicode_New(1); |
| 548 | if (!unicode) |
| 549 | return NULL; |
| 550 | unicode->str[0] = Py_CHARMASK(*u); |
| 551 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 552 | } |
| 553 | Py_INCREF(unicode); |
| 554 | return (PyObject *)unicode; |
| 555 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 556 | |
| 557 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 560 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 561 | if (!unicode) |
| 562 | return NULL; |
| 563 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 564 | return (PyObject *)unicode; |
| 565 | } |
| 566 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 567 | PyObject *PyUnicode_FromString(const char *u) |
| 568 | { |
| 569 | size_t size = strlen(u); |
| 570 | if (size > PY_SSIZE_T_MAX) { |
| 571 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 572 | return NULL; |
| 573 | } |
| 574 | |
| 575 | return PyUnicode_FromStringAndSize(u, size); |
| 576 | } |
| 577 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 578 | #ifdef HAVE_WCHAR_H |
| 579 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 580 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 581 | # define CONVERT_WCHAR_TO_SURROGATES |
| 582 | #endif |
| 583 | |
| 584 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 585 | |
| 586 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 587 | to convert from UTF32 to UTF16. */ |
| 588 | |
| 589 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 590 | Py_ssize_t size) |
| 591 | { |
| 592 | PyUnicodeObject *unicode; |
| 593 | register Py_ssize_t i; |
| 594 | Py_ssize_t alloc; |
| 595 | const wchar_t *orig_w; |
| 596 | |
| 597 | if (w == NULL) { |
| 598 | if (size == 0) |
| 599 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 600 | PyErr_BadInternalCall(); |
| 601 | return NULL; |
| 602 | } |
| 603 | |
| 604 | if (size == -1) { |
| 605 | size = wcslen(w); |
| 606 | } |
| 607 | |
| 608 | alloc = size; |
| 609 | orig_w = w; |
| 610 | for (i = size; i > 0; i--) { |
| 611 | if (*w > 0xFFFF) |
| 612 | alloc++; |
| 613 | w++; |
| 614 | } |
| 615 | w = orig_w; |
| 616 | unicode = _PyUnicode_New(alloc); |
| 617 | if (!unicode) |
| 618 | return NULL; |
| 619 | |
| 620 | /* Copy the wchar_t data into the new object */ |
| 621 | { |
| 622 | register Py_UNICODE *u; |
| 623 | u = PyUnicode_AS_UNICODE(unicode); |
| 624 | for (i = size; i > 0; i--) { |
| 625 | if (*w > 0xFFFF) { |
| 626 | wchar_t ordinal = *w++; |
| 627 | ordinal -= 0x10000; |
| 628 | *u++ = 0xD800 | (ordinal >> 10); |
| 629 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 630 | } |
| 631 | else |
| 632 | *u++ = *w++; |
| 633 | } |
| 634 | } |
| 635 | return (PyObject *)unicode; |
| 636 | } |
| 637 | |
| 638 | #else |
| 639 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 640 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 641 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 642 | { |
| 643 | PyUnicodeObject *unicode; |
| 644 | |
| 645 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 646 | if (size == 0) |
| 647 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 648 | PyErr_BadInternalCall(); |
| 649 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 652 | if (size == -1) { |
| 653 | size = wcslen(w); |
| 654 | } |
| 655 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 656 | unicode = _PyUnicode_New(size); |
| 657 | if (!unicode) |
| 658 | return NULL; |
| 659 | |
| 660 | /* Copy the wchar_t data into the new object */ |
| 661 | #ifdef HAVE_USABLE_WCHAR_T |
| 662 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 663 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 664 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 665 | register Py_UNICODE *u; |
| 666 | register Py_ssize_t i; |
| 667 | u = PyUnicode_AS_UNICODE(unicode); |
| 668 | for (i = size; i > 0; i--) |
| 669 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 670 | } |
| 671 | #endif |
| 672 | |
| 673 | return (PyObject *)unicode; |
| 674 | } |
| 675 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 676 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 677 | |
| 678 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 679 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 680 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 681 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 682 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 683 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 684 | *fmt++ = '%'; |
| 685 | if (width) { |
| 686 | if (zeropad) |
| 687 | *fmt++ = '0'; |
| 688 | fmt += sprintf(fmt, "%d", width); |
| 689 | } |
| 690 | if (precision) |
| 691 | fmt += sprintf(fmt, ".%d", precision); |
| 692 | if (longflag) |
| 693 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 694 | else if (longlongflag) { |
| 695 | /* longlongflag should only ever be nonzero on machines with |
| 696 | HAVE_LONG_LONG defined */ |
| 697 | #ifdef HAVE_LONG_LONG |
| 698 | char *f = PY_FORMAT_LONG_LONG; |
| 699 | while (*f) |
| 700 | *fmt++ = *f++; |
| 701 | #else |
| 702 | /* we shouldn't ever get here */ |
| 703 | assert(0); |
| 704 | *fmt++ = 'l'; |
| 705 | #endif |
| 706 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 707 | else if (size_tflag) { |
| 708 | char *f = PY_FORMAT_SIZE_T; |
| 709 | while (*f) |
| 710 | *fmt++ = *f++; |
| 711 | } |
| 712 | *fmt++ = c; |
| 713 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 716 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 717 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 718 | /* size of fixed-size buffer for formatting single arguments */ |
| 719 | #define ITEM_BUFFER_LEN 21 |
| 720 | /* maximum number of characters required for output of %ld. 21 characters |
| 721 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 722 | #define MAX_LONG_CHARS 21 |
| 723 | /* maximum number of characters required for output of %lld. |
| 724 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 725 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 726 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 727 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 728 | PyObject * |
| 729 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 730 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 731 | va_list count; |
| 732 | Py_ssize_t callcount = 0; |
| 733 | PyObject **callresults = NULL; |
| 734 | PyObject **callresult = NULL; |
| 735 | Py_ssize_t n = 0; |
| 736 | int width = 0; |
| 737 | int precision = 0; |
| 738 | int zeropad; |
| 739 | const char* f; |
| 740 | Py_UNICODE *s; |
| 741 | PyObject *string; |
| 742 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 743 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 744 | /* use abuffer instead of buffer, if we need more space |
| 745 | * (which can happen if there's a format specifier with width). */ |
| 746 | char *abuffer = NULL; |
| 747 | char *realbuffer; |
| 748 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 749 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 750 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 751 | |
| 752 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 753 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 754 | #else |
| 755 | #ifdef __va_copy |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 756 | __va_copy(count, vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 757 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 758 | count = vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 759 | #endif |
| 760 | #endif |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 761 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 762 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 763 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 764 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 765 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 766 | if (*f == '%') { |
| 767 | if (*(f+1)=='%') |
| 768 | continue; |
| 769 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 770 | ++callcount; |
| 771 | while (ISDIGIT((unsigned)*f)) |
| 772 | width = (width*10) + *f++ - '0'; |
| 773 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 774 | ; |
| 775 | if (*f == 's') |
| 776 | ++callcount; |
| 777 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 778 | } |
| 779 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 780 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 781 | if (callcount) { |
| 782 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 783 | if (!callresults) { |
| 784 | PyErr_NoMemory(); |
| 785 | return NULL; |
| 786 | } |
| 787 | callresult = callresults; |
| 788 | } |
| 789 | /* step 3: figure out how large a buffer we need */ |
| 790 | for (f = format; *f; f++) { |
| 791 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 792 | #ifdef HAVE_LONG_LONG |
| 793 | int longlongflag = 0; |
| 794 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 795 | const char* p = f; |
| 796 | width = 0; |
| 797 | while (ISDIGIT((unsigned)*f)) |
| 798 | width = (width*10) + *f++ - '0'; |
| 799 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 800 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 801 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 802 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 803 | * they don't affect the amount of space we reserve. |
| 804 | */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 805 | if (*f == 'l') { |
| 806 | if (f[1] == 'd' || f[1] == 'u') { |
| 807 | ++f; |
| 808 | } |
| 809 | #ifdef HAVE_LONG_LONG |
| 810 | else if (f[1] == 'l' && |
| 811 | (f[2] == 'd' || f[2] == 'u')) { |
| 812 | longlongflag = 1; |
| 813 | f += 2; |
| 814 | } |
| 815 | #endif |
| 816 | } |
| 817 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 818 | ++f; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 819 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 820 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 821 | switch (*f) { |
| 822 | case 'c': |
| 823 | (void)va_arg(count, int); |
| 824 | /* fall through... */ |
| 825 | case '%': |
| 826 | n++; |
| 827 | break; |
| 828 | case 'd': case 'u': case 'i': case 'x': |
| 829 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 830 | #ifdef HAVE_LONG_LONG |
| 831 | if (longlongflag) { |
| 832 | if (width < MAX_LONG_LONG_CHARS) |
| 833 | width = MAX_LONG_LONG_CHARS; |
| 834 | } |
| 835 | else |
| 836 | #endif |
| 837 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 838 | including sign. Decimal takes the most space. This |
| 839 | isn't enough for octal. If a width is specified we |
| 840 | need more (which we allocate later). */ |
| 841 | if (width < MAX_LONG_CHARS) |
| 842 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 843 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 844 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 845 | if (abuffersize < width) |
| 846 | abuffersize = width; |
| 847 | break; |
| 848 | case 's': |
| 849 | { |
| 850 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 851 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 852 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 853 | if (!str) |
| 854 | goto fail; |
| 855 | n += PyUnicode_GET_SIZE(str); |
| 856 | /* Remember the str and switch to the next slot */ |
| 857 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 858 | break; |
| 859 | } |
| 860 | case 'U': |
| 861 | { |
| 862 | PyObject *obj = va_arg(count, PyObject *); |
| 863 | assert(obj && PyUnicode_Check(obj)); |
| 864 | n += PyUnicode_GET_SIZE(obj); |
| 865 | break; |
| 866 | } |
| 867 | case 'V': |
| 868 | { |
| 869 | PyObject *obj = va_arg(count, PyObject *); |
| 870 | const char *str = va_arg(count, const char *); |
| 871 | assert(obj || str); |
| 872 | assert(!obj || PyUnicode_Check(obj)); |
| 873 | if (obj) |
| 874 | n += PyUnicode_GET_SIZE(obj); |
| 875 | else |
| 876 | n += strlen(str); |
| 877 | break; |
| 878 | } |
| 879 | case 'S': |
| 880 | { |
| 881 | PyObject *obj = va_arg(count, PyObject *); |
| 882 | PyObject *str; |
| 883 | assert(obj); |
| 884 | str = PyObject_Str(obj); |
| 885 | if (!str) |
| 886 | goto fail; |
| 887 | n += PyUnicode_GET_SIZE(str); |
| 888 | /* Remember the str and switch to the next slot */ |
| 889 | *callresult++ = str; |
| 890 | break; |
| 891 | } |
| 892 | case 'R': |
| 893 | { |
| 894 | PyObject *obj = va_arg(count, PyObject *); |
| 895 | PyObject *repr; |
| 896 | assert(obj); |
| 897 | repr = PyObject_Repr(obj); |
| 898 | if (!repr) |
| 899 | goto fail; |
| 900 | n += PyUnicode_GET_SIZE(repr); |
| 901 | /* Remember the repr and switch to the next slot */ |
| 902 | *callresult++ = repr; |
| 903 | break; |
| 904 | } |
| 905 | case 'A': |
| 906 | { |
| 907 | PyObject *obj = va_arg(count, PyObject *); |
| 908 | PyObject *ascii; |
| 909 | assert(obj); |
| 910 | ascii = PyObject_ASCII(obj); |
| 911 | if (!ascii) |
| 912 | goto fail; |
| 913 | n += PyUnicode_GET_SIZE(ascii); |
| 914 | /* Remember the repr and switch to the next slot */ |
| 915 | *callresult++ = ascii; |
| 916 | break; |
| 917 | } |
| 918 | case 'p': |
| 919 | (void) va_arg(count, int); |
| 920 | /* maximum 64-bit pointer representation: |
| 921 | * 0xffffffffffffffff |
| 922 | * so 19 characters is enough. |
| 923 | * XXX I count 18 -- what's the extra for? |
| 924 | */ |
| 925 | n += 19; |
| 926 | break; |
| 927 | default: |
| 928 | /* if we stumble upon an unknown |
| 929 | formatting code, copy the rest of |
| 930 | the format string to the output |
| 931 | string. (we cannot just skip the |
| 932 | code, since there's no way to know |
| 933 | what's in the argument list) */ |
| 934 | n += strlen(p); |
| 935 | goto expand; |
| 936 | } |
| 937 | } else |
| 938 | n++; |
| 939 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 940 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 941 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 942 | /* add 1 for sprintf's trailing null byte */ |
| 943 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 944 | if (!abuffer) { |
| 945 | PyErr_NoMemory(); |
| 946 | goto fail; |
| 947 | } |
| 948 | realbuffer = abuffer; |
| 949 | } |
| 950 | else |
| 951 | realbuffer = buffer; |
| 952 | /* step 4: fill the buffer */ |
| 953 | /* Since we've analyzed how much space we need for the worst case, |
| 954 | we don't have to resize the string. |
| 955 | There can be no errors beyond this point. */ |
| 956 | string = PyUnicode_FromUnicode(NULL, n); |
| 957 | if (!string) |
| 958 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 959 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 960 | s = PyUnicode_AS_UNICODE(string); |
| 961 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 962 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 963 | for (f = format; *f; f++) { |
| 964 | if (*f == '%') { |
| 965 | const char* p = f++; |
| 966 | int longflag = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 967 | int longlongflag = 0; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 968 | int size_tflag = 0; |
| 969 | zeropad = (*f == '0'); |
| 970 | /* parse the width.precision part */ |
| 971 | width = 0; |
| 972 | while (ISDIGIT((unsigned)*f)) |
| 973 | width = (width*10) + *f++ - '0'; |
| 974 | precision = 0; |
| 975 | if (*f == '.') { |
| 976 | f++; |
| 977 | while (ISDIGIT((unsigned)*f)) |
| 978 | precision = (precision*10) + *f++ - '0'; |
| 979 | } |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 980 | /* Handle %ld, %lu, %lld and %llu. */ |
| 981 | if (*f == 'l') { |
| 982 | if (f[1] == 'd' || f[1] == 'u') { |
| 983 | longflag = 1; |
| 984 | ++f; |
| 985 | } |
| 986 | #ifdef HAVE_LONG_LONG |
| 987 | else if (f[1] == 'l' && |
| 988 | (f[2] == 'd' || f[2] == 'u')) { |
| 989 | longlongflag = 1; |
| 990 | f += 2; |
| 991 | } |
| 992 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 993 | } |
| 994 | /* handle the size_t flag. */ |
| 995 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 996 | size_tflag = 1; |
| 997 | ++f; |
| 998 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 999 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1000 | switch (*f) { |
| 1001 | case 'c': |
| 1002 | *s++ = va_arg(vargs, int); |
| 1003 | break; |
| 1004 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1005 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1006 | width, precision, 'd'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1007 | if (longflag) |
| 1008 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1009 | #ifdef HAVE_LONG_LONG |
| 1010 | else if (longlongflag) |
| 1011 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1012 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1013 | else if (size_tflag) |
| 1014 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1015 | else |
| 1016 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1017 | appendstring(realbuffer); |
| 1018 | break; |
| 1019 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1020 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1021 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1022 | if (longflag) |
| 1023 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1024 | #ifdef HAVE_LONG_LONG |
| 1025 | else if (longlongflag) |
| 1026 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1027 | unsigned PY_LONG_LONG)); |
| 1028 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1029 | else if (size_tflag) |
| 1030 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1031 | else |
| 1032 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1033 | appendstring(realbuffer); |
| 1034 | break; |
| 1035 | case 'i': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1036 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1037 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1038 | appendstring(realbuffer); |
| 1039 | break; |
| 1040 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1041 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1042 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1043 | appendstring(realbuffer); |
| 1044 | break; |
| 1045 | case 's': |
| 1046 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1047 | /* unused, since we already have the result */ |
| 1048 | (void) va_arg(vargs, char *); |
| 1049 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1050 | PyUnicode_GET_SIZE(*callresult)); |
| 1051 | s += PyUnicode_GET_SIZE(*callresult); |
| 1052 | /* We're done with the unicode()/repr() => forget it */ |
| 1053 | Py_DECREF(*callresult); |
| 1054 | /* switch to next unicode()/repr() result */ |
| 1055 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | } |
| 1058 | case 'U': |
| 1059 | { |
| 1060 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1061 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1062 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1063 | s += size; |
| 1064 | break; |
| 1065 | } |
| 1066 | case 'V': |
| 1067 | { |
| 1068 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1069 | const char *str = va_arg(vargs, const char *); |
| 1070 | if (obj) { |
| 1071 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1072 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1073 | s += size; |
| 1074 | } else { |
| 1075 | appendstring(str); |
| 1076 | } |
| 1077 | break; |
| 1078 | } |
| 1079 | case 'S': |
| 1080 | case 'R': |
| 1081 | { |
| 1082 | Py_UNICODE *ucopy; |
| 1083 | Py_ssize_t usize; |
| 1084 | Py_ssize_t upos; |
| 1085 | /* unused, since we already have the result */ |
| 1086 | (void) va_arg(vargs, PyObject *); |
| 1087 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1088 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1089 | for (upos = 0; upos<usize;) |
| 1090 | *s++ = ucopy[upos++]; |
| 1091 | /* We're done with the unicode()/repr() => forget it */ |
| 1092 | Py_DECREF(*callresult); |
| 1093 | /* switch to next unicode()/repr() result */ |
| 1094 | ++callresult; |
| 1095 | break; |
| 1096 | } |
| 1097 | case 'p': |
| 1098 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1099 | /* %p is ill-defined: ensure leading 0x. */ |
| 1100 | if (buffer[1] == 'X') |
| 1101 | buffer[1] = 'x'; |
| 1102 | else if (buffer[1] != 'x') { |
| 1103 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1104 | buffer[0] = '0'; |
| 1105 | buffer[1] = 'x'; |
| 1106 | } |
| 1107 | appendstring(buffer); |
| 1108 | break; |
| 1109 | case '%': |
| 1110 | *s++ = '%'; |
| 1111 | break; |
| 1112 | default: |
| 1113 | appendstring(p); |
| 1114 | goto end; |
| 1115 | } |
| 1116 | } else |
| 1117 | *s++ = *f; |
| 1118 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1120 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1121 | if (callresults) |
| 1122 | PyObject_Free(callresults); |
| 1123 | if (abuffer) |
| 1124 | PyObject_Free(abuffer); |
| 1125 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1126 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1127 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1128 | if (callresults) { |
| 1129 | PyObject **callresult2 = callresults; |
| 1130 | while (callresult2 < callresult) { |
| 1131 | Py_DECREF(*callresult2); |
| 1132 | ++callresult2; |
| 1133 | } |
| 1134 | PyObject_Free(callresults); |
| 1135 | } |
| 1136 | if (abuffer) |
| 1137 | PyObject_Free(abuffer); |
| 1138 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | #undef appendstring |
| 1142 | |
| 1143 | PyObject * |
| 1144 | PyUnicode_FromFormat(const char *format, ...) |
| 1145 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1146 | PyObject* ret; |
| 1147 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1148 | |
| 1149 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1150 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1151 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1152 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1153 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1154 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1155 | va_end(vargs); |
| 1156 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1159 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1160 | wchar_t *w, |
| 1161 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1162 | { |
| 1163 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1164 | PyErr_BadInternalCall(); |
| 1165 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1166 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1169 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1170 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1172 | #ifdef HAVE_USABLE_WCHAR_T |
| 1173 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1174 | #else |
| 1175 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1176 | register Py_UNICODE *u; |
| 1177 | register Py_ssize_t i; |
| 1178 | u = PyUnicode_AS_UNICODE(unicode); |
| 1179 | for (i = size; i > 0; i--) |
| 1180 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1181 | } |
| 1182 | #endif |
| 1183 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1184 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1185 | return PyUnicode_GET_SIZE(unicode); |
| 1186 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1187 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | #endif |
| 1191 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1192 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1193 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1194 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1195 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1196 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1197 | PyErr_SetString(PyExc_ValueError, |
| 1198 | "chr() arg not in range(0x110000)"); |
| 1199 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1200 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1201 | |
| 1202 | #ifndef Py_UNICODE_WIDE |
| 1203 | if (ordinal > 0xffff) { |
| 1204 | ordinal -= 0x10000; |
| 1205 | s[0] = 0xD800 | (ordinal >> 10); |
| 1206 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1207 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1208 | } |
| 1209 | #endif |
| 1210 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1211 | s[0] = (Py_UNICODE)ordinal; |
| 1212 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1215 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1216 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1217 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1218 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1219 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1220 | Py_INCREF(obj); |
| 1221 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1222 | } |
| 1223 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1224 | /* For a Unicode subtype that's not a Unicode object, |
| 1225 | return a true Unicode object with the same data. */ |
| 1226 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1227 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1228 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1229 | PyErr_Format(PyExc_TypeError, |
| 1230 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1231 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1232 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1236 | const char *encoding, |
| 1237 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1238 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1239 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1240 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1241 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1243 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1244 | PyErr_BadInternalCall(); |
| 1245 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1246 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1247 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1248 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1249 | PyErr_SetString(PyExc_TypeError, |
| 1250 | "decoding str is not supported"); |
| 1251 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1252 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1253 | |
| 1254 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1255 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1256 | s = PyBytes_AS_STRING(obj); |
| 1257 | len = PyBytes_GET_SIZE(obj); |
| 1258 | } |
| 1259 | else if (PyByteArray_Check(obj)) { |
| 1260 | s = PyByteArray_AS_STRING(obj); |
| 1261 | len = PyByteArray_GET_SIZE(obj); |
| 1262 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1263 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1264 | /* Overwrite the error message with something more useful in |
| 1265 | case of a TypeError. */ |
| 1266 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1267 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1268 | "coercing to str: need string or buffer, " |
| 1269 | "%.80s found", |
| 1270 | Py_TYPE(obj)->tp_name); |
| 1271 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1272 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1273 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1274 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1275 | if (len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1276 | Py_INCREF(unicode_empty); |
| 1277 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1278 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1279 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1280 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1281 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1282 | return v; |
| 1283 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1284 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1285 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1289 | Py_ssize_t size, |
| 1290 | const char *encoding, |
| 1291 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1292 | { |
| 1293 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1294 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1295 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1296 | char *l; |
| 1297 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1298 | |
| 1299 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1300 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1301 | |
| 1302 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1303 | catch e.g. UTF_8 */ |
| 1304 | e = encoding; |
| 1305 | l = lower; |
| 1306 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1307 | if (ISUPPER(*e)) { |
| 1308 | *l++ = TOLOWER(*e++); |
| 1309 | } |
| 1310 | else if (*e == '_') { |
| 1311 | *l++ = '-'; |
| 1312 | e++; |
| 1313 | } |
| 1314 | else { |
| 1315 | *l++ = *e++; |
| 1316 | } |
| 1317 | } |
| 1318 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1319 | |
| 1320 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1321 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1322 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1323 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1324 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1325 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1326 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1327 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1328 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1329 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1330 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1331 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1332 | else if (strcmp(lower, "utf-16") == 0) |
| 1333 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1334 | else if (strcmp(lower, "utf-32") == 0) |
| 1335 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1336 | |
| 1337 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1338 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1339 | 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] | 1340 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1341 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1342 | if (buffer == NULL) |
| 1343 | goto onError; |
| 1344 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1345 | if (unicode == NULL) |
| 1346 | goto onError; |
| 1347 | if (!PyUnicode_Check(unicode)) { |
| 1348 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1349 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1350 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1351 | Py_DECREF(unicode); |
| 1352 | goto onError; |
| 1353 | } |
| 1354 | Py_DECREF(buffer); |
| 1355 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1356 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1357 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1358 | Py_XDECREF(buffer); |
| 1359 | return NULL; |
| 1360 | } |
| 1361 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1362 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1363 | const char *encoding, |
| 1364 | const char *errors) |
| 1365 | { |
| 1366 | PyObject *v; |
| 1367 | |
| 1368 | if (!PyUnicode_Check(unicode)) { |
| 1369 | PyErr_BadArgument(); |
| 1370 | goto onError; |
| 1371 | } |
| 1372 | |
| 1373 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1374 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1375 | |
| 1376 | /* Decode via the codec registry */ |
| 1377 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1378 | if (v == NULL) |
| 1379 | goto onError; |
| 1380 | return v; |
| 1381 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1382 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1383 | return NULL; |
| 1384 | } |
| 1385 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1386 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1387 | const char *encoding, |
| 1388 | const char *errors) |
| 1389 | { |
| 1390 | PyObject *v; |
| 1391 | |
| 1392 | if (!PyUnicode_Check(unicode)) { |
| 1393 | PyErr_BadArgument(); |
| 1394 | goto onError; |
| 1395 | } |
| 1396 | |
| 1397 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1398 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1399 | |
| 1400 | /* Decode via the codec registry */ |
| 1401 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1402 | if (v == NULL) |
| 1403 | goto onError; |
| 1404 | if (!PyUnicode_Check(v)) { |
| 1405 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1406 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1407 | Py_TYPE(v)->tp_name); |
| 1408 | Py_DECREF(v); |
| 1409 | goto onError; |
| 1410 | } |
| 1411 | return v; |
| 1412 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1413 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1414 | return NULL; |
| 1415 | } |
| 1416 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1417 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1418 | Py_ssize_t size, |
| 1419 | const char *encoding, |
| 1420 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1421 | { |
| 1422 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1423 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1424 | unicode = PyUnicode_FromUnicode(s, size); |
| 1425 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1426 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1427 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1428 | Py_DECREF(unicode); |
| 1429 | return v; |
| 1430 | } |
| 1431 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1432 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1433 | const char *encoding, |
| 1434 | const char *errors) |
| 1435 | { |
| 1436 | PyObject *v; |
| 1437 | |
| 1438 | if (!PyUnicode_Check(unicode)) { |
| 1439 | PyErr_BadArgument(); |
| 1440 | goto onError; |
| 1441 | } |
| 1442 | |
| 1443 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1444 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1445 | |
| 1446 | /* Encode via the codec registry */ |
| 1447 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1448 | if (v == NULL) |
| 1449 | goto onError; |
| 1450 | return v; |
| 1451 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1452 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1453 | return NULL; |
| 1454 | } |
| 1455 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1456 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1457 | const char *encoding, |
| 1458 | const char *errors) |
| 1459 | { |
| 1460 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1461 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1462 | if (!PyUnicode_Check(unicode)) { |
| 1463 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1464 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1465 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1466 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1467 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1468 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1469 | |
| 1470 | /* Shortcuts for common default encodings */ |
| 1471 | if (errors == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1472 | if (strcmp(encoding, "utf-8") == 0) |
| 1473 | return PyUnicode_AsUTF8String(unicode); |
| 1474 | else if (strcmp(encoding, "latin-1") == 0) |
| 1475 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1476 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1477 | else if (strcmp(encoding, "mbcs") == 0) |
| 1478 | return PyUnicode_AsMBCSString(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1479 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1480 | else if (strcmp(encoding, "ascii") == 0) |
| 1481 | return PyUnicode_AsASCIIString(unicode); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1482 | /* During bootstrap, we may need to find the encodings |
| 1483 | package, to load the file system encoding, and require the |
| 1484 | file system encoding in order to load the encodings |
| 1485 | package. |
| 1486 | |
| 1487 | Break out of this dependency by assuming that the path to |
| 1488 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1489 | instead, if the file system encoding is the locale's |
| 1490 | encoding. */ |
| 1491 | else if (Py_FileSystemDefaultEncoding && |
| 1492 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1493 | !PyThreadState_GET()->interp->codecs_initialized) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1494 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1495 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1496 | |
| 1497 | /* Encode via the codec registry */ |
| 1498 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1499 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1500 | return NULL; |
| 1501 | |
| 1502 | /* The normal path */ |
| 1503 | if (PyBytes_Check(v)) |
| 1504 | return v; |
| 1505 | |
| 1506 | /* 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] | 1507 | if (PyByteArray_Check(v)) { |
| 1508 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1509 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1510 | PyOS_snprintf(msg, sizeof(msg), |
| 1511 | "encoder %s returned buffer instead of bytes", |
| 1512 | encoding); |
| 1513 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1514 | Py_DECREF(v); |
| 1515 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1516 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1517 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1518 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1519 | Py_DECREF(v); |
| 1520 | return b; |
| 1521 | } |
| 1522 | |
| 1523 | PyErr_Format(PyExc_TypeError, |
| 1524 | "encoder did not return a bytes object (type=%.400s)", |
| 1525 | Py_TYPE(v)->tp_name); |
| 1526 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1527 | return NULL; |
| 1528 | } |
| 1529 | |
| 1530 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1531 | const char *encoding, |
| 1532 | const char *errors) |
| 1533 | { |
| 1534 | PyObject *v; |
| 1535 | |
| 1536 | if (!PyUnicode_Check(unicode)) { |
| 1537 | PyErr_BadArgument(); |
| 1538 | goto onError; |
| 1539 | } |
| 1540 | |
| 1541 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1542 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1543 | |
| 1544 | /* Encode via the codec registry */ |
| 1545 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1546 | if (v == NULL) |
| 1547 | goto onError; |
| 1548 | if (!PyUnicode_Check(v)) { |
| 1549 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1550 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1551 | Py_TYPE(v)->tp_name); |
| 1552 | Py_DECREF(v); |
| 1553 | goto onError; |
| 1554 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1555 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1556 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1557 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1558 | return NULL; |
| 1559 | } |
| 1560 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1561 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1562 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1563 | { |
| 1564 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1565 | if (v) |
| 1566 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1567 | if (errors != NULL) |
| 1568 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1569 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1570 | PyUnicode_GET_SIZE(unicode), |
| 1571 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1572 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1573 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1574 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1575 | return v; |
| 1576 | } |
| 1577 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1578 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1579 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1580 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1581 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1582 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1583 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1584 | PyObject* |
| 1585 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1586 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1587 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1588 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1589 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1590 | bootstrapping process where the codecs aren't ready yet. |
| 1591 | */ |
| 1592 | if (Py_FileSystemDefaultEncoding) { |
| 1593 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1594 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1595 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1596 | } |
| 1597 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1598 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1599 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1600 | } |
| 1601 | #endif |
| 1602 | return PyUnicode_Decode(s, size, |
| 1603 | Py_FileSystemDefaultEncoding, |
| 1604 | "replace"); |
| 1605 | } |
| 1606 | else { |
| 1607 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1608 | } |
| 1609 | } |
| 1610 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1611 | /* Convert the argument to a bytes object, according to the file |
Gregory P. Smith | cc47d8c | 2010-02-27 08:33:11 +0000 | [diff] [blame] | 1612 | system encoding. The addr param must be a PyObject**. |
| 1613 | This is designed to be used with "O&" in PyArg_Parse APIs. */ |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1614 | |
| 1615 | int |
| 1616 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1617 | { |
| 1618 | PyObject *output = NULL; |
| 1619 | Py_ssize_t size; |
| 1620 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1621 | if (arg == NULL) { |
| 1622 | Py_DECREF(*(PyObject**)addr); |
| 1623 | return 1; |
| 1624 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1625 | if (PyBytes_Check(arg) || PyByteArray_Check(arg)) { |
| 1626 | output = arg; |
| 1627 | Py_INCREF(output); |
| 1628 | } |
| 1629 | else { |
| 1630 | arg = PyUnicode_FromObject(arg); |
| 1631 | if (!arg) |
| 1632 | return 0; |
| 1633 | output = PyUnicode_AsEncodedObject(arg, |
| 1634 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1635 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1636 | Py_DECREF(arg); |
| 1637 | if (!output) |
| 1638 | return 0; |
| 1639 | if (!PyBytes_Check(output)) { |
| 1640 | Py_DECREF(output); |
| 1641 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1642 | return 0; |
| 1643 | } |
| 1644 | } |
| 1645 | if (PyBytes_Check(output)) { |
| 1646 | size = PyBytes_GET_SIZE(output); |
| 1647 | data = PyBytes_AS_STRING(output); |
| 1648 | } |
| 1649 | else { |
| 1650 | size = PyByteArray_GET_SIZE(output); |
| 1651 | data = PyByteArray_AS_STRING(output); |
| 1652 | } |
| 1653 | if (size != strlen(data)) { |
| 1654 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1655 | Py_DECREF(output); |
| 1656 | return 0; |
| 1657 | } |
| 1658 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1659 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1663 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1664 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1665 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1666 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1667 | if (!PyUnicode_Check(unicode)) { |
| 1668 | PyErr_BadArgument(); |
| 1669 | return NULL; |
| 1670 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1671 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1672 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1673 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1674 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1675 | *psize = PyBytes_GET_SIZE(bytes); |
| 1676 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1680 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1681 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1682 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1685 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1686 | { |
| 1687 | if (!PyUnicode_Check(unicode)) { |
| 1688 | PyErr_BadArgument(); |
| 1689 | goto onError; |
| 1690 | } |
| 1691 | return PyUnicode_AS_UNICODE(unicode); |
| 1692 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1693 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1694 | return NULL; |
| 1695 | } |
| 1696 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1697 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1698 | { |
| 1699 | if (!PyUnicode_Check(unicode)) { |
| 1700 | PyErr_BadArgument(); |
| 1701 | goto onError; |
| 1702 | } |
| 1703 | return PyUnicode_GET_SIZE(unicode); |
| 1704 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1705 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1706 | return -1; |
| 1707 | } |
| 1708 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1709 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1710 | { |
| 1711 | return unicode_default_encoding; |
| 1712 | } |
| 1713 | |
| 1714 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1715 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1716 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1717 | PyErr_Format(PyExc_ValueError, |
| 1718 | "Can only set default encoding to %s", |
| 1719 | unicode_default_encoding); |
| 1720 | return -1; |
| 1721 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1722 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1725 | /* error handling callback helper: |
| 1726 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1727 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1728 | and adjust various state variables. |
| 1729 | return 0 on success, -1 on error |
| 1730 | */ |
| 1731 | |
| 1732 | static |
| 1733 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1734 | const char *encoding, const char *reason, |
| 1735 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1736 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1737 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1738 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1739 | 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] | 1740 | |
| 1741 | PyObject *restuple = NULL; |
| 1742 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1743 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1744 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1745 | Py_ssize_t requiredsize; |
| 1746 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1747 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1748 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1749 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1750 | int res = -1; |
| 1751 | |
| 1752 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1753 | *errorHandler = PyCodec_LookupError(errors); |
| 1754 | if (*errorHandler == NULL) |
| 1755 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1759 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1760 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
| 1761 | if (*exceptionObject == NULL) |
| 1762 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1763 | } |
| 1764 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1765 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1766 | goto onError; |
| 1767 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1768 | goto onError; |
| 1769 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1770 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1774 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1775 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1776 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1777 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1778 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1779 | } |
| 1780 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1781 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1782 | |
| 1783 | /* Copy back the bytes variables, which might have been modified by the |
| 1784 | callback */ |
| 1785 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1786 | if (!inputobj) |
| 1787 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1788 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1789 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1790 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1791 | *input = PyBytes_AS_STRING(inputobj); |
| 1792 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1793 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1794 | /* we can DECREF safely, as the exception has another reference, |
| 1795 | so the object won't go away. */ |
| 1796 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1797 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1798 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1799 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1800 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1801 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1802 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1803 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1804 | |
| 1805 | /* need more space? (at least enough for what we |
| 1806 | have+the replacement+the rest of the string (starting |
| 1807 | at the new input position), so we won't have to check space |
| 1808 | when there are no errors in the rest of the string) */ |
| 1809 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1810 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1811 | requiredsize = *outpos + repsize + insize-newpos; |
| 1812 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1813 | if (requiredsize<2*outsize) |
| 1814 | requiredsize = 2*outsize; |
| 1815 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1816 | goto onError; |
| 1817 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1818 | } |
| 1819 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1820 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1821 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1822 | *outptr += repsize; |
| 1823 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1824 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1825 | /* we made it! */ |
| 1826 | res = 0; |
| 1827 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1828 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1829 | Py_XDECREF(restuple); |
| 1830 | return res; |
| 1831 | } |
| 1832 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1833 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1834 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1835 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1836 | |
| 1837 | /* Three simple macros defining base-64. */ |
| 1838 | |
| 1839 | /* Is c a base-64 character? */ |
| 1840 | |
| 1841 | #define IS_BASE64(c) \ |
| 1842 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1843 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1844 | ((c) >= '0' && (c) <= '9') || \ |
| 1845 | (c) == '+' || (c) == '/') |
| 1846 | |
| 1847 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1848 | |
| 1849 | #define FROM_BASE64(c) \ |
| 1850 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1851 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1852 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1853 | (c) == '+' ? 62 : 63) |
| 1854 | |
| 1855 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1856 | |
| 1857 | #define TO_BASE64(n) \ |
| 1858 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1859 | |
| 1860 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1861 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1862 | * byte not decoding to itself is the + which begins a base64 |
| 1863 | * string. */ |
| 1864 | |
| 1865 | #define DECODE_DIRECT(c) \ |
| 1866 | ((c) <= 127 && (c) != '+') |
| 1867 | |
| 1868 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1869 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1870 | * the above). See RFC2152. This array identifies these different |
| 1871 | * sets: |
| 1872 | * 0 : "Set D" |
| 1873 | * alphanumeric and '(),-./:? |
| 1874 | * 1 : "Set O" |
| 1875 | * !"#$%&*;<=>@[]^_`{|} |
| 1876 | * 2 : "whitespace" |
| 1877 | * ht nl cr sp |
| 1878 | * 3 : special (must be base64 encoded) |
| 1879 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1880 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1881 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1882 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1883 | char utf7_category[128] = { |
| 1884 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 1885 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 1886 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 1887 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1888 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 1889 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 1890 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 1891 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 1892 | /* @ A B C D E F G H I J K L M N O */ |
| 1893 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1894 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 1895 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 1896 | /* ` a b c d e f g h i j k l m n o */ |
| 1897 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1898 | /* p q r s t u v w x y z { | } ~ del */ |
| 1899 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1900 | }; |
| 1901 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1902 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 1903 | * answer depends on whether we are encoding set O as itself, and also |
| 1904 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 1905 | * clear that the answers to these questions vary between |
| 1906 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1907 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1908 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 1909 | ((c) < 128 && (c) > 0 && \ |
| 1910 | ((utf7_category[(c)] == 0) || \ |
| 1911 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 1912 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1913 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1914 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1915 | Py_ssize_t size, |
| 1916 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1917 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1918 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1919 | } |
| 1920 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1921 | /* The decoder. The only state we preserve is our read position, |
| 1922 | * i.e. how many characters we have consumed. So if we end in the |
| 1923 | * middle of a shift sequence we have to back off the read position |
| 1924 | * and the output to the beginning of the sequence, otherwise we lose |
| 1925 | * all the shift state (seen bits, number of bits seen, high |
| 1926 | * surrogate). */ |
| 1927 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1928 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1929 | Py_ssize_t size, |
| 1930 | const char *errors, |
| 1931 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1932 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1933 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1934 | Py_ssize_t startinpos; |
| 1935 | Py_ssize_t endinpos; |
| 1936 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1937 | const char *e; |
| 1938 | PyUnicodeObject *unicode; |
| 1939 | Py_UNICODE *p; |
| 1940 | const char *errmsg = ""; |
| 1941 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1942 | Py_UNICODE *shiftOutStart; |
| 1943 | unsigned int base64bits = 0; |
| 1944 | unsigned long base64buffer = 0; |
| 1945 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1946 | PyObject *errorHandler = NULL; |
| 1947 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1948 | |
| 1949 | unicode = _PyUnicode_New(size); |
| 1950 | if (!unicode) |
| 1951 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1952 | if (size == 0) { |
| 1953 | if (consumed) |
| 1954 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1955 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1956 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1957 | |
| 1958 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1959 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1960 | e = s + size; |
| 1961 | |
| 1962 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1963 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1964 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1965 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1966 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1967 | if (inShift) { /* in a base-64 section */ |
| 1968 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 1969 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 1970 | base64bits += 6; |
| 1971 | s++; |
| 1972 | if (base64bits >= 16) { |
| 1973 | /* we have enough bits for a UTF-16 value */ |
| 1974 | Py_UNICODE outCh = (Py_UNICODE) |
| 1975 | (base64buffer >> (base64bits-16)); |
| 1976 | base64bits -= 16; |
| 1977 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 1978 | if (surrogate) { |
| 1979 | /* expecting a second surrogate */ |
| 1980 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 1981 | #ifdef Py_UNICODE_WIDE |
| 1982 | *p++ = (((surrogate & 0x3FF)<<10) |
| 1983 | | (outCh & 0x3FF)) + 0x10000; |
| 1984 | #else |
| 1985 | *p++ = surrogate; |
| 1986 | *p++ = outCh; |
| 1987 | #endif |
| 1988 | surrogate = 0; |
| 1989 | } |
| 1990 | else { |
| 1991 | surrogate = 0; |
| 1992 | errmsg = "second surrogate missing"; |
| 1993 | goto utf7Error; |
| 1994 | } |
| 1995 | } |
| 1996 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 1997 | /* first surrogate */ |
| 1998 | surrogate = outCh; |
| 1999 | } |
| 2000 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2001 | errmsg = "unexpected second surrogate"; |
| 2002 | goto utf7Error; |
| 2003 | } |
| 2004 | else { |
| 2005 | *p++ = outCh; |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2010 | inShift = 0; |
| 2011 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2012 | if (surrogate) { |
| 2013 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2014 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2015 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2016 | if (base64bits > 0) { /* left-over bits */ |
| 2017 | if (base64bits >= 6) { |
| 2018 | /* We've seen at least one base-64 character */ |
| 2019 | errmsg = "partial character in shift sequence"; |
| 2020 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2021 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2022 | else { |
| 2023 | /* Some bits remain; they should be zero */ |
| 2024 | if (base64buffer != 0) { |
| 2025 | errmsg = "non-zero padding bits in shift sequence"; |
| 2026 | goto utf7Error; |
| 2027 | } |
| 2028 | } |
| 2029 | } |
| 2030 | if (ch != '-') { |
| 2031 | /* '-' is absorbed; other terminating |
| 2032 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2033 | *p++ = ch; |
| 2034 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2038 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2039 | s++; /* consume '+' */ |
| 2040 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2041 | s++; |
| 2042 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2043 | } |
| 2044 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2045 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2046 | shiftOutStart = p; |
| 2047 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2048 | } |
| 2049 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2050 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2051 | *p++ = ch; |
| 2052 | s++; |
| 2053 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2054 | else { |
| 2055 | startinpos = s-starts; |
| 2056 | s++; |
| 2057 | errmsg = "unexpected special character"; |
| 2058 | goto utf7Error; |
| 2059 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2060 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2061 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2062 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2063 | endinpos = s-starts; |
| 2064 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2065 | errors, &errorHandler, |
| 2066 | "utf7", errmsg, |
| 2067 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2068 | &unicode, &outpos, &p)) |
| 2069 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2072 | /* end of string */ |
| 2073 | |
| 2074 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2075 | /* if we're in an inconsistent state, that's an error */ |
| 2076 | if (surrogate || |
| 2077 | (base64bits >= 6) || |
| 2078 | (base64bits > 0 && base64buffer != 0)) { |
| 2079 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2080 | endinpos = size; |
| 2081 | if (unicode_decode_call_errorhandler( |
| 2082 | errors, &errorHandler, |
| 2083 | "utf7", "unterminated shift sequence", |
| 2084 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2085 | &unicode, &outpos, &p)) |
| 2086 | goto onError; |
| 2087 | if (s < e) |
| 2088 | goto restart; |
| 2089 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2090 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2091 | |
| 2092 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2093 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2094 | if (inShift) { |
| 2095 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2096 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2097 | } |
| 2098 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2099 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2100 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2101 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2102 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2103 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2104 | goto onError; |
| 2105 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2106 | Py_XDECREF(errorHandler); |
| 2107 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2108 | return (PyObject *)unicode; |
| 2109 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2110 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2111 | Py_XDECREF(errorHandler); |
| 2112 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2113 | Py_DECREF(unicode); |
| 2114 | return NULL; |
| 2115 | } |
| 2116 | |
| 2117 | |
| 2118 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2119 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2120 | int base64SetO, |
| 2121 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2122 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2123 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2124 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2125 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2126 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2127 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2128 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2129 | unsigned int base64bits = 0; |
| 2130 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2131 | char * out; |
| 2132 | char * start; |
| 2133 | |
| 2134 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2135 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2136 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2137 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2138 | return PyErr_NoMemory(); |
| 2139 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2140 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2141 | if (v == NULL) |
| 2142 | return NULL; |
| 2143 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2144 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2145 | for (;i < size; ++i) { |
| 2146 | Py_UNICODE ch = s[i]; |
| 2147 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2148 | if (inShift) { |
| 2149 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2150 | /* shifting out */ |
| 2151 | if (base64bits) { /* output remaining bits */ |
| 2152 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2153 | base64buffer = 0; |
| 2154 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2155 | } |
| 2156 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2157 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2158 | so no '-' is required, except if the character is itself a '-' */ |
| 2159 | if (IS_BASE64(ch) || ch == '-') { |
| 2160 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2161 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2162 | *out++ = (char) ch; |
| 2163 | } |
| 2164 | else { |
| 2165 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2166 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2167 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2168 | else { /* not in a shift sequence */ |
| 2169 | if (ch == '+') { |
| 2170 | *out++ = '+'; |
| 2171 | *out++ = '-'; |
| 2172 | } |
| 2173 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2174 | *out++ = (char) ch; |
| 2175 | } |
| 2176 | else { |
| 2177 | *out++ = '+'; |
| 2178 | inShift = 1; |
| 2179 | goto encode_char; |
| 2180 | } |
| 2181 | } |
| 2182 | continue; |
| 2183 | encode_char: |
| 2184 | #ifdef Py_UNICODE_WIDE |
| 2185 | if (ch >= 0x10000) { |
| 2186 | /* code first surrogate */ |
| 2187 | base64bits += 16; |
| 2188 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2189 | while (base64bits >= 6) { |
| 2190 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2191 | base64bits -= 6; |
| 2192 | } |
| 2193 | /* prepare second surrogate */ |
| 2194 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2195 | } |
| 2196 | #endif |
| 2197 | base64bits += 16; |
| 2198 | base64buffer = (base64buffer << 16) | ch; |
| 2199 | while (base64bits >= 6) { |
| 2200 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2201 | base64bits -= 6; |
| 2202 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2203 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2204 | if (base64bits) |
| 2205 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2206 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2207 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2208 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2209 | return NULL; |
| 2210 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2213 | #undef IS_BASE64 |
| 2214 | #undef FROM_BASE64 |
| 2215 | #undef TO_BASE64 |
| 2216 | #undef DECODE_DIRECT |
| 2217 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2219 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2220 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2221 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2222 | char utf8_code_length[256] = { |
| 2223 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 2224 | illegal prefix. see RFC 2279 for details */ |
| 2225 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2226 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2227 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2228 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2229 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2230 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2231 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2232 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2233 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2234 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2235 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2236 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2237 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2238 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2239 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2240 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 2241 | }; |
| 2242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2243 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2244 | Py_ssize_t size, |
| 2245 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2246 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2247 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2248 | } |
| 2249 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2250 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2251 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2252 | |
| 2253 | /* Mask to quickly check whether a C 'long' contains a |
| 2254 | non-ASCII, UTF8-encoded char. */ |
| 2255 | #if (SIZEOF_LONG == 8) |
| 2256 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2257 | #elif (SIZEOF_LONG == 4) |
| 2258 | # define ASCII_CHAR_MASK 0x80808080L |
| 2259 | #else |
| 2260 | # error C 'long' size should be either 4 or 8! |
| 2261 | #endif |
| 2262 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2263 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2264 | Py_ssize_t size, |
| 2265 | const char *errors, |
| 2266 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2267 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2268 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2269 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2270 | Py_ssize_t startinpos; |
| 2271 | Py_ssize_t endinpos; |
| 2272 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2273 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2274 | PyUnicodeObject *unicode; |
| 2275 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2276 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2277 | PyObject *errorHandler = NULL; |
| 2278 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2279 | |
| 2280 | /* Note: size will always be longer than the resulting Unicode |
| 2281 | character count */ |
| 2282 | unicode = _PyUnicode_New(size); |
| 2283 | if (!unicode) |
| 2284 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2285 | if (size == 0) { |
| 2286 | if (consumed) |
| 2287 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2288 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2289 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2290 | |
| 2291 | /* Unpack UTF-8 encoded data */ |
| 2292 | p = unicode->str; |
| 2293 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2294 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2295 | |
| 2296 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2297 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2298 | |
| 2299 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2300 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2301 | input will consist of an overwhelming majority of ASCII |
| 2302 | characters, we try to optimize for this case by checking |
| 2303 | as many characters as a C 'long' can contain. |
| 2304 | First, check if we can do an aligned read, as most CPUs have |
| 2305 | a penalty for unaligned reads. |
| 2306 | */ |
| 2307 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2308 | /* Help register allocation */ |
| 2309 | register const char *_s = s; |
| 2310 | register Py_UNICODE *_p = p; |
| 2311 | while (_s < aligned_end) { |
| 2312 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2313 | and do a fast unrolled copy if it only contains ASCII |
| 2314 | characters. */ |
| 2315 | unsigned long data = *(unsigned long *) _s; |
| 2316 | if (data & ASCII_CHAR_MASK) |
| 2317 | break; |
| 2318 | _p[0] = (unsigned char) _s[0]; |
| 2319 | _p[1] = (unsigned char) _s[1]; |
| 2320 | _p[2] = (unsigned char) _s[2]; |
| 2321 | _p[3] = (unsigned char) _s[3]; |
| 2322 | #if (SIZEOF_LONG == 8) |
| 2323 | _p[4] = (unsigned char) _s[4]; |
| 2324 | _p[5] = (unsigned char) _s[5]; |
| 2325 | _p[6] = (unsigned char) _s[6]; |
| 2326 | _p[7] = (unsigned char) _s[7]; |
| 2327 | #endif |
| 2328 | _s += SIZEOF_LONG; |
| 2329 | _p += SIZEOF_LONG; |
| 2330 | } |
| 2331 | s = _s; |
| 2332 | p = _p; |
| 2333 | if (s == e) |
| 2334 | break; |
| 2335 | ch = (unsigned char)*s; |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2340 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2341 | s++; |
| 2342 | continue; |
| 2343 | } |
| 2344 | |
| 2345 | n = utf8_code_length[ch]; |
| 2346 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2347 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2348 | if (consumed) |
| 2349 | break; |
| 2350 | else { |
| 2351 | errmsg = "unexpected end of data"; |
| 2352 | startinpos = s-starts; |
| 2353 | endinpos = size; |
| 2354 | goto utf8Error; |
| 2355 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2356 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2357 | |
| 2358 | switch (n) { |
| 2359 | |
| 2360 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2361 | errmsg = "unexpected code byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2362 | startinpos = s-starts; |
| 2363 | endinpos = startinpos+1; |
| 2364 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2365 | |
| 2366 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2367 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2368 | startinpos = s-starts; |
| 2369 | endinpos = startinpos+1; |
| 2370 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2371 | |
| 2372 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2373 | if ((s[1] & 0xc0) != 0x80) { |
| 2374 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2375 | startinpos = s-starts; |
| 2376 | endinpos = startinpos+2; |
| 2377 | goto utf8Error; |
| 2378 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2379 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2380 | if (ch < 0x80) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2381 | startinpos = s-starts; |
| 2382 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2383 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2384 | goto utf8Error; |
| 2385 | } |
| 2386 | else |
| 2387 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2388 | break; |
| 2389 | |
| 2390 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2391 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2392 | (s[2] & 0xc0) != 0x80) { |
| 2393 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2394 | startinpos = s-starts; |
| 2395 | endinpos = startinpos+3; |
| 2396 | goto utf8Error; |
| 2397 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2398 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2399 | if (ch < 0x0800 || (ch >= 0xd800 && ch <= 0xDFFF)) { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2400 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2401 | startinpos = s-starts; |
| 2402 | endinpos = startinpos+3; |
| 2403 | goto utf8Error; |
| 2404 | } |
| 2405 | else |
| 2406 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2407 | break; |
| 2408 | |
| 2409 | case 4: |
| 2410 | if ((s[1] & 0xc0) != 0x80 || |
| 2411 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2412 | (s[3] & 0xc0) != 0x80) { |
| 2413 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2414 | startinpos = s-starts; |
| 2415 | endinpos = startinpos+4; |
| 2416 | goto utf8Error; |
| 2417 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2418 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2419 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2420 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2421 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2422 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2423 | || (ch > 0x10ffff)) /* maximum value allowed for |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2424 | UTF-16 */ |
| 2425 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2426 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2427 | startinpos = s-starts; |
| 2428 | endinpos = startinpos+4; |
| 2429 | goto utf8Error; |
| 2430 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2431 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2432 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2433 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2434 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2435 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2436 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2437 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2438 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2439 | /* high surrogate = top 10 bits added to D800 */ |
| 2440 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2441 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2442 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2443 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2444 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2445 | break; |
| 2446 | |
| 2447 | default: |
| 2448 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2449 | errmsg = "unsupported Unicode code range"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2450 | startinpos = s-starts; |
| 2451 | endinpos = startinpos+n; |
| 2452 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2453 | } |
| 2454 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2455 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2456 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2457 | utf8Error: |
| 2458 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2459 | if (unicode_decode_call_errorhandler( |
| 2460 | errors, &errorHandler, |
| 2461 | "utf8", errmsg, |
| 2462 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2463 | &unicode, &outpos, &p)) |
| 2464 | goto onError; |
| 2465 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2466 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2467 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2468 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2469 | |
| 2470 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2471 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2472 | goto onError; |
| 2473 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2474 | Py_XDECREF(errorHandler); |
| 2475 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2476 | return (PyObject *)unicode; |
| 2477 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2478 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2479 | Py_XDECREF(errorHandler); |
| 2480 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2481 | Py_DECREF(unicode); |
| 2482 | return NULL; |
| 2483 | } |
| 2484 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2485 | #undef ASCII_CHAR_MASK |
| 2486 | |
| 2487 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2488 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2489 | and allocate exactly as much space needed at the end. Else allocate the |
| 2490 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2491 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2492 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2493 | PyObject * |
| 2494 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2495 | Py_ssize_t size, |
| 2496 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2497 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2498 | #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] | 2499 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2500 | Py_ssize_t i; /* index into s of next input byte */ |
| 2501 | PyObject *result; /* result string object */ |
| 2502 | char *p; /* next free byte in output buffer */ |
| 2503 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2504 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2505 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2506 | PyObject *errorHandler = NULL; |
| 2507 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2508 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2509 | assert(s != NULL); |
| 2510 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2511 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2512 | if (size <= MAX_SHORT_UNICHARS) { |
| 2513 | /* Write into the stack buffer; nallocated can't overflow. |
| 2514 | * At the end, we'll allocate exactly as much heap space as it |
| 2515 | * turns out we need. |
| 2516 | */ |
| 2517 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2518 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2519 | p = stackbuf; |
| 2520 | } |
| 2521 | else { |
| 2522 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2523 | nallocated = size * 4; |
| 2524 | if (nallocated / 4 != size) /* overflow! */ |
| 2525 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2526 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2527 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2528 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2529 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2530 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2531 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2532 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2533 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2534 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2535 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2536 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2537 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2539 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2540 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2541 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2542 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2543 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2544 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2545 | /* Encode UCS2 Unicode ordinals */ |
| 2546 | if (ch < 0x10000) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2547 | #ifndef Py_UNICODE_WIDE |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2548 | /* Special case: check for high surrogate */ |
| 2549 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2550 | Py_UCS4 ch2 = s[i]; |
| 2551 | /* Check for low surrogate and combine the two to |
| 2552 | form a UCS4 value */ |
| 2553 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2554 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2555 | i++; |
| 2556 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2557 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2558 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2559 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2560 | #endif |
| 2561 | if (ch >= 0xd800 && ch <= 0xdfff) { |
| 2562 | Py_ssize_t newpos; |
| 2563 | PyObject *rep; |
| 2564 | char *prep; |
| 2565 | int k; |
| 2566 | rep = unicode_encode_call_errorhandler |
| 2567 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2568 | s, size, &exc, i-1, i, &newpos); |
| 2569 | if (!rep) |
| 2570 | goto error; |
| 2571 | /* Implementation limitations: only support error handler that return |
| 2572 | bytes, and only support up to four replacement bytes. */ |
| 2573 | if (!PyBytes_Check(rep)) { |
| 2574 | PyErr_SetString(PyExc_TypeError, "error handler should have returned bytes"); |
| 2575 | Py_DECREF(rep); |
| 2576 | goto error; |
| 2577 | } |
| 2578 | if (PyBytes_Size(rep) > 4) { |
| 2579 | PyErr_SetString(PyExc_TypeError, "error handler returned too many bytes"); |
| 2580 | Py_DECREF(rep); |
| 2581 | goto error; |
| 2582 | } |
| 2583 | prep = PyBytes_AsString(rep); |
| 2584 | for(k = PyBytes_Size(rep); k > 0; k--) |
| 2585 | *p++ = *prep++; |
| 2586 | Py_DECREF(rep); |
| 2587 | continue; |
| 2588 | |
| 2589 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2590 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2591 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2592 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2593 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2594 | } |
Benjamin Peterson | adf6a6c | 2009-11-10 21:23:15 +0000 | [diff] [blame] | 2595 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2596 | encodeUCS4: |
Benjamin Peterson | adf6a6c | 2009-11-10 21:23:15 +0000 | [diff] [blame] | 2597 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2598 | /* Encode UCS4 Unicode ordinals */ |
| 2599 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2600 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2601 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2602 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2603 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2604 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2605 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2606 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2607 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2608 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2609 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2610 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2611 | } |
| 2612 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2613 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2614 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2615 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2616 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2617 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2618 | Py_XDECREF(errorHandler); |
| 2619 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2620 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2621 | error: |
| 2622 | Py_XDECREF(errorHandler); |
| 2623 | Py_XDECREF(exc); |
| 2624 | Py_XDECREF(result); |
| 2625 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2626 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2627 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2630 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2631 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2632 | if (!PyUnicode_Check(unicode)) { |
| 2633 | PyErr_BadArgument(); |
| 2634 | return NULL; |
| 2635 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2636 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2637 | PyUnicode_GET_SIZE(unicode), |
| 2638 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2641 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2642 | |
| 2643 | PyObject * |
| 2644 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2645 | Py_ssize_t size, |
| 2646 | const char *errors, |
| 2647 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2648 | { |
| 2649 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2650 | } |
| 2651 | |
| 2652 | PyObject * |
| 2653 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2654 | Py_ssize_t size, |
| 2655 | const char *errors, |
| 2656 | int *byteorder, |
| 2657 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2658 | { |
| 2659 | const char *starts = s; |
| 2660 | Py_ssize_t startinpos; |
| 2661 | Py_ssize_t endinpos; |
| 2662 | Py_ssize_t outpos; |
| 2663 | PyUnicodeObject *unicode; |
| 2664 | Py_UNICODE *p; |
| 2665 | #ifndef Py_UNICODE_WIDE |
| 2666 | int i, pairs; |
| 2667 | #else |
| 2668 | const int pairs = 0; |
| 2669 | #endif |
| 2670 | const unsigned char *q, *e; |
| 2671 | int bo = 0; /* assume native ordering by default */ |
| 2672 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2673 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2674 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2675 | int iorder[] = {0, 1, 2, 3}; |
| 2676 | #else |
| 2677 | int iorder[] = {3, 2, 1, 0}; |
| 2678 | #endif |
| 2679 | PyObject *errorHandler = NULL; |
| 2680 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2681 | /* On narrow builds we split characters outside the BMP into two |
| 2682 | codepoints => count how much extra space we need. */ |
| 2683 | #ifndef Py_UNICODE_WIDE |
| 2684 | for (i = pairs = 0; i < size/4; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2685 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2686 | pairs++; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2687 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2688 | |
| 2689 | /* This might be one to much, because of a BOM */ |
| 2690 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2691 | if (!unicode) |
| 2692 | return NULL; |
| 2693 | if (size == 0) |
| 2694 | return (PyObject *)unicode; |
| 2695 | |
| 2696 | /* Unpack UTF-32 encoded data */ |
| 2697 | p = unicode->str; |
| 2698 | q = (unsigned char *)s; |
| 2699 | e = q + size; |
| 2700 | |
| 2701 | if (byteorder) |
| 2702 | bo = *byteorder; |
| 2703 | |
| 2704 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2705 | byte order setting accordingly. In native mode, the leading BOM |
| 2706 | mark is skipped, in all other modes, it is copied to the output |
| 2707 | stream as-is (giving a ZWNBSP character). */ |
| 2708 | if (bo == 0) { |
| 2709 | if (size >= 4) { |
| 2710 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2711 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2712 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2713 | if (bom == 0x0000FEFF) { |
| 2714 | q += 4; |
| 2715 | bo = -1; |
| 2716 | } |
| 2717 | else if (bom == 0xFFFE0000) { |
| 2718 | q += 4; |
| 2719 | bo = 1; |
| 2720 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2721 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2722 | if (bom == 0x0000FEFF) { |
| 2723 | q += 4; |
| 2724 | bo = 1; |
| 2725 | } |
| 2726 | else if (bom == 0xFFFE0000) { |
| 2727 | q += 4; |
| 2728 | bo = -1; |
| 2729 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2730 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2731 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | if (bo == -1) { |
| 2735 | /* force LE */ |
| 2736 | iorder[0] = 0; |
| 2737 | iorder[1] = 1; |
| 2738 | iorder[2] = 2; |
| 2739 | iorder[3] = 3; |
| 2740 | } |
| 2741 | else if (bo == 1) { |
| 2742 | /* force BE */ |
| 2743 | iorder[0] = 3; |
| 2744 | iorder[1] = 2; |
| 2745 | iorder[2] = 1; |
| 2746 | iorder[3] = 0; |
| 2747 | } |
| 2748 | |
| 2749 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2750 | Py_UCS4 ch; |
| 2751 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2752 | if (e-q<4) { |
| 2753 | if (consumed) |
| 2754 | break; |
| 2755 | errmsg = "truncated data"; |
| 2756 | startinpos = ((const char *)q)-starts; |
| 2757 | endinpos = ((const char *)e)-starts; |
| 2758 | goto utf32Error; |
| 2759 | /* The remaining input chars are ignored if the callback |
| 2760 | chooses to skip the input */ |
| 2761 | } |
| 2762 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2763 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2764 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2765 | if (ch >= 0x110000) |
| 2766 | { |
| 2767 | errmsg = "codepoint not in range(0x110000)"; |
| 2768 | startinpos = ((const char *)q)-starts; |
| 2769 | endinpos = startinpos+4; |
| 2770 | goto utf32Error; |
| 2771 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2772 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2773 | if (ch >= 0x10000) |
| 2774 | { |
| 2775 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2776 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2777 | } |
| 2778 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2779 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2780 | *p++ = ch; |
| 2781 | q += 4; |
| 2782 | continue; |
| 2783 | utf32Error: |
| 2784 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2785 | if (unicode_decode_call_errorhandler( |
| 2786 | errors, &errorHandler, |
| 2787 | "utf32", errmsg, |
| 2788 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2789 | &unicode, &outpos, &p)) |
| 2790 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | if (byteorder) |
| 2794 | *byteorder = bo; |
| 2795 | |
| 2796 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2797 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2798 | |
| 2799 | /* Adjust length */ |
| 2800 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2801 | goto onError; |
| 2802 | |
| 2803 | Py_XDECREF(errorHandler); |
| 2804 | Py_XDECREF(exc); |
| 2805 | return (PyObject *)unicode; |
| 2806 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2807 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2808 | Py_DECREF(unicode); |
| 2809 | Py_XDECREF(errorHandler); |
| 2810 | Py_XDECREF(exc); |
| 2811 | return NULL; |
| 2812 | } |
| 2813 | |
| 2814 | PyObject * |
| 2815 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2816 | Py_ssize_t size, |
| 2817 | const char *errors, |
| 2818 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2819 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2820 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2821 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2822 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2823 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2824 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2825 | #else |
| 2826 | const int pairs = 0; |
| 2827 | #endif |
| 2828 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2829 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2830 | int iorder[] = {0, 1, 2, 3}; |
| 2831 | #else |
| 2832 | int iorder[] = {3, 2, 1, 0}; |
| 2833 | #endif |
| 2834 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2835 | #define STORECHAR(CH) \ |
| 2836 | do { \ |
| 2837 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2838 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2839 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2840 | p[iorder[0]] = (CH) & 0xff; \ |
| 2841 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2842 | } while(0) |
| 2843 | |
| 2844 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2845 | so we need less space. */ |
| 2846 | #ifndef Py_UNICODE_WIDE |
| 2847 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2848 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2849 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2850 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2851 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2852 | nsize = (size - pairs + (byteorder == 0)); |
| 2853 | bytesize = nsize * 4; |
| 2854 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2855 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2856 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2857 | if (v == NULL) |
| 2858 | return NULL; |
| 2859 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2860 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2861 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2862 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2863 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2864 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2865 | |
| 2866 | if (byteorder == -1) { |
| 2867 | /* force LE */ |
| 2868 | iorder[0] = 0; |
| 2869 | iorder[1] = 1; |
| 2870 | iorder[2] = 2; |
| 2871 | iorder[3] = 3; |
| 2872 | } |
| 2873 | else if (byteorder == 1) { |
| 2874 | /* force BE */ |
| 2875 | iorder[0] = 3; |
| 2876 | iorder[1] = 2; |
| 2877 | iorder[2] = 1; |
| 2878 | iorder[3] = 0; |
| 2879 | } |
| 2880 | |
| 2881 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2882 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2883 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2884 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2885 | Py_UCS4 ch2 = *s; |
| 2886 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2887 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2888 | s++; |
| 2889 | size--; |
| 2890 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2891 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2892 | #endif |
| 2893 | STORECHAR(ch); |
| 2894 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2895 | |
| 2896 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2897 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2898 | #undef STORECHAR |
| 2899 | } |
| 2900 | |
| 2901 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2902 | { |
| 2903 | if (!PyUnicode_Check(unicode)) { |
| 2904 | PyErr_BadArgument(); |
| 2905 | return NULL; |
| 2906 | } |
| 2907 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2908 | PyUnicode_GET_SIZE(unicode), |
| 2909 | NULL, |
| 2910 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2913 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2914 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2915 | PyObject * |
| 2916 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2917 | Py_ssize_t size, |
| 2918 | const char *errors, |
| 2919 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2920 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2921 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2922 | } |
| 2923 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2924 | /* Two masks for fast checking of whether a C 'long' may contain |
| 2925 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 2926 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 2927 | rare in most input. |
| 2928 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 2929 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2930 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2931 | #if (SIZEOF_LONG == 8) |
| 2932 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 2933 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 2934 | #elif (SIZEOF_LONG == 4) |
| 2935 | # define FAST_CHAR_MASK 0x80008000L |
| 2936 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 2937 | #else |
| 2938 | # error C 'long' size should be either 4 or 8! |
| 2939 | #endif |
| 2940 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2941 | PyObject * |
| 2942 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2943 | Py_ssize_t size, |
| 2944 | const char *errors, |
| 2945 | int *byteorder, |
| 2946 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2947 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2948 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2949 | Py_ssize_t startinpos; |
| 2950 | Py_ssize_t endinpos; |
| 2951 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2952 | PyUnicodeObject *unicode; |
| 2953 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2954 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2955 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2956 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2957 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2958 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2959 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2960 | int ihi = 1, ilo = 0; |
| 2961 | #else |
| 2962 | int ihi = 0, ilo = 1; |
| 2963 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2964 | PyObject *errorHandler = NULL; |
| 2965 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2966 | |
| 2967 | /* Note: size will always be longer than the resulting Unicode |
| 2968 | character count */ |
| 2969 | unicode = _PyUnicode_New(size); |
| 2970 | if (!unicode) |
| 2971 | return NULL; |
| 2972 | if (size == 0) |
| 2973 | return (PyObject *)unicode; |
| 2974 | |
| 2975 | /* Unpack UTF-16 encoded data */ |
| 2976 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2977 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2978 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2979 | |
| 2980 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2981 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2982 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2983 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2984 | byte order setting accordingly. In native mode, the leading BOM |
| 2985 | mark is skipped, in all other modes, it is copied to the output |
| 2986 | stream as-is (giving a ZWNBSP character). */ |
| 2987 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2988 | if (size >= 2) { |
| 2989 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2990 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2991 | if (bom == 0xFEFF) { |
| 2992 | q += 2; |
| 2993 | bo = -1; |
| 2994 | } |
| 2995 | else if (bom == 0xFFFE) { |
| 2996 | q += 2; |
| 2997 | bo = 1; |
| 2998 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2999 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3000 | if (bom == 0xFEFF) { |
| 3001 | q += 2; |
| 3002 | bo = 1; |
| 3003 | } |
| 3004 | else if (bom == 0xFFFE) { |
| 3005 | q += 2; |
| 3006 | bo = -1; |
| 3007 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3008 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3009 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3010 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3011 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3012 | if (bo == -1) { |
| 3013 | /* force LE */ |
| 3014 | ihi = 1; |
| 3015 | ilo = 0; |
| 3016 | } |
| 3017 | else if (bo == 1) { |
| 3018 | /* force BE */ |
| 3019 | ihi = 0; |
| 3020 | ilo = 1; |
| 3021 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3022 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3023 | native_ordering = ilo < ihi; |
| 3024 | #else |
| 3025 | native_ordering = ilo > ihi; |
| 3026 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3027 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3028 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3029 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3030 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3031 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3032 | reads are more expensive, better to defer to another iteration. */ |
| 3033 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3034 | /* Fast path for runs of non-surrogate chars. */ |
| 3035 | register const unsigned char *_q = q; |
| 3036 | Py_UNICODE *_p = p; |
| 3037 | if (native_ordering) { |
| 3038 | /* Native ordering is simple: as long as the input cannot |
| 3039 | possibly contain a surrogate char, do an unrolled copy |
| 3040 | of several 16-bit code points to the target object. |
| 3041 | The non-surrogate check is done on several input bytes |
| 3042 | at a time (as many as a C 'long' can contain). */ |
| 3043 | while (_q < aligned_end) { |
| 3044 | unsigned long data = * (unsigned long *) _q; |
| 3045 | if (data & FAST_CHAR_MASK) |
| 3046 | break; |
| 3047 | _p[0] = ((unsigned short *) _q)[0]; |
| 3048 | _p[1] = ((unsigned short *) _q)[1]; |
| 3049 | #if (SIZEOF_LONG == 8) |
| 3050 | _p[2] = ((unsigned short *) _q)[2]; |
| 3051 | _p[3] = ((unsigned short *) _q)[3]; |
| 3052 | #endif |
| 3053 | _q += SIZEOF_LONG; |
| 3054 | _p += SIZEOF_LONG / 2; |
| 3055 | } |
| 3056 | } |
| 3057 | else { |
| 3058 | /* Byteswapped ordering is similar, but we must decompose |
| 3059 | the copy bytewise, and take care of zero'ing out the |
| 3060 | upper bytes if the target object is in 32-bit units |
| 3061 | (that is, in UCS-4 builds). */ |
| 3062 | while (_q < aligned_end) { |
| 3063 | unsigned long data = * (unsigned long *) _q; |
| 3064 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3065 | break; |
| 3066 | /* Zero upper bytes in UCS-4 builds */ |
| 3067 | #if (Py_UNICODE_SIZE > 2) |
| 3068 | _p[0] = 0; |
| 3069 | _p[1] = 0; |
| 3070 | #if (SIZEOF_LONG == 8) |
| 3071 | _p[2] = 0; |
| 3072 | _p[3] = 0; |
| 3073 | #endif |
| 3074 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3075 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3076 | fill the two last bytes of each 4-byte unit. */ |
| 3077 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3078 | # define OFF 2 |
| 3079 | #else |
| 3080 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3081 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3082 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3083 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3084 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3085 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3086 | #if (SIZEOF_LONG == 8) |
| 3087 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3088 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3089 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3090 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3091 | #endif |
| 3092 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3093 | _q += SIZEOF_LONG; |
| 3094 | _p += SIZEOF_LONG / 2; |
| 3095 | } |
| 3096 | } |
| 3097 | p = _p; |
| 3098 | q = _q; |
| 3099 | if (q >= e) |
| 3100 | break; |
| 3101 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3102 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3103 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3104 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3105 | |
| 3106 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3107 | *p++ = ch; |
| 3108 | continue; |
| 3109 | } |
| 3110 | |
| 3111 | /* UTF-16 code pair: */ |
| 3112 | if (q > e) { |
| 3113 | errmsg = "unexpected end of data"; |
| 3114 | startinpos = (((const char *)q) - 2) - starts; |
| 3115 | endinpos = ((const char *)e) + 1 - starts; |
| 3116 | goto utf16Error; |
| 3117 | } |
| 3118 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3119 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3120 | q += 2; |
| 3121 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3122 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3123 | *p++ = ch; |
| 3124 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3125 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3126 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3127 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3128 | continue; |
| 3129 | } |
| 3130 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3131 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3132 | startinpos = (((const char *)q)-4)-starts; |
| 3133 | endinpos = startinpos+2; |
| 3134 | goto utf16Error; |
| 3135 | } |
| 3136 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3137 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3138 | errmsg = "illegal encoding"; |
| 3139 | startinpos = (((const char *)q)-2)-starts; |
| 3140 | endinpos = startinpos+2; |
| 3141 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3142 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3143 | utf16Error: |
| 3144 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3145 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3146 | errors, |
| 3147 | &errorHandler, |
| 3148 | "utf16", errmsg, |
| 3149 | &starts, |
| 3150 | (const char **)&e, |
| 3151 | &startinpos, |
| 3152 | &endinpos, |
| 3153 | &exc, |
| 3154 | (const char **)&q, |
| 3155 | &unicode, |
| 3156 | &outpos, |
| 3157 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3158 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3159 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3160 | /* remaining byte at the end? (size should be even) */ |
| 3161 | if (e == q) { |
| 3162 | if (!consumed) { |
| 3163 | errmsg = "truncated data"; |
| 3164 | startinpos = ((const char *)q) - starts; |
| 3165 | endinpos = ((const char *)e) + 1 - starts; |
| 3166 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3167 | if (unicode_decode_call_errorhandler( |
| 3168 | errors, |
| 3169 | &errorHandler, |
| 3170 | "utf16", errmsg, |
| 3171 | &starts, |
| 3172 | (const char **)&e, |
| 3173 | &startinpos, |
| 3174 | &endinpos, |
| 3175 | &exc, |
| 3176 | (const char **)&q, |
| 3177 | &unicode, |
| 3178 | &outpos, |
| 3179 | &p)) |
| 3180 | goto onError; |
| 3181 | /* The remaining input chars are ignored if the callback |
| 3182 | chooses to skip the input */ |
| 3183 | } |
| 3184 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3185 | |
| 3186 | if (byteorder) |
| 3187 | *byteorder = bo; |
| 3188 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3189 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3190 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3191 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3192 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3193 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3194 | goto onError; |
| 3195 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3196 | Py_XDECREF(errorHandler); |
| 3197 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3198 | return (PyObject *)unicode; |
| 3199 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3200 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3201 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3202 | Py_XDECREF(errorHandler); |
| 3203 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3204 | return NULL; |
| 3205 | } |
| 3206 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3207 | #undef FAST_CHAR_MASK |
| 3208 | #undef SWAPPED_FAST_CHAR_MASK |
| 3209 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3210 | PyObject * |
| 3211 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3212 | Py_ssize_t size, |
| 3213 | const char *errors, |
| 3214 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3215 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3216 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3217 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3218 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3219 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3220 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3221 | #else |
| 3222 | const int pairs = 0; |
| 3223 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3224 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3225 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3226 | int ihi = 1, ilo = 0; |
| 3227 | #else |
| 3228 | int ihi = 0, ilo = 1; |
| 3229 | #endif |
| 3230 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3231 | #define STORECHAR(CH) \ |
| 3232 | do { \ |
| 3233 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3234 | p[ilo] = (CH) & 0xff; \ |
| 3235 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3236 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3237 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3238 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3239 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3240 | if (s[i] >= 0x10000) |
| 3241 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3242 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3243 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3244 | if (size > PY_SSIZE_T_MAX || |
| 3245 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3246 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3247 | nsize = size + pairs + (byteorder == 0); |
| 3248 | bytesize = nsize * 2; |
| 3249 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3250 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3251 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3252 | if (v == NULL) |
| 3253 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3254 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3255 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3256 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3257 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3258 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3259 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3260 | |
| 3261 | if (byteorder == -1) { |
| 3262 | /* force LE */ |
| 3263 | ihi = 1; |
| 3264 | ilo = 0; |
| 3265 | } |
| 3266 | else if (byteorder == 1) { |
| 3267 | /* force BE */ |
| 3268 | ihi = 0; |
| 3269 | ilo = 1; |
| 3270 | } |
| 3271 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3272 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3273 | Py_UNICODE ch = *s++; |
| 3274 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3275 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3276 | if (ch >= 0x10000) { |
| 3277 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3278 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3279 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3280 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3281 | STORECHAR(ch); |
| 3282 | if (ch2) |
| 3283 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3284 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3285 | |
| 3286 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3287 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3288 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3292 | { |
| 3293 | if (!PyUnicode_Check(unicode)) { |
| 3294 | PyErr_BadArgument(); |
| 3295 | return NULL; |
| 3296 | } |
| 3297 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3298 | PyUnicode_GET_SIZE(unicode), |
| 3299 | NULL, |
| 3300 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3304 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3305 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3306 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3307 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3308 | Py_ssize_t size, |
| 3309 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3310 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3311 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3312 | Py_ssize_t startinpos; |
| 3313 | Py_ssize_t endinpos; |
| 3314 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3315 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3316 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3317 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3318 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3319 | char* message; |
| 3320 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3321 | PyObject *errorHandler = NULL; |
| 3322 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3323 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3324 | /* Escaped strings will always be longer than the resulting |
| 3325 | 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] | 3326 | length after conversion to the true value. |
| 3327 | (but if the error callback returns a long replacement string |
| 3328 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3329 | v = _PyUnicode_New(size); |
| 3330 | if (v == NULL) |
| 3331 | goto onError; |
| 3332 | if (size == 0) |
| 3333 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3334 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3335 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3336 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3338 | while (s < end) { |
| 3339 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3340 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3341 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3342 | |
| 3343 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3344 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3345 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3346 | continue; |
| 3347 | } |
| 3348 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3349 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3350 | /* \ - Escapes */ |
| 3351 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3352 | c = *s++; |
| 3353 | if (s > end) |
| 3354 | c = '\0'; /* Invalid after \ */ |
| 3355 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3356 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3357 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3358 | case '\n': break; |
| 3359 | case '\\': *p++ = '\\'; break; |
| 3360 | case '\'': *p++ = '\''; break; |
| 3361 | case '\"': *p++ = '\"'; break; |
| 3362 | case 'b': *p++ = '\b'; break; |
| 3363 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3364 | case 't': *p++ = '\t'; break; |
| 3365 | case 'n': *p++ = '\n'; break; |
| 3366 | case 'r': *p++ = '\r'; break; |
| 3367 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3368 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3369 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3370 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3371 | case '0': case '1': case '2': case '3': |
| 3372 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3373 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3374 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3375 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3376 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3377 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3379 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3380 | break; |
| 3381 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3382 | /* hex escapes */ |
| 3383 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3384 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3385 | digits = 2; |
| 3386 | message = "truncated \\xXX escape"; |
| 3387 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3388 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3389 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3390 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3391 | digits = 4; |
| 3392 | message = "truncated \\uXXXX escape"; |
| 3393 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3394 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3395 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3396 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3397 | digits = 8; |
| 3398 | message = "truncated \\UXXXXXXXX escape"; |
| 3399 | hexescape: |
| 3400 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3401 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3402 | if (s+digits>end) { |
| 3403 | endinpos = size; |
| 3404 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3405 | errors, &errorHandler, |
| 3406 | "unicodeescape", "end of string in escape sequence", |
| 3407 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3408 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3409 | goto onError; |
| 3410 | goto nextByte; |
| 3411 | } |
| 3412 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3413 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3414 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3415 | endinpos = (s+i+1)-starts; |
| 3416 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3417 | errors, &errorHandler, |
| 3418 | "unicodeescape", message, |
| 3419 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3420 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3421 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3422 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3423 | } |
| 3424 | chr = (chr<<4) & ~0xF; |
| 3425 | if (c >= '0' && c <= '9') |
| 3426 | chr += c - '0'; |
| 3427 | else if (c >= 'a' && c <= 'f') |
| 3428 | chr += 10 + c - 'a'; |
| 3429 | else |
| 3430 | chr += 10 + c - 'A'; |
| 3431 | } |
| 3432 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3433 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3434 | /* _decoding_error will have already written into the |
| 3435 | target buffer. */ |
| 3436 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3437 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3438 | /* when we get here, chr is a 32-bit unicode character */ |
| 3439 | if (chr <= 0xffff) |
| 3440 | /* UCS-2 character */ |
| 3441 | *p++ = (Py_UNICODE) chr; |
| 3442 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3443 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3444 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3445 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3446 | *p++ = chr; |
| 3447 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3448 | chr -= 0x10000L; |
| 3449 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3450 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3451 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3452 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3453 | endinpos = s-starts; |
| 3454 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3455 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3456 | errors, &errorHandler, |
| 3457 | "unicodeescape", "illegal Unicode character", |
| 3458 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3459 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3460 | goto onError; |
| 3461 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3462 | break; |
| 3463 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3464 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3465 | case 'N': |
| 3466 | message = "malformed \\N character escape"; |
| 3467 | if (ucnhash_CAPI == NULL) { |
| 3468 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3469 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3470 | if (ucnhash_CAPI == NULL) |
| 3471 | goto ucnhashError; |
| 3472 | } |
| 3473 | if (*s == '{') { |
| 3474 | const char *start = s+1; |
| 3475 | /* look for the closing brace */ |
| 3476 | while (*s != '}' && s < end) |
| 3477 | s++; |
| 3478 | if (s > start && s < end && *s == '}') { |
| 3479 | /* found a name. look it up in the unicode database */ |
| 3480 | message = "unknown Unicode character name"; |
| 3481 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3482 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3483 | goto store; |
| 3484 | } |
| 3485 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3486 | endinpos = s-starts; |
| 3487 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3488 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3489 | errors, &errorHandler, |
| 3490 | "unicodeescape", message, |
| 3491 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3492 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3493 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3494 | break; |
| 3495 | |
| 3496 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3497 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3498 | message = "\\ at end of string"; |
| 3499 | s--; |
| 3500 | endinpos = s-starts; |
| 3501 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3502 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3503 | errors, &errorHandler, |
| 3504 | "unicodeescape", message, |
| 3505 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3506 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3507 | goto onError; |
| 3508 | } |
| 3509 | else { |
| 3510 | *p++ = '\\'; |
| 3511 | *p++ = (unsigned char)s[-1]; |
| 3512 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3513 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3514 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3515 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3516 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3517 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3518 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3519 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3520 | Py_XDECREF(errorHandler); |
| 3521 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3522 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3523 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3524 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3525 | PyErr_SetString( |
| 3526 | PyExc_UnicodeError, |
| 3527 | "\\N escapes not supported (can't load unicodedata module)" |
| 3528 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3529 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3530 | Py_XDECREF(errorHandler); |
| 3531 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3532 | return NULL; |
| 3533 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3534 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3535 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3536 | Py_XDECREF(errorHandler); |
| 3537 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3538 | return NULL; |
| 3539 | } |
| 3540 | |
| 3541 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3542 | |
| 3543 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3544 | appropriate. |
| 3545 | |
| 3546 | */ |
| 3547 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3548 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3549 | Py_ssize_t size, |
| 3550 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3551 | { |
| 3552 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3553 | |
| 3554 | while (size-- > 0) { |
| 3555 | if (*s == ch) |
| 3556 | return s; |
| 3557 | s++; |
| 3558 | } |
| 3559 | |
| 3560 | return NULL; |
| 3561 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3562 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3563 | static const char *hexdigits = "0123456789abcdef"; |
| 3564 | |
| 3565 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3566 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3567 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3568 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3569 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3570 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3571 | #ifdef Py_UNICODE_WIDE |
| 3572 | const Py_ssize_t expandsize = 10; |
| 3573 | #else |
| 3574 | const Py_ssize_t expandsize = 6; |
| 3575 | #endif |
| 3576 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3577 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3578 | better to choose a different scheme. Perhaps scan the |
| 3579 | first N-chars of the string and allocate based on that size. |
| 3580 | */ |
| 3581 | /* Initial allocation is based on the longest-possible unichr |
| 3582 | escape. |
| 3583 | |
| 3584 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3585 | unichr, so in this case it's the longest unichr escape. In |
| 3586 | narrow (UTF-16) builds this is five chars per source unichr |
| 3587 | since there are two unichrs in the surrogate pair, so in narrow |
| 3588 | (UTF-16) builds it's not the longest unichr escape. |
| 3589 | |
| 3590 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3591 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3592 | escape. |
| 3593 | */ |
| 3594 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3595 | if (size == 0) |
| 3596 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3597 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3598 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3599 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3600 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3601 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3602 | 2 |
| 3603 | + expandsize*size |
| 3604 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3605 | if (repr == NULL) |
| 3606 | return NULL; |
| 3607 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3608 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3609 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3610 | while (size-- > 0) { |
| 3611 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3612 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3613 | /* Escape backslashes */ |
| 3614 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3615 | *p++ = '\\'; |
| 3616 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3617 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3618 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3619 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3620 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3621 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3622 | else if (ch >= 0x10000) { |
| 3623 | *p++ = '\\'; |
| 3624 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3625 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3626 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3627 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3628 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3629 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3630 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3631 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3632 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3633 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3634 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3635 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3636 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3637 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3638 | Py_UNICODE ch2; |
| 3639 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3640 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3641 | ch2 = *s++; |
| 3642 | size--; |
| 3643 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3644 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3645 | *p++ = '\\'; |
| 3646 | *p++ = 'U'; |
| 3647 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3648 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3649 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3650 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3651 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3652 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3653 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3654 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3655 | continue; |
| 3656 | } |
| 3657 | /* Fall through: isolated surrogates are copied as-is */ |
| 3658 | s--; |
| 3659 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3660 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3661 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3662 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3663 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3664 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3665 | *p++ = '\\'; |
| 3666 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3667 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3668 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3669 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3670 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3671 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3672 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3673 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3674 | else if (ch == '\t') { |
| 3675 | *p++ = '\\'; |
| 3676 | *p++ = 't'; |
| 3677 | } |
| 3678 | else if (ch == '\n') { |
| 3679 | *p++ = '\\'; |
| 3680 | *p++ = 'n'; |
| 3681 | } |
| 3682 | else if (ch == '\r') { |
| 3683 | *p++ = '\\'; |
| 3684 | *p++ = 'r'; |
| 3685 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3686 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3687 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3688 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3689 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3690 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3691 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3692 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3693 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3694 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | /* Copy everything else as-is */ |
| 3696 | else |
| 3697 | *p++ = (char) ch; |
| 3698 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3699 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3700 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3701 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3702 | return NULL; |
| 3703 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3706 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3707 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3708 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3709 | if (!PyUnicode_Check(unicode)) { |
| 3710 | PyErr_BadArgument(); |
| 3711 | return NULL; |
| 3712 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3713 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3714 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3715 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3719 | |
| 3720 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3721 | Py_ssize_t size, |
| 3722 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3723 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3724 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3725 | Py_ssize_t startinpos; |
| 3726 | Py_ssize_t endinpos; |
| 3727 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3728 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3729 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3730 | const char *end; |
| 3731 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3732 | PyObject *errorHandler = NULL; |
| 3733 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3735 | /* Escaped strings will always be longer than the resulting |
| 3736 | 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] | 3737 | length after conversion to the true value. (But decoding error |
| 3738 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3739 | v = _PyUnicode_New(size); |
| 3740 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3741 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3742 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3743 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3744 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3745 | end = s + size; |
| 3746 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3747 | unsigned char c; |
| 3748 | Py_UCS4 x; |
| 3749 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3750 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3751 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3752 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3753 | if (*s != '\\') { |
| 3754 | *p++ = (unsigned char)*s++; |
| 3755 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3756 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3757 | startinpos = s-starts; |
| 3758 | |
| 3759 | /* \u-escapes are only interpreted iff the number of leading |
| 3760 | backslashes if odd */ |
| 3761 | bs = s; |
| 3762 | for (;s < end;) { |
| 3763 | if (*s != '\\') |
| 3764 | break; |
| 3765 | *p++ = (unsigned char)*s++; |
| 3766 | } |
| 3767 | if (((s - bs) & 1) == 0 || |
| 3768 | s >= end || |
| 3769 | (*s != 'u' && *s != 'U')) { |
| 3770 | continue; |
| 3771 | } |
| 3772 | p--; |
| 3773 | count = *s=='u' ? 4 : 8; |
| 3774 | s++; |
| 3775 | |
| 3776 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3777 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3778 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3779 | c = (unsigned char)*s; |
| 3780 | if (!ISXDIGIT(c)) { |
| 3781 | endinpos = s-starts; |
| 3782 | if (unicode_decode_call_errorhandler( |
| 3783 | errors, &errorHandler, |
| 3784 | "rawunicodeescape", "truncated \\uXXXX", |
| 3785 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3786 | &v, &outpos, &p)) |
| 3787 | goto onError; |
| 3788 | goto nextByte; |
| 3789 | } |
| 3790 | x = (x<<4) & ~0xF; |
| 3791 | if (c >= '0' && c <= '9') |
| 3792 | x += c - '0'; |
| 3793 | else if (c >= 'a' && c <= 'f') |
| 3794 | x += 10 + c - 'a'; |
| 3795 | else |
| 3796 | x += 10 + c - 'A'; |
| 3797 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3798 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3799 | /* UCS-2 character */ |
| 3800 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3801 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3802 | /* UCS-4 character. Either store directly, or as |
| 3803 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3804 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3805 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3806 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3807 | x -= 0x10000L; |
| 3808 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3809 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3810 | #endif |
| 3811 | } else { |
| 3812 | endinpos = s-starts; |
| 3813 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3814 | if (unicode_decode_call_errorhandler( |
| 3815 | errors, &errorHandler, |
| 3816 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3817 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3818 | &v, &outpos, &p)) |
| 3819 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3820 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3821 | nextByte: |
| 3822 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3823 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3824 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3825 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3826 | Py_XDECREF(errorHandler); |
| 3827 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3828 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3829 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3830 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3831 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3832 | Py_XDECREF(errorHandler); |
| 3833 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3834 | return NULL; |
| 3835 | } |
| 3836 | |
| 3837 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3838 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3839 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3840 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3841 | char *p; |
| 3842 | char *q; |
| 3843 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3844 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3845 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3846 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3847 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3848 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3849 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3850 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3851 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3852 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3853 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3854 | if (repr == NULL) |
| 3855 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3856 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3857 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3858 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3859 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3860 | while (size-- > 0) { |
| 3861 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3862 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3863 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3864 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3865 | *p++ = '\\'; |
| 3866 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3867 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3868 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3869 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3870 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3871 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3872 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3873 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3874 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3875 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3876 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3877 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3878 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3879 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3880 | Py_UNICODE ch2; |
| 3881 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3882 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3883 | ch2 = *s++; |
| 3884 | size--; |
| 3885 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3886 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3887 | *p++ = '\\'; |
| 3888 | *p++ = 'U'; |
| 3889 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3890 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3891 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3892 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3893 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3894 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3895 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3896 | *p++ = hexdigits[ucs & 0xf]; |
| 3897 | continue; |
| 3898 | } |
| 3899 | /* Fall through: isolated surrogates are copied as-is */ |
| 3900 | s--; |
| 3901 | size++; |
| 3902 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3903 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3904 | /* Map 16-bit characters to '\uxxxx' */ |
| 3905 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3906 | *p++ = '\\'; |
| 3907 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3908 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3909 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3910 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3911 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3912 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3913 | /* Copy everything else as-is */ |
| 3914 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3915 | *p++ = (char) ch; |
| 3916 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3917 | size = p - q; |
| 3918 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3919 | assert(size > 0); |
| 3920 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3921 | return NULL; |
| 3922 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
| 3925 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3926 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3927 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3928 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3929 | PyErr_BadArgument(); |
| 3930 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3931 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3932 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3933 | PyUnicode_GET_SIZE(unicode)); |
| 3934 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3935 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3938 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3939 | |
| 3940 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3941 | Py_ssize_t size, |
| 3942 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3943 | { |
| 3944 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3945 | Py_ssize_t startinpos; |
| 3946 | Py_ssize_t endinpos; |
| 3947 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3948 | PyUnicodeObject *v; |
| 3949 | Py_UNICODE *p; |
| 3950 | const char *end; |
| 3951 | const char *reason; |
| 3952 | PyObject *errorHandler = NULL; |
| 3953 | PyObject *exc = NULL; |
| 3954 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3955 | #ifdef Py_UNICODE_WIDE |
| 3956 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3957 | #endif |
| 3958 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3959 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3960 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3961 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3962 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3963 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3964 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3965 | p = PyUnicode_AS_UNICODE(v); |
| 3966 | end = s + size; |
| 3967 | |
| 3968 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3969 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3970 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3971 | some malformed UCS-4 data. */ |
| 3972 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3973 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3974 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3975 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3976 | end-s < Py_UNICODE_SIZE |
| 3977 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3978 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3979 | startinpos = s - starts; |
| 3980 | if (end-s < Py_UNICODE_SIZE) { |
| 3981 | endinpos = end-starts; |
| 3982 | reason = "truncated input"; |
| 3983 | } |
| 3984 | else { |
| 3985 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3986 | reason = "illegal code point (> 0x10FFFF)"; |
| 3987 | } |
| 3988 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3989 | if (unicode_decode_call_errorhandler( |
| 3990 | errors, &errorHandler, |
| 3991 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3992 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3993 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3994 | goto onError; |
| 3995 | } |
| 3996 | } |
| 3997 | else { |
| 3998 | p++; |
| 3999 | s += Py_UNICODE_SIZE; |
| 4000 | } |
| 4001 | } |
| 4002 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4003 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4004 | goto onError; |
| 4005 | Py_XDECREF(errorHandler); |
| 4006 | Py_XDECREF(exc); |
| 4007 | return (PyObject *)v; |
| 4008 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4009 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4010 | Py_XDECREF(v); |
| 4011 | Py_XDECREF(errorHandler); |
| 4012 | Py_XDECREF(exc); |
| 4013 | return NULL; |
| 4014 | } |
| 4015 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4016 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4017 | |
| 4018 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4019 | Py_ssize_t size, |
| 4020 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4021 | { |
| 4022 | PyUnicodeObject *v; |
| 4023 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4024 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4026 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4027 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4028 | Py_UNICODE r = *(unsigned char*)s; |
| 4029 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4032 | v = _PyUnicode_New(size); |
| 4033 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4034 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4035 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4036 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4037 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4038 | e = s + size; |
| 4039 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4040 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4041 | unrolled_end = e - 4; |
| 4042 | while (s < unrolled_end) { |
| 4043 | p[0] = (unsigned char) s[0]; |
| 4044 | p[1] = (unsigned char) s[1]; |
| 4045 | p[2] = (unsigned char) s[2]; |
| 4046 | p[3] = (unsigned char) s[3]; |
| 4047 | s += 4; |
| 4048 | p += 4; |
| 4049 | } |
| 4050 | while (s < e) |
| 4051 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4052 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4053 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4054 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4055 | Py_XDECREF(v); |
| 4056 | return NULL; |
| 4057 | } |
| 4058 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4059 | /* create or adjust a UnicodeEncodeError */ |
| 4060 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4061 | const char *encoding, |
| 4062 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4063 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4064 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4065 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4066 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4067 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4068 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | } |
| 4070 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4071 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4072 | goto onError; |
| 4073 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4074 | goto onError; |
| 4075 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4076 | goto onError; |
| 4077 | return; |
| 4078 | onError: |
| 4079 | Py_DECREF(*exceptionObject); |
| 4080 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4081 | } |
| 4082 | } |
| 4083 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4084 | /* raises a UnicodeEncodeError */ |
| 4085 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4086 | const char *encoding, |
| 4087 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4088 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4089 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4090 | { |
| 4091 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4092 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4093 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4094 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | /* error handling callback helper: |
| 4098 | build arguments, call the callback and check the arguments, |
| 4099 | put the result into newpos and return the replacement string, which |
| 4100 | has to be freed by the caller */ |
| 4101 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4102 | PyObject **errorHandler, |
| 4103 | const char *encoding, const char *reason, |
| 4104 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4105 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4106 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4107 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4108 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4109 | |
| 4110 | PyObject *restuple; |
| 4111 | PyObject *resunicode; |
| 4112 | |
| 4113 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4114 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4115 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4116 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4120 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4121 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4122 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4123 | |
| 4124 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4125 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4126 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4127 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4128 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4129 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4130 | Py_DECREF(restuple); |
| 4131 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4132 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4133 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4134 | &resunicode, newpos)) { |
| 4135 | Py_DECREF(restuple); |
| 4136 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4137 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4138 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4139 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4140 | Py_DECREF(restuple); |
| 4141 | return NULL; |
| 4142 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4143 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4144 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4145 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4146 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4147 | Py_DECREF(restuple); |
| 4148 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4149 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4150 | Py_INCREF(resunicode); |
| 4151 | Py_DECREF(restuple); |
| 4152 | return resunicode; |
| 4153 | } |
| 4154 | |
| 4155 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4156 | Py_ssize_t size, |
| 4157 | const char *errors, |
| 4158 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4159 | { |
| 4160 | /* output object */ |
| 4161 | PyObject *res; |
| 4162 | /* pointers to the beginning and end+1 of input */ |
| 4163 | const Py_UNICODE *startp = p; |
| 4164 | const Py_UNICODE *endp = p + size; |
| 4165 | /* pointer to the beginning of the unencodable characters */ |
| 4166 | /* const Py_UNICODE *badp = NULL; */ |
| 4167 | /* pointer into the output */ |
| 4168 | char *str; |
| 4169 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4170 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4171 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4172 | 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] | 4173 | PyObject *errorHandler = NULL; |
| 4174 | PyObject *exc = NULL; |
| 4175 | /* the following variable is used for caching string comparisons |
| 4176 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4177 | int known_errorHandler = -1; |
| 4178 | |
| 4179 | /* allocate enough for a simple encoding without |
| 4180 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4181 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4182 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4183 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4184 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4185 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4186 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4187 | ressize = size; |
| 4188 | |
| 4189 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4190 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4191 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4192 | /* can we encode this? */ |
| 4193 | if (c<limit) { |
| 4194 | /* no overflow check, because we know that the space is enough */ |
| 4195 | *str++ = (char)c; |
| 4196 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4197 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4198 | else { |
| 4199 | Py_ssize_t unicodepos = p-startp; |
| 4200 | Py_ssize_t requiredsize; |
| 4201 | PyObject *repunicode; |
| 4202 | Py_ssize_t repsize; |
| 4203 | Py_ssize_t newpos; |
| 4204 | Py_ssize_t respos; |
| 4205 | Py_UNICODE *uni2; |
| 4206 | /* startpos for collecting unencodable chars */ |
| 4207 | const Py_UNICODE *collstart = p; |
| 4208 | const Py_UNICODE *collend = p; |
| 4209 | /* find all unecodable characters */ |
| 4210 | while ((collend < endp) && ((*collend)>=limit)) |
| 4211 | ++collend; |
| 4212 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4213 | if (known_errorHandler==-1) { |
| 4214 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4215 | known_errorHandler = 1; |
| 4216 | else if (!strcmp(errors, "replace")) |
| 4217 | known_errorHandler = 2; |
| 4218 | else if (!strcmp(errors, "ignore")) |
| 4219 | known_errorHandler = 3; |
| 4220 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4221 | known_errorHandler = 4; |
| 4222 | else |
| 4223 | known_errorHandler = 0; |
| 4224 | } |
| 4225 | switch (known_errorHandler) { |
| 4226 | case 1: /* strict */ |
| 4227 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4228 | goto onError; |
| 4229 | case 2: /* replace */ |
| 4230 | while (collstart++<collend) |
| 4231 | *str++ = '?'; /* fall through */ |
| 4232 | case 3: /* ignore */ |
| 4233 | p = collend; |
| 4234 | break; |
| 4235 | case 4: /* xmlcharrefreplace */ |
| 4236 | respos = str - PyBytes_AS_STRING(res); |
| 4237 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4238 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4239 | if (*p<10) |
| 4240 | repsize += 2+1+1; |
| 4241 | else if (*p<100) |
| 4242 | repsize += 2+2+1; |
| 4243 | else if (*p<1000) |
| 4244 | repsize += 2+3+1; |
| 4245 | else if (*p<10000) |
| 4246 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4247 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4248 | else |
| 4249 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4250 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4251 | else if (*p<100000) |
| 4252 | repsize += 2+5+1; |
| 4253 | else if (*p<1000000) |
| 4254 | repsize += 2+6+1; |
| 4255 | else |
| 4256 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4257 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4258 | } |
| 4259 | requiredsize = respos+repsize+(endp-collend); |
| 4260 | if (requiredsize > ressize) { |
| 4261 | if (requiredsize<2*ressize) |
| 4262 | requiredsize = 2*ressize; |
| 4263 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4264 | goto onError; |
| 4265 | str = PyBytes_AS_STRING(res) + respos; |
| 4266 | ressize = requiredsize; |
| 4267 | } |
| 4268 | /* generate replacement (temporarily (mis)uses p) */ |
| 4269 | for (p = collstart; p < collend; ++p) { |
| 4270 | str += sprintf(str, "&#%d;", (int)*p); |
| 4271 | } |
| 4272 | p = collend; |
| 4273 | break; |
| 4274 | default: |
| 4275 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4276 | encoding, reason, startp, size, &exc, |
| 4277 | collstart-startp, collend-startp, &newpos); |
| 4278 | if (repunicode == NULL) |
| 4279 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4280 | if (PyBytes_Check(repunicode)) { |
| 4281 | /* Directly copy bytes result to output. */ |
| 4282 | repsize = PyBytes_Size(repunicode); |
| 4283 | if (repsize > 1) { |
| 4284 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4285 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4286 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4287 | Py_DECREF(repunicode); |
| 4288 | goto onError; |
| 4289 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4290 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4291 | ressize += repsize-1; |
| 4292 | } |
| 4293 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4294 | str += repsize; |
| 4295 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4296 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4297 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4298 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4299 | /* need more space? (at least enough for what we |
| 4300 | have+the replacement+the rest of the string, so |
| 4301 | we won't have to check space for encodable characters) */ |
| 4302 | respos = str - PyBytes_AS_STRING(res); |
| 4303 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4304 | requiredsize = respos+repsize+(endp-collend); |
| 4305 | if (requiredsize > ressize) { |
| 4306 | if (requiredsize<2*ressize) |
| 4307 | requiredsize = 2*ressize; |
| 4308 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4309 | Py_DECREF(repunicode); |
| 4310 | goto onError; |
| 4311 | } |
| 4312 | str = PyBytes_AS_STRING(res) + respos; |
| 4313 | ressize = requiredsize; |
| 4314 | } |
| 4315 | /* check if there is anything unencodable in the replacement |
| 4316 | and copy it to the output */ |
| 4317 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4318 | c = *uni2; |
| 4319 | if (c >= limit) { |
| 4320 | raise_encode_exception(&exc, encoding, startp, size, |
| 4321 | unicodepos, unicodepos+1, reason); |
| 4322 | Py_DECREF(repunicode); |
| 4323 | goto onError; |
| 4324 | } |
| 4325 | *str = (char)c; |
| 4326 | } |
| 4327 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4328 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4329 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4330 | } |
| 4331 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4332 | /* Resize if we allocated to much */ |
| 4333 | size = str - PyBytes_AS_STRING(res); |
| 4334 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4335 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4336 | if (_PyBytes_Resize(&res, size) < 0) |
| 4337 | goto onError; |
| 4338 | } |
| 4339 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4340 | Py_XDECREF(errorHandler); |
| 4341 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4342 | return res; |
| 4343 | |
| 4344 | onError: |
| 4345 | Py_XDECREF(res); |
| 4346 | Py_XDECREF(errorHandler); |
| 4347 | Py_XDECREF(exc); |
| 4348 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4351 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4352 | Py_ssize_t size, |
| 4353 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4354 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4355 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4359 | { |
| 4360 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4361 | PyErr_BadArgument(); |
| 4362 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4363 | } |
| 4364 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4365 | PyUnicode_GET_SIZE(unicode), |
| 4366 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4370 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4371 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4372 | Py_ssize_t size, |
| 4373 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4374 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4375 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4376 | PyUnicodeObject *v; |
| 4377 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4378 | Py_ssize_t startinpos; |
| 4379 | Py_ssize_t endinpos; |
| 4380 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4381 | const char *e; |
| 4382 | PyObject *errorHandler = NULL; |
| 4383 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4384 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4385 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4386 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4387 | Py_UNICODE r = *(unsigned char*)s; |
| 4388 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4389 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4390 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4391 | v = _PyUnicode_New(size); |
| 4392 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4393 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4394 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4395 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4396 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4397 | e = s + size; |
| 4398 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4399 | register unsigned char c = (unsigned char)*s; |
| 4400 | if (c < 128) { |
| 4401 | *p++ = c; |
| 4402 | ++s; |
| 4403 | } |
| 4404 | else { |
| 4405 | startinpos = s-starts; |
| 4406 | endinpos = startinpos + 1; |
| 4407 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4408 | if (unicode_decode_call_errorhandler( |
| 4409 | errors, &errorHandler, |
| 4410 | "ascii", "ordinal not in range(128)", |
| 4411 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4412 | &v, &outpos, &p)) |
| 4413 | goto onError; |
| 4414 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4415 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4416 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4417 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4418 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4419 | Py_XDECREF(errorHandler); |
| 4420 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4422 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4423 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4424 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4425 | Py_XDECREF(errorHandler); |
| 4426 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4427 | return NULL; |
| 4428 | } |
| 4429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4430 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4431 | Py_ssize_t size, |
| 4432 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4433 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4434 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4438 | { |
| 4439 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4440 | PyErr_BadArgument(); |
| 4441 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4442 | } |
| 4443 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4444 | PyUnicode_GET_SIZE(unicode), |
| 4445 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4448 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4449 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4450 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4451 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4452 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4453 | #define NEED_RETRY |
| 4454 | #endif |
| 4455 | |
| 4456 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4457 | a) it assumes an incomplete character consists of a single byte, and |
| 4458 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4459 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4460 | |
| 4461 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4462 | { |
| 4463 | const char *curr = s + offset; |
| 4464 | |
| 4465 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4466 | const char *prev = CharPrev(s, curr); |
| 4467 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4468 | } |
| 4469 | return 0; |
| 4470 | } |
| 4471 | |
| 4472 | /* |
| 4473 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4474 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4475 | */ |
| 4476 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4477 | const char *s, /* MBCS string */ |
| 4478 | int size, /* sizeof MBCS string */ |
| 4479 | int final) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4480 | { |
| 4481 | Py_UNICODE *p; |
| 4482 | Py_ssize_t n = 0; |
| 4483 | int usize = 0; |
| 4484 | |
| 4485 | assert(size >= 0); |
| 4486 | |
| 4487 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4488 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4489 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4490 | |
| 4491 | /* First get the size of the result */ |
| 4492 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4493 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4494 | if (usize == 0) { |
| 4495 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4496 | return -1; |
| 4497 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4501 | /* Create unicode object */ |
| 4502 | *v = _PyUnicode_New(usize); |
| 4503 | if (*v == NULL) |
| 4504 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4505 | } |
| 4506 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4507 | /* Extend unicode object */ |
| 4508 | n = PyUnicode_GET_SIZE(*v); |
| 4509 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4510 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | /* Do the conversion */ |
| 4514 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4515 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4516 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4517 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4518 | return -1; |
| 4519 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | return size; |
| 4523 | } |
| 4524 | |
| 4525 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4526 | Py_ssize_t size, |
| 4527 | const char *errors, |
| 4528 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4529 | { |
| 4530 | PyUnicodeObject *v = NULL; |
| 4531 | int done; |
| 4532 | |
| 4533 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4534 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4535 | |
| 4536 | #ifdef NEED_RETRY |
| 4537 | retry: |
| 4538 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4539 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4540 | else |
| 4541 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4542 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4543 | |
| 4544 | if (done < 0) { |
| 4545 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4546 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4550 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4551 | |
| 4552 | #ifdef NEED_RETRY |
| 4553 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4554 | s += done; |
| 4555 | size -= done; |
| 4556 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4557 | } |
| 4558 | #endif |
| 4559 | |
| 4560 | return (PyObject *)v; |
| 4561 | } |
| 4562 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4563 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4564 | Py_ssize_t size, |
| 4565 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4566 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4567 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4568 | } |
| 4569 | |
| 4570 | /* |
| 4571 | * Convert unicode into string object (MBCS). |
| 4572 | * Returns 0 if succeed, -1 otherwise. |
| 4573 | */ |
| 4574 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4575 | const Py_UNICODE *p, /* unicode */ |
| 4576 | int size) /* size of unicode */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4577 | { |
| 4578 | int mbcssize = 0; |
| 4579 | Py_ssize_t n = 0; |
| 4580 | |
| 4581 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4582 | |
| 4583 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4584 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4585 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4586 | if (mbcssize == 0) { |
| 4587 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4588 | return -1; |
| 4589 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4592 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4593 | /* Create string object */ |
| 4594 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4595 | if (*repr == NULL) |
| 4596 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4597 | } |
| 4598 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4599 | /* Extend string object */ |
| 4600 | n = PyBytes_Size(*repr); |
| 4601 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4602 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | /* Do the conversion */ |
| 4606 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4607 | char *s = PyBytes_AS_STRING(*repr) + n; |
| 4608 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4609 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4610 | return -1; |
| 4611 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4618 | Py_ssize_t size, |
| 4619 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4620 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4621 | PyObject *repr = NULL; |
| 4622 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4623 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4624 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4625 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4626 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4627 | ret = encode_mbcs(&repr, p, INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4628 | else |
| 4629 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4630 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4631 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4632 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4633 | Py_XDECREF(repr); |
| 4634 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4635 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4636 | |
| 4637 | #ifdef NEED_RETRY |
| 4638 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4639 | p += INT_MAX; |
| 4640 | size -= INT_MAX; |
| 4641 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4642 | } |
| 4643 | #endif |
| 4644 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4645 | return repr; |
| 4646 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4647 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4648 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4649 | { |
| 4650 | if (!PyUnicode_Check(unicode)) { |
| 4651 | PyErr_BadArgument(); |
| 4652 | return NULL; |
| 4653 | } |
| 4654 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4655 | PyUnicode_GET_SIZE(unicode), |
| 4656 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4659 | #undef NEED_RETRY |
| 4660 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4661 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4662 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4663 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4664 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4665 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4666 | Py_ssize_t size, |
| 4667 | PyObject *mapping, |
| 4668 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4669 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4670 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4671 | Py_ssize_t startinpos; |
| 4672 | Py_ssize_t endinpos; |
| 4673 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4674 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4675 | PyUnicodeObject *v; |
| 4676 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4677 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4678 | PyObject *errorHandler = NULL; |
| 4679 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4680 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4681 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4683 | /* Default to Latin-1 */ |
| 4684 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4685 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4686 | |
| 4687 | v = _PyUnicode_New(size); |
| 4688 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4689 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4690 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4691 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4692 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4693 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4694 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4695 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4696 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4697 | while (s < e) { |
| 4698 | unsigned char ch = *s; |
| 4699 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4700 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4701 | if (ch < maplen) |
| 4702 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4703 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4704 | if (x == 0xfffe) { |
| 4705 | /* undefined mapping */ |
| 4706 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4707 | startinpos = s-starts; |
| 4708 | endinpos = startinpos+1; |
| 4709 | if (unicode_decode_call_errorhandler( |
| 4710 | errors, &errorHandler, |
| 4711 | "charmap", "character maps to <undefined>", |
| 4712 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4713 | &v, &outpos, &p)) { |
| 4714 | goto onError; |
| 4715 | } |
| 4716 | continue; |
| 4717 | } |
| 4718 | *p++ = x; |
| 4719 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4720 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4721 | } |
| 4722 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4723 | while (s < e) { |
| 4724 | unsigned char ch = *s; |
| 4725 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4726 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4727 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4728 | w = PyLong_FromLong((long)ch); |
| 4729 | if (w == NULL) |
| 4730 | goto onError; |
| 4731 | x = PyObject_GetItem(mapping, w); |
| 4732 | Py_DECREF(w); |
| 4733 | if (x == NULL) { |
| 4734 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4735 | /* No mapping found means: mapping is undefined. */ |
| 4736 | PyErr_Clear(); |
| 4737 | x = Py_None; |
| 4738 | Py_INCREF(x); |
| 4739 | } else |
| 4740 | goto onError; |
| 4741 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4742 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4743 | /* Apply mapping */ |
| 4744 | if (PyLong_Check(x)) { |
| 4745 | long value = PyLong_AS_LONG(x); |
| 4746 | if (value < 0 || value > 65535) { |
| 4747 | PyErr_SetString(PyExc_TypeError, |
| 4748 | "character mapping must be in range(65536)"); |
| 4749 | Py_DECREF(x); |
| 4750 | goto onError; |
| 4751 | } |
| 4752 | *p++ = (Py_UNICODE)value; |
| 4753 | } |
| 4754 | else if (x == Py_None) { |
| 4755 | /* undefined mapping */ |
| 4756 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4757 | startinpos = s-starts; |
| 4758 | endinpos = startinpos+1; |
| 4759 | if (unicode_decode_call_errorhandler( |
| 4760 | errors, &errorHandler, |
| 4761 | "charmap", "character maps to <undefined>", |
| 4762 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4763 | &v, &outpos, &p)) { |
| 4764 | Py_DECREF(x); |
| 4765 | goto onError; |
| 4766 | } |
| 4767 | Py_DECREF(x); |
| 4768 | continue; |
| 4769 | } |
| 4770 | else if (PyUnicode_Check(x)) { |
| 4771 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4772 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4773 | if (targetsize == 1) |
| 4774 | /* 1-1 mapping */ |
| 4775 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4776 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4777 | else if (targetsize > 1) { |
| 4778 | /* 1-n mapping */ |
| 4779 | if (targetsize > extrachars) { |
| 4780 | /* resize first */ |
| 4781 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4782 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4783 | (targetsize << 2); |
| 4784 | extrachars += needed; |
| 4785 | /* XXX overflow detection missing */ |
| 4786 | if (_PyUnicode_Resize(&v, |
| 4787 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4788 | Py_DECREF(x); |
| 4789 | goto onError; |
| 4790 | } |
| 4791 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4792 | } |
| 4793 | Py_UNICODE_COPY(p, |
| 4794 | PyUnicode_AS_UNICODE(x), |
| 4795 | targetsize); |
| 4796 | p += targetsize; |
| 4797 | extrachars -= targetsize; |
| 4798 | } |
| 4799 | /* 1-0 mapping: skip the character */ |
| 4800 | } |
| 4801 | else { |
| 4802 | /* wrong return value */ |
| 4803 | PyErr_SetString(PyExc_TypeError, |
| 4804 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4805 | Py_DECREF(x); |
| 4806 | goto onError; |
| 4807 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4808 | Py_DECREF(x); |
| 4809 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4810 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4811 | } |
| 4812 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4813 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4814 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4815 | Py_XDECREF(errorHandler); |
| 4816 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4817 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4818 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4819 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4820 | Py_XDECREF(errorHandler); |
| 4821 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4822 | Py_XDECREF(v); |
| 4823 | return NULL; |
| 4824 | } |
| 4825 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4826 | /* Charmap encoding: the lookup table */ |
| 4827 | |
| 4828 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4829 | PyObject_HEAD |
| 4830 | unsigned char level1[32]; |
| 4831 | int count2, count3; |
| 4832 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4833 | }; |
| 4834 | |
| 4835 | static PyObject* |
| 4836 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4837 | { |
| 4838 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4839 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4840 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4844 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4845 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4846 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4847 | }; |
| 4848 | |
| 4849 | static void |
| 4850 | encoding_map_dealloc(PyObject* o) |
| 4851 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4852 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4856 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4857 | "EncodingMap", /*tp_name*/ |
| 4858 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4859 | 0, /*tp_itemsize*/ |
| 4860 | /* methods */ |
| 4861 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4862 | 0, /*tp_print*/ |
| 4863 | 0, /*tp_getattr*/ |
| 4864 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4865 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4866 | 0, /*tp_repr*/ |
| 4867 | 0, /*tp_as_number*/ |
| 4868 | 0, /*tp_as_sequence*/ |
| 4869 | 0, /*tp_as_mapping*/ |
| 4870 | 0, /*tp_hash*/ |
| 4871 | 0, /*tp_call*/ |
| 4872 | 0, /*tp_str*/ |
| 4873 | 0, /*tp_getattro*/ |
| 4874 | 0, /*tp_setattro*/ |
| 4875 | 0, /*tp_as_buffer*/ |
| 4876 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4877 | 0, /*tp_doc*/ |
| 4878 | 0, /*tp_traverse*/ |
| 4879 | 0, /*tp_clear*/ |
| 4880 | 0, /*tp_richcompare*/ |
| 4881 | 0, /*tp_weaklistoffset*/ |
| 4882 | 0, /*tp_iter*/ |
| 4883 | 0, /*tp_iternext*/ |
| 4884 | encoding_map_methods, /*tp_methods*/ |
| 4885 | 0, /*tp_members*/ |
| 4886 | 0, /*tp_getset*/ |
| 4887 | 0, /*tp_base*/ |
| 4888 | 0, /*tp_dict*/ |
| 4889 | 0, /*tp_descr_get*/ |
| 4890 | 0, /*tp_descr_set*/ |
| 4891 | 0, /*tp_dictoffset*/ |
| 4892 | 0, /*tp_init*/ |
| 4893 | 0, /*tp_alloc*/ |
| 4894 | 0, /*tp_new*/ |
| 4895 | 0, /*tp_free*/ |
| 4896 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4897 | }; |
| 4898 | |
| 4899 | PyObject* |
| 4900 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4901 | { |
| 4902 | Py_UNICODE *decode; |
| 4903 | PyObject *result; |
| 4904 | struct encoding_map *mresult; |
| 4905 | int i; |
| 4906 | int need_dict = 0; |
| 4907 | unsigned char level1[32]; |
| 4908 | unsigned char level2[512]; |
| 4909 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4910 | int count2 = 0, count3 = 0; |
| 4911 | |
| 4912 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4913 | PyErr_BadArgument(); |
| 4914 | return NULL; |
| 4915 | } |
| 4916 | decode = PyUnicode_AS_UNICODE(string); |
| 4917 | memset(level1, 0xFF, sizeof level1); |
| 4918 | memset(level2, 0xFF, sizeof level2); |
| 4919 | |
| 4920 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4921 | or if there are non-BMP characters, we need to use |
| 4922 | a mapping dictionary. */ |
| 4923 | if (decode[0] != 0) |
| 4924 | need_dict = 1; |
| 4925 | for (i = 1; i < 256; i++) { |
| 4926 | int l1, l2; |
| 4927 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4928 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4929 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4930 | #endif |
| 4931 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4932 | need_dict = 1; |
| 4933 | break; |
| 4934 | } |
| 4935 | if (decode[i] == 0xFFFE) |
| 4936 | /* unmapped character */ |
| 4937 | continue; |
| 4938 | l1 = decode[i] >> 11; |
| 4939 | l2 = decode[i] >> 7; |
| 4940 | if (level1[l1] == 0xFF) |
| 4941 | level1[l1] = count2++; |
| 4942 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4943 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4947 | need_dict = 1; |
| 4948 | |
| 4949 | if (need_dict) { |
| 4950 | PyObject *result = PyDict_New(); |
| 4951 | PyObject *key, *value; |
| 4952 | if (!result) |
| 4953 | return NULL; |
| 4954 | for (i = 0; i < 256; i++) { |
| 4955 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4956 | key = PyLong_FromLong(decode[i]); |
| 4957 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4958 | if (!key || !value) |
| 4959 | goto failed1; |
| 4960 | if (PyDict_SetItem(result, key, value) == -1) |
| 4961 | goto failed1; |
| 4962 | Py_DECREF(key); |
| 4963 | Py_DECREF(value); |
| 4964 | } |
| 4965 | return result; |
| 4966 | failed1: |
| 4967 | Py_XDECREF(key); |
| 4968 | Py_XDECREF(value); |
| 4969 | Py_DECREF(result); |
| 4970 | return NULL; |
| 4971 | } |
| 4972 | |
| 4973 | /* Create a three-level trie */ |
| 4974 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4975 | 16*count2 + 128*count3 - 1); |
| 4976 | if (!result) |
| 4977 | return PyErr_NoMemory(); |
| 4978 | PyObject_Init(result, &EncodingMapType); |
| 4979 | mresult = (struct encoding_map*)result; |
| 4980 | mresult->count2 = count2; |
| 4981 | mresult->count3 = count3; |
| 4982 | mlevel1 = mresult->level1; |
| 4983 | mlevel2 = mresult->level23; |
| 4984 | mlevel3 = mresult->level23 + 16*count2; |
| 4985 | memcpy(mlevel1, level1, 32); |
| 4986 | memset(mlevel2, 0xFF, 16*count2); |
| 4987 | memset(mlevel3, 0, 128*count3); |
| 4988 | count3 = 0; |
| 4989 | for (i = 1; i < 256; i++) { |
| 4990 | int o1, o2, o3, i2, i3; |
| 4991 | if (decode[i] == 0xFFFE) |
| 4992 | /* unmapped character */ |
| 4993 | continue; |
| 4994 | o1 = decode[i]>>11; |
| 4995 | o2 = (decode[i]>>7) & 0xF; |
| 4996 | i2 = 16*mlevel1[o1] + o2; |
| 4997 | if (mlevel2[i2] == 0xFF) |
| 4998 | mlevel2[i2] = count3++; |
| 4999 | o3 = decode[i] & 0x7F; |
| 5000 | i3 = 128*mlevel2[i2] + o3; |
| 5001 | mlevel3[i3] = i; |
| 5002 | } |
| 5003 | return result; |
| 5004 | } |
| 5005 | |
| 5006 | static int |
| 5007 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5008 | { |
| 5009 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5010 | int l1 = c>>11; |
| 5011 | int l2 = (c>>7) & 0xF; |
| 5012 | int l3 = c & 0x7F; |
| 5013 | int i; |
| 5014 | |
| 5015 | #ifdef Py_UNICODE_WIDE |
| 5016 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5017 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5018 | } |
| 5019 | #endif |
| 5020 | if (c == 0) |
| 5021 | return 0; |
| 5022 | /* level 1*/ |
| 5023 | i = map->level1[l1]; |
| 5024 | if (i == 0xFF) { |
| 5025 | return -1; |
| 5026 | } |
| 5027 | /* level 2*/ |
| 5028 | i = map->level23[16*i+l2]; |
| 5029 | if (i == 0xFF) { |
| 5030 | return -1; |
| 5031 | } |
| 5032 | /* level 3 */ |
| 5033 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5034 | if (i == 0) { |
| 5035 | return -1; |
| 5036 | } |
| 5037 | return i; |
| 5038 | } |
| 5039 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5040 | /* Lookup the character ch in the mapping. If the character |
| 5041 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5042 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5043 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5044 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5045 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5046 | PyObject *x; |
| 5047 | |
| 5048 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5049 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5050 | x = PyObject_GetItem(mapping, w); |
| 5051 | Py_DECREF(w); |
| 5052 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5054 | /* No mapping found means: mapping is undefined. */ |
| 5055 | PyErr_Clear(); |
| 5056 | x = Py_None; |
| 5057 | Py_INCREF(x); |
| 5058 | return x; |
| 5059 | } else |
| 5060 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5061 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5062 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5063 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5064 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | long value = PyLong_AS_LONG(x); |
| 5066 | if (value < 0 || value > 255) { |
| 5067 | PyErr_SetString(PyExc_TypeError, |
| 5068 | "character mapping must be in range(256)"); |
| 5069 | Py_DECREF(x); |
| 5070 | return NULL; |
| 5071 | } |
| 5072 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5073 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5074 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5075 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5076 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5077 | /* wrong return value */ |
| 5078 | PyErr_Format(PyExc_TypeError, |
| 5079 | "character mapping must return integer, bytes or None, not %.400s", |
| 5080 | x->ob_type->tp_name); |
| 5081 | Py_DECREF(x); |
| 5082 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5083 | } |
| 5084 | } |
| 5085 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5086 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5087 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5088 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5089 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5090 | /* exponentially overallocate to minimize reallocations */ |
| 5091 | if (requiredsize < 2*outsize) |
| 5092 | requiredsize = 2*outsize; |
| 5093 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5094 | return -1; |
| 5095 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5098 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5099 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5100 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5101 | /* 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] | 5102 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5103 | space is available. Return a new reference to the object that |
| 5104 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5105 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5106 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5107 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5108 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5109 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5110 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5111 | PyObject *rep; |
| 5112 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5113 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5114 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5115 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5116 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5117 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5118 | if (res == -1) |
| 5119 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5120 | if (outsize<requiredsize) |
| 5121 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5122 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5123 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5124 | outstart[(*outpos)++] = (char)res; |
| 5125 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5129 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5130 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5131 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5132 | Py_DECREF(rep); |
| 5133 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5134 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5135 | if (PyLong_Check(rep)) { |
| 5136 | Py_ssize_t requiredsize = *outpos+1; |
| 5137 | if (outsize<requiredsize) |
| 5138 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5139 | Py_DECREF(rep); |
| 5140 | return enc_EXCEPTION; |
| 5141 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5142 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5143 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5144 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5145 | else { |
| 5146 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5147 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5148 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5149 | if (outsize<requiredsize) |
| 5150 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5151 | Py_DECREF(rep); |
| 5152 | return enc_EXCEPTION; |
| 5153 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5154 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5155 | memcpy(outstart + *outpos, repchars, repsize); |
| 5156 | *outpos += repsize; |
| 5157 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5158 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5159 | Py_DECREF(rep); |
| 5160 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | /* handle an error in PyUnicode_EncodeCharmap |
| 5164 | Return 0 on success, -1 on error */ |
| 5165 | static |
| 5166 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5167 | 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] | 5168 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5169 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5170 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5171 | { |
| 5172 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5173 | Py_ssize_t repsize; |
| 5174 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5175 | Py_UNICODE *uni2; |
| 5176 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5177 | Py_ssize_t collstartpos = *inpos; |
| 5178 | Py_ssize_t collendpos = *inpos+1; |
| 5179 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5180 | char *encoding = "charmap"; |
| 5181 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5182 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5183 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5184 | /* find all unencodable characters */ |
| 5185 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5186 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5187 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5188 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5189 | if (res != -1) |
| 5190 | break; |
| 5191 | ++collendpos; |
| 5192 | continue; |
| 5193 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5194 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5195 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5196 | if (rep==NULL) |
| 5197 | return -1; |
| 5198 | else if (rep!=Py_None) { |
| 5199 | Py_DECREF(rep); |
| 5200 | break; |
| 5201 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5202 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5203 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5204 | } |
| 5205 | /* cache callback name lookup |
| 5206 | * (if not done yet, i.e. it's the first error) */ |
| 5207 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5208 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5209 | *known_errorHandler = 1; |
| 5210 | else if (!strcmp(errors, "replace")) |
| 5211 | *known_errorHandler = 2; |
| 5212 | else if (!strcmp(errors, "ignore")) |
| 5213 | *known_errorHandler = 3; |
| 5214 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5215 | *known_errorHandler = 4; |
| 5216 | else |
| 5217 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5218 | } |
| 5219 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5220 | case 1: /* strict */ |
| 5221 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5222 | return -1; |
| 5223 | case 2: /* replace */ |
| 5224 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5225 | x = charmapencode_output('?', mapping, res, respos); |
| 5226 | if (x==enc_EXCEPTION) { |
| 5227 | return -1; |
| 5228 | } |
| 5229 | else if (x==enc_FAILED) { |
| 5230 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5231 | return -1; |
| 5232 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5233 | } |
| 5234 | /* fall through */ |
| 5235 | case 3: /* ignore */ |
| 5236 | *inpos = collendpos; |
| 5237 | break; |
| 5238 | case 4: /* xmlcharrefreplace */ |
| 5239 | /* generate replacement (temporarily (mis)uses p) */ |
| 5240 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5241 | char buffer[2+29+1+1]; |
| 5242 | char *cp; |
| 5243 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5244 | for (cp = buffer; *cp; ++cp) { |
| 5245 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5246 | if (x==enc_EXCEPTION) |
| 5247 | return -1; |
| 5248 | else if (x==enc_FAILED) { |
| 5249 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5250 | return -1; |
| 5251 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5252 | } |
| 5253 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5254 | *inpos = collendpos; |
| 5255 | break; |
| 5256 | default: |
| 5257 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5258 | encoding, reason, p, size, exceptionObject, |
| 5259 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5260 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5261 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5262 | if (PyBytes_Check(repunicode)) { |
| 5263 | /* Directly copy bytes result to output. */ |
| 5264 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5265 | Py_ssize_t requiredsize; |
| 5266 | repsize = PyBytes_Size(repunicode); |
| 5267 | requiredsize = *respos + repsize; |
| 5268 | if (requiredsize > outsize) |
| 5269 | /* Make room for all additional bytes. */ |
| 5270 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5271 | Py_DECREF(repunicode); |
| 5272 | return -1; |
| 5273 | } |
| 5274 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5275 | PyBytes_AsString(repunicode), repsize); |
| 5276 | *respos += repsize; |
| 5277 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5278 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5279 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5280 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5281 | /* generate replacement */ |
| 5282 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5283 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5284 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5285 | if (x==enc_EXCEPTION) { |
| 5286 | return -1; |
| 5287 | } |
| 5288 | else if (x==enc_FAILED) { |
| 5289 | Py_DECREF(repunicode); |
| 5290 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5291 | return -1; |
| 5292 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5293 | } |
| 5294 | *inpos = newpos; |
| 5295 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5296 | } |
| 5297 | return 0; |
| 5298 | } |
| 5299 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5300 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5301 | Py_ssize_t size, |
| 5302 | PyObject *mapping, |
| 5303 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5305 | /* output object */ |
| 5306 | PyObject *res = NULL; |
| 5307 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5308 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5309 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5310 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5311 | PyObject *errorHandler = NULL; |
| 5312 | PyObject *exc = NULL; |
| 5313 | /* the following variable is used for caching string comparisons |
| 5314 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5315 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5316 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5317 | |
| 5318 | /* Default to Latin-1 */ |
| 5319 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5320 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5322 | /* allocate enough for a simple encoding without |
| 5323 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5324 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5325 | if (res == NULL) |
| 5326 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5327 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5328 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5329 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5330 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5331 | /* try to encode it */ |
| 5332 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5333 | if (x==enc_EXCEPTION) /* error */ |
| 5334 | goto onError; |
| 5335 | if (x==enc_FAILED) { /* unencodable character */ |
| 5336 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5337 | &exc, |
| 5338 | &known_errorHandler, &errorHandler, errors, |
| 5339 | &res, &respos)) { |
| 5340 | goto onError; |
| 5341 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5342 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5343 | else |
| 5344 | /* done with this character => adjust input position */ |
| 5345 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5346 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5347 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5348 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5349 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5350 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5351 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5352 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5353 | Py_XDECREF(exc); |
| 5354 | Py_XDECREF(errorHandler); |
| 5355 | return res; |
| 5356 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5357 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5358 | Py_XDECREF(res); |
| 5359 | Py_XDECREF(exc); |
| 5360 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5361 | return NULL; |
| 5362 | } |
| 5363 | |
| 5364 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5365 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5366 | { |
| 5367 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5368 | PyErr_BadArgument(); |
| 5369 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 | } |
| 5371 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5372 | PyUnicode_GET_SIZE(unicode), |
| 5373 | mapping, |
| 5374 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5377 | /* create or adjust a UnicodeTranslateError */ |
| 5378 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5380 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5381 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5383 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5384 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5385 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | } |
| 5387 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5388 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5389 | goto onError; |
| 5390 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5391 | goto onError; |
| 5392 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5393 | goto onError; |
| 5394 | return; |
| 5395 | onError: |
| 5396 | Py_DECREF(*exceptionObject); |
| 5397 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5398 | } |
| 5399 | } |
| 5400 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5401 | /* raises a UnicodeTranslateError */ |
| 5402 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5403 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5404 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5405 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5406 | { |
| 5407 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5408 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5409 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5410 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
| 5413 | /* error handling callback helper: |
| 5414 | build arguments, call the callback and check the arguments, |
| 5415 | put the result into newpos and return the replacement string, which |
| 5416 | has to be freed by the caller */ |
| 5417 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5418 | PyObject **errorHandler, |
| 5419 | const char *reason, |
| 5420 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5421 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5422 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5423 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5424 | 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] | 5425 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5426 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5427 | PyObject *restuple; |
| 5428 | PyObject *resunicode; |
| 5429 | |
| 5430 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5431 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5432 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5433 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
| 5436 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5437 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5438 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5439 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5440 | |
| 5441 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5442 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5443 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5444 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5445 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5446 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5447 | Py_DECREF(restuple); |
| 5448 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5449 | } |
| 5450 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5451 | &resunicode, &i_newpos)) { |
| 5452 | Py_DECREF(restuple); |
| 5453 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5454 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5455 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5456 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5457 | else |
| 5458 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5459 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5460 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5461 | Py_DECREF(restuple); |
| 5462 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5463 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5464 | Py_INCREF(resunicode); |
| 5465 | Py_DECREF(restuple); |
| 5466 | return resunicode; |
| 5467 | } |
| 5468 | |
| 5469 | /* Lookup the character ch in the mapping and put the result in result, |
| 5470 | which must be decrefed by the caller. |
| 5471 | Return 0 on success, -1 on error */ |
| 5472 | static |
| 5473 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5474 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5475 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5476 | PyObject *x; |
| 5477 | |
| 5478 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5479 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5480 | x = PyObject_GetItem(mapping, w); |
| 5481 | Py_DECREF(w); |
| 5482 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5483 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5484 | /* No mapping found means: use 1:1 mapping. */ |
| 5485 | PyErr_Clear(); |
| 5486 | *result = NULL; |
| 5487 | return 0; |
| 5488 | } else |
| 5489 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5490 | } |
| 5491 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5492 | *result = x; |
| 5493 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5494 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5495 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5496 | long value = PyLong_AS_LONG(x); |
| 5497 | long max = PyUnicode_GetMax(); |
| 5498 | if (value < 0 || value > max) { |
| 5499 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5500 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5501 | Py_DECREF(x); |
| 5502 | return -1; |
| 5503 | } |
| 5504 | *result = x; |
| 5505 | return 0; |
| 5506 | } |
| 5507 | else if (PyUnicode_Check(x)) { |
| 5508 | *result = x; |
| 5509 | return 0; |
| 5510 | } |
| 5511 | else { |
| 5512 | /* wrong return value */ |
| 5513 | PyErr_SetString(PyExc_TypeError, |
| 5514 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5515 | Py_DECREF(x); |
| 5516 | return -1; |
| 5517 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5518 | } |
| 5519 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5520 | if not reallocate and adjust various state variables. |
| 5521 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5522 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5523 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5524 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5525 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5526 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5527 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5528 | /* remember old output position */ |
| 5529 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5530 | /* exponentially overallocate to minimize reallocations */ |
| 5531 | if (requiredsize < 2 * oldsize) |
| 5532 | requiredsize = 2 * oldsize; |
| 5533 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5534 | return -1; |
| 5535 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5536 | } |
| 5537 | return 0; |
| 5538 | } |
| 5539 | /* lookup the character, put the result in the output string and adjust |
| 5540 | various state variables. Return a new reference to the object that |
| 5541 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5542 | undefined (in which case no character was written). |
| 5543 | The called must decref result. |
| 5544 | Return 0 on success, -1 on error. */ |
| 5545 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5546 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5547 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5548 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5549 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5550 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5551 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5552 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5553 | /* not found => default to 1:1 mapping */ |
| 5554 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5555 | } |
| 5556 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5557 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5558 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5559 | /* no overflow check, because we know that the space is enough */ |
| 5560 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5561 | } |
| 5562 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5563 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5564 | if (repsize==1) { |
| 5565 | /* no overflow check, because we know that the space is enough */ |
| 5566 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5567 | } |
| 5568 | else if (repsize!=0) { |
| 5569 | /* more than one character */ |
| 5570 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5571 | (insize - (curinp-startinp)) + |
| 5572 | repsize - 1; |
| 5573 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5574 | return -1; |
| 5575 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5576 | *outp += repsize; |
| 5577 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5578 | } |
| 5579 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5580 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5581 | return 0; |
| 5582 | } |
| 5583 | |
| 5584 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5585 | Py_ssize_t size, |
| 5586 | PyObject *mapping, |
| 5587 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5588 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5589 | /* output object */ |
| 5590 | PyObject *res = NULL; |
| 5591 | /* pointers to the beginning and end+1 of input */ |
| 5592 | const Py_UNICODE *startp = p; |
| 5593 | const Py_UNICODE *endp = p + size; |
| 5594 | /* pointer into the output */ |
| 5595 | Py_UNICODE *str; |
| 5596 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5597 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5598 | char *reason = "character maps to <undefined>"; |
| 5599 | PyObject *errorHandler = NULL; |
| 5600 | PyObject *exc = NULL; |
| 5601 | /* the following variable is used for caching string comparisons |
| 5602 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5603 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5604 | int known_errorHandler = -1; |
| 5605 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5607 | PyErr_BadArgument(); |
| 5608 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5609 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5610 | |
| 5611 | /* allocate enough for a simple 1:1 translation without |
| 5612 | replacements, if we need more, we'll resize */ |
| 5613 | res = PyUnicode_FromUnicode(NULL, size); |
| 5614 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5615 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5616 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5617 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5618 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5619 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5620 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5621 | /* try to encode it */ |
| 5622 | PyObject *x = NULL; |
| 5623 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5624 | Py_XDECREF(x); |
| 5625 | goto onError; |
| 5626 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5627 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5628 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5629 | ++p; |
| 5630 | else { /* untranslatable character */ |
| 5631 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5632 | Py_ssize_t repsize; |
| 5633 | Py_ssize_t newpos; |
| 5634 | Py_UNICODE *uni2; |
| 5635 | /* startpos for collecting untranslatable chars */ |
| 5636 | const Py_UNICODE *collstart = p; |
| 5637 | const Py_UNICODE *collend = p+1; |
| 5638 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5640 | /* find all untranslatable characters */ |
| 5641 | while (collend < endp) { |
| 5642 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5643 | goto onError; |
| 5644 | Py_XDECREF(x); |
| 5645 | if (x!=Py_None) |
| 5646 | break; |
| 5647 | ++collend; |
| 5648 | } |
| 5649 | /* cache callback name lookup |
| 5650 | * (if not done yet, i.e. it's the first error) */ |
| 5651 | if (known_errorHandler==-1) { |
| 5652 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5653 | known_errorHandler = 1; |
| 5654 | else if (!strcmp(errors, "replace")) |
| 5655 | known_errorHandler = 2; |
| 5656 | else if (!strcmp(errors, "ignore")) |
| 5657 | known_errorHandler = 3; |
| 5658 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5659 | known_errorHandler = 4; |
| 5660 | else |
| 5661 | known_errorHandler = 0; |
| 5662 | } |
| 5663 | switch (known_errorHandler) { |
| 5664 | case 1: /* strict */ |
| 5665 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5666 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5667 | case 2: /* replace */ |
| 5668 | /* No need to check for space, this is a 1:1 replacement */ |
| 5669 | for (coll = collstart; coll<collend; ++coll) |
| 5670 | *str++ = '?'; |
| 5671 | /* fall through */ |
| 5672 | case 3: /* ignore */ |
| 5673 | p = collend; |
| 5674 | break; |
| 5675 | case 4: /* xmlcharrefreplace */ |
| 5676 | /* generate replacement (temporarily (mis)uses p) */ |
| 5677 | for (p = collstart; p < collend; ++p) { |
| 5678 | char buffer[2+29+1+1]; |
| 5679 | char *cp; |
| 5680 | sprintf(buffer, "&#%d;", (int)*p); |
| 5681 | if (charmaptranslate_makespace(&res, &str, |
| 5682 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5683 | goto onError; |
| 5684 | for (cp = buffer; *cp; ++cp) |
| 5685 | *str++ = *cp; |
| 5686 | } |
| 5687 | p = collend; |
| 5688 | break; |
| 5689 | default: |
| 5690 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5691 | reason, startp, size, &exc, |
| 5692 | collstart-startp, collend-startp, &newpos); |
| 5693 | if (repunicode == NULL) |
| 5694 | goto onError; |
| 5695 | /* generate replacement */ |
| 5696 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5697 | if (charmaptranslate_makespace(&res, &str, |
| 5698 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5699 | Py_DECREF(repunicode); |
| 5700 | goto onError; |
| 5701 | } |
| 5702 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5703 | *str++ = *uni2; |
| 5704 | p = startp + newpos; |
| 5705 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5706 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5707 | } |
| 5708 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5709 | /* Resize if we allocated to much */ |
| 5710 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5711 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5712 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5713 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5714 | } |
| 5715 | Py_XDECREF(exc); |
| 5716 | Py_XDECREF(errorHandler); |
| 5717 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5718 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5719 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5720 | Py_XDECREF(res); |
| 5721 | Py_XDECREF(exc); |
| 5722 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | return NULL; |
| 5724 | } |
| 5725 | |
| 5726 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5727 | PyObject *mapping, |
| 5728 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | { |
| 5730 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5731 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | str = PyUnicode_FromObject(str); |
| 5733 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5734 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5736 | PyUnicode_GET_SIZE(str), |
| 5737 | mapping, |
| 5738 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | Py_DECREF(str); |
| 5740 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5741 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5742 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5743 | Py_XDECREF(str); |
| 5744 | return NULL; |
| 5745 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5746 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5747 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5748 | |
| 5749 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5750 | Py_ssize_t length, |
| 5751 | char *output, |
| 5752 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5753 | { |
| 5754 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5755 | PyObject *errorHandler = NULL; |
| 5756 | PyObject *exc = NULL; |
| 5757 | const char *encoding = "decimal"; |
| 5758 | const char *reason = "invalid decimal Unicode string"; |
| 5759 | /* the following variable is used for caching string comparisons |
| 5760 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5761 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5762 | |
| 5763 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5764 | PyErr_BadArgument(); |
| 5765 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | p = s; |
| 5769 | end = s + length; |
| 5770 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5771 | register Py_UNICODE ch = *p; |
| 5772 | int decimal; |
| 5773 | PyObject *repunicode; |
| 5774 | Py_ssize_t repsize; |
| 5775 | Py_ssize_t newpos; |
| 5776 | Py_UNICODE *uni2; |
| 5777 | Py_UNICODE *collstart; |
| 5778 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5779 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5780 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5781 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5782 | ++p; |
| 5783 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5784 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5785 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5786 | if (decimal >= 0) { |
| 5787 | *output++ = '0' + decimal; |
| 5788 | ++p; |
| 5789 | continue; |
| 5790 | } |
| 5791 | if (0 < ch && ch < 256) { |
| 5792 | *output++ = (char)ch; |
| 5793 | ++p; |
| 5794 | continue; |
| 5795 | } |
| 5796 | /* All other characters are considered unencodable */ |
| 5797 | collstart = p; |
| 5798 | collend = p+1; |
| 5799 | while (collend < end) { |
| 5800 | if ((0 < *collend && *collend < 256) || |
| 5801 | !Py_UNICODE_ISSPACE(*collend) || |
| 5802 | Py_UNICODE_TODECIMAL(*collend)) |
| 5803 | break; |
| 5804 | } |
| 5805 | /* cache callback name lookup |
| 5806 | * (if not done yet, i.e. it's the first error) */ |
| 5807 | if (known_errorHandler==-1) { |
| 5808 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5809 | known_errorHandler = 1; |
| 5810 | else if (!strcmp(errors, "replace")) |
| 5811 | known_errorHandler = 2; |
| 5812 | else if (!strcmp(errors, "ignore")) |
| 5813 | known_errorHandler = 3; |
| 5814 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5815 | known_errorHandler = 4; |
| 5816 | else |
| 5817 | known_errorHandler = 0; |
| 5818 | } |
| 5819 | switch (known_errorHandler) { |
| 5820 | case 1: /* strict */ |
| 5821 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5822 | goto onError; |
| 5823 | case 2: /* replace */ |
| 5824 | for (p = collstart; p < collend; ++p) |
| 5825 | *output++ = '?'; |
| 5826 | /* fall through */ |
| 5827 | case 3: /* ignore */ |
| 5828 | p = collend; |
| 5829 | break; |
| 5830 | case 4: /* xmlcharrefreplace */ |
| 5831 | /* generate replacement (temporarily (mis)uses p) */ |
| 5832 | for (p = collstart; p < collend; ++p) |
| 5833 | output += sprintf(output, "&#%d;", (int)*p); |
| 5834 | p = collend; |
| 5835 | break; |
| 5836 | default: |
| 5837 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5838 | encoding, reason, s, length, &exc, |
| 5839 | collstart-s, collend-s, &newpos); |
| 5840 | if (repunicode == NULL) |
| 5841 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5842 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5843 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5844 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 5845 | Py_DECREF(repunicode); |
| 5846 | goto onError; |
| 5847 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5848 | /* generate replacement */ |
| 5849 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5850 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5851 | Py_UNICODE ch = *uni2; |
| 5852 | if (Py_UNICODE_ISSPACE(ch)) |
| 5853 | *output++ = ' '; |
| 5854 | else { |
| 5855 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5856 | if (decimal >= 0) |
| 5857 | *output++ = '0' + decimal; |
| 5858 | else if (0 < ch && ch < 256) |
| 5859 | *output++ = (char)ch; |
| 5860 | else { |
| 5861 | Py_DECREF(repunicode); |
| 5862 | raise_encode_exception(&exc, encoding, |
| 5863 | s, length, collstart-s, collend-s, reason); |
| 5864 | goto onError; |
| 5865 | } |
| 5866 | } |
| 5867 | } |
| 5868 | p = s + newpos; |
| 5869 | Py_DECREF(repunicode); |
| 5870 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5871 | } |
| 5872 | /* 0-terminate the output string */ |
| 5873 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5874 | Py_XDECREF(exc); |
| 5875 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5876 | return 0; |
| 5877 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5878 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5879 | Py_XDECREF(exc); |
| 5880 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5881 | return -1; |
| 5882 | } |
| 5883 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | /* --- Helpers ------------------------------------------------------------ */ |
| 5885 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5886 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5887 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5888 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5889 | #include "stringlib/count.h" |
| 5890 | #include "stringlib/find.h" |
| 5891 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5892 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5893 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5894 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 5895 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5896 | #include "stringlib/localeutil.h" |
| 5897 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5898 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5899 | #define ADJUST_INDICES(start, end, len) \ |
| 5900 | if (end > len) \ |
| 5901 | end = len; \ |
| 5902 | else if (end < 0) { \ |
| 5903 | end += len; \ |
| 5904 | if (end < 0) \ |
| 5905 | end = 0; \ |
| 5906 | } \ |
| 5907 | if (start < 0) { \ |
| 5908 | start += len; \ |
| 5909 | if (start < 0) \ |
| 5910 | start = 0; \ |
| 5911 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5912 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5913 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5914 | PyObject *substr, |
| 5915 | Py_ssize_t start, |
| 5916 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5918 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5919 | PyUnicodeObject* str_obj; |
| 5920 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5921 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5922 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5923 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5924 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5925 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5926 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5927 | Py_DECREF(str_obj); |
| 5928 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5930 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5931 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5932 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5933 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 5934 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5935 | ); |
| 5936 | |
| 5937 | Py_DECREF(sub_obj); |
| 5938 | Py_DECREF(str_obj); |
| 5939 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5940 | return result; |
| 5941 | } |
| 5942 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5943 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5944 | PyObject *sub, |
| 5945 | Py_ssize_t start, |
| 5946 | Py_ssize_t end, |
| 5947 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5948 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5949 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5951 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5952 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5953 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5954 | sub = PyUnicode_FromObject(sub); |
| 5955 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5956 | Py_DECREF(str); |
| 5957 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5958 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5959 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5960 | if (direction > 0) |
| 5961 | result = stringlib_find_slice( |
| 5962 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5963 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5964 | start, end |
| 5965 | ); |
| 5966 | else |
| 5967 | result = stringlib_rfind_slice( |
| 5968 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5969 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5970 | start, end |
| 5971 | ); |
| 5972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5974 | Py_DECREF(sub); |
| 5975 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | return result; |
| 5977 | } |
| 5978 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5979 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5981 | PyUnicodeObject *substring, |
| 5982 | Py_ssize_t start, |
| 5983 | Py_ssize_t end, |
| 5984 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5985 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5986 | if (substring->length == 0) |
| 5987 | return 1; |
| 5988 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5989 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5990 | end -= substring->length; |
| 5991 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5992 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | |
| 5994 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5995 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5996 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | } else { |
| 5998 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5999 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
| 6002 | return 0; |
| 6003 | } |
| 6004 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6005 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6006 | PyObject *substr, |
| 6007 | Py_ssize_t start, |
| 6008 | Py_ssize_t end, |
| 6009 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6010 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6011 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6012 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6013 | str = PyUnicode_FromObject(str); |
| 6014 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6015 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | substr = PyUnicode_FromObject(substr); |
| 6017 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6018 | Py_DECREF(str); |
| 6019 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6021 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6023 | (PyUnicodeObject *)substr, |
| 6024 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | Py_DECREF(str); |
| 6026 | Py_DECREF(substr); |
| 6027 | return result; |
| 6028 | } |
| 6029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | /* Apply fixfct filter to the Unicode object self and return a |
| 6031 | reference to the modified object */ |
| 6032 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6033 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6035 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | { |
| 6037 | |
| 6038 | PyUnicodeObject *u; |
| 6039 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6040 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6043 | |
| 6044 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6045 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6046 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6047 | /* fixfct should return TRUE if it modified the buffer. If |
| 6048 | FALSE, return a reference to the original buffer instead |
| 6049 | (to save space, not time) */ |
| 6050 | Py_INCREF(self); |
| 6051 | Py_DECREF(u); |
| 6052 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | } |
| 6054 | return (PyObject*) u; |
| 6055 | } |
| 6056 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6057 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | int fixupper(PyUnicodeObject *self) |
| 6059 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6060 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | Py_UNICODE *s = self->str; |
| 6062 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6063 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6065 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6066 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6067 | ch = Py_UNICODE_TOUPPER(*s); |
| 6068 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6070 | *s = ch; |
| 6071 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | s++; |
| 6073 | } |
| 6074 | |
| 6075 | return status; |
| 6076 | } |
| 6077 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6078 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | int fixlower(PyUnicodeObject *self) |
| 6080 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6081 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | Py_UNICODE *s = self->str; |
| 6083 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6086 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6087 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6088 | ch = Py_UNICODE_TOLOWER(*s); |
| 6089 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6090 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6091 | *s = ch; |
| 6092 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | s++; |
| 6094 | } |
| 6095 | |
| 6096 | return status; |
| 6097 | } |
| 6098 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6099 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | int fixswapcase(PyUnicodeObject *self) |
| 6101 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6102 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | Py_UNICODE *s = self->str; |
| 6104 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6106 | while (len-- > 0) { |
| 6107 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6108 | *s = Py_UNICODE_TOLOWER(*s); |
| 6109 | status = 1; |
| 6110 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6111 | *s = Py_UNICODE_TOUPPER(*s); |
| 6112 | status = 1; |
| 6113 | } |
| 6114 | s++; |
| 6115 | } |
| 6116 | |
| 6117 | return status; |
| 6118 | } |
| 6119 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6120 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | int fixcapitalize(PyUnicodeObject *self) |
| 6122 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6123 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6124 | Py_UNICODE *s = self->str; |
| 6125 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6126 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6127 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6128 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6129 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6130 | *s = Py_UNICODE_TOUPPER(*s); |
| 6131 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6132 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6133 | s++; |
| 6134 | while (--len > 0) { |
| 6135 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6136 | *s = Py_UNICODE_TOLOWER(*s); |
| 6137 | status = 1; |
| 6138 | } |
| 6139 | s++; |
| 6140 | } |
| 6141 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | static |
| 6145 | int fixtitle(PyUnicodeObject *self) |
| 6146 | { |
| 6147 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6148 | register Py_UNICODE *e; |
| 6149 | int previous_is_cased; |
| 6150 | |
| 6151 | /* Shortcut for single character strings */ |
| 6152 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6153 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6154 | if (*p != ch) { |
| 6155 | *p = ch; |
| 6156 | return 1; |
| 6157 | } |
| 6158 | else |
| 6159 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6160 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6161 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | e = p + PyUnicode_GET_SIZE(self); |
| 6163 | previous_is_cased = 0; |
| 6164 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6165 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6166 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | if (previous_is_cased) |
| 6168 | *p = Py_UNICODE_TOLOWER(ch); |
| 6169 | else |
| 6170 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6171 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | if (Py_UNICODE_ISLOWER(ch) || |
| 6173 | Py_UNICODE_ISUPPER(ch) || |
| 6174 | Py_UNICODE_ISTITLE(ch)) |
| 6175 | previous_is_cased = 1; |
| 6176 | else |
| 6177 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6178 | } |
| 6179 | return 1; |
| 6180 | } |
| 6181 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6182 | PyObject * |
| 6183 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6185 | const Py_UNICODE blank = ' '; |
| 6186 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6187 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6188 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6189 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6190 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6191 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6192 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6193 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6194 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6195 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6196 | fseq = PySequence_Fast(seq, ""); |
| 6197 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6198 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6199 | } |
| 6200 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6201 | /* NOTE: the following code can't call back into Python code, |
| 6202 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6203 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6204 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6205 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6206 | /* If empty sequence, return u"". */ |
| 6207 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6208 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6209 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6210 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6211 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6212 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6213 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | item = items[0]; |
| 6215 | if (PyUnicode_CheckExact(item)) { |
| 6216 | Py_INCREF(item); |
| 6217 | res = (PyUnicodeObject *)item; |
| 6218 | goto Done; |
| 6219 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6220 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6221 | else { |
| 6222 | /* Set up sep and seplen */ |
| 6223 | if (separator == NULL) { |
| 6224 | sep = ␣ |
| 6225 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6226 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6227 | else { |
| 6228 | if (!PyUnicode_Check(separator)) { |
| 6229 | PyErr_Format(PyExc_TypeError, |
| 6230 | "separator: expected str instance," |
| 6231 | " %.80s found", |
| 6232 | Py_TYPE(separator)->tp_name); |
| 6233 | goto onError; |
| 6234 | } |
| 6235 | sep = PyUnicode_AS_UNICODE(separator); |
| 6236 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6237 | } |
| 6238 | } |
| 6239 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6240 | /* There are at least two things to join, or else we have a subclass |
| 6241 | * of str in the sequence. |
| 6242 | * Do a pre-pass to figure out the total amount of space we'll |
| 6243 | * need (sz), and see whether all argument are strings. |
| 6244 | */ |
| 6245 | sz = 0; |
| 6246 | for (i = 0; i < seqlen; i++) { |
| 6247 | const Py_ssize_t old_sz = sz; |
| 6248 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6249 | if (!PyUnicode_Check(item)) { |
| 6250 | PyErr_Format(PyExc_TypeError, |
| 6251 | "sequence item %zd: expected str instance," |
| 6252 | " %.80s found", |
| 6253 | i, Py_TYPE(item)->tp_name); |
| 6254 | goto onError; |
| 6255 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6256 | sz += PyUnicode_GET_SIZE(item); |
| 6257 | if (i != 0) |
| 6258 | sz += seplen; |
| 6259 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6260 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6261 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6262 | goto onError; |
| 6263 | } |
| 6264 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6265 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6266 | res = _PyUnicode_New(sz); |
| 6267 | if (res == NULL) |
| 6268 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6269 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6270 | /* Catenate everything. */ |
| 6271 | res_p = PyUnicode_AS_UNICODE(res); |
| 6272 | for (i = 0; i < seqlen; ++i) { |
| 6273 | Py_ssize_t itemlen; |
| 6274 | item = items[i]; |
| 6275 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6276 | /* Copy item, and maybe the separator. */ |
| 6277 | if (i) { |
| 6278 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6279 | res_p += seplen; |
| 6280 | } |
| 6281 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6282 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6283 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6284 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6286 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6287 | return (PyObject *)res; |
| 6288 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6289 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6290 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6291 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6292 | return NULL; |
| 6293 | } |
| 6294 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6295 | static |
| 6296 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | Py_ssize_t left, |
| 6298 | Py_ssize_t right, |
| 6299 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6300 | { |
| 6301 | PyUnicodeObject *u; |
| 6302 | |
| 6303 | if (left < 0) |
| 6304 | left = 0; |
| 6305 | if (right < 0) |
| 6306 | right = 0; |
| 6307 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6308 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6309 | Py_INCREF(self); |
| 6310 | return self; |
| 6311 | } |
| 6312 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6313 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6314 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6315 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6316 | return NULL; |
| 6317 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6318 | u = _PyUnicode_New(left + self->length + right); |
| 6319 | if (u) { |
| 6320 | if (left) |
| 6321 | Py_UNICODE_FILL(u->str, fill, left); |
| 6322 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6323 | if (right) |
| 6324 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6325 | } |
| 6326 | |
| 6327 | return u; |
| 6328 | } |
| 6329 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6330 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6331 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6332 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6333 | |
| 6334 | string = PyUnicode_FromObject(string); |
| 6335 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6336 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6337 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6338 | list = stringlib_splitlines( |
| 6339 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6340 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6341 | |
| 6342 | Py_DECREF(string); |
| 6343 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6344 | } |
| 6345 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6346 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6347 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6348 | PyUnicodeObject *substring, |
| 6349 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6350 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6355 | return stringlib_split_whitespace( |
| 6356 | (PyObject*) self, self->str, self->length, maxcount |
| 6357 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6358 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6359 | return stringlib_split( |
| 6360 | (PyObject*) self, self->str, self->length, |
| 6361 | substring->str, substring->length, |
| 6362 | maxcount |
| 6363 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6364 | } |
| 6365 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6366 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6367 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6368 | PyUnicodeObject *substring, |
| 6369 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6370 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6371 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6372 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6373 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6374 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6375 | return stringlib_rsplit_whitespace( |
| 6376 | (PyObject*) self, self->str, self->length, maxcount |
| 6377 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6378 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6379 | return stringlib_rsplit( |
| 6380 | (PyObject*) self, self->str, self->length, |
| 6381 | substring->str, substring->length, |
| 6382 | maxcount |
| 6383 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
| 6386 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6387 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6388 | PyUnicodeObject *str1, |
| 6389 | PyUnicodeObject *str2, |
| 6390 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | { |
| 6392 | PyUnicodeObject *u; |
| 6393 | |
| 6394 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6395 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6396 | else if (maxcount == 0 || self->length == 0) |
| 6397 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6398 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6399 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6400 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6401 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6402 | if (str1->length == 0) |
| 6403 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6404 | if (str1->length == 1) { |
| 6405 | /* replace characters */ |
| 6406 | Py_UNICODE u1, u2; |
| 6407 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6408 | goto nothing; |
| 6409 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6410 | if (!u) |
| 6411 | return NULL; |
| 6412 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6413 | u1 = str1->str[0]; |
| 6414 | u2 = str2->str[0]; |
| 6415 | for (i = 0; i < u->length; i++) |
| 6416 | if (u->str[i] == u1) { |
| 6417 | if (--maxcount < 0) |
| 6418 | break; |
| 6419 | u->str[i] = u2; |
| 6420 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6422 | i = stringlib_find( |
| 6423 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6425 | if (i < 0) |
| 6426 | goto nothing; |
| 6427 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6428 | if (!u) |
| 6429 | return NULL; |
| 6430 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6431 | |
| 6432 | /* change everything in-place, starting with this one */ |
| 6433 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6434 | i += str1->length; |
| 6435 | |
| 6436 | while ( --maxcount > 0) { |
| 6437 | i = stringlib_find(self->str+i, self->length-i, |
| 6438 | str1->str, str1->length, |
| 6439 | i); |
| 6440 | if (i == -1) |
| 6441 | break; |
| 6442 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6443 | i += str1->length; |
| 6444 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6445 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6447 | |
| 6448 | Py_ssize_t n, i, j, e; |
| 6449 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6450 | Py_UNICODE *p; |
| 6451 | |
| 6452 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6453 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6454 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6455 | if (n == 0) |
| 6456 | goto nothing; |
| 6457 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6458 | delta = (str2->length - str1->length); |
| 6459 | if (delta == 0) { |
| 6460 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6461 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6462 | product = n * (str2->length - str1->length); |
| 6463 | if ((product / (str2->length - str1->length)) != n) { |
| 6464 | PyErr_SetString(PyExc_OverflowError, |
| 6465 | "replace string is too long"); |
| 6466 | return NULL; |
| 6467 | } |
| 6468 | new_size = self->length + product; |
| 6469 | if (new_size < 0) { |
| 6470 | PyErr_SetString(PyExc_OverflowError, |
| 6471 | "replace string is too long"); |
| 6472 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6473 | } |
| 6474 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6475 | u = _PyUnicode_New(new_size); |
| 6476 | if (!u) |
| 6477 | return NULL; |
| 6478 | i = 0; |
| 6479 | p = u->str; |
| 6480 | e = self->length - str1->length; |
| 6481 | if (str1->length > 0) { |
| 6482 | while (n-- > 0) { |
| 6483 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6484 | j = stringlib_find(self->str+i, self->length-i, |
| 6485 | str1->str, str1->length, |
| 6486 | i); |
| 6487 | if (j == -1) |
| 6488 | break; |
| 6489 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6490 | /* copy unchanged part [i:j] */ |
| 6491 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6492 | p += j - i; |
| 6493 | } |
| 6494 | /* copy substitution string */ |
| 6495 | if (str2->length > 0) { |
| 6496 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6497 | p += str2->length; |
| 6498 | } |
| 6499 | i = j + str1->length; |
| 6500 | } |
| 6501 | if (i < self->length) |
| 6502 | /* copy tail [i:] */ |
| 6503 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6504 | } else { |
| 6505 | /* interleave */ |
| 6506 | while (n > 0) { |
| 6507 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6508 | p += str2->length; |
| 6509 | if (--n <= 0) |
| 6510 | break; |
| 6511 | *p++ = self->str[i++]; |
| 6512 | } |
| 6513 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6514 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6515 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6516 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6517 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6518 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6519 | /* nothing to replace; return original string (when possible) */ |
| 6520 | if (PyUnicode_CheckExact(self)) { |
| 6521 | Py_INCREF(self); |
| 6522 | return (PyObject *) self; |
| 6523 | } |
| 6524 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6525 | } |
| 6526 | |
| 6527 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6529 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6530 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6531 | \n\ |
| 6532 | 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] | 6533 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6534 | |
| 6535 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6536 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6537 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6538 | return fixup(self, fixtitle); |
| 6539 | } |
| 6540 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6541 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6542 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | \n\ |
| 6544 | 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] | 6545 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6546 | |
| 6547 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6548 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6549 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6550 | return fixup(self, fixcapitalize); |
| 6551 | } |
| 6552 | |
| 6553 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6554 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6555 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | \n\ |
| 6557 | 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] | 6558 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6559 | |
| 6560 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6561 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6562 | { |
| 6563 | PyObject *list; |
| 6564 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6565 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | /* Split into words */ |
| 6568 | list = split(self, NULL, -1); |
| 6569 | if (!list) |
| 6570 | return NULL; |
| 6571 | |
| 6572 | /* Capitalize each word */ |
| 6573 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6574 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6575 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6576 | if (item == NULL) |
| 6577 | goto onError; |
| 6578 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6579 | PyList_SET_ITEM(list, i, item); |
| 6580 | } |
| 6581 | |
| 6582 | /* Join the words to form a new string */ |
| 6583 | item = PyUnicode_Join(NULL, list); |
| 6584 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6585 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | Py_DECREF(list); |
| 6587 | return (PyObject *)item; |
| 6588 | } |
| 6589 | #endif |
| 6590 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6591 | /* Argument converter. Coerces to a single unicode character */ |
| 6592 | |
| 6593 | static int |
| 6594 | convert_uc(PyObject *obj, void *addr) |
| 6595 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6596 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6597 | PyObject *uniobj; |
| 6598 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6599 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6600 | uniobj = PyUnicode_FromObject(obj); |
| 6601 | if (uniobj == NULL) { |
| 6602 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6603 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6604 | return 0; |
| 6605 | } |
| 6606 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6607 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6608 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6609 | Py_DECREF(uniobj); |
| 6610 | return 0; |
| 6611 | } |
| 6612 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6613 | *fillcharloc = unistr[0]; |
| 6614 | Py_DECREF(uniobj); |
| 6615 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6618 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6619 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6620 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6621 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6622 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6623 | |
| 6624 | static PyObject * |
| 6625 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6626 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6627 | Py_ssize_t marg, left; |
| 6628 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6629 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6630 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6631 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6632 | return NULL; |
| 6633 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6634 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6635 | Py_INCREF(self); |
| 6636 | return (PyObject*) self; |
| 6637 | } |
| 6638 | |
| 6639 | marg = width - self->length; |
| 6640 | left = marg / 2 + (marg & width & 1); |
| 6641 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6642 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6643 | } |
| 6644 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6645 | #if 0 |
| 6646 | |
| 6647 | /* This code should go into some future Unicode collation support |
| 6648 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6649 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6650 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6651 | /* speedy UTF-16 code point order comparison */ |
| 6652 | /* gleaned from: */ |
| 6653 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6654 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6655 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6656 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6657 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6658 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6659 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6660 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6661 | }; |
| 6662 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | static int |
| 6664 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6665 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6666 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6667 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | Py_UNICODE *s1 = str1->str; |
| 6669 | Py_UNICODE *s2 = str2->str; |
| 6670 | |
| 6671 | len1 = str1->length; |
| 6672 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6673 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6675 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6676 | |
| 6677 | c1 = *s1++; |
| 6678 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6679 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6680 | if (c1 > (1<<11) * 26) |
| 6681 | c1 += utf16Fixup[c1>>11]; |
| 6682 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6683 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6684 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6685 | |
| 6686 | if (c1 != c2) |
| 6687 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6688 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6689 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | } |
| 6691 | |
| 6692 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6693 | } |
| 6694 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6695 | #else |
| 6696 | |
| 6697 | static int |
| 6698 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6699 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6700 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6701 | |
| 6702 | Py_UNICODE *s1 = str1->str; |
| 6703 | Py_UNICODE *s2 = str2->str; |
| 6704 | |
| 6705 | len1 = str1->length; |
| 6706 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6707 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6708 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6709 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6710 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6711 | c1 = *s1++; |
| 6712 | c2 = *s2++; |
| 6713 | |
| 6714 | if (c1 != c2) |
| 6715 | return (c1 < c2) ? -1 : 1; |
| 6716 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6717 | len1--; len2--; |
| 6718 | } |
| 6719 | |
| 6720 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6721 | } |
| 6722 | |
| 6723 | #endif |
| 6724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6726 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6727 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6728 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6729 | return unicode_compare((PyUnicodeObject *)left, |
| 6730 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6731 | PyErr_Format(PyExc_TypeError, |
| 6732 | "Can't compare %.100s and %.100s", |
| 6733 | left->ob_type->tp_name, |
| 6734 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | return -1; |
| 6736 | } |
| 6737 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6738 | int |
| 6739 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6740 | { |
| 6741 | int i; |
| 6742 | Py_UNICODE *id; |
| 6743 | assert(PyUnicode_Check(uni)); |
| 6744 | id = PyUnicode_AS_UNICODE(uni); |
| 6745 | /* Compare Unicode string and source character set string */ |
| 6746 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6747 | if (id[i] != str[i]) |
| 6748 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 6749 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 6750 | to C strings identical up to that point. */ |
| 6751 | if (PyUnicode_GET_SIZE(uni) != i) |
| 6752 | /* We'll say the Python string is longer. */ |
| 6753 | return 1; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6754 | if (id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6755 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6756 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6757 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6758 | return 0; |
| 6759 | } |
| 6760 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6761 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6762 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6763 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6764 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6765 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6766 | PyObject *right, |
| 6767 | int op) |
| 6768 | { |
| 6769 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6770 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6771 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6772 | PyObject *v; |
| 6773 | if (((PyUnicodeObject *) left)->length != |
| 6774 | ((PyUnicodeObject *) right)->length) { |
| 6775 | if (op == Py_EQ) { |
| 6776 | Py_INCREF(Py_False); |
| 6777 | return Py_False; |
| 6778 | } |
| 6779 | if (op == Py_NE) { |
| 6780 | Py_INCREF(Py_True); |
| 6781 | return Py_True; |
| 6782 | } |
| 6783 | } |
| 6784 | if (left == right) |
| 6785 | result = 0; |
| 6786 | else |
| 6787 | result = unicode_compare((PyUnicodeObject *)left, |
| 6788 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6789 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6790 | /* Convert the return value to a Boolean */ |
| 6791 | switch (op) { |
| 6792 | case Py_EQ: |
| 6793 | v = TEST_COND(result == 0); |
| 6794 | break; |
| 6795 | case Py_NE: |
| 6796 | v = TEST_COND(result != 0); |
| 6797 | break; |
| 6798 | case Py_LE: |
| 6799 | v = TEST_COND(result <= 0); |
| 6800 | break; |
| 6801 | case Py_GE: |
| 6802 | v = TEST_COND(result >= 0); |
| 6803 | break; |
| 6804 | case Py_LT: |
| 6805 | v = TEST_COND(result == -1); |
| 6806 | break; |
| 6807 | case Py_GT: |
| 6808 | v = TEST_COND(result == 1); |
| 6809 | break; |
| 6810 | default: |
| 6811 | PyErr_BadArgument(); |
| 6812 | return NULL; |
| 6813 | } |
| 6814 | Py_INCREF(v); |
| 6815 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6816 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6817 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6818 | Py_INCREF(Py_NotImplemented); |
| 6819 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6822 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6823 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6824 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6825 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6826 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6827 | |
| 6828 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6829 | sub = PyUnicode_FromObject(element); |
| 6830 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6831 | PyErr_Format(PyExc_TypeError, |
| 6832 | "'in <string>' requires string as left operand, not %s", |
| 6833 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6834 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6837 | str = PyUnicode_FromObject(container); |
| 6838 | if (!str) { |
| 6839 | Py_DECREF(sub); |
| 6840 | return -1; |
| 6841 | } |
| 6842 | |
| 6843 | result = stringlib_contains_obj(str, sub); |
| 6844 | |
| 6845 | Py_DECREF(str); |
| 6846 | Py_DECREF(sub); |
| 6847 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6848 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6849 | } |
| 6850 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6852 | |
| 6853 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6854 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | { |
| 6856 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6857 | |
| 6858 | /* Coerce the two arguments */ |
| 6859 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6860 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6861 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6862 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6863 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6864 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6865 | |
| 6866 | /* Shortcuts */ |
| 6867 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6868 | Py_DECREF(v); |
| 6869 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | } |
| 6871 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6872 | Py_DECREF(u); |
| 6873 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6874 | } |
| 6875 | |
| 6876 | /* Concat the two Unicode strings */ |
| 6877 | w = _PyUnicode_New(u->length + v->length); |
| 6878 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6879 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6880 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6881 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6882 | |
| 6883 | Py_DECREF(u); |
| 6884 | Py_DECREF(v); |
| 6885 | return (PyObject *)w; |
| 6886 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6887 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6888 | Py_XDECREF(u); |
| 6889 | Py_XDECREF(v); |
| 6890 | return NULL; |
| 6891 | } |
| 6892 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6893 | void |
| 6894 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6895 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6896 | PyObject *new; |
| 6897 | if (*pleft == NULL) |
| 6898 | return; |
| 6899 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6900 | Py_DECREF(*pleft); |
| 6901 | *pleft = NULL; |
| 6902 | return; |
| 6903 | } |
| 6904 | new = PyUnicode_Concat(*pleft, right); |
| 6905 | Py_DECREF(*pleft); |
| 6906 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
| 6909 | void |
| 6910 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6911 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6912 | PyUnicode_Append(pleft, right); |
| 6913 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6914 | } |
| 6915 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6916 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6917 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6918 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6919 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6920 | 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] | 6921 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6922 | |
| 6923 | static PyObject * |
| 6924 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6925 | { |
| 6926 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6927 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6928 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6929 | PyObject *result; |
| 6930 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6931 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6932 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6933 | return NULL; |
| 6934 | |
| 6935 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6936 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6937 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6938 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6939 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6940 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6941 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6942 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6943 | substring->str, substring->length, |
| 6944 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6945 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | |
| 6947 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6948 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6949 | return result; |
| 6950 | } |
| 6951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6952 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6953 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6954 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6955 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6956 | 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] | 6957 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6958 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6959 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6960 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6961 | |
| 6962 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 6963 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6964 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 6965 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6966 | char *encoding = NULL; |
| 6967 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6968 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6969 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 6970 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 6971 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 6973 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6974 | if (v == NULL) |
| 6975 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6976 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6977 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6978 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6979 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6980 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6981 | Py_DECREF(v); |
| 6982 | return NULL; |
| 6983 | } |
| 6984 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6985 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6986 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6987 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6990 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6991 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6992 | \n\ |
| 6993 | 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] | 6994 | 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] | 6995 | |
| 6996 | static PyObject* |
| 6997 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6998 | { |
| 6999 | Py_UNICODE *e; |
| 7000 | Py_UNICODE *p; |
| 7001 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7002 | Py_UNICODE *qe; |
| 7003 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | PyUnicodeObject *u; |
| 7005 | int tabsize = 8; |
| 7006 | |
| 7007 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7008 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7010 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7011 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7012 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7013 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7014 | for (p = self->str; p < e; p++) |
| 7015 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7016 | if (tabsize > 0) { |
| 7017 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7018 | if (j > PY_SSIZE_T_MAX - incr) |
| 7019 | goto overflow1; |
| 7020 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7021 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7022 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7023 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7024 | if (j > PY_SSIZE_T_MAX - 1) |
| 7025 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7026 | j++; |
| 7027 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7028 | if (i > PY_SSIZE_T_MAX - j) |
| 7029 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7030 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7031 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7032 | } |
| 7033 | } |
| 7034 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7035 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7036 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7037 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | /* Second pass: create output string and fill it */ |
| 7039 | u = _PyUnicode_New(i + j); |
| 7040 | if (!u) |
| 7041 | return NULL; |
| 7042 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7043 | j = 0; /* same as in first pass */ |
| 7044 | q = u->str; /* next output char */ |
| 7045 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7046 | |
| 7047 | for (p = self->str; p < e; p++) |
| 7048 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7049 | if (tabsize > 0) { |
| 7050 | i = tabsize - (j % tabsize); |
| 7051 | j += i; |
| 7052 | while (i--) { |
| 7053 | if (q >= qe) |
| 7054 | goto overflow2; |
| 7055 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7056 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7057 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7058 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7059 | else { |
| 7060 | if (q >= qe) |
| 7061 | goto overflow2; |
| 7062 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7063 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 | if (*p == '\n' || *p == '\r') |
| 7065 | j = 0; |
| 7066 | } |
| 7067 | |
| 7068 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7069 | |
| 7070 | overflow2: |
| 7071 | Py_DECREF(u); |
| 7072 | overflow1: |
| 7073 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7074 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7075 | } |
| 7076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7077 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7078 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7079 | \n\ |
| 7080 | 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] | 7081 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7082 | arguments start and end are interpreted as in slice notation.\n\ |
| 7083 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7084 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | |
| 7086 | static PyObject * |
| 7087 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7088 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7089 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7090 | Py_ssize_t start; |
| 7091 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7092 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7093 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7094 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7095 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7096 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7097 | result = stringlib_find_slice( |
| 7098 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7099 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7100 | start, end |
| 7101 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7102 | |
| 7103 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7104 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7105 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
| 7108 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7109 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7110 | { |
| 7111 | if (index < 0 || index >= self->length) { |
| 7112 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7113 | return NULL; |
| 7114 | } |
| 7115 | |
| 7116 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7117 | } |
| 7118 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7119 | /* Believe it or not, this produces the same value for ASCII strings |
| 7120 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7121 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7122 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7123 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7124 | Py_ssize_t len; |
| 7125 | Py_UNICODE *p; |
| 7126 | long x; |
| 7127 | |
| 7128 | if (self->hash != -1) |
| 7129 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7130 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7131 | p = self->str; |
| 7132 | x = *p << 7; |
| 7133 | while (--len >= 0) |
| 7134 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7135 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7136 | if (x == -1) |
| 7137 | x = -2; |
| 7138 | self->hash = x; |
| 7139 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7140 | } |
| 7141 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7142 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7143 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7144 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7145 | 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] | 7146 | |
| 7147 | static PyObject * |
| 7148 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7149 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7150 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7151 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7152 | Py_ssize_t start; |
| 7153 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7155 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7156 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7157 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7158 | result = stringlib_find_slice( |
| 7159 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7160 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7161 | start, end |
| 7162 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7163 | |
| 7164 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7165 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7166 | if (result < 0) { |
| 7167 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7168 | return NULL; |
| 7169 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7170 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7171 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7172 | } |
| 7173 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7174 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7175 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7176 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7177 | 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] | 7178 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7179 | |
| 7180 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7181 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | { |
| 7183 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7184 | register const Py_UNICODE *e; |
| 7185 | int cased; |
| 7186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7187 | /* Shortcut for single character strings */ |
| 7188 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7189 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7190 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7191 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7192 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7193 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7194 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7195 | e = p + PyUnicode_GET_SIZE(self); |
| 7196 | cased = 0; |
| 7197 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7198 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7199 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7200 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7201 | return PyBool_FromLong(0); |
| 7202 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7203 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7204 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7205 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7206 | } |
| 7207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7208 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7209 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7210 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7211 | 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] | 7212 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7213 | |
| 7214 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7215 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7216 | { |
| 7217 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7218 | register const Py_UNICODE *e; |
| 7219 | int cased; |
| 7220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7221 | /* Shortcut for single character strings */ |
| 7222 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7223 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7224 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7225 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7226 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7227 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7228 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7229 | e = p + PyUnicode_GET_SIZE(self); |
| 7230 | cased = 0; |
| 7231 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7232 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7233 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7234 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7235 | return PyBool_FromLong(0); |
| 7236 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7237 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7238 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7239 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | } |
| 7241 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7242 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7243 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7244 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7245 | Return True if S is a titlecased string and there is at least one\n\ |
| 7246 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7247 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7248 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7249 | |
| 7250 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7251 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7252 | { |
| 7253 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7254 | register const Py_UNICODE *e; |
| 7255 | int cased, previous_is_cased; |
| 7256 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7257 | /* Shortcut for single character strings */ |
| 7258 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7259 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7260 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7261 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7262 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7263 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7264 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7266 | e = p + PyUnicode_GET_SIZE(self); |
| 7267 | cased = 0; |
| 7268 | previous_is_cased = 0; |
| 7269 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7270 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7271 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7272 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7273 | if (previous_is_cased) |
| 7274 | return PyBool_FromLong(0); |
| 7275 | previous_is_cased = 1; |
| 7276 | cased = 1; |
| 7277 | } |
| 7278 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7279 | if (!previous_is_cased) |
| 7280 | return PyBool_FromLong(0); |
| 7281 | previous_is_cased = 1; |
| 7282 | cased = 1; |
| 7283 | } |
| 7284 | else |
| 7285 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7286 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7287 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7288 | } |
| 7289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7290 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7291 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7292 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7293 | Return True if all characters in S are whitespace\n\ |
| 7294 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7295 | |
| 7296 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7297 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | { |
| 7299 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7300 | register const Py_UNICODE *e; |
| 7301 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7302 | /* Shortcut for single character strings */ |
| 7303 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7304 | Py_UNICODE_ISSPACE(*p)) |
| 7305 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7306 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7307 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7308 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7309 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7310 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7311 | e = p + PyUnicode_GET_SIZE(self); |
| 7312 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7313 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7314 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7315 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7316 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7317 | } |
| 7318 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7319 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7320 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7321 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7322 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7323 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7324 | |
| 7325 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7326 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7327 | { |
| 7328 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7329 | register const Py_UNICODE *e; |
| 7330 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7331 | /* Shortcut for single character strings */ |
| 7332 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7333 | Py_UNICODE_ISALPHA(*p)) |
| 7334 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7335 | |
| 7336 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7337 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7338 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7339 | |
| 7340 | e = p + PyUnicode_GET_SIZE(self); |
| 7341 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7342 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7343 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7344 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7345 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7346 | } |
| 7347 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7348 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7349 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7350 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7351 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7352 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7353 | |
| 7354 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7355 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7356 | { |
| 7357 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7358 | register const Py_UNICODE *e; |
| 7359 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7360 | /* Shortcut for single character strings */ |
| 7361 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7362 | Py_UNICODE_ISALNUM(*p)) |
| 7363 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7364 | |
| 7365 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7366 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7367 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7368 | |
| 7369 | e = p + PyUnicode_GET_SIZE(self); |
| 7370 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7371 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7372 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7373 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7374 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7377 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7378 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7379 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7380 | 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] | 7381 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7382 | |
| 7383 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7384 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7385 | { |
| 7386 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7387 | register const Py_UNICODE *e; |
| 7388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7389 | /* Shortcut for single character strings */ |
| 7390 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7391 | Py_UNICODE_ISDECIMAL(*p)) |
| 7392 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7393 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7394 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7395 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7396 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7397 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 | e = p + PyUnicode_GET_SIZE(self); |
| 7399 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7400 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7401 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7402 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7403 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7404 | } |
| 7405 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7406 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7407 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7408 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7409 | Return True if all characters in S are digits\n\ |
| 7410 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | |
| 7412 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7413 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7414 | { |
| 7415 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7416 | register const Py_UNICODE *e; |
| 7417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7418 | /* Shortcut for single character strings */ |
| 7419 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7420 | Py_UNICODE_ISDIGIT(*p)) |
| 7421 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7422 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7423 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7424 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7425 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7426 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7427 | e = p + PyUnicode_GET_SIZE(self); |
| 7428 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7429 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7430 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7431 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7432 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7433 | } |
| 7434 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7435 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7436 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7438 | 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] | 7439 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | |
| 7441 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7442 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7443 | { |
| 7444 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7445 | register const Py_UNICODE *e; |
| 7446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7447 | /* Shortcut for single character strings */ |
| 7448 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7449 | Py_UNICODE_ISNUMERIC(*p)) |
| 7450 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7451 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7452 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7453 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7454 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7455 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 | e = p + PyUnicode_GET_SIZE(self); |
| 7457 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7458 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7459 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7460 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7461 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7462 | } |
| 7463 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7464 | int |
| 7465 | PyUnicode_IsIdentifier(PyObject *self) |
| 7466 | { |
| 7467 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7468 | register const Py_UNICODE *e; |
| 7469 | |
| 7470 | /* Special case for empty strings */ |
| 7471 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7472 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7473 | |
| 7474 | /* PEP 3131 says that the first character must be in |
| 7475 | XID_Start and subsequent characters in XID_Continue, |
| 7476 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7477 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7478 | letters, digits, underscore). However, given the current |
| 7479 | definition of XID_Start and XID_Continue, it is sufficient |
| 7480 | to check just for these, except that _ must be allowed |
| 7481 | as starting an identifier. */ |
| 7482 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7483 | return 0; |
| 7484 | |
| 7485 | e = p + PyUnicode_GET_SIZE(self); |
| 7486 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7487 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7488 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7489 | } |
| 7490 | return 1; |
| 7491 | } |
| 7492 | |
| 7493 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7494 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7495 | \n\ |
| 7496 | Return True if S is a valid identifier according\n\ |
| 7497 | to the language definition."); |
| 7498 | |
| 7499 | static PyObject* |
| 7500 | unicode_isidentifier(PyObject *self) |
| 7501 | { |
| 7502 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7503 | } |
| 7504 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7505 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7506 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7507 | \n\ |
| 7508 | Return True if all characters in S are considered\n\ |
| 7509 | printable in repr() or S is empty, False otherwise."); |
| 7510 | |
| 7511 | static PyObject* |
| 7512 | unicode_isprintable(PyObject *self) |
| 7513 | { |
| 7514 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7515 | register const Py_UNICODE *e; |
| 7516 | |
| 7517 | /* Shortcut for single character strings */ |
| 7518 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7519 | Py_RETURN_TRUE; |
| 7520 | } |
| 7521 | |
| 7522 | e = p + PyUnicode_GET_SIZE(self); |
| 7523 | for (; p < e; p++) { |
| 7524 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7525 | Py_RETURN_FALSE; |
| 7526 | } |
| 7527 | } |
| 7528 | Py_RETURN_TRUE; |
| 7529 | } |
| 7530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7531 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7532 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7533 | \n\ |
| 7534 | Return a string which is the concatenation of the strings in the\n\ |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7535 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7536 | |
| 7537 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7538 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7539 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7540 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7541 | } |
| 7542 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7543 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7544 | unicode_length(PyUnicodeObject *self) |
| 7545 | { |
| 7546 | return self->length; |
| 7547 | } |
| 7548 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7549 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7550 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7551 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7552 | 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] | 7553 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7554 | |
| 7555 | static PyObject * |
| 7556 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7557 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7558 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7559 | Py_UNICODE fillchar = ' '; |
| 7560 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7561 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7562 | return NULL; |
| 7563 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7564 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7565 | Py_INCREF(self); |
| 7566 | return (PyObject*) self; |
| 7567 | } |
| 7568 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7569 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7570 | } |
| 7571 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7572 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7573 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7574 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7575 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7576 | |
| 7577 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7578 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7579 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7580 | return fixup(self, fixlower); |
| 7581 | } |
| 7582 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7583 | #define LEFTSTRIP 0 |
| 7584 | #define RIGHTSTRIP 1 |
| 7585 | #define BOTHSTRIP 2 |
| 7586 | |
| 7587 | /* Arrays indexed by above */ |
| 7588 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7589 | |
| 7590 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7591 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7592 | /* externally visible for str.strip(unicode) */ |
| 7593 | PyObject * |
| 7594 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7595 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7596 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7597 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7598 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7599 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7600 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7601 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7602 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7603 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7604 | i = 0; |
| 7605 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7606 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7607 | i++; |
| 7608 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7609 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7610 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7611 | j = len; |
| 7612 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7613 | do { |
| 7614 | j--; |
| 7615 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7616 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7617 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7618 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7619 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7620 | Py_INCREF(self); |
| 7621 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7622 | } |
| 7623 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7624 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7625 | } |
| 7626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7627 | |
| 7628 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7629 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7630 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7631 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7632 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7633 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7634 | i = 0; |
| 7635 | if (striptype != RIGHTSTRIP) { |
| 7636 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7637 | i++; |
| 7638 | } |
| 7639 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7640 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7641 | j = len; |
| 7642 | if (striptype != LEFTSTRIP) { |
| 7643 | do { |
| 7644 | j--; |
| 7645 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7646 | j++; |
| 7647 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7648 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7649 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7650 | Py_INCREF(self); |
| 7651 | return (PyObject*)self; |
| 7652 | } |
| 7653 | else |
| 7654 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7655 | } |
| 7656 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7657 | |
| 7658 | static PyObject * |
| 7659 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7660 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7661 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7662 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7663 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7664 | return 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 (sep != NULL && sep != Py_None) { |
| 7667 | if (PyUnicode_Check(sep)) |
| 7668 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7669 | else { |
| 7670 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7671 | "%s arg must be None or str", |
| 7672 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7673 | return NULL; |
| 7674 | } |
| 7675 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7676 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7677 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7678 | } |
| 7679 | |
| 7680 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7681 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7682 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7683 | \n\ |
| 7684 | Return a copy of the string S with leading and trailing\n\ |
| 7685 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7686 | 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] | 7687 | |
| 7688 | static PyObject * |
| 7689 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7690 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7691 | if (PyTuple_GET_SIZE(args) == 0) |
| 7692 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7693 | else |
| 7694 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7695 | } |
| 7696 | |
| 7697 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7698 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7699 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7700 | \n\ |
| 7701 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7702 | 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] | 7703 | |
| 7704 | static PyObject * |
| 7705 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7706 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7707 | if (PyTuple_GET_SIZE(args) == 0) |
| 7708 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7709 | else |
| 7710 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7711 | } |
| 7712 | |
| 7713 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7714 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7715 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7716 | \n\ |
| 7717 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7718 | 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] | 7719 | |
| 7720 | static PyObject * |
| 7721 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7722 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7723 | if (PyTuple_GET_SIZE(args) == 0) |
| 7724 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7725 | else |
| 7726 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7727 | } |
| 7728 | |
| 7729 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7730 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7731 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7732 | { |
| 7733 | PyUnicodeObject *u; |
| 7734 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7735 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7736 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7737 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7738 | if (len < 1) { |
| 7739 | Py_INCREF(unicode_empty); |
| 7740 | return (PyObject *)unicode_empty; |
| 7741 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7742 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7743 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7744 | /* no repeat, return original string */ |
| 7745 | Py_INCREF(str); |
| 7746 | return (PyObject*) str; |
| 7747 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7748 | |
| 7749 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7750 | * needed doesn't overflow size_t |
| 7751 | */ |
| 7752 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7753 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7754 | PyErr_SetString(PyExc_OverflowError, |
| 7755 | "repeated string is too long"); |
| 7756 | return NULL; |
| 7757 | } |
| 7758 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7759 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7760 | PyErr_SetString(PyExc_OverflowError, |
| 7761 | "repeated string is too long"); |
| 7762 | return NULL; |
| 7763 | } |
| 7764 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7765 | if (!u) |
| 7766 | return NULL; |
| 7767 | |
| 7768 | p = u->str; |
| 7769 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7770 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7771 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7772 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7773 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7774 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7775 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7776 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7777 | Py_UNICODE_COPY(p+done, p, n); |
| 7778 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7779 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 | } |
| 7781 | |
| 7782 | return (PyObject*) u; |
| 7783 | } |
| 7784 | |
| 7785 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7786 | PyObject *subobj, |
| 7787 | PyObject *replobj, |
| 7788 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | { |
| 7790 | PyObject *self; |
| 7791 | PyObject *str1; |
| 7792 | PyObject *str2; |
| 7793 | PyObject *result; |
| 7794 | |
| 7795 | self = PyUnicode_FromObject(obj); |
| 7796 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7797 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7798 | str1 = PyUnicode_FromObject(subobj); |
| 7799 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7800 | Py_DECREF(self); |
| 7801 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7802 | } |
| 7803 | str2 = PyUnicode_FromObject(replobj); |
| 7804 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7805 | Py_DECREF(self); |
| 7806 | Py_DECREF(str1); |
| 7807 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7809 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7810 | (PyUnicodeObject *)str1, |
| 7811 | (PyUnicodeObject *)str2, |
| 7812 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7813 | Py_DECREF(self); |
| 7814 | Py_DECREF(str1); |
| 7815 | Py_DECREF(str2); |
| 7816 | return result; |
| 7817 | } |
| 7818 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7819 | PyDoc_STRVAR(replace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7820 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | \n\ |
| 7822 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7823 | old replaced by new. If the optional argument count is\n\ |
| 7824 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7825 | |
| 7826 | static PyObject* |
| 7827 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7828 | { |
| 7829 | PyUnicodeObject *str1; |
| 7830 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7831 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | PyObject *result; |
| 7833 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7834 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7835 | return NULL; |
| 7836 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7837 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7838 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7840 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | Py_DECREF(str1); |
| 7842 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7843 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | |
| 7845 | result = replace(self, str1, str2, maxcount); |
| 7846 | |
| 7847 | Py_DECREF(str1); |
| 7848 | Py_DECREF(str2); |
| 7849 | return result; |
| 7850 | } |
| 7851 | |
| 7852 | static |
| 7853 | PyObject *unicode_repr(PyObject *unicode) |
| 7854 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7855 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7856 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7857 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7858 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7859 | |
| 7860 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7861 | better to choose a different scheme. Perhaps scan the |
| 7862 | first N-chars of the string and allocate based on that size. |
| 7863 | */ |
| 7864 | /* Initial allocation is based on the longest-possible unichr |
| 7865 | escape. |
| 7866 | |
| 7867 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7868 | unichr, so in this case it's the longest unichr escape. In |
| 7869 | narrow (UTF-16) builds this is five chars per source unichr |
| 7870 | since there are two unichrs in the surrogate pair, so in narrow |
| 7871 | (UTF-16) builds it's not the longest unichr escape. |
| 7872 | |
| 7873 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7874 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7875 | escape. |
| 7876 | */ |
| 7877 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7878 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7879 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7880 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7881 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7882 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7883 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7884 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7885 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7886 | if (repr == NULL) |
| 7887 | return NULL; |
| 7888 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7889 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7890 | |
| 7891 | /* Add quote */ |
| 7892 | *p++ = (findchar(s, size, '\'') && |
| 7893 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7894 | while (size-- > 0) { |
| 7895 | Py_UNICODE ch = *s++; |
| 7896 | |
| 7897 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7898 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7899 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7900 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7901 | continue; |
| 7902 | } |
| 7903 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7904 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7905 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7906 | *p++ = '\\'; |
| 7907 | *p++ = 't'; |
| 7908 | } |
| 7909 | else if (ch == '\n') { |
| 7910 | *p++ = '\\'; |
| 7911 | *p++ = 'n'; |
| 7912 | } |
| 7913 | else if (ch == '\r') { |
| 7914 | *p++ = '\\'; |
| 7915 | *p++ = 'r'; |
| 7916 | } |
| 7917 | |
| 7918 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7919 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7920 | *p++ = '\\'; |
| 7921 | *p++ = 'x'; |
| 7922 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7923 | *p++ = hexdigits[ch & 0x000F]; |
| 7924 | } |
| 7925 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7926 | /* Copy ASCII characters as-is */ |
| 7927 | else if (ch < 0x7F) { |
| 7928 | *p++ = ch; |
| 7929 | } |
| 7930 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7931 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7932 | else { |
| 7933 | Py_UCS4 ucs = ch; |
| 7934 | |
| 7935 | #ifndef Py_UNICODE_WIDE |
| 7936 | Py_UNICODE ch2 = 0; |
| 7937 | /* Get code point from surrogate pair */ |
| 7938 | if (size > 0) { |
| 7939 | ch2 = *s; |
| 7940 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7941 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7942 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7943 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7944 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7945 | size--; |
| 7946 | } |
| 7947 | } |
| 7948 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7949 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7950 | (categories Z* and C* except ASCII space) |
| 7951 | */ |
| 7952 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 7953 | /* Map 8-bit characters to '\xhh' */ |
| 7954 | if (ucs <= 0xff) { |
| 7955 | *p++ = '\\'; |
| 7956 | *p++ = 'x'; |
| 7957 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7958 | *p++ = hexdigits[ch & 0x000F]; |
| 7959 | } |
| 7960 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7961 | else if (ucs >= 0x10000) { |
| 7962 | *p++ = '\\'; |
| 7963 | *p++ = 'U'; |
| 7964 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7965 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7966 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7967 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7968 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7969 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7970 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7971 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7972 | } |
| 7973 | /* Map 16-bit characters to '\uxxxx' */ |
| 7974 | else { |
| 7975 | *p++ = '\\'; |
| 7976 | *p++ = 'u'; |
| 7977 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 7978 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 7979 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 7980 | *p++ = hexdigits[ucs & 0x000F]; |
| 7981 | } |
| 7982 | } |
| 7983 | /* Copy characters as-is */ |
| 7984 | else { |
| 7985 | *p++ = ch; |
| 7986 | #ifndef Py_UNICODE_WIDE |
| 7987 | if (ucs >= 0x10000) |
| 7988 | *p++ = ch2; |
| 7989 | #endif |
| 7990 | } |
| 7991 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7992 | } |
| 7993 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7994 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7995 | |
| 7996 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 7997 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7998 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | } |
| 8000 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8001 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8002 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8003 | \n\ |
| 8004 | 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] | 8005 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8006 | arguments start and end are interpreted as in slice notation.\n\ |
| 8007 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8008 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8009 | |
| 8010 | static PyObject * |
| 8011 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8012 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8013 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8014 | Py_ssize_t start; |
| 8015 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8016 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8017 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8018 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8019 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8020 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8021 | result = stringlib_rfind_slice( |
| 8022 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8023 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8024 | start, end |
| 8025 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8026 | |
| 8027 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8028 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8029 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8030 | } |
| 8031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8032 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8033 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8034 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8035 | 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] | 8036 | |
| 8037 | static PyObject * |
| 8038 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8039 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8040 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8041 | Py_ssize_t start; |
| 8042 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8043 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8044 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8045 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8046 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8047 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8048 | result = stringlib_rfind_slice( |
| 8049 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8050 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8051 | start, end |
| 8052 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | |
| 8054 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8056 | if (result < 0) { |
| 8057 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8058 | return NULL; |
| 8059 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8060 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8061 | } |
| 8062 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8063 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8064 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8065 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8066 | 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] | 8067 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8068 | |
| 8069 | static PyObject * |
| 8070 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8071 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8072 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8073 | Py_UNICODE fillchar = ' '; |
| 8074 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8075 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8076 | return NULL; |
| 8077 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8078 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8079 | Py_INCREF(self); |
| 8080 | return (PyObject*) self; |
| 8081 | } |
| 8082 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8083 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8084 | } |
| 8085 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8086 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | PyObject *sep, |
| 8088 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | { |
| 8090 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8091 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8092 | s = PyUnicode_FromObject(s); |
| 8093 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8094 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | if (sep != NULL) { |
| 8096 | sep = PyUnicode_FromObject(sep); |
| 8097 | if (sep == NULL) { |
| 8098 | Py_DECREF(s); |
| 8099 | return NULL; |
| 8100 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8101 | } |
| 8102 | |
| 8103 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8104 | |
| 8105 | Py_DECREF(s); |
| 8106 | Py_XDECREF(sep); |
| 8107 | return result; |
| 8108 | } |
| 8109 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8110 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8111 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8112 | \n\ |
| 8113 | Return a list of the words in S, using sep as the\n\ |
| 8114 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8115 | 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] | 8116 | whitespace string is a separator and empty strings are\n\ |
| 8117 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8118 | |
| 8119 | static PyObject* |
| 8120 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8121 | { |
| 8122 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8123 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8124 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8125 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | return NULL; |
| 8127 | |
| 8128 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8129 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8130 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8131 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8132 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8133 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8134 | } |
| 8135 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8136 | PyObject * |
| 8137 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8138 | { |
| 8139 | PyObject* str_obj; |
| 8140 | PyObject* sep_obj; |
| 8141 | PyObject* out; |
| 8142 | |
| 8143 | str_obj = PyUnicode_FromObject(str_in); |
| 8144 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8145 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8146 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8147 | if (!sep_obj) { |
| 8148 | Py_DECREF(str_obj); |
| 8149 | return NULL; |
| 8150 | } |
| 8151 | |
| 8152 | out = stringlib_partition( |
| 8153 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8154 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8155 | ); |
| 8156 | |
| 8157 | Py_DECREF(sep_obj); |
| 8158 | Py_DECREF(str_obj); |
| 8159 | |
| 8160 | return out; |
| 8161 | } |
| 8162 | |
| 8163 | |
| 8164 | PyObject * |
| 8165 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8166 | { |
| 8167 | PyObject* str_obj; |
| 8168 | PyObject* sep_obj; |
| 8169 | PyObject* out; |
| 8170 | |
| 8171 | str_obj = PyUnicode_FromObject(str_in); |
| 8172 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8173 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8174 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8175 | if (!sep_obj) { |
| 8176 | Py_DECREF(str_obj); |
| 8177 | return NULL; |
| 8178 | } |
| 8179 | |
| 8180 | out = stringlib_rpartition( |
| 8181 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8182 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8183 | ); |
| 8184 | |
| 8185 | Py_DECREF(sep_obj); |
| 8186 | Py_DECREF(str_obj); |
| 8187 | |
| 8188 | return out; |
| 8189 | } |
| 8190 | |
| 8191 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8192 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8193 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8194 | 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] | 8195 | 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] | 8196 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8197 | |
| 8198 | static PyObject* |
| 8199 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8200 | { |
| 8201 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8202 | } |
| 8203 | |
| 8204 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8205 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8206 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8207 | 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] | 8208 | 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] | 8209 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8210 | |
| 8211 | static PyObject* |
| 8212 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8213 | { |
| 8214 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8215 | } |
| 8216 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8217 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8218 | PyObject *sep, |
| 8219 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8220 | { |
| 8221 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8222 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8223 | s = PyUnicode_FromObject(s); |
| 8224 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8225 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8226 | if (sep != NULL) { |
| 8227 | sep = PyUnicode_FromObject(sep); |
| 8228 | if (sep == NULL) { |
| 8229 | Py_DECREF(s); |
| 8230 | return NULL; |
| 8231 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8232 | } |
| 8233 | |
| 8234 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8235 | |
| 8236 | Py_DECREF(s); |
| 8237 | Py_XDECREF(sep); |
| 8238 | return result; |
| 8239 | } |
| 8240 | |
| 8241 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8242 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8243 | \n\ |
| 8244 | Return a list of the words in S, using sep as the\n\ |
| 8245 | delimiter string, starting at the end of the string and\n\ |
| 8246 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8247 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8248 | is a separator."); |
| 8249 | |
| 8250 | static PyObject* |
| 8251 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8252 | { |
| 8253 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8254 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8255 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8256 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8257 | return NULL; |
| 8258 | |
| 8259 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8260 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8261 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8262 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8263 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8264 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8265 | } |
| 8266 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8267 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8268 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8269 | \n\ |
| 8270 | 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] | 8271 | 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] | 8272 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8273 | |
| 8274 | static PyObject* |
| 8275 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8276 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8277 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8278 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8279 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8280 | return NULL; |
| 8281 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8282 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8283 | } |
| 8284 | |
| 8285 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8286 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8287 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8288 | if (PyUnicode_CheckExact(self)) { |
| 8289 | Py_INCREF(self); |
| 8290 | return self; |
| 8291 | } else |
| 8292 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8293 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8294 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8295 | } |
| 8296 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8297 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8298 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8299 | \n\ |
| 8300 | 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] | 8301 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8302 | |
| 8303 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8304 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8305 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8306 | return fixup(self, fixswapcase); |
| 8307 | } |
| 8308 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8309 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8310 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8311 | \n\ |
| 8312 | Return a translation table usable for str.translate().\n\ |
| 8313 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8314 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8315 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8316 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8317 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8318 | character at the same position in y. If there is a third argument, it\n\ |
| 8319 | must be a string, whose characters will be mapped to None in the result."); |
| 8320 | |
| 8321 | static PyObject* |
| 8322 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8323 | { |
| 8324 | PyObject *x, *y = NULL, *z = NULL; |
| 8325 | PyObject *new = NULL, *key, *value; |
| 8326 | Py_ssize_t i = 0; |
| 8327 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8328 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8329 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8330 | return NULL; |
| 8331 | new = PyDict_New(); |
| 8332 | if (!new) |
| 8333 | return NULL; |
| 8334 | if (y != NULL) { |
| 8335 | /* x must be a string too, of equal length */ |
| 8336 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8337 | if (!PyUnicode_Check(x)) { |
| 8338 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8339 | "be a string if there is a second argument"); |
| 8340 | goto err; |
| 8341 | } |
| 8342 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8343 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8344 | "arguments must have equal length"); |
| 8345 | goto err; |
| 8346 | } |
| 8347 | /* create entries for translating chars in x to those in y */ |
| 8348 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8349 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8350 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8351 | if (!key || !value) |
| 8352 | goto err; |
| 8353 | res = PyDict_SetItem(new, key, value); |
| 8354 | Py_DECREF(key); |
| 8355 | Py_DECREF(value); |
| 8356 | if (res < 0) |
| 8357 | goto err; |
| 8358 | } |
| 8359 | /* create entries for deleting chars in z */ |
| 8360 | if (z != NULL) { |
| 8361 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8362 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8363 | if (!key) |
| 8364 | goto err; |
| 8365 | res = PyDict_SetItem(new, key, Py_None); |
| 8366 | Py_DECREF(key); |
| 8367 | if (res < 0) |
| 8368 | goto err; |
| 8369 | } |
| 8370 | } |
| 8371 | } else { |
| 8372 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8373 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8374 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8375 | "to maketrans it must be a dict"); |
| 8376 | goto err; |
| 8377 | } |
| 8378 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8379 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8380 | if (PyUnicode_Check(key)) { |
| 8381 | /* convert string keys to integer keys */ |
| 8382 | PyObject *newkey; |
| 8383 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8384 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8385 | "table must be of length 1"); |
| 8386 | goto err; |
| 8387 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8388 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8389 | if (!newkey) |
| 8390 | goto err; |
| 8391 | res = PyDict_SetItem(new, newkey, value); |
| 8392 | Py_DECREF(newkey); |
| 8393 | if (res < 0) |
| 8394 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8395 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8396 | /* just keep integer keys */ |
| 8397 | if (PyDict_SetItem(new, key, value) < 0) |
| 8398 | goto err; |
| 8399 | } else { |
| 8400 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8401 | "be strings or integers"); |
| 8402 | goto err; |
| 8403 | } |
| 8404 | } |
| 8405 | } |
| 8406 | return new; |
| 8407 | err: |
| 8408 | Py_DECREF(new); |
| 8409 | return NULL; |
| 8410 | } |
| 8411 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8412 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8413 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8414 | \n\ |
| 8415 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8416 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8417 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8418 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8419 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8420 | |
| 8421 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8422 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8424 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8425 | } |
| 8426 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8427 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8428 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8429 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8430 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8431 | |
| 8432 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8433 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8434 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8435 | return fixup(self, fixupper); |
| 8436 | } |
| 8437 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8438 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8439 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8440 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8441 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8442 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8443 | |
| 8444 | static PyObject * |
| 8445 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8446 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8447 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8448 | PyUnicodeObject *u; |
| 8449 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8450 | Py_ssize_t width; |
| 8451 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8452 | return NULL; |
| 8453 | |
| 8454 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8455 | if (PyUnicode_CheckExact(self)) { |
| 8456 | Py_INCREF(self); |
| 8457 | return (PyObject*) self; |
| 8458 | } |
| 8459 | else |
| 8460 | return PyUnicode_FromUnicode( |
| 8461 | PyUnicode_AS_UNICODE(self), |
| 8462 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8464 | } |
| 8465 | |
| 8466 | fill = width - self->length; |
| 8467 | |
| 8468 | u = pad(self, fill, 0, '0'); |
| 8469 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8470 | if (u == NULL) |
| 8471 | return NULL; |
| 8472 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8473 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8474 | /* move sign to beginning of string */ |
| 8475 | u->str[0] = u->str[fill]; |
| 8476 | u->str[fill] = '0'; |
| 8477 | } |
| 8478 | |
| 8479 | return (PyObject*) u; |
| 8480 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8481 | |
| 8482 | #if 0 |
| 8483 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8484 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8485 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8486 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8487 | } |
| 8488 | #endif |
| 8489 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8490 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8491 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8492 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8493 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8494 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8495 | With optional end, stop comparing S at that position.\n\ |
| 8496 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8497 | |
| 8498 | static PyObject * |
| 8499 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8501 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8502 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8503 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8504 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8505 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8506 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8507 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8508 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8509 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8510 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8511 | if (PyTuple_Check(subobj)) { |
| 8512 | Py_ssize_t i; |
| 8513 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8514 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8515 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8516 | if (substring == NULL) |
| 8517 | return NULL; |
| 8518 | result = tailmatch(self, substring, start, end, -1); |
| 8519 | Py_DECREF(substring); |
| 8520 | if (result) { |
| 8521 | Py_RETURN_TRUE; |
| 8522 | } |
| 8523 | } |
| 8524 | /* nothing matched */ |
| 8525 | Py_RETURN_FALSE; |
| 8526 | } |
| 8527 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8528 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8530 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8531 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8532 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8533 | } |
| 8534 | |
| 8535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8536 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8537 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8538 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8539 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8540 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8541 | With optional end, stop comparing S at that position.\n\ |
| 8542 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | |
| 8544 | static PyObject * |
| 8545 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8546 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8547 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8548 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8549 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8550 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8551 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8552 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8553 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8554 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8555 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8556 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8557 | if (PyTuple_Check(subobj)) { |
| 8558 | Py_ssize_t i; |
| 8559 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8560 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8561 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8562 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8563 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8564 | result = tailmatch(self, substring, start, end, +1); |
| 8565 | Py_DECREF(substring); |
| 8566 | if (result) { |
| 8567 | Py_RETURN_TRUE; |
| 8568 | } |
| 8569 | } |
| 8570 | Py_RETURN_FALSE; |
| 8571 | } |
| 8572 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8573 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8574 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8575 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8576 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8577 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8578 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8579 | } |
| 8580 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8581 | #include "stringlib/string_format.h" |
| 8582 | |
| 8583 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8584 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8585 | \n\ |
| 8586 | "); |
| 8587 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8588 | static PyObject * |
| 8589 | unicode__format__(PyObject* self, PyObject* args) |
| 8590 | { |
| 8591 | PyObject *format_spec; |
| 8592 | |
| 8593 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8594 | return NULL; |
| 8595 | |
| 8596 | return _PyUnicode_FormatAdvanced(self, |
| 8597 | PyUnicode_AS_UNICODE(format_spec), |
| 8598 | PyUnicode_GET_SIZE(format_spec)); |
| 8599 | } |
| 8600 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8601 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8602 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8603 | \n\ |
| 8604 | "); |
| 8605 | |
| 8606 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8607 | unicode__sizeof__(PyUnicodeObject *v) |
| 8608 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8609 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8610 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8611 | } |
| 8612 | |
| 8613 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8614 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8615 | |
| 8616 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8617 | unicode_getnewargs(PyUnicodeObject *v) |
| 8618 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8619 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8620 | } |
| 8621 | |
| 8622 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8623 | static PyMethodDef unicode_methods[] = { |
| 8624 | |
| 8625 | /* Order is according to common usage: often used methods should |
| 8626 | appear first, since lookup is done sequentially. */ |
| 8627 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8628 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8629 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8630 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8631 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8632 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8633 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8634 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8635 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8636 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8637 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8638 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8639 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8640 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8641 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8642 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8643 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8644 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8645 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8646 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8647 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8648 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8649 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8650 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8651 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8652 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8653 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8654 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8655 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8656 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8657 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8658 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8659 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8660 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8661 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8662 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8663 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8664 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8665 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8666 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8667 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8668 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8669 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8670 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8671 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8672 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8673 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8674 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8675 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8676 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8677 | #endif |
| 8678 | |
| 8679 | #if 0 |
| 8680 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8681 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8682 | #endif |
| 8683 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8684 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8685 | {NULL, NULL} |
| 8686 | }; |
| 8687 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8688 | static PyObject * |
| 8689 | unicode_mod(PyObject *v, PyObject *w) |
| 8690 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8691 | if (!PyUnicode_Check(v)) { |
| 8692 | Py_INCREF(Py_NotImplemented); |
| 8693 | return Py_NotImplemented; |
| 8694 | } |
| 8695 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8696 | } |
| 8697 | |
| 8698 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8699 | 0, /*nb_add*/ |
| 8700 | 0, /*nb_subtract*/ |
| 8701 | 0, /*nb_multiply*/ |
| 8702 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8703 | }; |
| 8704 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8705 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8706 | (lenfunc) unicode_length, /* sq_length */ |
| 8707 | PyUnicode_Concat, /* sq_concat */ |
| 8708 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8709 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8710 | 0, /* sq_slice */ |
| 8711 | 0, /* sq_ass_item */ |
| 8712 | 0, /* sq_ass_slice */ |
| 8713 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8714 | }; |
| 8715 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8716 | static PyObject* |
| 8717 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8718 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8719 | if (PyIndex_Check(item)) { |
| 8720 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8721 | if (i == -1 && PyErr_Occurred()) |
| 8722 | return NULL; |
| 8723 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8724 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8725 | return unicode_getitem(self, i); |
| 8726 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8727 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8728 | Py_UNICODE* source_buf; |
| 8729 | Py_UNICODE* result_buf; |
| 8730 | PyObject* result; |
| 8731 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8732 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8733 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8734 | return NULL; |
| 8735 | } |
| 8736 | |
| 8737 | if (slicelength <= 0) { |
| 8738 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8739 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8740 | PyUnicode_CheckExact(self)) { |
| 8741 | Py_INCREF(self); |
| 8742 | return (PyObject *)self; |
| 8743 | } else if (step == 1) { |
| 8744 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8745 | } else { |
| 8746 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8747 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8748 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8749 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8750 | if (result_buf == NULL) |
| 8751 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8752 | |
| 8753 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8754 | result_buf[i] = source_buf[cur]; |
| 8755 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8756 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8757 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8758 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8759 | return result; |
| 8760 | } |
| 8761 | } else { |
| 8762 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8763 | return NULL; |
| 8764 | } |
| 8765 | } |
| 8766 | |
| 8767 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8768 | (lenfunc)unicode_length, /* mp_length */ |
| 8769 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8770 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8771 | }; |
| 8772 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8774 | /* Helpers for PyUnicode_Format() */ |
| 8775 | |
| 8776 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8777 | 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] | 8778 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8779 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8780 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8781 | (*p_argidx)++; |
| 8782 | if (arglen < 0) |
| 8783 | return args; |
| 8784 | else |
| 8785 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8786 | } |
| 8787 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8788 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | return NULL; |
| 8790 | } |
| 8791 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8792 | /* Returns a new reference to a PyUnicode object, or NULL on failure. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8793 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8794 | static PyObject * |
| 8795 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8796 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8797 | char *p; |
| 8798 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8799 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8801 | x = PyFloat_AsDouble(v); |
| 8802 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8803 | return NULL; |
| 8804 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8805 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8806 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8807 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8808 | p = PyOS_double_to_string(x, type, prec, |
| 8809 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8810 | if (p == NULL) |
| 8811 | return NULL; |
| 8812 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8813 | PyMem_Free(p); |
| 8814 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8815 | } |
| 8816 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8817 | static PyObject* |
| 8818 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8819 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8820 | char *buf; |
| 8821 | int len; |
| 8822 | PyObject *str; /* temporary string object. */ |
| 8823 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8824 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8825 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 8826 | if (!str) |
| 8827 | return NULL; |
| 8828 | result = PyUnicode_FromStringAndSize(buf, len); |
| 8829 | Py_DECREF(str); |
| 8830 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8831 | } |
| 8832 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8833 | static int |
| 8834 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8835 | size_t buflen, |
| 8836 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8837 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8838 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8839 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8840 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 8841 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 8842 | buf[1] = '\0'; |
| 8843 | return 1; |
| 8844 | } |
| 8845 | #ifndef Py_UNICODE_WIDE |
| 8846 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 8847 | /* Decode a valid surrogate pair */ |
| 8848 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 8849 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 8850 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 8851 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 8852 | buf[0] = c0; |
| 8853 | buf[1] = c1; |
| 8854 | buf[2] = '\0'; |
| 8855 | return 2; |
| 8856 | } |
| 8857 | } |
| 8858 | #endif |
| 8859 | goto onError; |
| 8860 | } |
| 8861 | else { |
| 8862 | /* Integer input truncated to a character */ |
| 8863 | long x; |
| 8864 | x = PyLong_AsLong(v); |
| 8865 | if (x == -1 && PyErr_Occurred()) |
| 8866 | goto onError; |
| 8867 | |
| 8868 | if (x < 0 || x > 0x10ffff) { |
| 8869 | PyErr_SetString(PyExc_OverflowError, |
| 8870 | "%c arg not in range(0x110000)"); |
| 8871 | return -1; |
| 8872 | } |
| 8873 | |
| 8874 | #ifndef Py_UNICODE_WIDE |
| 8875 | if (x > 0xffff) { |
| 8876 | x -= 0x10000; |
| 8877 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 8878 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 8879 | return 2; |
| 8880 | } |
| 8881 | #endif |
| 8882 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8883 | buf[1] = '\0'; |
| 8884 | return 1; |
| 8885 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8886 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8887 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8888 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8889 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8890 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8891 | } |
| 8892 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8893 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8894 | FORMATBUFLEN is the length of the buffer in which chars are formatted. |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8895 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8896 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8898 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8899 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8900 | { |
| 8901 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8902 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8903 | int args_owned = 0; |
| 8904 | PyUnicodeObject *result = NULL; |
| 8905 | PyObject *dict = NULL; |
| 8906 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8907 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8908 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8909 | PyErr_BadInternalCall(); |
| 8910 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8911 | } |
| 8912 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8913 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8914 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8915 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8916 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8917 | |
| 8918 | reslen = rescnt = fmtcnt + 100; |
| 8919 | result = _PyUnicode_New(reslen); |
| 8920 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8921 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8922 | res = PyUnicode_AS_UNICODE(result); |
| 8923 | |
| 8924 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8925 | arglen = PyTuple_Size(args); |
| 8926 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8927 | } |
| 8928 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8929 | arglen = -1; |
| 8930 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8931 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8932 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 8933 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8934 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8935 | |
| 8936 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8937 | if (*fmt != '%') { |
| 8938 | if (--rescnt < 0) { |
| 8939 | rescnt = fmtcnt + 100; |
| 8940 | reslen += rescnt; |
| 8941 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 8942 | goto onError; |
| 8943 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8944 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8945 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8947 | } |
| 8948 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8949 | /* Got a format specifier */ |
| 8950 | int flags = 0; |
| 8951 | Py_ssize_t width = -1; |
| 8952 | int prec = -1; |
| 8953 | Py_UNICODE c = '\0'; |
| 8954 | Py_UNICODE fill; |
| 8955 | int isnumok; |
| 8956 | PyObject *v = NULL; |
| 8957 | PyObject *temp = NULL; |
| 8958 | Py_UNICODE *pbuf; |
| 8959 | Py_UNICODE sign; |
| 8960 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8961 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8962 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8963 | fmt++; |
| 8964 | if (*fmt == '(') { |
| 8965 | Py_UNICODE *keystart; |
| 8966 | Py_ssize_t keylen; |
| 8967 | PyObject *key; |
| 8968 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 8969 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8970 | if (dict == NULL) { |
| 8971 | PyErr_SetString(PyExc_TypeError, |
| 8972 | "format requires a mapping"); |
| 8973 | goto onError; |
| 8974 | } |
| 8975 | ++fmt; |
| 8976 | --fmtcnt; |
| 8977 | keystart = fmt; |
| 8978 | /* Skip over balanced parentheses */ |
| 8979 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8980 | if (*fmt == ')') |
| 8981 | --pcount; |
| 8982 | else if (*fmt == '(') |
| 8983 | ++pcount; |
| 8984 | fmt++; |
| 8985 | } |
| 8986 | keylen = fmt - keystart - 1; |
| 8987 | if (fmtcnt < 0 || pcount > 0) { |
| 8988 | PyErr_SetString(PyExc_ValueError, |
| 8989 | "incomplete format key"); |
| 8990 | goto onError; |
| 8991 | } |
| 8992 | #if 0 |
| 8993 | /* keys are converted to strings using UTF-8 and |
| 8994 | then looked up since Python uses strings to hold |
| 8995 | variables names etc. in its namespaces and we |
| 8996 | wouldn't want to break common idioms. */ |
| 8997 | key = PyUnicode_EncodeUTF8(keystart, |
| 8998 | keylen, |
| 8999 | NULL); |
| 9000 | #else |
| 9001 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9002 | #endif |
| 9003 | if (key == NULL) |
| 9004 | goto onError; |
| 9005 | if (args_owned) { |
| 9006 | Py_DECREF(args); |
| 9007 | args_owned = 0; |
| 9008 | } |
| 9009 | args = PyObject_GetItem(dict, key); |
| 9010 | Py_DECREF(key); |
| 9011 | if (args == NULL) { |
| 9012 | goto onError; |
| 9013 | } |
| 9014 | args_owned = 1; |
| 9015 | arglen = -1; |
| 9016 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9017 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9018 | while (--fmtcnt >= 0) { |
| 9019 | switch (c = *fmt++) { |
| 9020 | case '-': flags |= F_LJUST; continue; |
| 9021 | case '+': flags |= F_SIGN; continue; |
| 9022 | case ' ': flags |= F_BLANK; continue; |
| 9023 | case '#': flags |= F_ALT; continue; |
| 9024 | case '0': flags |= F_ZERO; continue; |
| 9025 | } |
| 9026 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9027 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9028 | if (c == '*') { |
| 9029 | v = getnextarg(args, arglen, &argidx); |
| 9030 | if (v == NULL) |
| 9031 | goto onError; |
| 9032 | if (!PyLong_Check(v)) { |
| 9033 | PyErr_SetString(PyExc_TypeError, |
| 9034 | "* wants int"); |
| 9035 | goto onError; |
| 9036 | } |
| 9037 | width = PyLong_AsLong(v); |
| 9038 | if (width == -1 && PyErr_Occurred()) |
| 9039 | goto onError; |
| 9040 | if (width < 0) { |
| 9041 | flags |= F_LJUST; |
| 9042 | width = -width; |
| 9043 | } |
| 9044 | if (--fmtcnt >= 0) |
| 9045 | c = *fmt++; |
| 9046 | } |
| 9047 | else if (c >= '0' && c <= '9') { |
| 9048 | width = c - '0'; |
| 9049 | while (--fmtcnt >= 0) { |
| 9050 | c = *fmt++; |
| 9051 | if (c < '0' || c > '9') |
| 9052 | break; |
| 9053 | if ((width*10) / 10 != width) { |
| 9054 | PyErr_SetString(PyExc_ValueError, |
| 9055 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9056 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9057 | } |
| 9058 | width = width*10 + (c - '0'); |
| 9059 | } |
| 9060 | } |
| 9061 | if (c == '.') { |
| 9062 | prec = 0; |
| 9063 | if (--fmtcnt >= 0) |
| 9064 | c = *fmt++; |
| 9065 | if (c == '*') { |
| 9066 | v = getnextarg(args, arglen, &argidx); |
| 9067 | if (v == NULL) |
| 9068 | goto onError; |
| 9069 | if (!PyLong_Check(v)) { |
| 9070 | PyErr_SetString(PyExc_TypeError, |
| 9071 | "* wants int"); |
| 9072 | goto onError; |
| 9073 | } |
| 9074 | prec = PyLong_AsLong(v); |
| 9075 | if (prec == -1 && PyErr_Occurred()) |
| 9076 | goto onError; |
| 9077 | if (prec < 0) |
| 9078 | prec = 0; |
| 9079 | if (--fmtcnt >= 0) |
| 9080 | c = *fmt++; |
| 9081 | } |
| 9082 | else if (c >= '0' && c <= '9') { |
| 9083 | prec = c - '0'; |
| 9084 | while (--fmtcnt >= 0) { |
| 9085 | c = Py_CHARMASK(*fmt++); |
| 9086 | if (c < '0' || c > '9') |
| 9087 | break; |
| 9088 | if ((prec*10) / 10 != prec) { |
| 9089 | PyErr_SetString(PyExc_ValueError, |
| 9090 | "prec too big"); |
| 9091 | goto onError; |
| 9092 | } |
| 9093 | prec = prec*10 + (c - '0'); |
| 9094 | } |
| 9095 | } |
| 9096 | } /* prec */ |
| 9097 | if (fmtcnt >= 0) { |
| 9098 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9099 | if (--fmtcnt >= 0) |
| 9100 | c = *fmt++; |
| 9101 | } |
| 9102 | } |
| 9103 | if (fmtcnt < 0) { |
| 9104 | PyErr_SetString(PyExc_ValueError, |
| 9105 | "incomplete format"); |
| 9106 | goto onError; |
| 9107 | } |
| 9108 | if (c != '%') { |
| 9109 | v = getnextarg(args, arglen, &argidx); |
| 9110 | if (v == NULL) |
| 9111 | goto onError; |
| 9112 | } |
| 9113 | sign = 0; |
| 9114 | fill = ' '; |
| 9115 | switch (c) { |
| 9116 | |
| 9117 | case '%': |
| 9118 | pbuf = formatbuf; |
| 9119 | /* presume that buffer length is at least 1 */ |
| 9120 | pbuf[0] = '%'; |
| 9121 | len = 1; |
| 9122 | break; |
| 9123 | |
| 9124 | case 's': |
| 9125 | case 'r': |
| 9126 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9127 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9128 | temp = v; |
| 9129 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9130 | } |
| 9131 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9132 | if (c == 's') |
| 9133 | temp = PyObject_Str(v); |
| 9134 | else if (c == 'r') |
| 9135 | temp = PyObject_Repr(v); |
| 9136 | else |
| 9137 | temp = PyObject_ASCII(v); |
| 9138 | if (temp == NULL) |
| 9139 | goto onError; |
| 9140 | if (PyUnicode_Check(temp)) |
| 9141 | /* nothing to do */; |
| 9142 | else { |
| 9143 | Py_DECREF(temp); |
| 9144 | PyErr_SetString(PyExc_TypeError, |
| 9145 | "%s argument has non-string str()"); |
| 9146 | goto onError; |
| 9147 | } |
| 9148 | } |
| 9149 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9150 | len = PyUnicode_GET_SIZE(temp); |
| 9151 | if (prec >= 0 && len > prec) |
| 9152 | len = prec; |
| 9153 | break; |
| 9154 | |
| 9155 | case 'i': |
| 9156 | case 'd': |
| 9157 | case 'u': |
| 9158 | case 'o': |
| 9159 | case 'x': |
| 9160 | case 'X': |
| 9161 | if (c == 'i') |
| 9162 | c = 'd'; |
| 9163 | isnumok = 0; |
| 9164 | if (PyNumber_Check(v)) { |
| 9165 | PyObject *iobj=NULL; |
| 9166 | |
| 9167 | if (PyLong_Check(v)) { |
| 9168 | iobj = v; |
| 9169 | Py_INCREF(iobj); |
| 9170 | } |
| 9171 | else { |
| 9172 | iobj = PyNumber_Long(v); |
| 9173 | } |
| 9174 | if (iobj!=NULL) { |
| 9175 | if (PyLong_Check(iobj)) { |
| 9176 | isnumok = 1; |
| 9177 | temp = formatlong(iobj, flags, prec, c); |
| 9178 | Py_DECREF(iobj); |
| 9179 | if (!temp) |
| 9180 | goto onError; |
| 9181 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9182 | len = PyUnicode_GET_SIZE(temp); |
| 9183 | sign = 1; |
| 9184 | } |
| 9185 | else { |
| 9186 | Py_DECREF(iobj); |
| 9187 | } |
| 9188 | } |
| 9189 | } |
| 9190 | if (!isnumok) { |
| 9191 | PyErr_Format(PyExc_TypeError, |
| 9192 | "%%%c format: a number is required, " |
| 9193 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9194 | goto onError; |
| 9195 | } |
| 9196 | if (flags & F_ZERO) |
| 9197 | fill = '0'; |
| 9198 | break; |
| 9199 | |
| 9200 | case 'e': |
| 9201 | case 'E': |
| 9202 | case 'f': |
| 9203 | case 'F': |
| 9204 | case 'g': |
| 9205 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9206 | temp = formatfloat(v, flags, prec, c); |
| 9207 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9208 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9209 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9210 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9211 | sign = 1; |
| 9212 | if (flags & F_ZERO) |
| 9213 | fill = '0'; |
| 9214 | break; |
| 9215 | |
| 9216 | case 'c': |
| 9217 | pbuf = formatbuf; |
| 9218 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9219 | if (len < 0) |
| 9220 | goto onError; |
| 9221 | break; |
| 9222 | |
| 9223 | default: |
| 9224 | PyErr_Format(PyExc_ValueError, |
| 9225 | "unsupported format character '%c' (0x%x) " |
| 9226 | "at index %zd", |
| 9227 | (31<=c && c<=126) ? (char)c : '?', |
| 9228 | (int)c, |
| 9229 | (Py_ssize_t)(fmt - 1 - |
| 9230 | PyUnicode_AS_UNICODE(uformat))); |
| 9231 | goto onError; |
| 9232 | } |
| 9233 | if (sign) { |
| 9234 | if (*pbuf == '-' || *pbuf == '+') { |
| 9235 | sign = *pbuf++; |
| 9236 | len--; |
| 9237 | } |
| 9238 | else if (flags & F_SIGN) |
| 9239 | sign = '+'; |
| 9240 | else if (flags & F_BLANK) |
| 9241 | sign = ' '; |
| 9242 | else |
| 9243 | sign = 0; |
| 9244 | } |
| 9245 | if (width < len) |
| 9246 | width = len; |
| 9247 | if (rescnt - (sign != 0) < width) { |
| 9248 | reslen -= rescnt; |
| 9249 | rescnt = width + fmtcnt + 100; |
| 9250 | reslen += rescnt; |
| 9251 | if (reslen < 0) { |
| 9252 | Py_XDECREF(temp); |
| 9253 | PyErr_NoMemory(); |
| 9254 | goto onError; |
| 9255 | } |
| 9256 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9257 | Py_XDECREF(temp); |
| 9258 | goto onError; |
| 9259 | } |
| 9260 | res = PyUnicode_AS_UNICODE(result) |
| 9261 | + reslen - rescnt; |
| 9262 | } |
| 9263 | if (sign) { |
| 9264 | if (fill != ' ') |
| 9265 | *res++ = sign; |
| 9266 | rescnt--; |
| 9267 | if (width > len) |
| 9268 | width--; |
| 9269 | } |
| 9270 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9271 | assert(pbuf[0] == '0'); |
| 9272 | assert(pbuf[1] == c); |
| 9273 | if (fill != ' ') { |
| 9274 | *res++ = *pbuf++; |
| 9275 | *res++ = *pbuf++; |
| 9276 | } |
| 9277 | rescnt -= 2; |
| 9278 | width -= 2; |
| 9279 | if (width < 0) |
| 9280 | width = 0; |
| 9281 | len -= 2; |
| 9282 | } |
| 9283 | if (width > len && !(flags & F_LJUST)) { |
| 9284 | do { |
| 9285 | --rescnt; |
| 9286 | *res++ = fill; |
| 9287 | } while (--width > len); |
| 9288 | } |
| 9289 | if (fill == ' ') { |
| 9290 | if (sign) |
| 9291 | *res++ = sign; |
| 9292 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9293 | assert(pbuf[0] == '0'); |
| 9294 | assert(pbuf[1] == c); |
| 9295 | *res++ = *pbuf++; |
| 9296 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9297 | } |
| 9298 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9299 | Py_UNICODE_COPY(res, pbuf, len); |
| 9300 | res += len; |
| 9301 | rescnt -= len; |
| 9302 | while (--width >= len) { |
| 9303 | --rescnt; |
| 9304 | *res++ = ' '; |
| 9305 | } |
| 9306 | if (dict && (argidx < arglen) && c != '%') { |
| 9307 | PyErr_SetString(PyExc_TypeError, |
| 9308 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9309 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9310 | goto onError; |
| 9311 | } |
| 9312 | Py_XDECREF(temp); |
| 9313 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9314 | } /* until end */ |
| 9315 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9316 | PyErr_SetString(PyExc_TypeError, |
| 9317 | "not all arguments converted during string formatting"); |
| 9318 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9319 | } |
| 9320 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9321 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9322 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9323 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9324 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9325 | } |
| 9326 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9327 | return (PyObject *)result; |
| 9328 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9329 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9330 | Py_XDECREF(result); |
| 9331 | Py_DECREF(uformat); |
| 9332 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9333 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9334 | } |
| 9335 | return NULL; |
| 9336 | } |
| 9337 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9338 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9339 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9340 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9341 | static PyObject * |
| 9342 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9343 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9344 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9345 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9346 | char *encoding = NULL; |
| 9347 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9348 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9349 | if (type != &PyUnicode_Type) |
| 9350 | return unicode_subtype_new(type, args, kwds); |
| 9351 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9352 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9353 | return NULL; |
| 9354 | if (x == NULL) |
| 9355 | return (PyObject *)_PyUnicode_New(0); |
| 9356 | if (encoding == NULL && errors == NULL) |
| 9357 | return PyObject_Str(x); |
| 9358 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9359 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9360 | } |
| 9361 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9362 | static PyObject * |
| 9363 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9364 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9365 | PyUnicodeObject *tmp, *pnew; |
| 9366 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9367 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9368 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9369 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9370 | if (tmp == NULL) |
| 9371 | return NULL; |
| 9372 | assert(PyUnicode_Check(tmp)); |
| 9373 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9374 | if (pnew == NULL) { |
| 9375 | Py_DECREF(tmp); |
| 9376 | return NULL; |
| 9377 | } |
| 9378 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9379 | if (pnew->str == NULL) { |
| 9380 | _Py_ForgetReference((PyObject *)pnew); |
| 9381 | PyObject_Del(pnew); |
| 9382 | Py_DECREF(tmp); |
| 9383 | return PyErr_NoMemory(); |
| 9384 | } |
| 9385 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9386 | pnew->length = n; |
| 9387 | pnew->hash = tmp->hash; |
| 9388 | Py_DECREF(tmp); |
| 9389 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9390 | } |
| 9391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9392 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9393 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9394 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9395 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9396 | encoding defaults to the current default string encoding.\n\ |
| 9397 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9398 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9399 | static PyObject *unicode_iter(PyObject *seq); |
| 9400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9401 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9402 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9403 | "str", /* tp_name */ |
| 9404 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9405 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9406 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9407 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9408 | 0, /* tp_print */ |
| 9409 | 0, /* tp_getattr */ |
| 9410 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9411 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9412 | unicode_repr, /* tp_repr */ |
| 9413 | &unicode_as_number, /* tp_as_number */ |
| 9414 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9415 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9416 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9417 | 0, /* tp_call*/ |
| 9418 | (reprfunc) unicode_str, /* tp_str */ |
| 9419 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9420 | 0, /* tp_setattro */ |
| 9421 | 0, /* tp_as_buffer */ |
| 9422 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9423 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9424 | unicode_doc, /* tp_doc */ |
| 9425 | 0, /* tp_traverse */ |
| 9426 | 0, /* tp_clear */ |
| 9427 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9428 | 0, /* tp_weaklistoffset */ |
| 9429 | unicode_iter, /* tp_iter */ |
| 9430 | 0, /* tp_iternext */ |
| 9431 | unicode_methods, /* tp_methods */ |
| 9432 | 0, /* tp_members */ |
| 9433 | 0, /* tp_getset */ |
| 9434 | &PyBaseObject_Type, /* tp_base */ |
| 9435 | 0, /* tp_dict */ |
| 9436 | 0, /* tp_descr_get */ |
| 9437 | 0, /* tp_descr_set */ |
| 9438 | 0, /* tp_dictoffset */ |
| 9439 | 0, /* tp_init */ |
| 9440 | 0, /* tp_alloc */ |
| 9441 | unicode_new, /* tp_new */ |
| 9442 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9443 | }; |
| 9444 | |
| 9445 | /* Initialize the Unicode implementation */ |
| 9446 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9447 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9448 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9449 | int i; |
| 9450 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9451 | /* XXX - move this array to unicodectype.c ? */ |
| 9452 | Py_UNICODE linebreak[] = { |
| 9453 | 0x000A, /* LINE FEED */ |
| 9454 | 0x000D, /* CARRIAGE RETURN */ |
| 9455 | 0x001C, /* FILE SEPARATOR */ |
| 9456 | 0x001D, /* GROUP SEPARATOR */ |
| 9457 | 0x001E, /* RECORD SEPARATOR */ |
| 9458 | 0x0085, /* NEXT LINE */ |
| 9459 | 0x2028, /* LINE SEPARATOR */ |
| 9460 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9461 | }; |
| 9462 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9463 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9464 | free_list = NULL; |
| 9465 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9466 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9467 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9468 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9469 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9470 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9471 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9472 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9473 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9474 | |
| 9475 | /* initialize the linebreak bloom filter */ |
| 9476 | bloom_linebreak = make_bloom_mask( |
| 9477 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9478 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9479 | |
| 9480 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9481 | } |
| 9482 | |
| 9483 | /* Finalize the Unicode implementation */ |
| 9484 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9485 | int |
| 9486 | PyUnicode_ClearFreeList(void) |
| 9487 | { |
| 9488 | int freelist_size = numfree; |
| 9489 | PyUnicodeObject *u; |
| 9490 | |
| 9491 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9492 | PyUnicodeObject *v = u; |
| 9493 | u = *(PyUnicodeObject **)u; |
| 9494 | if (v->str) |
| 9495 | PyObject_DEL(v->str); |
| 9496 | Py_XDECREF(v->defenc); |
| 9497 | PyObject_Del(v); |
| 9498 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9499 | } |
| 9500 | free_list = NULL; |
| 9501 | assert(numfree == 0); |
| 9502 | return freelist_size; |
| 9503 | } |
| 9504 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9505 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9506 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9507 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9508 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9509 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9510 | Py_XDECREF(unicode_empty); |
| 9511 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9512 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9513 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9514 | if (unicode_latin1[i]) { |
| 9515 | Py_DECREF(unicode_latin1[i]); |
| 9516 | unicode_latin1[i] = NULL; |
| 9517 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9518 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9519 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9520 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9521 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9522 | void |
| 9523 | PyUnicode_InternInPlace(PyObject **p) |
| 9524 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9525 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9526 | PyObject *t; |
| 9527 | if (s == NULL || !PyUnicode_Check(s)) |
| 9528 | Py_FatalError( |
| 9529 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9530 | /* If it's a subclass, we don't really know what putting |
| 9531 | it in the interned dict might do. */ |
| 9532 | if (!PyUnicode_CheckExact(s)) |
| 9533 | return; |
| 9534 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9535 | return; |
| 9536 | if (interned == NULL) { |
| 9537 | interned = PyDict_New(); |
| 9538 | if (interned == NULL) { |
| 9539 | PyErr_Clear(); /* Don't leave an exception */ |
| 9540 | return; |
| 9541 | } |
| 9542 | } |
| 9543 | /* It might be that the GetItem call fails even |
| 9544 | though the key is present in the dictionary, |
| 9545 | namely when this happens during a stack overflow. */ |
| 9546 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9547 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9548 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9549 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9550 | if (t) { |
| 9551 | Py_INCREF(t); |
| 9552 | Py_DECREF(*p); |
| 9553 | *p = t; |
| 9554 | return; |
| 9555 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9556 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9557 | PyThreadState_GET()->recursion_critical = 1; |
| 9558 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9559 | PyErr_Clear(); |
| 9560 | PyThreadState_GET()->recursion_critical = 0; |
| 9561 | return; |
| 9562 | } |
| 9563 | PyThreadState_GET()->recursion_critical = 0; |
| 9564 | /* The two references in interned are not counted by refcnt. |
| 9565 | The deallocator will take care of this */ |
| 9566 | Py_REFCNT(s) -= 2; |
| 9567 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9568 | } |
| 9569 | |
| 9570 | void |
| 9571 | PyUnicode_InternImmortal(PyObject **p) |
| 9572 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9573 | PyUnicode_InternInPlace(p); |
| 9574 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9575 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9576 | Py_INCREF(*p); |
| 9577 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9578 | } |
| 9579 | |
| 9580 | PyObject * |
| 9581 | PyUnicode_InternFromString(const char *cp) |
| 9582 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9583 | PyObject *s = PyUnicode_FromString(cp); |
| 9584 | if (s == NULL) |
| 9585 | return NULL; |
| 9586 | PyUnicode_InternInPlace(&s); |
| 9587 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9588 | } |
| 9589 | |
| 9590 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9591 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9592 | PyObject *keys; |
| 9593 | PyUnicodeObject *s; |
| 9594 | Py_ssize_t i, n; |
| 9595 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9596 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9597 | if (interned == NULL || !PyDict_Check(interned)) |
| 9598 | return; |
| 9599 | keys = PyDict_Keys(interned); |
| 9600 | if (keys == NULL || !PyList_Check(keys)) { |
| 9601 | PyErr_Clear(); |
| 9602 | return; |
| 9603 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9604 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9605 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9606 | detector, interned unicode strings are not forcibly deallocated; |
| 9607 | rather, we give them their stolen references back, and then clear |
| 9608 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9609 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9610 | n = PyList_GET_SIZE(keys); |
| 9611 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9612 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9613 | for (i = 0; i < n; i++) { |
| 9614 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9615 | switch (s->state) { |
| 9616 | case SSTATE_NOT_INTERNED: |
| 9617 | /* XXX Shouldn't happen */ |
| 9618 | break; |
| 9619 | case SSTATE_INTERNED_IMMORTAL: |
| 9620 | Py_REFCNT(s) += 1; |
| 9621 | immortal_size += s->length; |
| 9622 | break; |
| 9623 | case SSTATE_INTERNED_MORTAL: |
| 9624 | Py_REFCNT(s) += 2; |
| 9625 | mortal_size += s->length; |
| 9626 | break; |
| 9627 | default: |
| 9628 | Py_FatalError("Inconsistent interned string state."); |
| 9629 | } |
| 9630 | s->state = SSTATE_NOT_INTERNED; |
| 9631 | } |
| 9632 | fprintf(stderr, "total size of all interned strings: " |
| 9633 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9634 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9635 | Py_DECREF(keys); |
| 9636 | PyDict_Clear(interned); |
| 9637 | Py_DECREF(interned); |
| 9638 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9639 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9640 | |
| 9641 | |
| 9642 | /********************* Unicode Iterator **************************/ |
| 9643 | |
| 9644 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9645 | PyObject_HEAD |
| 9646 | Py_ssize_t it_index; |
| 9647 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9648 | } unicodeiterobject; |
| 9649 | |
| 9650 | static void |
| 9651 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9652 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9653 | _PyObject_GC_UNTRACK(it); |
| 9654 | Py_XDECREF(it->it_seq); |
| 9655 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9656 | } |
| 9657 | |
| 9658 | static int |
| 9659 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9660 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9661 | Py_VISIT(it->it_seq); |
| 9662 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9663 | } |
| 9664 | |
| 9665 | static PyObject * |
| 9666 | unicodeiter_next(unicodeiterobject *it) |
| 9667 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9668 | PyUnicodeObject *seq; |
| 9669 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9670 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9671 | assert(it != NULL); |
| 9672 | seq = it->it_seq; |
| 9673 | if (seq == NULL) |
| 9674 | return NULL; |
| 9675 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9676 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9677 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9678 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9679 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9680 | if (item != NULL) |
| 9681 | ++it->it_index; |
| 9682 | return item; |
| 9683 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9684 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9685 | Py_DECREF(seq); |
| 9686 | it->it_seq = NULL; |
| 9687 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9688 | } |
| 9689 | |
| 9690 | static PyObject * |
| 9691 | unicodeiter_len(unicodeiterobject *it) |
| 9692 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9693 | Py_ssize_t len = 0; |
| 9694 | if (it->it_seq) |
| 9695 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9696 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9697 | } |
| 9698 | |
| 9699 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9700 | |
| 9701 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9702 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9703 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9704 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9705 | }; |
| 9706 | |
| 9707 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9708 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9709 | "str_iterator", /* tp_name */ |
| 9710 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9711 | 0, /* tp_itemsize */ |
| 9712 | /* methods */ |
| 9713 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9714 | 0, /* tp_print */ |
| 9715 | 0, /* tp_getattr */ |
| 9716 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9717 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9718 | 0, /* tp_repr */ |
| 9719 | 0, /* tp_as_number */ |
| 9720 | 0, /* tp_as_sequence */ |
| 9721 | 0, /* tp_as_mapping */ |
| 9722 | 0, /* tp_hash */ |
| 9723 | 0, /* tp_call */ |
| 9724 | 0, /* tp_str */ |
| 9725 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9726 | 0, /* tp_setattro */ |
| 9727 | 0, /* tp_as_buffer */ |
| 9728 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9729 | 0, /* tp_doc */ |
| 9730 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9731 | 0, /* tp_clear */ |
| 9732 | 0, /* tp_richcompare */ |
| 9733 | 0, /* tp_weaklistoffset */ |
| 9734 | PyObject_SelfIter, /* tp_iter */ |
| 9735 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9736 | unicodeiter_methods, /* tp_methods */ |
| 9737 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9738 | }; |
| 9739 | |
| 9740 | static PyObject * |
| 9741 | unicode_iter(PyObject *seq) |
| 9742 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9743 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9744 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9745 | if (!PyUnicode_Check(seq)) { |
| 9746 | PyErr_BadInternalCall(); |
| 9747 | return NULL; |
| 9748 | } |
| 9749 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9750 | if (it == NULL) |
| 9751 | return NULL; |
| 9752 | it->it_index = 0; |
| 9753 | Py_INCREF(seq); |
| 9754 | it->it_seq = (PyUnicodeObject *)seq; |
| 9755 | _PyObject_GC_TRACK(it); |
| 9756 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9757 | } |
| 9758 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9759 | size_t |
| 9760 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9761 | { |
| 9762 | int res = 0; |
| 9763 | while(*u++) |
| 9764 | res++; |
| 9765 | return res; |
| 9766 | } |
| 9767 | |
| 9768 | Py_UNICODE* |
| 9769 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9770 | { |
| 9771 | Py_UNICODE *u = s1; |
| 9772 | while ((*u++ = *s2++)); |
| 9773 | return s1; |
| 9774 | } |
| 9775 | |
| 9776 | Py_UNICODE* |
| 9777 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9778 | { |
| 9779 | Py_UNICODE *u = s1; |
| 9780 | while ((*u++ = *s2++)) |
| 9781 | if (n-- == 0) |
| 9782 | break; |
| 9783 | return s1; |
| 9784 | } |
| 9785 | |
| 9786 | int |
| 9787 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9788 | { |
| 9789 | while (*s1 && *s2 && *s1 == *s2) |
| 9790 | s1++, s2++; |
| 9791 | if (*s1 && *s2) |
| 9792 | return (*s1 < *s2) ? -1 : +1; |
| 9793 | if (*s1) |
| 9794 | return 1; |
| 9795 | if (*s2) |
| 9796 | return -1; |
| 9797 | return 0; |
| 9798 | } |
| 9799 | |
| 9800 | Py_UNICODE* |
| 9801 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9802 | { |
| 9803 | const Py_UNICODE *p; |
| 9804 | for (p = s; *p; p++) |
| 9805 | if (*p == c) |
| 9806 | return (Py_UNICODE*)p; |
| 9807 | return NULL; |
| 9808 | } |
| 9809 | |
| 9810 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9811 | #ifdef __cplusplus |
| 9812 | } |
| 9813 | #endif |