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, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 129 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 130 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 131 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 132 | /* case 0x000C: * FORM FEED */ |
| 133 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 134 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 136 | /* case 0x001C: * FILE SEPARATOR */ |
| 137 | /* case 0x001D: * GROUP SEPARATOR */ |
| 138 | /* case 0x001E: * RECORD SEPARATOR */ |
| 139 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 140 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 141 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 142 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 146 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 147 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 149 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 150 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 151 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 153 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 154 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 157 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 158 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 159 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 160 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 161 | |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 162 | static void raise_encode_exception(PyObject **exceptionObject, |
| 163 | const char *encoding, |
| 164 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 165 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 166 | const char *reason); |
| 167 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 168 | /* Same for linebreaks */ |
| 169 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 171 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 172 | /* 0x000B, * LINE TABULATION */ |
| 173 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 174 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 175 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 177 | /* 0x001C, * FILE SEPARATOR */ |
| 178 | /* 0x001D, * GROUP SEPARATOR */ |
| 179 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 180 | 0, 0, 0, 0, 1, 1, 1, 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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 185 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 186 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 187 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 188 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 189 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 190 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 191 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 192 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 193 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 197 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 198 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 199 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 200 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 201 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 202 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 203 | /* This is actually an illegal character, so it should |
| 204 | not be passed to unichr. */ |
| 205 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 206 | #endif |
| 207 | } |
| 208 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 209 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 210 | |
| 211 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 212 | to keep things simple, we use a single bitmask, using the least 5 |
| 213 | bits from each unicode characters as the bit index. */ |
| 214 | |
| 215 | /* the linebreak mask is set up by Unicode_Init below */ |
| 216 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 217 | #if LONG_BIT >= 128 |
| 218 | #define BLOOM_WIDTH 128 |
| 219 | #elif LONG_BIT >= 64 |
| 220 | #define BLOOM_WIDTH 64 |
| 221 | #elif LONG_BIT >= 32 |
| 222 | #define BLOOM_WIDTH 32 |
| 223 | #else |
| 224 | #error "LONG_BIT is smaller than 32" |
| 225 | #endif |
| 226 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 227 | #define BLOOM_MASK unsigned long |
| 228 | |
| 229 | static BLOOM_MASK bloom_linebreak; |
| 230 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 231 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 232 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 233 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 234 | #define BLOOM_LINEBREAK(ch) \ |
| 235 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 236 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 237 | |
| 238 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 239 | { |
| 240 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 241 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 242 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 243 | Py_ssize_t i; |
| 244 | |
| 245 | mask = 0; |
| 246 | for (i = 0; i < len; i++) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 247 | BLOOM_ADD(mask, ptr[i]); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 248 | |
| 249 | return mask; |
| 250 | } |
| 251 | |
| 252 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 253 | { |
| 254 | Py_ssize_t i; |
| 255 | |
| 256 | for (i = 0; i < setlen; i++) |
| 257 | if (set[i] == chr) |
| 258 | return 1; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 263 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 264 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | /* --- Unicode Object ----------------------------------------------------- */ |
| 267 | |
| 268 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 269 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 270 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 271 | { |
| 272 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 274 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 275 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 276 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 278 | /* Resizing shared object (unicode_empty or single character |
| 279 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 280 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 281 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 282 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 283 | (unicode->length == 1 && |
| 284 | unicode->str[0] < 256U && |
| 285 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 286 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 287 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | return -1; |
| 289 | } |
| 290 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 291 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 292 | The overallocation is also used by fastsearch, which assumes that it's |
| 293 | safe to look at str[length] (without making any assumptions about what |
| 294 | it contains). */ |
| 295 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 296 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 297 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 298 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 300 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 301 | PyErr_NoMemory(); |
| 302 | return -1; |
| 303 | } |
| 304 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 305 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 307 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 308 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 309 | if (unicode->defenc) { |
| 310 | Py_DECREF(unicode->defenc); |
| 311 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 312 | } |
| 313 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 314 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | /* 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] | 319 | Ux0000 terminated; some code (e.g. new_identifier) |
| 320 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 321 | |
| 322 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 323 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 324 | |
| 325 | */ |
| 326 | |
| 327 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 328 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 329 | { |
| 330 | register PyUnicodeObject *unicode; |
| 331 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 332 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 333 | if (length == 0 && unicode_empty != NULL) { |
| 334 | Py_INCREF(unicode_empty); |
| 335 | return unicode_empty; |
| 336 | } |
| 337 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 338 | /* Ensure we won't overflow the size. */ |
| 339 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 340 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 341 | } |
| 342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 343 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 344 | if (free_list) { |
| 345 | unicode = free_list; |
| 346 | free_list = *(PyUnicodeObject **)unicode; |
| 347 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 348 | if (unicode->str) { |
| 349 | /* Keep-Alive optimization: we only upsize the buffer, |
| 350 | never downsize it. */ |
| 351 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 352 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 353 | PyObject_DEL(unicode->str); |
| 354 | unicode->str = NULL; |
| 355 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 356 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 357 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 358 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 359 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 360 | } |
| 361 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 362 | } |
| 363 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 364 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 365 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 366 | if (unicode == NULL) |
| 367 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 368 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 369 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 372 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 373 | PyErr_NoMemory(); |
| 374 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 375 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 376 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 377 | * the caller fails before initializing str -- unicode_resize() |
| 378 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 379 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 380 | * We don't want unicode_resize to read uninitialized memory in |
| 381 | * that case. |
| 382 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 383 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 384 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 385 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 386 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 387 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 388 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 389 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 390 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 391 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 392 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 393 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 394 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 395 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 396 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 400 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 401 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 402 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 403 | case SSTATE_NOT_INTERNED: |
| 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_MORTAL: |
| 407 | /* revive dead object temporarily for DelItem */ |
| 408 | Py_REFCNT(unicode) = 3; |
| 409 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 410 | Py_FatalError( |
| 411 | "deletion of interned string failed"); |
| 412 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 413 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 414 | case SSTATE_INTERNED_IMMORTAL: |
| 415 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 416 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 417 | default: |
| 418 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 421 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 422 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 423 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 424 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 425 | PyObject_DEL(unicode->str); |
| 426 | unicode->str = NULL; |
| 427 | unicode->length = 0; |
| 428 | } |
| 429 | if (unicode->defenc) { |
| 430 | Py_DECREF(unicode->defenc); |
| 431 | unicode->defenc = NULL; |
| 432 | } |
| 433 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 434 | *(PyUnicodeObject **)unicode = free_list; |
| 435 | free_list = unicode; |
| 436 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 437 | } |
| 438 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 439 | PyObject_DEL(unicode->str); |
| 440 | Py_XDECREF(unicode->defenc); |
| 441 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 445 | static |
| 446 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 447 | { |
| 448 | register PyUnicodeObject *v; |
| 449 | |
| 450 | /* Argument checks */ |
| 451 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 452 | PyErr_BadInternalCall(); |
| 453 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 454 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 455 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 456 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 457 | PyErr_BadInternalCall(); |
| 458 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* Resizing unicode_empty and single character objects is not |
| 462 | possible since these are being shared. We simply return a fresh |
| 463 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 464 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 465 | (v == unicode_empty || v->length == 1)) { |
| 466 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 467 | if (w == NULL) |
| 468 | return -1; |
| 469 | Py_UNICODE_COPY(w->str, v->str, |
| 470 | length < v->length ? length : v->length); |
| 471 | Py_DECREF(*unicode); |
| 472 | *unicode = w; |
| 473 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 477 | objects, since we can modify them in-place. */ |
| 478 | return unicode_resize(v, length); |
| 479 | } |
| 480 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 481 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 482 | { |
| 483 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 484 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 486 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 487 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 488 | { |
| 489 | PyUnicodeObject *unicode; |
| 490 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 491 | /* If the Unicode data is known at construction time, we can apply |
| 492 | some optimizations which share commonly used objects. */ |
| 493 | if (u != NULL) { |
| 494 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 495 | /* Optimization for empty strings */ |
| 496 | if (size == 0 && unicode_empty != NULL) { |
| 497 | Py_INCREF(unicode_empty); |
| 498 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 499 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 500 | |
| 501 | /* Single character Unicode objects in the Latin-1 range are |
| 502 | shared when using this constructor */ |
| 503 | if (size == 1 && *u < 256) { |
| 504 | unicode = unicode_latin1[*u]; |
| 505 | if (!unicode) { |
| 506 | unicode = _PyUnicode_New(1); |
| 507 | if (!unicode) |
| 508 | return NULL; |
| 509 | unicode->str[0] = *u; |
| 510 | unicode_latin1[*u] = unicode; |
| 511 | } |
| 512 | Py_INCREF(unicode); |
| 513 | return (PyObject *)unicode; |
| 514 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 515 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 517 | unicode = _PyUnicode_New(size); |
| 518 | if (!unicode) |
| 519 | return NULL; |
| 520 | |
| 521 | /* Copy the Unicode data into the new object */ |
| 522 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 523 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 524 | |
| 525 | return (PyObject *)unicode; |
| 526 | } |
| 527 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 528 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 529 | { |
| 530 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 531 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 532 | if (size < 0) { |
| 533 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 534 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 535 | return NULL; |
| 536 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 537 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 538 | /* 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] | 539 | some optimizations which share commonly used objects. |
| 540 | Also, this means the input must be UTF-8, so fall back to the |
| 541 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 542 | if (u != NULL) { |
| 543 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 544 | /* Optimization for empty strings */ |
| 545 | if (size == 0 && unicode_empty != NULL) { |
| 546 | Py_INCREF(unicode_empty); |
| 547 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 548 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 549 | |
| 550 | /* Single characters are shared when using this constructor. |
| 551 | Restrict to ASCII, since the input must be UTF-8. */ |
| 552 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 553 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 554 | if (!unicode) { |
| 555 | unicode = _PyUnicode_New(1); |
| 556 | if (!unicode) |
| 557 | return NULL; |
| 558 | unicode->str[0] = Py_CHARMASK(*u); |
| 559 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 560 | } |
| 561 | Py_INCREF(unicode); |
| 562 | return (PyObject *)unicode; |
| 563 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 564 | |
| 565 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 568 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 569 | if (!unicode) |
| 570 | return NULL; |
| 571 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 572 | return (PyObject *)unicode; |
| 573 | } |
| 574 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 575 | PyObject *PyUnicode_FromString(const char *u) |
| 576 | { |
| 577 | size_t size = strlen(u); |
| 578 | if (size > PY_SSIZE_T_MAX) { |
| 579 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | return PyUnicode_FromStringAndSize(u, size); |
| 584 | } |
| 585 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 586 | #ifdef HAVE_WCHAR_H |
| 587 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 588 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 589 | # define CONVERT_WCHAR_TO_SURROGATES |
| 590 | #endif |
| 591 | |
| 592 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 593 | |
| 594 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 595 | to convert from UTF32 to UTF16. */ |
| 596 | |
| 597 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 598 | Py_ssize_t size) |
| 599 | { |
| 600 | PyUnicodeObject *unicode; |
| 601 | register Py_ssize_t i; |
| 602 | Py_ssize_t alloc; |
| 603 | const wchar_t *orig_w; |
| 604 | |
| 605 | if (w == NULL) { |
| 606 | if (size == 0) |
| 607 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 608 | PyErr_BadInternalCall(); |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | if (size == -1) { |
| 613 | size = wcslen(w); |
| 614 | } |
| 615 | |
| 616 | alloc = size; |
| 617 | orig_w = w; |
| 618 | for (i = size; i > 0; i--) { |
| 619 | if (*w > 0xFFFF) |
| 620 | alloc++; |
| 621 | w++; |
| 622 | } |
| 623 | w = orig_w; |
| 624 | unicode = _PyUnicode_New(alloc); |
| 625 | if (!unicode) |
| 626 | return NULL; |
| 627 | |
| 628 | /* Copy the wchar_t data into the new object */ |
| 629 | { |
| 630 | register Py_UNICODE *u; |
| 631 | u = PyUnicode_AS_UNICODE(unicode); |
| 632 | for (i = size; i > 0; i--) { |
| 633 | if (*w > 0xFFFF) { |
| 634 | wchar_t ordinal = *w++; |
| 635 | ordinal -= 0x10000; |
| 636 | *u++ = 0xD800 | (ordinal >> 10); |
| 637 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 638 | } |
| 639 | else |
| 640 | *u++ = *w++; |
| 641 | } |
| 642 | } |
| 643 | return (PyObject *)unicode; |
| 644 | } |
| 645 | |
| 646 | #else |
| 647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 648 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 649 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 650 | { |
| 651 | PyUnicodeObject *unicode; |
| 652 | |
| 653 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 654 | if (size == 0) |
| 655 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 656 | PyErr_BadInternalCall(); |
| 657 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 660 | if (size == -1) { |
| 661 | size = wcslen(w); |
| 662 | } |
| 663 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 664 | unicode = _PyUnicode_New(size); |
| 665 | if (!unicode) |
| 666 | return NULL; |
| 667 | |
| 668 | /* Copy the wchar_t data into the new object */ |
| 669 | #ifdef HAVE_USABLE_WCHAR_T |
| 670 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 671 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 672 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 673 | register Py_UNICODE *u; |
| 674 | register Py_ssize_t i; |
| 675 | u = PyUnicode_AS_UNICODE(unicode); |
| 676 | for (i = size; i > 0; i--) |
| 677 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 678 | } |
| 679 | #endif |
| 680 | |
| 681 | return (PyObject *)unicode; |
| 682 | } |
| 683 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 684 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 685 | |
| 686 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 687 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 688 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 689 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 690 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 691 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 692 | *fmt++ = '%'; |
| 693 | if (width) { |
| 694 | if (zeropad) |
| 695 | *fmt++ = '0'; |
| 696 | fmt += sprintf(fmt, "%d", width); |
| 697 | } |
| 698 | if (precision) |
| 699 | fmt += sprintf(fmt, ".%d", precision); |
| 700 | if (longflag) |
| 701 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 702 | else if (longlongflag) { |
| 703 | /* longlongflag should only ever be nonzero on machines with |
| 704 | HAVE_LONG_LONG defined */ |
| 705 | #ifdef HAVE_LONG_LONG |
| 706 | char *f = PY_FORMAT_LONG_LONG; |
| 707 | while (*f) |
| 708 | *fmt++ = *f++; |
| 709 | #else |
| 710 | /* we shouldn't ever get here */ |
| 711 | assert(0); |
| 712 | *fmt++ = 'l'; |
| 713 | #endif |
| 714 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 715 | else if (size_tflag) { |
| 716 | char *f = PY_FORMAT_SIZE_T; |
| 717 | while (*f) |
| 718 | *fmt++ = *f++; |
| 719 | } |
| 720 | *fmt++ = c; |
| 721 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 724 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 725 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 726 | /* size of fixed-size buffer for formatting single arguments */ |
| 727 | #define ITEM_BUFFER_LEN 21 |
| 728 | /* maximum number of characters required for output of %ld. 21 characters |
| 729 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 730 | #define MAX_LONG_CHARS 21 |
| 731 | /* maximum number of characters required for output of %lld. |
| 732 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 733 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 734 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 735 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 736 | PyObject * |
| 737 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 738 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 739 | va_list count; |
| 740 | Py_ssize_t callcount = 0; |
| 741 | PyObject **callresults = NULL; |
| 742 | PyObject **callresult = NULL; |
| 743 | Py_ssize_t n = 0; |
| 744 | int width = 0; |
| 745 | int precision = 0; |
| 746 | int zeropad; |
| 747 | const char* f; |
| 748 | Py_UNICODE *s; |
| 749 | PyObject *string; |
| 750 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 751 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 752 | /* use abuffer instead of buffer, if we need more space |
| 753 | * (which can happen if there's a format specifier with width). */ |
| 754 | char *abuffer = NULL; |
| 755 | char *realbuffer; |
| 756 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 757 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 758 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 759 | |
| 760 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 761 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 762 | #else |
| 763 | #ifdef __va_copy |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 764 | __va_copy(count, vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 765 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 766 | count = vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 767 | #endif |
| 768 | #endif |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 769 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 770 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 771 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 772 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 773 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 774 | if (*f == '%') { |
| 775 | if (*(f+1)=='%') |
| 776 | continue; |
| 777 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 778 | ++callcount; |
| 779 | while (ISDIGIT((unsigned)*f)) |
| 780 | width = (width*10) + *f++ - '0'; |
| 781 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 782 | ; |
| 783 | if (*f == 's') |
| 784 | ++callcount; |
| 785 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 786 | } |
| 787 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 788 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 789 | if (callcount) { |
| 790 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 791 | if (!callresults) { |
| 792 | PyErr_NoMemory(); |
| 793 | return NULL; |
| 794 | } |
| 795 | callresult = callresults; |
| 796 | } |
| 797 | /* step 3: figure out how large a buffer we need */ |
| 798 | for (f = format; *f; f++) { |
| 799 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 800 | #ifdef HAVE_LONG_LONG |
| 801 | int longlongflag = 0; |
| 802 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 803 | const char* p = f; |
| 804 | width = 0; |
| 805 | while (ISDIGIT((unsigned)*f)) |
| 806 | width = (width*10) + *f++ - '0'; |
| 807 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 808 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 809 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 810 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 811 | * they don't affect the amount of space we reserve. |
| 812 | */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 813 | if (*f == 'l') { |
| 814 | if (f[1] == 'd' || f[1] == 'u') { |
| 815 | ++f; |
| 816 | } |
| 817 | #ifdef HAVE_LONG_LONG |
| 818 | else if (f[1] == 'l' && |
| 819 | (f[2] == 'd' || f[2] == 'u')) { |
| 820 | longlongflag = 1; |
| 821 | f += 2; |
| 822 | } |
| 823 | #endif |
| 824 | } |
| 825 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 826 | ++f; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 827 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 828 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 829 | switch (*f) { |
| 830 | case 'c': |
| 831 | (void)va_arg(count, int); |
| 832 | /* fall through... */ |
| 833 | case '%': |
| 834 | n++; |
| 835 | break; |
| 836 | case 'd': case 'u': case 'i': case 'x': |
| 837 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 838 | #ifdef HAVE_LONG_LONG |
| 839 | if (longlongflag) { |
| 840 | if (width < MAX_LONG_LONG_CHARS) |
| 841 | width = MAX_LONG_LONG_CHARS; |
| 842 | } |
| 843 | else |
| 844 | #endif |
| 845 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 846 | including sign. Decimal takes the most space. This |
| 847 | isn't enough for octal. If a width is specified we |
| 848 | need more (which we allocate later). */ |
| 849 | if (width < MAX_LONG_CHARS) |
| 850 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 851 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 852 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 853 | if (abuffersize < width) |
| 854 | abuffersize = width; |
| 855 | break; |
| 856 | case 's': |
| 857 | { |
| 858 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 859 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 860 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 861 | if (!str) |
| 862 | goto fail; |
| 863 | n += PyUnicode_GET_SIZE(str); |
| 864 | /* Remember the str and switch to the next slot */ |
| 865 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 866 | break; |
| 867 | } |
| 868 | case 'U': |
| 869 | { |
| 870 | PyObject *obj = va_arg(count, PyObject *); |
| 871 | assert(obj && PyUnicode_Check(obj)); |
| 872 | n += PyUnicode_GET_SIZE(obj); |
| 873 | break; |
| 874 | } |
| 875 | case 'V': |
| 876 | { |
| 877 | PyObject *obj = va_arg(count, PyObject *); |
| 878 | const char *str = va_arg(count, const char *); |
| 879 | assert(obj || str); |
| 880 | assert(!obj || PyUnicode_Check(obj)); |
| 881 | if (obj) |
| 882 | n += PyUnicode_GET_SIZE(obj); |
| 883 | else |
| 884 | n += strlen(str); |
| 885 | break; |
| 886 | } |
| 887 | case 'S': |
| 888 | { |
| 889 | PyObject *obj = va_arg(count, PyObject *); |
| 890 | PyObject *str; |
| 891 | assert(obj); |
| 892 | str = PyObject_Str(obj); |
| 893 | if (!str) |
| 894 | goto fail; |
| 895 | n += PyUnicode_GET_SIZE(str); |
| 896 | /* Remember the str and switch to the next slot */ |
| 897 | *callresult++ = str; |
| 898 | break; |
| 899 | } |
| 900 | case 'R': |
| 901 | { |
| 902 | PyObject *obj = va_arg(count, PyObject *); |
| 903 | PyObject *repr; |
| 904 | assert(obj); |
| 905 | repr = PyObject_Repr(obj); |
| 906 | if (!repr) |
| 907 | goto fail; |
| 908 | n += PyUnicode_GET_SIZE(repr); |
| 909 | /* Remember the repr and switch to the next slot */ |
| 910 | *callresult++ = repr; |
| 911 | break; |
| 912 | } |
| 913 | case 'A': |
| 914 | { |
| 915 | PyObject *obj = va_arg(count, PyObject *); |
| 916 | PyObject *ascii; |
| 917 | assert(obj); |
| 918 | ascii = PyObject_ASCII(obj); |
| 919 | if (!ascii) |
| 920 | goto fail; |
| 921 | n += PyUnicode_GET_SIZE(ascii); |
| 922 | /* Remember the repr and switch to the next slot */ |
| 923 | *callresult++ = ascii; |
| 924 | break; |
| 925 | } |
| 926 | case 'p': |
| 927 | (void) va_arg(count, int); |
| 928 | /* maximum 64-bit pointer representation: |
| 929 | * 0xffffffffffffffff |
| 930 | * so 19 characters is enough. |
| 931 | * XXX I count 18 -- what's the extra for? |
| 932 | */ |
| 933 | n += 19; |
| 934 | break; |
| 935 | default: |
| 936 | /* if we stumble upon an unknown |
| 937 | formatting code, copy the rest of |
| 938 | the format string to the output |
| 939 | string. (we cannot just skip the |
| 940 | code, since there's no way to know |
| 941 | what's in the argument list) */ |
| 942 | n += strlen(p); |
| 943 | goto expand; |
| 944 | } |
| 945 | } else |
| 946 | n++; |
| 947 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 948 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 949 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 950 | /* add 1 for sprintf's trailing null byte */ |
| 951 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 952 | if (!abuffer) { |
| 953 | PyErr_NoMemory(); |
| 954 | goto fail; |
| 955 | } |
| 956 | realbuffer = abuffer; |
| 957 | } |
| 958 | else |
| 959 | realbuffer = buffer; |
| 960 | /* step 4: fill the buffer */ |
| 961 | /* Since we've analyzed how much space we need for the worst case, |
| 962 | we don't have to resize the string. |
| 963 | There can be no errors beyond this point. */ |
| 964 | string = PyUnicode_FromUnicode(NULL, n); |
| 965 | if (!string) |
| 966 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 967 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 968 | s = PyUnicode_AS_UNICODE(string); |
| 969 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 970 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 971 | for (f = format; *f; f++) { |
| 972 | if (*f == '%') { |
| 973 | const char* p = f++; |
| 974 | int longflag = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 975 | int longlongflag = 0; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 976 | int size_tflag = 0; |
| 977 | zeropad = (*f == '0'); |
| 978 | /* parse the width.precision part */ |
| 979 | width = 0; |
| 980 | while (ISDIGIT((unsigned)*f)) |
| 981 | width = (width*10) + *f++ - '0'; |
| 982 | precision = 0; |
| 983 | if (*f == '.') { |
| 984 | f++; |
| 985 | while (ISDIGIT((unsigned)*f)) |
| 986 | precision = (precision*10) + *f++ - '0'; |
| 987 | } |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 988 | /* Handle %ld, %lu, %lld and %llu. */ |
| 989 | if (*f == 'l') { |
| 990 | if (f[1] == 'd' || f[1] == 'u') { |
| 991 | longflag = 1; |
| 992 | ++f; |
| 993 | } |
| 994 | #ifdef HAVE_LONG_LONG |
| 995 | else if (f[1] == 'l' && |
| 996 | (f[2] == 'd' || f[2] == 'u')) { |
| 997 | longlongflag = 1; |
| 998 | f += 2; |
| 999 | } |
| 1000 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1001 | } |
| 1002 | /* handle the size_t flag. */ |
| 1003 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 1004 | size_tflag = 1; |
| 1005 | ++f; |
| 1006 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1007 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1008 | switch (*f) { |
| 1009 | case 'c': |
| 1010 | *s++ = va_arg(vargs, int); |
| 1011 | break; |
| 1012 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1013 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1014 | width, precision, 'd'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1015 | if (longflag) |
| 1016 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1017 | #ifdef HAVE_LONG_LONG |
| 1018 | else if (longlongflag) |
| 1019 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1020 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1021 | else if (size_tflag) |
| 1022 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1023 | else |
| 1024 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1025 | appendstring(realbuffer); |
| 1026 | break; |
| 1027 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1028 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1029 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1030 | if (longflag) |
| 1031 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1032 | #ifdef HAVE_LONG_LONG |
| 1033 | else if (longlongflag) |
| 1034 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1035 | unsigned PY_LONG_LONG)); |
| 1036 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1037 | else if (size_tflag) |
| 1038 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1039 | else |
| 1040 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1041 | appendstring(realbuffer); |
| 1042 | break; |
| 1043 | case 'i': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1044 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1045 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1046 | appendstring(realbuffer); |
| 1047 | break; |
| 1048 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1049 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1050 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1051 | appendstring(realbuffer); |
| 1052 | break; |
| 1053 | case 's': |
| 1054 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1055 | /* unused, since we already have the result */ |
| 1056 | (void) va_arg(vargs, char *); |
| 1057 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1058 | PyUnicode_GET_SIZE(*callresult)); |
| 1059 | s += PyUnicode_GET_SIZE(*callresult); |
| 1060 | /* We're done with the unicode()/repr() => forget it */ |
| 1061 | Py_DECREF(*callresult); |
| 1062 | /* switch to next unicode()/repr() result */ |
| 1063 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | } |
| 1066 | case 'U': |
| 1067 | { |
| 1068 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1069 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1070 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1071 | s += size; |
| 1072 | break; |
| 1073 | } |
| 1074 | case 'V': |
| 1075 | { |
| 1076 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1077 | const char *str = va_arg(vargs, const char *); |
| 1078 | if (obj) { |
| 1079 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1080 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1081 | s += size; |
| 1082 | } else { |
| 1083 | appendstring(str); |
| 1084 | } |
| 1085 | break; |
| 1086 | } |
| 1087 | case 'S': |
| 1088 | case 'R': |
| 1089 | { |
| 1090 | Py_UNICODE *ucopy; |
| 1091 | Py_ssize_t usize; |
| 1092 | Py_ssize_t upos; |
| 1093 | /* unused, since we already have the result */ |
| 1094 | (void) va_arg(vargs, PyObject *); |
| 1095 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1096 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1097 | for (upos = 0; upos<usize;) |
| 1098 | *s++ = ucopy[upos++]; |
| 1099 | /* We're done with the unicode()/repr() => forget it */ |
| 1100 | Py_DECREF(*callresult); |
| 1101 | /* switch to next unicode()/repr() result */ |
| 1102 | ++callresult; |
| 1103 | break; |
| 1104 | } |
| 1105 | case 'p': |
| 1106 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1107 | /* %p is ill-defined: ensure leading 0x. */ |
| 1108 | if (buffer[1] == 'X') |
| 1109 | buffer[1] = 'x'; |
| 1110 | else if (buffer[1] != 'x') { |
| 1111 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1112 | buffer[0] = '0'; |
| 1113 | buffer[1] = 'x'; |
| 1114 | } |
| 1115 | appendstring(buffer); |
| 1116 | break; |
| 1117 | case '%': |
| 1118 | *s++ = '%'; |
| 1119 | break; |
| 1120 | default: |
| 1121 | appendstring(p); |
| 1122 | goto end; |
| 1123 | } |
| 1124 | } else |
| 1125 | *s++ = *f; |
| 1126 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1127 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1128 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1129 | if (callresults) |
| 1130 | PyObject_Free(callresults); |
| 1131 | if (abuffer) |
| 1132 | PyObject_Free(abuffer); |
| 1133 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1134 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1135 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1136 | if (callresults) { |
| 1137 | PyObject **callresult2 = callresults; |
| 1138 | while (callresult2 < callresult) { |
| 1139 | Py_DECREF(*callresult2); |
| 1140 | ++callresult2; |
| 1141 | } |
| 1142 | PyObject_Free(callresults); |
| 1143 | } |
| 1144 | if (abuffer) |
| 1145 | PyObject_Free(abuffer); |
| 1146 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | #undef appendstring |
| 1150 | |
| 1151 | PyObject * |
| 1152 | PyUnicode_FromFormat(const char *format, ...) |
| 1153 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1154 | PyObject* ret; |
| 1155 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1156 | |
| 1157 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1158 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1159 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1160 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1161 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1162 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1163 | va_end(vargs); |
| 1164 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1167 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1168 | wchar_t *w, |
| 1169 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1170 | { |
| 1171 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1172 | PyErr_BadInternalCall(); |
| 1173 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1174 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1175 | |
| 1176 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1177 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1178 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1180 | #ifdef HAVE_USABLE_WCHAR_T |
| 1181 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1182 | #else |
| 1183 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1184 | register Py_UNICODE *u; |
| 1185 | register Py_ssize_t i; |
| 1186 | u = PyUnicode_AS_UNICODE(unicode); |
| 1187 | for (i = size; i > 0; i--) |
| 1188 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1189 | } |
| 1190 | #endif |
| 1191 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1192 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1193 | return PyUnicode_GET_SIZE(unicode); |
| 1194 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1195 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | #endif |
| 1199 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1200 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1201 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1202 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1203 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1204 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1205 | PyErr_SetString(PyExc_ValueError, |
| 1206 | "chr() arg not in range(0x110000)"); |
| 1207 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1208 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1209 | |
| 1210 | #ifndef Py_UNICODE_WIDE |
| 1211 | if (ordinal > 0xffff) { |
| 1212 | ordinal -= 0x10000; |
| 1213 | s[0] = 0xD800 | (ordinal >> 10); |
| 1214 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1215 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1216 | } |
| 1217 | #endif |
| 1218 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1219 | s[0] = (Py_UNICODE)ordinal; |
| 1220 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1223 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1224 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1225 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1226 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1227 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1228 | Py_INCREF(obj); |
| 1229 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1230 | } |
| 1231 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1232 | /* For a Unicode subtype that's not a Unicode object, |
| 1233 | return a true Unicode object with the same data. */ |
| 1234 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1235 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1236 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1237 | PyErr_Format(PyExc_TypeError, |
| 1238 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1239 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1240 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1244 | const char *encoding, |
| 1245 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1246 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1247 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1248 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1249 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1251 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1252 | PyErr_BadInternalCall(); |
| 1253 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1254 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1255 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1256 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1257 | PyErr_SetString(PyExc_TypeError, |
| 1258 | "decoding str is not supported"); |
| 1259 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1260 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1263 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1264 | s = PyBytes_AS_STRING(obj); |
| 1265 | len = PyBytes_GET_SIZE(obj); |
| 1266 | } |
| 1267 | else if (PyByteArray_Check(obj)) { |
| 1268 | s = PyByteArray_AS_STRING(obj); |
| 1269 | len = PyByteArray_GET_SIZE(obj); |
| 1270 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1271 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1272 | /* Overwrite the error message with something more useful in |
| 1273 | case of a TypeError. */ |
| 1274 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1275 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1276 | "coercing to str: need string or buffer, " |
| 1277 | "%.80s found", |
| 1278 | Py_TYPE(obj)->tp_name); |
| 1279 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1280 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1281 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1282 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1283 | if (len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1284 | Py_INCREF(unicode_empty); |
| 1285 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1286 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1287 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1288 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1289 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1290 | return v; |
| 1291 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1292 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1293 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1297 | Py_ssize_t size, |
| 1298 | const char *encoding, |
| 1299 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1300 | { |
| 1301 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1302 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1303 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1304 | char *l; |
| 1305 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1306 | |
| 1307 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1308 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1309 | |
| 1310 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1311 | catch e.g. UTF_8 */ |
| 1312 | e = encoding; |
| 1313 | l = lower; |
| 1314 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1315 | if (ISUPPER(*e)) { |
| 1316 | *l++ = TOLOWER(*e++); |
| 1317 | } |
| 1318 | else if (*e == '_') { |
| 1319 | *l++ = '-'; |
| 1320 | e++; |
| 1321 | } |
| 1322 | else { |
| 1323 | *l++ = *e++; |
| 1324 | } |
| 1325 | } |
| 1326 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1329 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1330 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1331 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1332 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1333 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1334 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1335 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1336 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1337 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1338 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1339 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1340 | else if (strcmp(lower, "utf-16") == 0) |
| 1341 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1342 | else if (strcmp(lower, "utf-32") == 0) |
| 1343 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1344 | |
| 1345 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1346 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1347 | 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] | 1348 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1349 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1350 | if (buffer == NULL) |
| 1351 | goto onError; |
| 1352 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1353 | if (unicode == NULL) |
| 1354 | goto onError; |
| 1355 | if (!PyUnicode_Check(unicode)) { |
| 1356 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1357 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1358 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1359 | Py_DECREF(unicode); |
| 1360 | goto onError; |
| 1361 | } |
| 1362 | Py_DECREF(buffer); |
| 1363 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1364 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1365 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1366 | Py_XDECREF(buffer); |
| 1367 | return NULL; |
| 1368 | } |
| 1369 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1370 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1371 | const char *encoding, |
| 1372 | const char *errors) |
| 1373 | { |
| 1374 | PyObject *v; |
| 1375 | |
| 1376 | if (!PyUnicode_Check(unicode)) { |
| 1377 | PyErr_BadArgument(); |
| 1378 | goto onError; |
| 1379 | } |
| 1380 | |
| 1381 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1382 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1383 | |
| 1384 | /* Decode via the codec registry */ |
| 1385 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1386 | if (v == NULL) |
| 1387 | goto onError; |
| 1388 | return v; |
| 1389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1390 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1391 | return NULL; |
| 1392 | } |
| 1393 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1394 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1395 | const char *encoding, |
| 1396 | const char *errors) |
| 1397 | { |
| 1398 | PyObject *v; |
| 1399 | |
| 1400 | if (!PyUnicode_Check(unicode)) { |
| 1401 | PyErr_BadArgument(); |
| 1402 | goto onError; |
| 1403 | } |
| 1404 | |
| 1405 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1406 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1407 | |
| 1408 | /* Decode via the codec registry */ |
| 1409 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1410 | if (v == NULL) |
| 1411 | goto onError; |
| 1412 | if (!PyUnicode_Check(v)) { |
| 1413 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1414 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1415 | Py_TYPE(v)->tp_name); |
| 1416 | Py_DECREF(v); |
| 1417 | goto onError; |
| 1418 | } |
| 1419 | return v; |
| 1420 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1421 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1422 | return NULL; |
| 1423 | } |
| 1424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1425 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1426 | Py_ssize_t size, |
| 1427 | const char *encoding, |
| 1428 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1429 | { |
| 1430 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1432 | unicode = PyUnicode_FromUnicode(s, size); |
| 1433 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1434 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1435 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1436 | Py_DECREF(unicode); |
| 1437 | return v; |
| 1438 | } |
| 1439 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1440 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1441 | const char *encoding, |
| 1442 | const char *errors) |
| 1443 | { |
| 1444 | PyObject *v; |
| 1445 | |
| 1446 | if (!PyUnicode_Check(unicode)) { |
| 1447 | PyErr_BadArgument(); |
| 1448 | goto onError; |
| 1449 | } |
| 1450 | |
| 1451 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1452 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1453 | |
| 1454 | /* Encode via the codec registry */ |
| 1455 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1456 | if (v == NULL) |
| 1457 | goto onError; |
| 1458 | return v; |
| 1459 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1460 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1461 | return NULL; |
| 1462 | } |
| 1463 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1464 | PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode) |
| 1465 | { |
| 1466 | if (Py_FileSystemDefaultEncoding) |
| 1467 | return PyUnicode_AsEncodedString(unicode, |
| 1468 | Py_FileSystemDefaultEncoding, |
| 1469 | "surrogateescape"); |
| 1470 | else |
| 1471 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1472 | PyUnicode_GET_SIZE(unicode), |
| 1473 | "surrogateescape"); |
| 1474 | } |
| 1475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1476 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1477 | const char *encoding, |
| 1478 | const char *errors) |
| 1479 | { |
| 1480 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1481 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1482 | if (!PyUnicode_Check(unicode)) { |
| 1483 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1484 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1485 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1486 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1487 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1488 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1489 | |
| 1490 | /* Shortcuts for common default encodings */ |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1491 | if (strcmp(encoding, "utf-8") == 0) |
| 1492 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1493 | PyUnicode_GET_SIZE(unicode), |
| 1494 | errors); |
| 1495 | else if (strcmp(encoding, "latin-1") == 0) |
| 1496 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1497 | PyUnicode_GET_SIZE(unicode), |
| 1498 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1499 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1500 | else if (strcmp(encoding, "mbcs") == 0) |
| 1501 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1502 | PyUnicode_GET_SIZE(unicode), |
| 1503 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1504 | #endif |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1505 | else if (strcmp(encoding, "ascii") == 0) |
| 1506 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1507 | PyUnicode_GET_SIZE(unicode), |
| 1508 | errors); |
| 1509 | /* During bootstrap, we may need to find the encodings |
| 1510 | package, to load the file system encoding, and require the |
| 1511 | file system encoding in order to load the encodings |
| 1512 | package. |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1513 | |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1514 | Break out of this dependency by assuming that the path to |
| 1515 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1516 | instead, if the file system encoding is the locale's |
| 1517 | encoding. */ |
| 1518 | else if (Py_FileSystemDefaultEncoding && |
| 1519 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1520 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1521 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1522 | PyUnicode_GET_SIZE(unicode), |
| 1523 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1524 | |
| 1525 | /* Encode via the codec registry */ |
| 1526 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1527 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1528 | return NULL; |
| 1529 | |
| 1530 | /* The normal path */ |
| 1531 | if (PyBytes_Check(v)) |
| 1532 | return v; |
| 1533 | |
| 1534 | /* 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] | 1535 | if (PyByteArray_Check(v)) { |
| 1536 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1537 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1538 | PyOS_snprintf(msg, sizeof(msg), |
| 1539 | "encoder %s returned buffer instead of bytes", |
| 1540 | encoding); |
| 1541 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1542 | Py_DECREF(v); |
| 1543 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1544 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1545 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1546 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1547 | Py_DECREF(v); |
| 1548 | return b; |
| 1549 | } |
| 1550 | |
| 1551 | PyErr_Format(PyExc_TypeError, |
| 1552 | "encoder did not return a bytes object (type=%.400s)", |
| 1553 | Py_TYPE(v)->tp_name); |
| 1554 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1555 | return NULL; |
| 1556 | } |
| 1557 | |
| 1558 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1559 | const char *encoding, |
| 1560 | const char *errors) |
| 1561 | { |
| 1562 | PyObject *v; |
| 1563 | |
| 1564 | if (!PyUnicode_Check(unicode)) { |
| 1565 | PyErr_BadArgument(); |
| 1566 | goto onError; |
| 1567 | } |
| 1568 | |
| 1569 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1570 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* Encode via the codec registry */ |
| 1573 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1574 | if (v == NULL) |
| 1575 | goto onError; |
| 1576 | if (!PyUnicode_Check(v)) { |
| 1577 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1578 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1579 | Py_TYPE(v)->tp_name); |
| 1580 | Py_DECREF(v); |
| 1581 | goto onError; |
| 1582 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1583 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1584 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1585 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1586 | return NULL; |
| 1587 | } |
| 1588 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1589 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1590 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1591 | { |
| 1592 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1593 | if (v) |
| 1594 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1595 | if (errors != NULL) |
| 1596 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1597 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1598 | PyUnicode_GET_SIZE(unicode), |
| 1599 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1600 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1601 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1602 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1603 | return v; |
| 1604 | } |
| 1605 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1606 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1607 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1608 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1609 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1610 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1611 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1612 | PyObject* |
| 1613 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1614 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1615 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1616 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1617 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1618 | bootstrapping process where the codecs aren't ready yet. |
| 1619 | */ |
| 1620 | if (Py_FileSystemDefaultEncoding) { |
| 1621 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1622 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1623 | return PyUnicode_DecodeMBCS(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1624 | } |
| 1625 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1626 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1627 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1628 | } |
| 1629 | #endif |
| 1630 | return PyUnicode_Decode(s, size, |
| 1631 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1632 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1633 | } |
| 1634 | else { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1635 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1639 | /* Convert the argument to a bytes object, according to the file |
Gregory P. Smith | cc47d8c | 2010-02-27 08:33:11 +0000 | [diff] [blame] | 1640 | system encoding. The addr param must be a PyObject**. |
| 1641 | 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] | 1642 | |
| 1643 | int |
| 1644 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1645 | { |
| 1646 | PyObject *output = NULL; |
| 1647 | Py_ssize_t size; |
| 1648 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1649 | if (arg == NULL) { |
| 1650 | Py_DECREF(*(PyObject**)addr); |
| 1651 | return 1; |
| 1652 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1653 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1654 | output = arg; |
| 1655 | Py_INCREF(output); |
| 1656 | } |
| 1657 | else { |
| 1658 | arg = PyUnicode_FromObject(arg); |
| 1659 | if (!arg) |
| 1660 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1661 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1662 | Py_DECREF(arg); |
| 1663 | if (!output) |
| 1664 | return 0; |
| 1665 | if (!PyBytes_Check(output)) { |
| 1666 | Py_DECREF(output); |
| 1667 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1668 | return 0; |
| 1669 | } |
| 1670 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1671 | size = PyBytes_GET_SIZE(output); |
| 1672 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1673 | if (size != strlen(data)) { |
| 1674 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1675 | Py_DECREF(output); |
| 1676 | return 0; |
| 1677 | } |
| 1678 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1679 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1683 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1684 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1685 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1686 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1687 | if (!PyUnicode_Check(unicode)) { |
| 1688 | PyErr_BadArgument(); |
| 1689 | return NULL; |
| 1690 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1691 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1692 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1693 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1694 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1695 | *psize = PyBytes_GET_SIZE(bytes); |
| 1696 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1700 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1701 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1702 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1705 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1706 | { |
| 1707 | if (!PyUnicode_Check(unicode)) { |
| 1708 | PyErr_BadArgument(); |
| 1709 | goto onError; |
| 1710 | } |
| 1711 | return PyUnicode_AS_UNICODE(unicode); |
| 1712 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1713 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1714 | return NULL; |
| 1715 | } |
| 1716 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1717 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1718 | { |
| 1719 | if (!PyUnicode_Check(unicode)) { |
| 1720 | PyErr_BadArgument(); |
| 1721 | goto onError; |
| 1722 | } |
| 1723 | return PyUnicode_GET_SIZE(unicode); |
| 1724 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1725 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1726 | return -1; |
| 1727 | } |
| 1728 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1729 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1730 | { |
| 1731 | return unicode_default_encoding; |
| 1732 | } |
| 1733 | |
| 1734 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1735 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1736 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1737 | PyErr_Format(PyExc_ValueError, |
| 1738 | "Can only set default encoding to %s", |
| 1739 | unicode_default_encoding); |
| 1740 | return -1; |
| 1741 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1742 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1745 | /* error handling callback helper: |
| 1746 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1747 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1748 | and adjust various state variables. |
| 1749 | return 0 on success, -1 on error |
| 1750 | */ |
| 1751 | |
| 1752 | static |
| 1753 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1754 | const char *encoding, const char *reason, |
| 1755 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1756 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1757 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1758 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1759 | 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] | 1760 | |
| 1761 | PyObject *restuple = NULL; |
| 1762 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1763 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1764 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1765 | Py_ssize_t requiredsize; |
| 1766 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1767 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1768 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1769 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1770 | int res = -1; |
| 1771 | |
| 1772 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1773 | *errorHandler = PyCodec_LookupError(errors); |
| 1774 | if (*errorHandler == NULL) |
| 1775 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1779 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1780 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
| 1781 | if (*exceptionObject == NULL) |
| 1782 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1783 | } |
| 1784 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1785 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1786 | goto onError; |
| 1787 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1788 | goto onError; |
| 1789 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1790 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1794 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1795 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1796 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1797 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1798 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1799 | } |
| 1800 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1801 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1802 | |
| 1803 | /* Copy back the bytes variables, which might have been modified by the |
| 1804 | callback */ |
| 1805 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1806 | if (!inputobj) |
| 1807 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1808 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1809 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1810 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1811 | *input = PyBytes_AS_STRING(inputobj); |
| 1812 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1813 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1814 | /* we can DECREF safely, as the exception has another reference, |
| 1815 | so the object won't go away. */ |
| 1816 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1817 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1818 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1819 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1820 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1821 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1822 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1823 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1824 | |
| 1825 | /* need more space? (at least enough for what we |
| 1826 | have+the replacement+the rest of the string (starting |
| 1827 | at the new input position), so we won't have to check space |
| 1828 | when there are no errors in the rest of the string) */ |
| 1829 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1830 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1831 | requiredsize = *outpos + repsize + insize-newpos; |
| 1832 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1833 | if (requiredsize<2*outsize) |
| 1834 | requiredsize = 2*outsize; |
| 1835 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1836 | goto onError; |
| 1837 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1838 | } |
| 1839 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1840 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1841 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1842 | *outptr += repsize; |
| 1843 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1844 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1845 | /* we made it! */ |
| 1846 | res = 0; |
| 1847 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1848 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1849 | Py_XDECREF(restuple); |
| 1850 | return res; |
| 1851 | } |
| 1852 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1853 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1854 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1855 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1856 | |
| 1857 | /* Three simple macros defining base-64. */ |
| 1858 | |
| 1859 | /* Is c a base-64 character? */ |
| 1860 | |
| 1861 | #define IS_BASE64(c) \ |
| 1862 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1863 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1864 | ((c) >= '0' && (c) <= '9') || \ |
| 1865 | (c) == '+' || (c) == '/') |
| 1866 | |
| 1867 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1868 | |
| 1869 | #define FROM_BASE64(c) \ |
| 1870 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1871 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1872 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1873 | (c) == '+' ? 62 : 63) |
| 1874 | |
| 1875 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1876 | |
| 1877 | #define TO_BASE64(n) \ |
| 1878 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1879 | |
| 1880 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1881 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1882 | * byte not decoding to itself is the + which begins a base64 |
| 1883 | * string. */ |
| 1884 | |
| 1885 | #define DECODE_DIRECT(c) \ |
| 1886 | ((c) <= 127 && (c) != '+') |
| 1887 | |
| 1888 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1889 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1890 | * the above). See RFC2152. This array identifies these different |
| 1891 | * sets: |
| 1892 | * 0 : "Set D" |
| 1893 | * alphanumeric and '(),-./:? |
| 1894 | * 1 : "Set O" |
| 1895 | * !"#$%&*;<=>@[]^_`{|} |
| 1896 | * 2 : "whitespace" |
| 1897 | * ht nl cr sp |
| 1898 | * 3 : special (must be base64 encoded) |
| 1899 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1900 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1901 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1902 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1903 | char utf7_category[128] = { |
| 1904 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 1905 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 1906 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 1907 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1908 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 1909 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 1910 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 1911 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 1912 | /* @ A B C D E F G H I J K L M N O */ |
| 1913 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1914 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 1915 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 1916 | /* ` a b c d e f g h i j k l m n o */ |
| 1917 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1918 | /* p q r s t u v w x y z { | } ~ del */ |
| 1919 | 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] | 1920 | }; |
| 1921 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1922 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 1923 | * answer depends on whether we are encoding set O as itself, and also |
| 1924 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 1925 | * clear that the answers to these questions vary between |
| 1926 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1927 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1928 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 1929 | ((c) < 128 && (c) > 0 && \ |
| 1930 | ((utf7_category[(c)] == 0) || \ |
| 1931 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 1932 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1933 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1934 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1935 | Py_ssize_t size, |
| 1936 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1937 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1938 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1939 | } |
| 1940 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1941 | /* The decoder. The only state we preserve is our read position, |
| 1942 | * i.e. how many characters we have consumed. So if we end in the |
| 1943 | * middle of a shift sequence we have to back off the read position |
| 1944 | * and the output to the beginning of the sequence, otherwise we lose |
| 1945 | * all the shift state (seen bits, number of bits seen, high |
| 1946 | * surrogate). */ |
| 1947 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1948 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1949 | Py_ssize_t size, |
| 1950 | const char *errors, |
| 1951 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1952 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1953 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1954 | Py_ssize_t startinpos; |
| 1955 | Py_ssize_t endinpos; |
| 1956 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1957 | const char *e; |
| 1958 | PyUnicodeObject *unicode; |
| 1959 | Py_UNICODE *p; |
| 1960 | const char *errmsg = ""; |
| 1961 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1962 | Py_UNICODE *shiftOutStart; |
| 1963 | unsigned int base64bits = 0; |
| 1964 | unsigned long base64buffer = 0; |
| 1965 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1966 | PyObject *errorHandler = NULL; |
| 1967 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1968 | |
| 1969 | unicode = _PyUnicode_New(size); |
| 1970 | if (!unicode) |
| 1971 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1972 | if (size == 0) { |
| 1973 | if (consumed) |
| 1974 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1975 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1976 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1977 | |
| 1978 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1979 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1980 | e = s + size; |
| 1981 | |
| 1982 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1983 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1984 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1985 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1986 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1987 | if (inShift) { /* in a base-64 section */ |
| 1988 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 1989 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 1990 | base64bits += 6; |
| 1991 | s++; |
| 1992 | if (base64bits >= 16) { |
| 1993 | /* we have enough bits for a UTF-16 value */ |
| 1994 | Py_UNICODE outCh = (Py_UNICODE) |
| 1995 | (base64buffer >> (base64bits-16)); |
| 1996 | base64bits -= 16; |
| 1997 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 1998 | if (surrogate) { |
| 1999 | /* expecting a second surrogate */ |
| 2000 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2001 | #ifdef Py_UNICODE_WIDE |
| 2002 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2003 | | (outCh & 0x3FF)) + 0x10000; |
| 2004 | #else |
| 2005 | *p++ = surrogate; |
| 2006 | *p++ = outCh; |
| 2007 | #endif |
| 2008 | surrogate = 0; |
| 2009 | } |
| 2010 | else { |
| 2011 | surrogate = 0; |
| 2012 | errmsg = "second surrogate missing"; |
| 2013 | goto utf7Error; |
| 2014 | } |
| 2015 | } |
| 2016 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2017 | /* first surrogate */ |
| 2018 | surrogate = outCh; |
| 2019 | } |
| 2020 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2021 | errmsg = "unexpected second surrogate"; |
| 2022 | goto utf7Error; |
| 2023 | } |
| 2024 | else { |
| 2025 | *p++ = outCh; |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2030 | inShift = 0; |
| 2031 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2032 | if (surrogate) { |
| 2033 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2034 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2035 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2036 | if (base64bits > 0) { /* left-over bits */ |
| 2037 | if (base64bits >= 6) { |
| 2038 | /* We've seen at least one base-64 character */ |
| 2039 | errmsg = "partial character in shift sequence"; |
| 2040 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2041 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2042 | else { |
| 2043 | /* Some bits remain; they should be zero */ |
| 2044 | if (base64buffer != 0) { |
| 2045 | errmsg = "non-zero padding bits in shift sequence"; |
| 2046 | goto utf7Error; |
| 2047 | } |
| 2048 | } |
| 2049 | } |
| 2050 | if (ch != '-') { |
| 2051 | /* '-' is absorbed; other terminating |
| 2052 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2053 | *p++ = ch; |
| 2054 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2058 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2059 | s++; /* consume '+' */ |
| 2060 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2061 | s++; |
| 2062 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2063 | } |
| 2064 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2065 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2066 | shiftOutStart = p; |
| 2067 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2068 | } |
| 2069 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2070 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2071 | *p++ = ch; |
| 2072 | s++; |
| 2073 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2074 | else { |
| 2075 | startinpos = s-starts; |
| 2076 | s++; |
| 2077 | errmsg = "unexpected special character"; |
| 2078 | goto utf7Error; |
| 2079 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2080 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2081 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2082 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2083 | endinpos = s-starts; |
| 2084 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2085 | errors, &errorHandler, |
| 2086 | "utf7", errmsg, |
| 2087 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2088 | &unicode, &outpos, &p)) |
| 2089 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2092 | /* end of string */ |
| 2093 | |
| 2094 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2095 | /* if we're in an inconsistent state, that's an error */ |
| 2096 | if (surrogate || |
| 2097 | (base64bits >= 6) || |
| 2098 | (base64bits > 0 && base64buffer != 0)) { |
| 2099 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2100 | endinpos = size; |
| 2101 | if (unicode_decode_call_errorhandler( |
| 2102 | errors, &errorHandler, |
| 2103 | "utf7", "unterminated shift sequence", |
| 2104 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2105 | &unicode, &outpos, &p)) |
| 2106 | goto onError; |
| 2107 | if (s < e) |
| 2108 | goto restart; |
| 2109 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2110 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2111 | |
| 2112 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2113 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2114 | if (inShift) { |
| 2115 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2116 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2117 | } |
| 2118 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2119 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2120 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2121 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2122 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2123 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2124 | goto onError; |
| 2125 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2126 | Py_XDECREF(errorHandler); |
| 2127 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2128 | return (PyObject *)unicode; |
| 2129 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2130 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2131 | Py_XDECREF(errorHandler); |
| 2132 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2133 | Py_DECREF(unicode); |
| 2134 | return NULL; |
| 2135 | } |
| 2136 | |
| 2137 | |
| 2138 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2139 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2140 | int base64SetO, |
| 2141 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2142 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2143 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2144 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2145 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2146 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2147 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2148 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2149 | unsigned int base64bits = 0; |
| 2150 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2151 | char * out; |
| 2152 | char * start; |
| 2153 | |
| 2154 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2155 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2156 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2157 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2158 | return PyErr_NoMemory(); |
| 2159 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2160 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2161 | if (v == NULL) |
| 2162 | return NULL; |
| 2163 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2164 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2165 | for (;i < size; ++i) { |
| 2166 | Py_UNICODE ch = s[i]; |
| 2167 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2168 | if (inShift) { |
| 2169 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2170 | /* shifting out */ |
| 2171 | if (base64bits) { /* output remaining bits */ |
| 2172 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2173 | base64buffer = 0; |
| 2174 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2175 | } |
| 2176 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2177 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2178 | so no '-' is required, except if the character is itself a '-' */ |
| 2179 | if (IS_BASE64(ch) || ch == '-') { |
| 2180 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2181 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2182 | *out++ = (char) ch; |
| 2183 | } |
| 2184 | else { |
| 2185 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2186 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2187 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2188 | else { /* not in a shift sequence */ |
| 2189 | if (ch == '+') { |
| 2190 | *out++ = '+'; |
| 2191 | *out++ = '-'; |
| 2192 | } |
| 2193 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2194 | *out++ = (char) ch; |
| 2195 | } |
| 2196 | else { |
| 2197 | *out++ = '+'; |
| 2198 | inShift = 1; |
| 2199 | goto encode_char; |
| 2200 | } |
| 2201 | } |
| 2202 | continue; |
| 2203 | encode_char: |
| 2204 | #ifdef Py_UNICODE_WIDE |
| 2205 | if (ch >= 0x10000) { |
| 2206 | /* code first surrogate */ |
| 2207 | base64bits += 16; |
| 2208 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2209 | while (base64bits >= 6) { |
| 2210 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2211 | base64bits -= 6; |
| 2212 | } |
| 2213 | /* prepare second surrogate */ |
| 2214 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2215 | } |
| 2216 | #endif |
| 2217 | base64bits += 16; |
| 2218 | base64buffer = (base64buffer << 16) | ch; |
| 2219 | while (base64bits >= 6) { |
| 2220 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2221 | base64bits -= 6; |
| 2222 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2223 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2224 | if (base64bits) |
| 2225 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2226 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2227 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2228 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2229 | return NULL; |
| 2230 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2233 | #undef IS_BASE64 |
| 2234 | #undef FROM_BASE64 |
| 2235 | #undef TO_BASE64 |
| 2236 | #undef DECODE_DIRECT |
| 2237 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2238 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2239 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2240 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2241 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2242 | char utf8_code_length[256] = { |
| 2243 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 2244 | illegal prefix. see RFC 2279 for details */ |
| 2245 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2246 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2247 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2248 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2249 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2250 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2251 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2252 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2253 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2254 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2255 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2256 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2257 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2258 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2259 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2260 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 2261 | }; |
| 2262 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2263 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2264 | Py_ssize_t size, |
| 2265 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2266 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2267 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2268 | } |
| 2269 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2270 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2271 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2272 | |
| 2273 | /* Mask to quickly check whether a C 'long' contains a |
| 2274 | non-ASCII, UTF8-encoded char. */ |
| 2275 | #if (SIZEOF_LONG == 8) |
| 2276 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2277 | #elif (SIZEOF_LONG == 4) |
| 2278 | # define ASCII_CHAR_MASK 0x80808080L |
| 2279 | #else |
| 2280 | # error C 'long' size should be either 4 or 8! |
| 2281 | #endif |
| 2282 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2283 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2284 | Py_ssize_t size, |
| 2285 | const char *errors, |
| 2286 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2287 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2288 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2289 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2290 | Py_ssize_t startinpos; |
| 2291 | Py_ssize_t endinpos; |
| 2292 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2293 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2294 | PyUnicodeObject *unicode; |
| 2295 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2296 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2297 | PyObject *errorHandler = NULL; |
| 2298 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2299 | |
| 2300 | /* Note: size will always be longer than the resulting Unicode |
| 2301 | character count */ |
| 2302 | unicode = _PyUnicode_New(size); |
| 2303 | if (!unicode) |
| 2304 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2305 | if (size == 0) { |
| 2306 | if (consumed) |
| 2307 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2308 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2309 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2310 | |
| 2311 | /* Unpack UTF-8 encoded data */ |
| 2312 | p = unicode->str; |
| 2313 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2314 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2315 | |
| 2316 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2317 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2318 | |
| 2319 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2320 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2321 | input will consist of an overwhelming majority of ASCII |
| 2322 | characters, we try to optimize for this case by checking |
| 2323 | as many characters as a C 'long' can contain. |
| 2324 | First, check if we can do an aligned read, as most CPUs have |
| 2325 | a penalty for unaligned reads. |
| 2326 | */ |
| 2327 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2328 | /* Help register allocation */ |
| 2329 | register const char *_s = s; |
| 2330 | register Py_UNICODE *_p = p; |
| 2331 | while (_s < aligned_end) { |
| 2332 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2333 | and do a fast unrolled copy if it only contains ASCII |
| 2334 | characters. */ |
| 2335 | unsigned long data = *(unsigned long *) _s; |
| 2336 | if (data & ASCII_CHAR_MASK) |
| 2337 | break; |
| 2338 | _p[0] = (unsigned char) _s[0]; |
| 2339 | _p[1] = (unsigned char) _s[1]; |
| 2340 | _p[2] = (unsigned char) _s[2]; |
| 2341 | _p[3] = (unsigned char) _s[3]; |
| 2342 | #if (SIZEOF_LONG == 8) |
| 2343 | _p[4] = (unsigned char) _s[4]; |
| 2344 | _p[5] = (unsigned char) _s[5]; |
| 2345 | _p[6] = (unsigned char) _s[6]; |
| 2346 | _p[7] = (unsigned char) _s[7]; |
| 2347 | #endif |
| 2348 | _s += SIZEOF_LONG; |
| 2349 | _p += SIZEOF_LONG; |
| 2350 | } |
| 2351 | s = _s; |
| 2352 | p = _p; |
| 2353 | if (s == e) |
| 2354 | break; |
| 2355 | ch = (unsigned char)*s; |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2360 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2361 | s++; |
| 2362 | continue; |
| 2363 | } |
| 2364 | |
| 2365 | n = utf8_code_length[ch]; |
| 2366 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2367 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2368 | if (consumed) |
| 2369 | break; |
| 2370 | else { |
| 2371 | errmsg = "unexpected end of data"; |
| 2372 | startinpos = s-starts; |
| 2373 | endinpos = size; |
| 2374 | goto utf8Error; |
| 2375 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2376 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2377 | |
| 2378 | switch (n) { |
| 2379 | |
| 2380 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2381 | errmsg = "unexpected code byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2382 | startinpos = s-starts; |
| 2383 | endinpos = startinpos+1; |
| 2384 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2385 | |
| 2386 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2387 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2388 | startinpos = s-starts; |
| 2389 | endinpos = startinpos+1; |
| 2390 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2391 | |
| 2392 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2393 | if ((s[1] & 0xc0) != 0x80) { |
| 2394 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2395 | startinpos = s-starts; |
| 2396 | endinpos = startinpos+2; |
| 2397 | goto utf8Error; |
| 2398 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2399 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2400 | if (ch < 0x80) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2401 | startinpos = s-starts; |
| 2402 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2403 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2404 | goto utf8Error; |
| 2405 | } |
| 2406 | else |
| 2407 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2408 | break; |
| 2409 | |
| 2410 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2411 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2412 | (s[2] & 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+3; |
| 2416 | goto utf8Error; |
| 2417 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2418 | 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] | 2419 | if (ch < 0x0800 || (ch >= 0xd800 && ch <= 0xDFFF)) { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2420 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2421 | startinpos = s-starts; |
| 2422 | endinpos = startinpos+3; |
| 2423 | goto utf8Error; |
| 2424 | } |
| 2425 | else |
| 2426 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2427 | break; |
| 2428 | |
| 2429 | case 4: |
| 2430 | if ((s[1] & 0xc0) != 0x80 || |
| 2431 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2432 | (s[3] & 0xc0) != 0x80) { |
| 2433 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2434 | startinpos = s-starts; |
| 2435 | endinpos = startinpos+4; |
| 2436 | goto utf8Error; |
| 2437 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2438 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2439 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2440 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2441 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2442 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2443 | || (ch > 0x10ffff)) /* maximum value allowed for |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2444 | UTF-16 */ |
| 2445 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2446 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2447 | startinpos = s-starts; |
| 2448 | endinpos = startinpos+4; |
| 2449 | goto utf8Error; |
| 2450 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2451 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2452 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2453 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2454 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2455 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2456 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2457 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2458 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2459 | /* high surrogate = top 10 bits added to D800 */ |
| 2460 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2461 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2462 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2463 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2464 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2465 | break; |
| 2466 | |
| 2467 | default: |
| 2468 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2469 | errmsg = "unsupported Unicode code range"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2470 | startinpos = s-starts; |
| 2471 | endinpos = startinpos+n; |
| 2472 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2473 | } |
| 2474 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2475 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2476 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2477 | utf8Error: |
| 2478 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2479 | if (unicode_decode_call_errorhandler( |
| 2480 | errors, &errorHandler, |
| 2481 | "utf8", errmsg, |
| 2482 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2483 | &unicode, &outpos, &p)) |
| 2484 | goto onError; |
| 2485 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2486 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2487 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2488 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2489 | |
| 2490 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2491 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2492 | goto onError; |
| 2493 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2494 | Py_XDECREF(errorHandler); |
| 2495 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2496 | return (PyObject *)unicode; |
| 2497 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2498 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2499 | Py_XDECREF(errorHandler); |
| 2500 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2501 | Py_DECREF(unicode); |
| 2502 | return NULL; |
| 2503 | } |
| 2504 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2505 | #undef ASCII_CHAR_MASK |
| 2506 | |
| 2507 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2508 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2509 | and allocate exactly as much space needed at the end. Else allocate the |
| 2510 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2511 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2512 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2513 | PyObject * |
| 2514 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2515 | Py_ssize_t size, |
| 2516 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2517 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2518 | #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] | 2519 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2520 | Py_ssize_t i; /* index into s of next input byte */ |
| 2521 | PyObject *result; /* result string object */ |
| 2522 | char *p; /* next free byte in output buffer */ |
| 2523 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2524 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2525 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2526 | PyObject *errorHandler = NULL; |
| 2527 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2528 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2529 | assert(s != NULL); |
| 2530 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2531 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2532 | if (size <= MAX_SHORT_UNICHARS) { |
| 2533 | /* Write into the stack buffer; nallocated can't overflow. |
| 2534 | * At the end, we'll allocate exactly as much heap space as it |
| 2535 | * turns out we need. |
| 2536 | */ |
| 2537 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2538 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2539 | p = stackbuf; |
| 2540 | } |
| 2541 | else { |
| 2542 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2543 | nallocated = size * 4; |
| 2544 | if (nallocated / 4 != size) /* overflow! */ |
| 2545 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2546 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2547 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2548 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2549 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2550 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2551 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2552 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2553 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2554 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2555 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2556 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2557 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2559 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2560 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2561 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2562 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2563 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2564 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2565 | /* Special case: check for high and low surrogate */ |
| 2566 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2567 | Py_UCS4 ch2 = s[i]; |
| 2568 | /* Combine the two surrogates to form a UCS4 value */ |
| 2569 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2570 | i++; |
| 2571 | |
| 2572 | /* Encode UCS4 Unicode ordinals */ |
| 2573 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2574 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2575 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2576 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2577 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2578 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2579 | Py_ssize_t newpos; |
| 2580 | PyObject *rep; |
| 2581 | Py_ssize_t repsize, k; |
| 2582 | rep = unicode_encode_call_errorhandler |
| 2583 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2584 | s, size, &exc, i-1, i, &newpos); |
| 2585 | if (!rep) |
| 2586 | goto error; |
| 2587 | |
| 2588 | if (PyBytes_Check(rep)) |
| 2589 | repsize = PyBytes_GET_SIZE(rep); |
| 2590 | else |
| 2591 | repsize = PyUnicode_GET_SIZE(rep); |
| 2592 | |
| 2593 | if (repsize > 4) { |
| 2594 | Py_ssize_t offset; |
| 2595 | |
| 2596 | if (result == NULL) |
| 2597 | offset = p - stackbuf; |
| 2598 | else |
| 2599 | offset = p - PyBytes_AS_STRING(result); |
| 2600 | |
| 2601 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2602 | /* integer overflow */ |
| 2603 | PyErr_NoMemory(); |
| 2604 | goto error; |
| 2605 | } |
| 2606 | nallocated += repsize - 4; |
| 2607 | if (result != NULL) { |
| 2608 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2609 | goto error; |
| 2610 | } else { |
| 2611 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2612 | if (result == NULL) |
| 2613 | goto error; |
| 2614 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2615 | } |
| 2616 | p = PyBytes_AS_STRING(result) + offset; |
| 2617 | } |
| 2618 | |
| 2619 | if (PyBytes_Check(rep)) { |
| 2620 | char *prep = PyBytes_AS_STRING(rep); |
| 2621 | for(k = repsize; k > 0; k--) |
| 2622 | *p++ = *prep++; |
| 2623 | } else /* rep is unicode */ { |
| 2624 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2625 | Py_UNICODE c; |
| 2626 | |
| 2627 | for(k=0; k<repsize; k++) { |
| 2628 | c = prep[k]; |
| 2629 | if (0x80 <= c) { |
| 2630 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2631 | i-1, i, "surrogates not allowed"); |
| 2632 | goto error; |
| 2633 | } |
| 2634 | *p++ = (char)prep[k]; |
| 2635 | } |
| 2636 | } |
| 2637 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2638 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2639 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2640 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2641 | } else if (ch < 0x10000) { |
| 2642 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2643 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2644 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2645 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2646 | /* Encode UCS4 Unicode ordinals */ |
| 2647 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2648 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2649 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2650 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2651 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2652 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2653 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2654 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2655 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2656 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2657 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2658 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2659 | } |
| 2660 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2661 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2662 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2663 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2664 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2665 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2666 | Py_XDECREF(errorHandler); |
| 2667 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2668 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2669 | error: |
| 2670 | Py_XDECREF(errorHandler); |
| 2671 | Py_XDECREF(exc); |
| 2672 | Py_XDECREF(result); |
| 2673 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2674 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2675 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2678 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2679 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2680 | if (!PyUnicode_Check(unicode)) { |
| 2681 | PyErr_BadArgument(); |
| 2682 | return NULL; |
| 2683 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2684 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2685 | PyUnicode_GET_SIZE(unicode), |
| 2686 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2689 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2690 | |
| 2691 | PyObject * |
| 2692 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2693 | Py_ssize_t size, |
| 2694 | const char *errors, |
| 2695 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2696 | { |
| 2697 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2698 | } |
| 2699 | |
| 2700 | PyObject * |
| 2701 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2702 | Py_ssize_t size, |
| 2703 | const char *errors, |
| 2704 | int *byteorder, |
| 2705 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2706 | { |
| 2707 | const char *starts = s; |
| 2708 | Py_ssize_t startinpos; |
| 2709 | Py_ssize_t endinpos; |
| 2710 | Py_ssize_t outpos; |
| 2711 | PyUnicodeObject *unicode; |
| 2712 | Py_UNICODE *p; |
| 2713 | #ifndef Py_UNICODE_WIDE |
| 2714 | int i, pairs; |
| 2715 | #else |
| 2716 | const int pairs = 0; |
| 2717 | #endif |
| 2718 | const unsigned char *q, *e; |
| 2719 | int bo = 0; /* assume native ordering by default */ |
| 2720 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2721 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2722 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2723 | int iorder[] = {0, 1, 2, 3}; |
| 2724 | #else |
| 2725 | int iorder[] = {3, 2, 1, 0}; |
| 2726 | #endif |
| 2727 | PyObject *errorHandler = NULL; |
| 2728 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2729 | /* On narrow builds we split characters outside the BMP into two |
| 2730 | codepoints => count how much extra space we need. */ |
| 2731 | #ifndef Py_UNICODE_WIDE |
| 2732 | for (i = pairs = 0; i < size/4; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2733 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2734 | pairs++; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2735 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2736 | |
| 2737 | /* This might be one to much, because of a BOM */ |
| 2738 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2739 | if (!unicode) |
| 2740 | return NULL; |
| 2741 | if (size == 0) |
| 2742 | return (PyObject *)unicode; |
| 2743 | |
| 2744 | /* Unpack UTF-32 encoded data */ |
| 2745 | p = unicode->str; |
| 2746 | q = (unsigned char *)s; |
| 2747 | e = q + size; |
| 2748 | |
| 2749 | if (byteorder) |
| 2750 | bo = *byteorder; |
| 2751 | |
| 2752 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2753 | byte order setting accordingly. In native mode, the leading BOM |
| 2754 | mark is skipped, in all other modes, it is copied to the output |
| 2755 | stream as-is (giving a ZWNBSP character). */ |
| 2756 | if (bo == 0) { |
| 2757 | if (size >= 4) { |
| 2758 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2759 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2760 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2761 | if (bom == 0x0000FEFF) { |
| 2762 | q += 4; |
| 2763 | bo = -1; |
| 2764 | } |
| 2765 | else if (bom == 0xFFFE0000) { |
| 2766 | q += 4; |
| 2767 | bo = 1; |
| 2768 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2769 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2770 | if (bom == 0x0000FEFF) { |
| 2771 | q += 4; |
| 2772 | bo = 1; |
| 2773 | } |
| 2774 | else if (bom == 0xFFFE0000) { |
| 2775 | q += 4; |
| 2776 | bo = -1; |
| 2777 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2778 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2779 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | if (bo == -1) { |
| 2783 | /* force LE */ |
| 2784 | iorder[0] = 0; |
| 2785 | iorder[1] = 1; |
| 2786 | iorder[2] = 2; |
| 2787 | iorder[3] = 3; |
| 2788 | } |
| 2789 | else if (bo == 1) { |
| 2790 | /* force BE */ |
| 2791 | iorder[0] = 3; |
| 2792 | iorder[1] = 2; |
| 2793 | iorder[2] = 1; |
| 2794 | iorder[3] = 0; |
| 2795 | } |
| 2796 | |
| 2797 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2798 | Py_UCS4 ch; |
| 2799 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2800 | if (e-q<4) { |
| 2801 | if (consumed) |
| 2802 | break; |
| 2803 | errmsg = "truncated data"; |
| 2804 | startinpos = ((const char *)q)-starts; |
| 2805 | endinpos = ((const char *)e)-starts; |
| 2806 | goto utf32Error; |
| 2807 | /* The remaining input chars are ignored if the callback |
| 2808 | chooses to skip the input */ |
| 2809 | } |
| 2810 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2811 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2812 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2813 | if (ch >= 0x110000) |
| 2814 | { |
| 2815 | errmsg = "codepoint not in range(0x110000)"; |
| 2816 | startinpos = ((const char *)q)-starts; |
| 2817 | endinpos = startinpos+4; |
| 2818 | goto utf32Error; |
| 2819 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2820 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2821 | if (ch >= 0x10000) |
| 2822 | { |
| 2823 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2824 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2825 | } |
| 2826 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2827 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2828 | *p++ = ch; |
| 2829 | q += 4; |
| 2830 | continue; |
| 2831 | utf32Error: |
| 2832 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2833 | if (unicode_decode_call_errorhandler( |
| 2834 | errors, &errorHandler, |
| 2835 | "utf32", errmsg, |
| 2836 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2837 | &unicode, &outpos, &p)) |
| 2838 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | if (byteorder) |
| 2842 | *byteorder = bo; |
| 2843 | |
| 2844 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2845 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2846 | |
| 2847 | /* Adjust length */ |
| 2848 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2849 | goto onError; |
| 2850 | |
| 2851 | Py_XDECREF(errorHandler); |
| 2852 | Py_XDECREF(exc); |
| 2853 | return (PyObject *)unicode; |
| 2854 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2855 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2856 | Py_DECREF(unicode); |
| 2857 | Py_XDECREF(errorHandler); |
| 2858 | Py_XDECREF(exc); |
| 2859 | return NULL; |
| 2860 | } |
| 2861 | |
| 2862 | PyObject * |
| 2863 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2864 | Py_ssize_t size, |
| 2865 | const char *errors, |
| 2866 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2867 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2868 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2869 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2870 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2871 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2872 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2873 | #else |
| 2874 | const int pairs = 0; |
| 2875 | #endif |
| 2876 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2877 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2878 | int iorder[] = {0, 1, 2, 3}; |
| 2879 | #else |
| 2880 | int iorder[] = {3, 2, 1, 0}; |
| 2881 | #endif |
| 2882 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2883 | #define STORECHAR(CH) \ |
| 2884 | do { \ |
| 2885 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2886 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2887 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2888 | p[iorder[0]] = (CH) & 0xff; \ |
| 2889 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2890 | } while(0) |
| 2891 | |
| 2892 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2893 | so we need less space. */ |
| 2894 | #ifndef Py_UNICODE_WIDE |
| 2895 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2896 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2897 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2898 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2899 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2900 | nsize = (size - pairs + (byteorder == 0)); |
| 2901 | bytesize = nsize * 4; |
| 2902 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2903 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2904 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2905 | if (v == NULL) |
| 2906 | return NULL; |
| 2907 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2908 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2909 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2910 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2911 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2912 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2913 | |
| 2914 | if (byteorder == -1) { |
| 2915 | /* force LE */ |
| 2916 | iorder[0] = 0; |
| 2917 | iorder[1] = 1; |
| 2918 | iorder[2] = 2; |
| 2919 | iorder[3] = 3; |
| 2920 | } |
| 2921 | else if (byteorder == 1) { |
| 2922 | /* force BE */ |
| 2923 | iorder[0] = 3; |
| 2924 | iorder[1] = 2; |
| 2925 | iorder[2] = 1; |
| 2926 | iorder[3] = 0; |
| 2927 | } |
| 2928 | |
| 2929 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2930 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2931 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2932 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2933 | Py_UCS4 ch2 = *s; |
| 2934 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2935 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2936 | s++; |
| 2937 | size--; |
| 2938 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2939 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2940 | #endif |
| 2941 | STORECHAR(ch); |
| 2942 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2943 | |
| 2944 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2945 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2946 | #undef STORECHAR |
| 2947 | } |
| 2948 | |
| 2949 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2950 | { |
| 2951 | if (!PyUnicode_Check(unicode)) { |
| 2952 | PyErr_BadArgument(); |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2956 | PyUnicode_GET_SIZE(unicode), |
| 2957 | NULL, |
| 2958 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2961 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2962 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2963 | PyObject * |
| 2964 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2965 | Py_ssize_t size, |
| 2966 | const char *errors, |
| 2967 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2968 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2969 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2970 | } |
| 2971 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2972 | /* Two masks for fast checking of whether a C 'long' may contain |
| 2973 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 2974 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 2975 | rare in most input. |
| 2976 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 2977 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2978 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2979 | #if (SIZEOF_LONG == 8) |
| 2980 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 2981 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 2982 | #elif (SIZEOF_LONG == 4) |
| 2983 | # define FAST_CHAR_MASK 0x80008000L |
| 2984 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 2985 | #else |
| 2986 | # error C 'long' size should be either 4 or 8! |
| 2987 | #endif |
| 2988 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2989 | PyObject * |
| 2990 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2991 | Py_ssize_t size, |
| 2992 | const char *errors, |
| 2993 | int *byteorder, |
| 2994 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2995 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2996 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2997 | Py_ssize_t startinpos; |
| 2998 | Py_ssize_t endinpos; |
| 2999 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3000 | PyUnicodeObject *unicode; |
| 3001 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3002 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3003 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3004 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3005 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3006 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3007 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3008 | int ihi = 1, ilo = 0; |
| 3009 | #else |
| 3010 | int ihi = 0, ilo = 1; |
| 3011 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3012 | PyObject *errorHandler = NULL; |
| 3013 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3014 | |
| 3015 | /* Note: size will always be longer than the resulting Unicode |
| 3016 | character count */ |
| 3017 | unicode = _PyUnicode_New(size); |
| 3018 | if (!unicode) |
| 3019 | return NULL; |
| 3020 | if (size == 0) |
| 3021 | return (PyObject *)unicode; |
| 3022 | |
| 3023 | /* Unpack UTF-16 encoded data */ |
| 3024 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3025 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3026 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3027 | |
| 3028 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3029 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3030 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3031 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3032 | byte order setting accordingly. In native mode, the leading BOM |
| 3033 | mark is skipped, in all other modes, it is copied to the output |
| 3034 | stream as-is (giving a ZWNBSP character). */ |
| 3035 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3036 | if (size >= 2) { |
| 3037 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3038 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3039 | if (bom == 0xFEFF) { |
| 3040 | q += 2; |
| 3041 | bo = -1; |
| 3042 | } |
| 3043 | else if (bom == 0xFFFE) { |
| 3044 | q += 2; |
| 3045 | bo = 1; |
| 3046 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3047 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3048 | if (bom == 0xFEFF) { |
| 3049 | q += 2; |
| 3050 | bo = 1; |
| 3051 | } |
| 3052 | else if (bom == 0xFFFE) { |
| 3053 | q += 2; |
| 3054 | bo = -1; |
| 3055 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3056 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3057 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3058 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3059 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3060 | if (bo == -1) { |
| 3061 | /* force LE */ |
| 3062 | ihi = 1; |
| 3063 | ilo = 0; |
| 3064 | } |
| 3065 | else if (bo == 1) { |
| 3066 | /* force BE */ |
| 3067 | ihi = 0; |
| 3068 | ilo = 1; |
| 3069 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3070 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3071 | native_ordering = ilo < ihi; |
| 3072 | #else |
| 3073 | native_ordering = ilo > ihi; |
| 3074 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3075 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3076 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3077 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3078 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3079 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3080 | reads are more expensive, better to defer to another iteration. */ |
| 3081 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3082 | /* Fast path for runs of non-surrogate chars. */ |
| 3083 | register const unsigned char *_q = q; |
| 3084 | Py_UNICODE *_p = p; |
| 3085 | if (native_ordering) { |
| 3086 | /* Native ordering is simple: as long as the input cannot |
| 3087 | possibly contain a surrogate char, do an unrolled copy |
| 3088 | of several 16-bit code points to the target object. |
| 3089 | The non-surrogate check is done on several input bytes |
| 3090 | at a time (as many as a C 'long' can contain). */ |
| 3091 | while (_q < aligned_end) { |
| 3092 | unsigned long data = * (unsigned long *) _q; |
| 3093 | if (data & FAST_CHAR_MASK) |
| 3094 | break; |
| 3095 | _p[0] = ((unsigned short *) _q)[0]; |
| 3096 | _p[1] = ((unsigned short *) _q)[1]; |
| 3097 | #if (SIZEOF_LONG == 8) |
| 3098 | _p[2] = ((unsigned short *) _q)[2]; |
| 3099 | _p[3] = ((unsigned short *) _q)[3]; |
| 3100 | #endif |
| 3101 | _q += SIZEOF_LONG; |
| 3102 | _p += SIZEOF_LONG / 2; |
| 3103 | } |
| 3104 | } |
| 3105 | else { |
| 3106 | /* Byteswapped ordering is similar, but we must decompose |
| 3107 | the copy bytewise, and take care of zero'ing out the |
| 3108 | upper bytes if the target object is in 32-bit units |
| 3109 | (that is, in UCS-4 builds). */ |
| 3110 | while (_q < aligned_end) { |
| 3111 | unsigned long data = * (unsigned long *) _q; |
| 3112 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3113 | break; |
| 3114 | /* Zero upper bytes in UCS-4 builds */ |
| 3115 | #if (Py_UNICODE_SIZE > 2) |
| 3116 | _p[0] = 0; |
| 3117 | _p[1] = 0; |
| 3118 | #if (SIZEOF_LONG == 8) |
| 3119 | _p[2] = 0; |
| 3120 | _p[3] = 0; |
| 3121 | #endif |
| 3122 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3123 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3124 | fill the two last bytes of each 4-byte unit. */ |
| 3125 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3126 | # define OFF 2 |
| 3127 | #else |
| 3128 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3129 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3130 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3131 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3132 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3133 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3134 | #if (SIZEOF_LONG == 8) |
| 3135 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3136 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3137 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3138 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3139 | #endif |
| 3140 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3141 | _q += SIZEOF_LONG; |
| 3142 | _p += SIZEOF_LONG / 2; |
| 3143 | } |
| 3144 | } |
| 3145 | p = _p; |
| 3146 | q = _q; |
| 3147 | if (q >= e) |
| 3148 | break; |
| 3149 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3150 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3151 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3152 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3153 | |
| 3154 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3155 | *p++ = ch; |
| 3156 | continue; |
| 3157 | } |
| 3158 | |
| 3159 | /* UTF-16 code pair: */ |
| 3160 | if (q > e) { |
| 3161 | errmsg = "unexpected end of data"; |
| 3162 | startinpos = (((const char *)q) - 2) - starts; |
| 3163 | endinpos = ((const char *)e) + 1 - starts; |
| 3164 | goto utf16Error; |
| 3165 | } |
| 3166 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3167 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3168 | q += 2; |
| 3169 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3170 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3171 | *p++ = ch; |
| 3172 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3173 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3174 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3175 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3176 | continue; |
| 3177 | } |
| 3178 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3179 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3180 | startinpos = (((const char *)q)-4)-starts; |
| 3181 | endinpos = startinpos+2; |
| 3182 | goto utf16Error; |
| 3183 | } |
| 3184 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3185 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3186 | errmsg = "illegal encoding"; |
| 3187 | startinpos = (((const char *)q)-2)-starts; |
| 3188 | endinpos = startinpos+2; |
| 3189 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3190 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3191 | utf16Error: |
| 3192 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3193 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3194 | errors, |
| 3195 | &errorHandler, |
| 3196 | "utf16", errmsg, |
| 3197 | &starts, |
| 3198 | (const char **)&e, |
| 3199 | &startinpos, |
| 3200 | &endinpos, |
| 3201 | &exc, |
| 3202 | (const char **)&q, |
| 3203 | &unicode, |
| 3204 | &outpos, |
| 3205 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3206 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3207 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3208 | /* remaining byte at the end? (size should be even) */ |
| 3209 | if (e == q) { |
| 3210 | if (!consumed) { |
| 3211 | errmsg = "truncated data"; |
| 3212 | startinpos = ((const char *)q) - starts; |
| 3213 | endinpos = ((const char *)e) + 1 - starts; |
| 3214 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3215 | if (unicode_decode_call_errorhandler( |
| 3216 | errors, |
| 3217 | &errorHandler, |
| 3218 | "utf16", errmsg, |
| 3219 | &starts, |
| 3220 | (const char **)&e, |
| 3221 | &startinpos, |
| 3222 | &endinpos, |
| 3223 | &exc, |
| 3224 | (const char **)&q, |
| 3225 | &unicode, |
| 3226 | &outpos, |
| 3227 | &p)) |
| 3228 | goto onError; |
| 3229 | /* The remaining input chars are ignored if the callback |
| 3230 | chooses to skip the input */ |
| 3231 | } |
| 3232 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3233 | |
| 3234 | if (byteorder) |
| 3235 | *byteorder = bo; |
| 3236 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3237 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3238 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3240 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3241 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3242 | goto onError; |
| 3243 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3244 | Py_XDECREF(errorHandler); |
| 3245 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3246 | return (PyObject *)unicode; |
| 3247 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3248 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3249 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3250 | Py_XDECREF(errorHandler); |
| 3251 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3252 | return NULL; |
| 3253 | } |
| 3254 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3255 | #undef FAST_CHAR_MASK |
| 3256 | #undef SWAPPED_FAST_CHAR_MASK |
| 3257 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3258 | PyObject * |
| 3259 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3260 | Py_ssize_t size, |
| 3261 | const char *errors, |
| 3262 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3263 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3264 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3265 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3266 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3267 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3268 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3269 | #else |
| 3270 | const int pairs = 0; |
| 3271 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3272 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3273 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3274 | int ihi = 1, ilo = 0; |
| 3275 | #else |
| 3276 | int ihi = 0, ilo = 1; |
| 3277 | #endif |
| 3278 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3279 | #define STORECHAR(CH) \ |
| 3280 | do { \ |
| 3281 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3282 | p[ilo] = (CH) & 0xff; \ |
| 3283 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3284 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3285 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3286 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3287 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3288 | if (s[i] >= 0x10000) |
| 3289 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3290 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3291 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3292 | if (size > PY_SSIZE_T_MAX || |
| 3293 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3294 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3295 | nsize = size + pairs + (byteorder == 0); |
| 3296 | bytesize = nsize * 2; |
| 3297 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3298 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3299 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3300 | if (v == NULL) |
| 3301 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3302 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3303 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3304 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3305 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3306 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3307 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3308 | |
| 3309 | if (byteorder == -1) { |
| 3310 | /* force LE */ |
| 3311 | ihi = 1; |
| 3312 | ilo = 0; |
| 3313 | } |
| 3314 | else if (byteorder == 1) { |
| 3315 | /* force BE */ |
| 3316 | ihi = 0; |
| 3317 | ilo = 1; |
| 3318 | } |
| 3319 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3320 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3321 | Py_UNICODE ch = *s++; |
| 3322 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3323 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3324 | if (ch >= 0x10000) { |
| 3325 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3326 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3327 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3328 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3329 | STORECHAR(ch); |
| 3330 | if (ch2) |
| 3331 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3332 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3333 | |
| 3334 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3335 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3336 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3340 | { |
| 3341 | if (!PyUnicode_Check(unicode)) { |
| 3342 | PyErr_BadArgument(); |
| 3343 | return NULL; |
| 3344 | } |
| 3345 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3346 | PyUnicode_GET_SIZE(unicode), |
| 3347 | NULL, |
| 3348 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3352 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3353 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3354 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3355 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3356 | Py_ssize_t size, |
| 3357 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3358 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3359 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3360 | Py_ssize_t startinpos; |
| 3361 | Py_ssize_t endinpos; |
| 3362 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3363 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3364 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3365 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3366 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3367 | char* message; |
| 3368 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3369 | PyObject *errorHandler = NULL; |
| 3370 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3371 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3372 | /* Escaped strings will always be longer than the resulting |
| 3373 | 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] | 3374 | length after conversion to the true value. |
| 3375 | (but if the error callback returns a long replacement string |
| 3376 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3377 | v = _PyUnicode_New(size); |
| 3378 | if (v == NULL) |
| 3379 | goto onError; |
| 3380 | if (size == 0) |
| 3381 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3382 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3383 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3384 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3385 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3386 | while (s < end) { |
| 3387 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3388 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3389 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3390 | |
| 3391 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3392 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3393 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3394 | continue; |
| 3395 | } |
| 3396 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3397 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3398 | /* \ - Escapes */ |
| 3399 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3400 | c = *s++; |
| 3401 | if (s > end) |
| 3402 | c = '\0'; /* Invalid after \ */ |
| 3403 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3404 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3405 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3406 | case '\n': break; |
| 3407 | case '\\': *p++ = '\\'; break; |
| 3408 | case '\'': *p++ = '\''; break; |
| 3409 | case '\"': *p++ = '\"'; break; |
| 3410 | case 'b': *p++ = '\b'; break; |
| 3411 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3412 | case 't': *p++ = '\t'; break; |
| 3413 | case 'n': *p++ = '\n'; break; |
| 3414 | case 'r': *p++ = '\r'; break; |
| 3415 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3416 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3417 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3418 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3419 | case '0': case '1': case '2': case '3': |
| 3420 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3421 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3422 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3423 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3424 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3425 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3426 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3427 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3428 | break; |
| 3429 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3430 | /* hex escapes */ |
| 3431 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3432 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3433 | digits = 2; |
| 3434 | message = "truncated \\xXX escape"; |
| 3435 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3436 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3437 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3438 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3439 | digits = 4; |
| 3440 | message = "truncated \\uXXXX escape"; |
| 3441 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3442 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3443 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3444 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3445 | digits = 8; |
| 3446 | message = "truncated \\UXXXXXXXX escape"; |
| 3447 | hexescape: |
| 3448 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3449 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3450 | if (s+digits>end) { |
| 3451 | endinpos = size; |
| 3452 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3453 | errors, &errorHandler, |
| 3454 | "unicodeescape", "end of string in escape sequence", |
| 3455 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3456 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3457 | goto onError; |
| 3458 | goto nextByte; |
| 3459 | } |
| 3460 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3461 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3462 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3463 | endinpos = (s+i+1)-starts; |
| 3464 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3465 | errors, &errorHandler, |
| 3466 | "unicodeescape", message, |
| 3467 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3468 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3469 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3470 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3471 | } |
| 3472 | chr = (chr<<4) & ~0xF; |
| 3473 | if (c >= '0' && c <= '9') |
| 3474 | chr += c - '0'; |
| 3475 | else if (c >= 'a' && c <= 'f') |
| 3476 | chr += 10 + c - 'a'; |
| 3477 | else |
| 3478 | chr += 10 + c - 'A'; |
| 3479 | } |
| 3480 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3481 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3482 | /* _decoding_error will have already written into the |
| 3483 | target buffer. */ |
| 3484 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3485 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3486 | /* when we get here, chr is a 32-bit unicode character */ |
| 3487 | if (chr <= 0xffff) |
| 3488 | /* UCS-2 character */ |
| 3489 | *p++ = (Py_UNICODE) chr; |
| 3490 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3491 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3492 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3493 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3494 | *p++ = chr; |
| 3495 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3496 | chr -= 0x10000L; |
| 3497 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3498 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3499 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3500 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3501 | endinpos = s-starts; |
| 3502 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3503 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3504 | errors, &errorHandler, |
| 3505 | "unicodeescape", "illegal Unicode character", |
| 3506 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3507 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3508 | goto onError; |
| 3509 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3510 | break; |
| 3511 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3512 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3513 | case 'N': |
| 3514 | message = "malformed \\N character escape"; |
| 3515 | if (ucnhash_CAPI == NULL) { |
| 3516 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3517 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3518 | if (ucnhash_CAPI == NULL) |
| 3519 | goto ucnhashError; |
| 3520 | } |
| 3521 | if (*s == '{') { |
| 3522 | const char *start = s+1; |
| 3523 | /* look for the closing brace */ |
| 3524 | while (*s != '}' && s < end) |
| 3525 | s++; |
| 3526 | if (s > start && s < end && *s == '}') { |
| 3527 | /* found a name. look it up in the unicode database */ |
| 3528 | message = "unknown Unicode character name"; |
| 3529 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3530 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3531 | goto store; |
| 3532 | } |
| 3533 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3534 | endinpos = s-starts; |
| 3535 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3536 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3537 | errors, &errorHandler, |
| 3538 | "unicodeescape", message, |
| 3539 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3540 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3541 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3542 | break; |
| 3543 | |
| 3544 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3545 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3546 | message = "\\ at end of string"; |
| 3547 | s--; |
| 3548 | endinpos = s-starts; |
| 3549 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3550 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3551 | errors, &errorHandler, |
| 3552 | "unicodeescape", message, |
| 3553 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3554 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3555 | goto onError; |
| 3556 | } |
| 3557 | else { |
| 3558 | *p++ = '\\'; |
| 3559 | *p++ = (unsigned char)s[-1]; |
| 3560 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3561 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3562 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3563 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3564 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3565 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3566 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3567 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3568 | Py_XDECREF(errorHandler); |
| 3569 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3570 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3571 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3572 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3573 | PyErr_SetString( |
| 3574 | PyExc_UnicodeError, |
| 3575 | "\\N escapes not supported (can't load unicodedata module)" |
| 3576 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3577 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3578 | Py_XDECREF(errorHandler); |
| 3579 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3580 | return NULL; |
| 3581 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3582 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3583 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3584 | Py_XDECREF(errorHandler); |
| 3585 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3586 | return NULL; |
| 3587 | } |
| 3588 | |
| 3589 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3590 | |
| 3591 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3592 | appropriate. |
| 3593 | |
| 3594 | */ |
| 3595 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3596 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3597 | Py_ssize_t size, |
| 3598 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3599 | { |
| 3600 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3601 | |
| 3602 | while (size-- > 0) { |
| 3603 | if (*s == ch) |
| 3604 | return s; |
| 3605 | s++; |
| 3606 | } |
| 3607 | |
| 3608 | return NULL; |
| 3609 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3610 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3611 | static const char *hexdigits = "0123456789abcdef"; |
| 3612 | |
| 3613 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3614 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3615 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3616 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3617 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3618 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3619 | #ifdef Py_UNICODE_WIDE |
| 3620 | const Py_ssize_t expandsize = 10; |
| 3621 | #else |
| 3622 | const Py_ssize_t expandsize = 6; |
| 3623 | #endif |
| 3624 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3625 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3626 | better to choose a different scheme. Perhaps scan the |
| 3627 | first N-chars of the string and allocate based on that size. |
| 3628 | */ |
| 3629 | /* Initial allocation is based on the longest-possible unichr |
| 3630 | escape. |
| 3631 | |
| 3632 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3633 | unichr, so in this case it's the longest unichr escape. In |
| 3634 | narrow (UTF-16) builds this is five chars per source unichr |
| 3635 | since there are two unichrs in the surrogate pair, so in narrow |
| 3636 | (UTF-16) builds it's not the longest unichr escape. |
| 3637 | |
| 3638 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3639 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3640 | escape. |
| 3641 | */ |
| 3642 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3643 | if (size == 0) |
| 3644 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3645 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3646 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3647 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3648 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3649 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3650 | 2 |
| 3651 | + expandsize*size |
| 3652 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3653 | if (repr == NULL) |
| 3654 | return NULL; |
| 3655 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3656 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3657 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3658 | while (size-- > 0) { |
| 3659 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3660 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3661 | /* Escape backslashes */ |
| 3662 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3663 | *p++ = '\\'; |
| 3664 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3665 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3666 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3667 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3668 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3669 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3670 | else if (ch >= 0x10000) { |
| 3671 | *p++ = '\\'; |
| 3672 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3673 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3674 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3675 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3676 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3677 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3678 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3679 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3680 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3681 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3682 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3683 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3684 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3685 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3686 | Py_UNICODE ch2; |
| 3687 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3688 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3689 | ch2 = *s++; |
| 3690 | size--; |
| 3691 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3692 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3693 | *p++ = '\\'; |
| 3694 | *p++ = 'U'; |
| 3695 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3696 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3697 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3698 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3699 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3700 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3701 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3702 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3703 | continue; |
| 3704 | } |
| 3705 | /* Fall through: isolated surrogates are copied as-is */ |
| 3706 | s--; |
| 3707 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3708 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3709 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3710 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3711 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3712 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3713 | *p++ = '\\'; |
| 3714 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3715 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3716 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3717 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3718 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3719 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3720 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3721 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3722 | else if (ch == '\t') { |
| 3723 | *p++ = '\\'; |
| 3724 | *p++ = 't'; |
| 3725 | } |
| 3726 | else if (ch == '\n') { |
| 3727 | *p++ = '\\'; |
| 3728 | *p++ = 'n'; |
| 3729 | } |
| 3730 | else if (ch == '\r') { |
| 3731 | *p++ = '\\'; |
| 3732 | *p++ = 'r'; |
| 3733 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3734 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3735 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3736 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3737 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3738 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3739 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3740 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3741 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3742 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3743 | /* Copy everything else as-is */ |
| 3744 | else |
| 3745 | *p++ = (char) ch; |
| 3746 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3747 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3748 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3749 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3750 | return NULL; |
| 3751 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3754 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3755 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3756 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3757 | if (!PyUnicode_Check(unicode)) { |
| 3758 | PyErr_BadArgument(); |
| 3759 | return NULL; |
| 3760 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3761 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3762 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3763 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3767 | |
| 3768 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3769 | Py_ssize_t size, |
| 3770 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3771 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3772 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3773 | Py_ssize_t startinpos; |
| 3774 | Py_ssize_t endinpos; |
| 3775 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3776 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3777 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3778 | const char *end; |
| 3779 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3780 | PyObject *errorHandler = NULL; |
| 3781 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3782 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3783 | /* Escaped strings will always be longer than the resulting |
| 3784 | 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] | 3785 | length after conversion to the true value. (But decoding error |
| 3786 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3787 | v = _PyUnicode_New(size); |
| 3788 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3789 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3790 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3791 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3792 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3793 | end = s + size; |
| 3794 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3795 | unsigned char c; |
| 3796 | Py_UCS4 x; |
| 3797 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3798 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3799 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3800 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3801 | if (*s != '\\') { |
| 3802 | *p++ = (unsigned char)*s++; |
| 3803 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3804 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3805 | startinpos = s-starts; |
| 3806 | |
| 3807 | /* \u-escapes are only interpreted iff the number of leading |
| 3808 | backslashes if odd */ |
| 3809 | bs = s; |
| 3810 | for (;s < end;) { |
| 3811 | if (*s != '\\') |
| 3812 | break; |
| 3813 | *p++ = (unsigned char)*s++; |
| 3814 | } |
| 3815 | if (((s - bs) & 1) == 0 || |
| 3816 | s >= end || |
| 3817 | (*s != 'u' && *s != 'U')) { |
| 3818 | continue; |
| 3819 | } |
| 3820 | p--; |
| 3821 | count = *s=='u' ? 4 : 8; |
| 3822 | s++; |
| 3823 | |
| 3824 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3825 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3826 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3827 | c = (unsigned char)*s; |
| 3828 | if (!ISXDIGIT(c)) { |
| 3829 | endinpos = s-starts; |
| 3830 | if (unicode_decode_call_errorhandler( |
| 3831 | errors, &errorHandler, |
| 3832 | "rawunicodeescape", "truncated \\uXXXX", |
| 3833 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3834 | &v, &outpos, &p)) |
| 3835 | goto onError; |
| 3836 | goto nextByte; |
| 3837 | } |
| 3838 | x = (x<<4) & ~0xF; |
| 3839 | if (c >= '0' && c <= '9') |
| 3840 | x += c - '0'; |
| 3841 | else if (c >= 'a' && c <= 'f') |
| 3842 | x += 10 + c - 'a'; |
| 3843 | else |
| 3844 | x += 10 + c - 'A'; |
| 3845 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3846 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3847 | /* UCS-2 character */ |
| 3848 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3849 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3850 | /* UCS-4 character. Either store directly, or as |
| 3851 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3852 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3853 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3854 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3855 | x -= 0x10000L; |
| 3856 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3857 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3858 | #endif |
| 3859 | } else { |
| 3860 | endinpos = s-starts; |
| 3861 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3862 | if (unicode_decode_call_errorhandler( |
| 3863 | errors, &errorHandler, |
| 3864 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3865 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3866 | &v, &outpos, &p)) |
| 3867 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3868 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3869 | nextByte: |
| 3870 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3871 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3872 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3873 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3874 | Py_XDECREF(errorHandler); |
| 3875 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3876 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3877 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3878 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3879 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3880 | Py_XDECREF(errorHandler); |
| 3881 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3882 | return NULL; |
| 3883 | } |
| 3884 | |
| 3885 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3886 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3887 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3888 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3889 | char *p; |
| 3890 | char *q; |
| 3891 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3892 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3893 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3894 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3895 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3896 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3897 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3898 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3899 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3900 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3901 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3902 | if (repr == NULL) |
| 3903 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3904 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3905 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3906 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3907 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3908 | while (size-- > 0) { |
| 3909 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3910 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3911 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3912 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3913 | *p++ = '\\'; |
| 3914 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3915 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3916 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3917 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3918 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3919 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3920 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3921 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3922 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3923 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3924 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3925 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3926 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3927 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3928 | Py_UNICODE ch2; |
| 3929 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3930 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3931 | ch2 = *s++; |
| 3932 | size--; |
| 3933 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3934 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3935 | *p++ = '\\'; |
| 3936 | *p++ = 'U'; |
| 3937 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3938 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3939 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3940 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3941 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3942 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3943 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3944 | *p++ = hexdigits[ucs & 0xf]; |
| 3945 | continue; |
| 3946 | } |
| 3947 | /* Fall through: isolated surrogates are copied as-is */ |
| 3948 | s--; |
| 3949 | size++; |
| 3950 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3951 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3952 | /* Map 16-bit characters to '\uxxxx' */ |
| 3953 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3954 | *p++ = '\\'; |
| 3955 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3956 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3957 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3958 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3959 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3960 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3961 | /* Copy everything else as-is */ |
| 3962 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3963 | *p++ = (char) ch; |
| 3964 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3965 | size = p - q; |
| 3966 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3967 | assert(size > 0); |
| 3968 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3969 | return NULL; |
| 3970 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3974 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3975 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3976 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3977 | PyErr_BadArgument(); |
| 3978 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3979 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3980 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3981 | PyUnicode_GET_SIZE(unicode)); |
| 3982 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3983 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3986 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3987 | |
| 3988 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3989 | Py_ssize_t size, |
| 3990 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3991 | { |
| 3992 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3993 | Py_ssize_t startinpos; |
| 3994 | Py_ssize_t endinpos; |
| 3995 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3996 | PyUnicodeObject *v; |
| 3997 | Py_UNICODE *p; |
| 3998 | const char *end; |
| 3999 | const char *reason; |
| 4000 | PyObject *errorHandler = NULL; |
| 4001 | PyObject *exc = NULL; |
| 4002 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4003 | #ifdef Py_UNICODE_WIDE |
| 4004 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4005 | #endif |
| 4006 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4007 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4008 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4009 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4010 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4011 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4012 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4013 | p = PyUnicode_AS_UNICODE(v); |
| 4014 | end = s + size; |
| 4015 | |
| 4016 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4017 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4018 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4019 | some malformed UCS-4 data. */ |
| 4020 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4021 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4022 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4023 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4024 | end-s < Py_UNICODE_SIZE |
| 4025 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4026 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4027 | startinpos = s - starts; |
| 4028 | if (end-s < Py_UNICODE_SIZE) { |
| 4029 | endinpos = end-starts; |
| 4030 | reason = "truncated input"; |
| 4031 | } |
| 4032 | else { |
| 4033 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4034 | reason = "illegal code point (> 0x10FFFF)"; |
| 4035 | } |
| 4036 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4037 | if (unicode_decode_call_errorhandler( |
| 4038 | errors, &errorHandler, |
| 4039 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4040 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4041 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4042 | goto onError; |
| 4043 | } |
| 4044 | } |
| 4045 | else { |
| 4046 | p++; |
| 4047 | s += Py_UNICODE_SIZE; |
| 4048 | } |
| 4049 | } |
| 4050 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4051 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4052 | goto onError; |
| 4053 | Py_XDECREF(errorHandler); |
| 4054 | Py_XDECREF(exc); |
| 4055 | return (PyObject *)v; |
| 4056 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4057 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4058 | Py_XDECREF(v); |
| 4059 | Py_XDECREF(errorHandler); |
| 4060 | Py_XDECREF(exc); |
| 4061 | return NULL; |
| 4062 | } |
| 4063 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4064 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4065 | |
| 4066 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4067 | Py_ssize_t size, |
| 4068 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | { |
| 4070 | PyUnicodeObject *v; |
| 4071 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4072 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4074 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4075 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4076 | Py_UNICODE r = *(unsigned char*)s; |
| 4077 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4080 | v = _PyUnicode_New(size); |
| 4081 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4082 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4083 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4084 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4085 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4086 | e = s + size; |
| 4087 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4088 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4089 | unrolled_end = e - 4; |
| 4090 | while (s < unrolled_end) { |
| 4091 | p[0] = (unsigned char) s[0]; |
| 4092 | p[1] = (unsigned char) s[1]; |
| 4093 | p[2] = (unsigned char) s[2]; |
| 4094 | p[3] = (unsigned char) s[3]; |
| 4095 | s += 4; |
| 4096 | p += 4; |
| 4097 | } |
| 4098 | while (s < e) |
| 4099 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4100 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4101 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4102 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4103 | Py_XDECREF(v); |
| 4104 | return NULL; |
| 4105 | } |
| 4106 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4107 | /* create or adjust a UnicodeEncodeError */ |
| 4108 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4109 | const char *encoding, |
| 4110 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4111 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4112 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4113 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4114 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4115 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4116 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4117 | } |
| 4118 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4119 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4120 | goto onError; |
| 4121 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4122 | goto onError; |
| 4123 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4124 | goto onError; |
| 4125 | return; |
| 4126 | onError: |
| 4127 | Py_DECREF(*exceptionObject); |
| 4128 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4129 | } |
| 4130 | } |
| 4131 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4132 | /* raises a UnicodeEncodeError */ |
| 4133 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4134 | const char *encoding, |
| 4135 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4136 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4137 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4138 | { |
| 4139 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4140 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4141 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4142 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | /* error handling callback helper: |
| 4146 | build arguments, call the callback and check the arguments, |
| 4147 | put the result into newpos and return the replacement string, which |
| 4148 | has to be freed by the caller */ |
| 4149 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4150 | PyObject **errorHandler, |
| 4151 | const char *encoding, const char *reason, |
| 4152 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4153 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4154 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4155 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4156 | 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] | 4157 | |
| 4158 | PyObject *restuple; |
| 4159 | PyObject *resunicode; |
| 4160 | |
| 4161 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4162 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4163 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4164 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4168 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4169 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4170 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4171 | |
| 4172 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4173 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4174 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4175 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4176 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4177 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4178 | Py_DECREF(restuple); |
| 4179 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4180 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4181 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4182 | &resunicode, newpos)) { |
| 4183 | Py_DECREF(restuple); |
| 4184 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4185 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4186 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4187 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4188 | Py_DECREF(restuple); |
| 4189 | return NULL; |
| 4190 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4191 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4192 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4193 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4194 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4195 | Py_DECREF(restuple); |
| 4196 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4197 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4198 | Py_INCREF(resunicode); |
| 4199 | Py_DECREF(restuple); |
| 4200 | return resunicode; |
| 4201 | } |
| 4202 | |
| 4203 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4204 | Py_ssize_t size, |
| 4205 | const char *errors, |
| 4206 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4207 | { |
| 4208 | /* output object */ |
| 4209 | PyObject *res; |
| 4210 | /* pointers to the beginning and end+1 of input */ |
| 4211 | const Py_UNICODE *startp = p; |
| 4212 | const Py_UNICODE *endp = p + size; |
| 4213 | /* pointer to the beginning of the unencodable characters */ |
| 4214 | /* const Py_UNICODE *badp = NULL; */ |
| 4215 | /* pointer into the output */ |
| 4216 | char *str; |
| 4217 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4218 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4219 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4220 | 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] | 4221 | PyObject *errorHandler = NULL; |
| 4222 | PyObject *exc = NULL; |
| 4223 | /* the following variable is used for caching string comparisons |
| 4224 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4225 | int known_errorHandler = -1; |
| 4226 | |
| 4227 | /* allocate enough for a simple encoding without |
| 4228 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4229 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4230 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4231 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4232 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4233 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4234 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4235 | ressize = size; |
| 4236 | |
| 4237 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4238 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4239 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4240 | /* can we encode this? */ |
| 4241 | if (c<limit) { |
| 4242 | /* no overflow check, because we know that the space is enough */ |
| 4243 | *str++ = (char)c; |
| 4244 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4245 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4246 | else { |
| 4247 | Py_ssize_t unicodepos = p-startp; |
| 4248 | Py_ssize_t requiredsize; |
| 4249 | PyObject *repunicode; |
| 4250 | Py_ssize_t repsize; |
| 4251 | Py_ssize_t newpos; |
| 4252 | Py_ssize_t respos; |
| 4253 | Py_UNICODE *uni2; |
| 4254 | /* startpos for collecting unencodable chars */ |
| 4255 | const Py_UNICODE *collstart = p; |
| 4256 | const Py_UNICODE *collend = p; |
| 4257 | /* find all unecodable characters */ |
| 4258 | while ((collend < endp) && ((*collend)>=limit)) |
| 4259 | ++collend; |
| 4260 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4261 | if (known_errorHandler==-1) { |
| 4262 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4263 | known_errorHandler = 1; |
| 4264 | else if (!strcmp(errors, "replace")) |
| 4265 | known_errorHandler = 2; |
| 4266 | else if (!strcmp(errors, "ignore")) |
| 4267 | known_errorHandler = 3; |
| 4268 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4269 | known_errorHandler = 4; |
| 4270 | else |
| 4271 | known_errorHandler = 0; |
| 4272 | } |
| 4273 | switch (known_errorHandler) { |
| 4274 | case 1: /* strict */ |
| 4275 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4276 | goto onError; |
| 4277 | case 2: /* replace */ |
| 4278 | while (collstart++<collend) |
| 4279 | *str++ = '?'; /* fall through */ |
| 4280 | case 3: /* ignore */ |
| 4281 | p = collend; |
| 4282 | break; |
| 4283 | case 4: /* xmlcharrefreplace */ |
| 4284 | respos = str - PyBytes_AS_STRING(res); |
| 4285 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4286 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4287 | if (*p<10) |
| 4288 | repsize += 2+1+1; |
| 4289 | else if (*p<100) |
| 4290 | repsize += 2+2+1; |
| 4291 | else if (*p<1000) |
| 4292 | repsize += 2+3+1; |
| 4293 | else if (*p<10000) |
| 4294 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4295 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4296 | else |
| 4297 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4298 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4299 | else if (*p<100000) |
| 4300 | repsize += 2+5+1; |
| 4301 | else if (*p<1000000) |
| 4302 | repsize += 2+6+1; |
| 4303 | else |
| 4304 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4305 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4306 | } |
| 4307 | requiredsize = respos+repsize+(endp-collend); |
| 4308 | if (requiredsize > ressize) { |
| 4309 | if (requiredsize<2*ressize) |
| 4310 | requiredsize = 2*ressize; |
| 4311 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4312 | goto onError; |
| 4313 | str = PyBytes_AS_STRING(res) + respos; |
| 4314 | ressize = requiredsize; |
| 4315 | } |
| 4316 | /* generate replacement (temporarily (mis)uses p) */ |
| 4317 | for (p = collstart; p < collend; ++p) { |
| 4318 | str += sprintf(str, "&#%d;", (int)*p); |
| 4319 | } |
| 4320 | p = collend; |
| 4321 | break; |
| 4322 | default: |
| 4323 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4324 | encoding, reason, startp, size, &exc, |
| 4325 | collstart-startp, collend-startp, &newpos); |
| 4326 | if (repunicode == NULL) |
| 4327 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4328 | if (PyBytes_Check(repunicode)) { |
| 4329 | /* Directly copy bytes result to output. */ |
| 4330 | repsize = PyBytes_Size(repunicode); |
| 4331 | if (repsize > 1) { |
| 4332 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4333 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4334 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4335 | Py_DECREF(repunicode); |
| 4336 | goto onError; |
| 4337 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4338 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4339 | ressize += repsize-1; |
| 4340 | } |
| 4341 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4342 | str += repsize; |
| 4343 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4344 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4345 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4346 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4347 | /* need more space? (at least enough for what we |
| 4348 | have+the replacement+the rest of the string, so |
| 4349 | we won't have to check space for encodable characters) */ |
| 4350 | respos = str - PyBytes_AS_STRING(res); |
| 4351 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4352 | requiredsize = respos+repsize+(endp-collend); |
| 4353 | if (requiredsize > ressize) { |
| 4354 | if (requiredsize<2*ressize) |
| 4355 | requiredsize = 2*ressize; |
| 4356 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4357 | Py_DECREF(repunicode); |
| 4358 | goto onError; |
| 4359 | } |
| 4360 | str = PyBytes_AS_STRING(res) + respos; |
| 4361 | ressize = requiredsize; |
| 4362 | } |
| 4363 | /* check if there is anything unencodable in the replacement |
| 4364 | and copy it to the output */ |
| 4365 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4366 | c = *uni2; |
| 4367 | if (c >= limit) { |
| 4368 | raise_encode_exception(&exc, encoding, startp, size, |
| 4369 | unicodepos, unicodepos+1, reason); |
| 4370 | Py_DECREF(repunicode); |
| 4371 | goto onError; |
| 4372 | } |
| 4373 | *str = (char)c; |
| 4374 | } |
| 4375 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4376 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4377 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4378 | } |
| 4379 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4380 | /* Resize if we allocated to much */ |
| 4381 | size = str - PyBytes_AS_STRING(res); |
| 4382 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4383 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4384 | if (_PyBytes_Resize(&res, size) < 0) |
| 4385 | goto onError; |
| 4386 | } |
| 4387 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4388 | Py_XDECREF(errorHandler); |
| 4389 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4390 | return res; |
| 4391 | |
| 4392 | onError: |
| 4393 | Py_XDECREF(res); |
| 4394 | Py_XDECREF(errorHandler); |
| 4395 | Py_XDECREF(exc); |
| 4396 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4399 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4400 | Py_ssize_t size, |
| 4401 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4402 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4403 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4407 | { |
| 4408 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4409 | PyErr_BadArgument(); |
| 4410 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4411 | } |
| 4412 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4413 | PyUnicode_GET_SIZE(unicode), |
| 4414 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4418 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4419 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4420 | Py_ssize_t size, |
| 4421 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4422 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4423 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4424 | PyUnicodeObject *v; |
| 4425 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4426 | Py_ssize_t startinpos; |
| 4427 | Py_ssize_t endinpos; |
| 4428 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4429 | const char *e; |
| 4430 | PyObject *errorHandler = NULL; |
| 4431 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4432 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4433 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4434 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4435 | Py_UNICODE r = *(unsigned char*)s; |
| 4436 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4437 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4438 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4439 | v = _PyUnicode_New(size); |
| 4440 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4441 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4442 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4443 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4444 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4445 | e = s + size; |
| 4446 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4447 | register unsigned char c = (unsigned char)*s; |
| 4448 | if (c < 128) { |
| 4449 | *p++ = c; |
| 4450 | ++s; |
| 4451 | } |
| 4452 | else { |
| 4453 | startinpos = s-starts; |
| 4454 | endinpos = startinpos + 1; |
| 4455 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4456 | if (unicode_decode_call_errorhandler( |
| 4457 | errors, &errorHandler, |
| 4458 | "ascii", "ordinal not in range(128)", |
| 4459 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4460 | &v, &outpos, &p)) |
| 4461 | goto onError; |
| 4462 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4463 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4464 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4465 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4466 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4467 | Py_XDECREF(errorHandler); |
| 4468 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4469 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4470 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4471 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4472 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4473 | Py_XDECREF(errorHandler); |
| 4474 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4475 | return NULL; |
| 4476 | } |
| 4477 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4478 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4479 | Py_ssize_t size, |
| 4480 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4481 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4482 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4486 | { |
| 4487 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4488 | PyErr_BadArgument(); |
| 4489 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4490 | } |
| 4491 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4492 | PyUnicode_GET_SIZE(unicode), |
| 4493 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4496 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4497 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4498 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4499 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4500 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4501 | #define NEED_RETRY |
| 4502 | #endif |
| 4503 | |
| 4504 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4505 | a) it assumes an incomplete character consists of a single byte, and |
| 4506 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4507 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4508 | |
| 4509 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4510 | { |
| 4511 | const char *curr = s + offset; |
| 4512 | |
| 4513 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4514 | const char *prev = CharPrev(s, curr); |
| 4515 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4516 | } |
| 4517 | return 0; |
| 4518 | } |
| 4519 | |
| 4520 | /* |
| 4521 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4522 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4523 | */ |
| 4524 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4525 | const char *s, /* MBCS string */ |
| 4526 | int size, /* sizeof MBCS string */ |
| 4527 | int final) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4528 | { |
| 4529 | Py_UNICODE *p; |
| 4530 | Py_ssize_t n = 0; |
| 4531 | int usize = 0; |
| 4532 | |
| 4533 | assert(size >= 0); |
| 4534 | |
| 4535 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4536 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4537 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4538 | |
| 4539 | /* First get the size of the result */ |
| 4540 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4541 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4542 | if (usize == 0) { |
| 4543 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4544 | return -1; |
| 4545 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4549 | /* Create unicode object */ |
| 4550 | *v = _PyUnicode_New(usize); |
| 4551 | if (*v == NULL) |
| 4552 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4553 | } |
| 4554 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4555 | /* Extend unicode object */ |
| 4556 | n = PyUnicode_GET_SIZE(*v); |
| 4557 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4558 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | /* Do the conversion */ |
| 4562 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4563 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4564 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4565 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4566 | return -1; |
| 4567 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | return size; |
| 4571 | } |
| 4572 | |
| 4573 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4574 | Py_ssize_t size, |
| 4575 | const char *errors, |
| 4576 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4577 | { |
| 4578 | PyUnicodeObject *v = NULL; |
| 4579 | int done; |
| 4580 | |
| 4581 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4582 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4583 | |
| 4584 | #ifdef NEED_RETRY |
| 4585 | retry: |
| 4586 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4587 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4588 | else |
| 4589 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4590 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4591 | |
| 4592 | if (done < 0) { |
| 4593 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4594 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4598 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4599 | |
| 4600 | #ifdef NEED_RETRY |
| 4601 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4602 | s += done; |
| 4603 | size -= done; |
| 4604 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4605 | } |
| 4606 | #endif |
| 4607 | |
| 4608 | return (PyObject *)v; |
| 4609 | } |
| 4610 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4611 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4612 | Py_ssize_t size, |
| 4613 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4614 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4615 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4616 | } |
| 4617 | |
| 4618 | /* |
| 4619 | * Convert unicode into string object (MBCS). |
| 4620 | * Returns 0 if succeed, -1 otherwise. |
| 4621 | */ |
| 4622 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4623 | const Py_UNICODE *p, /* unicode */ |
| 4624 | int size) /* size of unicode */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4625 | { |
| 4626 | int mbcssize = 0; |
| 4627 | Py_ssize_t n = 0; |
| 4628 | |
| 4629 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4630 | |
| 4631 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4632 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4633 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4634 | if (mbcssize == 0) { |
| 4635 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4636 | return -1; |
| 4637 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4640 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4641 | /* Create string object */ |
| 4642 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4643 | if (*repr == NULL) |
| 4644 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4645 | } |
| 4646 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4647 | /* Extend string object */ |
| 4648 | n = PyBytes_Size(*repr); |
| 4649 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4650 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | /* Do the conversion */ |
| 4654 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4655 | char *s = PyBytes_AS_STRING(*repr) + n; |
| 4656 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4657 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4658 | return -1; |
| 4659 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4666 | Py_ssize_t size, |
| 4667 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4668 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4669 | PyObject *repr = NULL; |
| 4670 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4671 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4672 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4673 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4674 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4675 | ret = encode_mbcs(&repr, p, INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4676 | else |
| 4677 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4678 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4679 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4680 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4681 | Py_XDECREF(repr); |
| 4682 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4683 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4684 | |
| 4685 | #ifdef NEED_RETRY |
| 4686 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4687 | p += INT_MAX; |
| 4688 | size -= INT_MAX; |
| 4689 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4690 | } |
| 4691 | #endif |
| 4692 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4693 | return repr; |
| 4694 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4695 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4696 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4697 | { |
| 4698 | if (!PyUnicode_Check(unicode)) { |
| 4699 | PyErr_BadArgument(); |
| 4700 | return NULL; |
| 4701 | } |
| 4702 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4703 | PyUnicode_GET_SIZE(unicode), |
| 4704 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4707 | #undef NEED_RETRY |
| 4708 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4709 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4710 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4711 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4712 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4713 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4714 | Py_ssize_t size, |
| 4715 | PyObject *mapping, |
| 4716 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4717 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4718 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4719 | Py_ssize_t startinpos; |
| 4720 | Py_ssize_t endinpos; |
| 4721 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4722 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4723 | PyUnicodeObject *v; |
| 4724 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4725 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4726 | PyObject *errorHandler = NULL; |
| 4727 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4728 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4729 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4731 | /* Default to Latin-1 */ |
| 4732 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4733 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4734 | |
| 4735 | v = _PyUnicode_New(size); |
| 4736 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4737 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4738 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4739 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4740 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4741 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4742 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4743 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4744 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4745 | while (s < e) { |
| 4746 | unsigned char ch = *s; |
| 4747 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4748 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4749 | if (ch < maplen) |
| 4750 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4751 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4752 | if (x == 0xfffe) { |
| 4753 | /* undefined mapping */ |
| 4754 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4755 | startinpos = s-starts; |
| 4756 | endinpos = startinpos+1; |
| 4757 | if (unicode_decode_call_errorhandler( |
| 4758 | errors, &errorHandler, |
| 4759 | "charmap", "character maps to <undefined>", |
| 4760 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4761 | &v, &outpos, &p)) { |
| 4762 | goto onError; |
| 4763 | } |
| 4764 | continue; |
| 4765 | } |
| 4766 | *p++ = x; |
| 4767 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4768 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4769 | } |
| 4770 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4771 | while (s < e) { |
| 4772 | unsigned char ch = *s; |
| 4773 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4774 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4775 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4776 | w = PyLong_FromLong((long)ch); |
| 4777 | if (w == NULL) |
| 4778 | goto onError; |
| 4779 | x = PyObject_GetItem(mapping, w); |
| 4780 | Py_DECREF(w); |
| 4781 | if (x == NULL) { |
| 4782 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4783 | /* No mapping found means: mapping is undefined. */ |
| 4784 | PyErr_Clear(); |
| 4785 | x = Py_None; |
| 4786 | Py_INCREF(x); |
| 4787 | } else |
| 4788 | goto onError; |
| 4789 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4790 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4791 | /* Apply mapping */ |
| 4792 | if (PyLong_Check(x)) { |
| 4793 | long value = PyLong_AS_LONG(x); |
| 4794 | if (value < 0 || value > 65535) { |
| 4795 | PyErr_SetString(PyExc_TypeError, |
| 4796 | "character mapping must be in range(65536)"); |
| 4797 | Py_DECREF(x); |
| 4798 | goto onError; |
| 4799 | } |
| 4800 | *p++ = (Py_UNICODE)value; |
| 4801 | } |
| 4802 | else if (x == Py_None) { |
| 4803 | /* undefined mapping */ |
| 4804 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4805 | startinpos = s-starts; |
| 4806 | endinpos = startinpos+1; |
| 4807 | if (unicode_decode_call_errorhandler( |
| 4808 | errors, &errorHandler, |
| 4809 | "charmap", "character maps to <undefined>", |
| 4810 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4811 | &v, &outpos, &p)) { |
| 4812 | Py_DECREF(x); |
| 4813 | goto onError; |
| 4814 | } |
| 4815 | Py_DECREF(x); |
| 4816 | continue; |
| 4817 | } |
| 4818 | else if (PyUnicode_Check(x)) { |
| 4819 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4820 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4821 | if (targetsize == 1) |
| 4822 | /* 1-1 mapping */ |
| 4823 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4824 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4825 | else if (targetsize > 1) { |
| 4826 | /* 1-n mapping */ |
| 4827 | if (targetsize > extrachars) { |
| 4828 | /* resize first */ |
| 4829 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4830 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4831 | (targetsize << 2); |
| 4832 | extrachars += needed; |
| 4833 | /* XXX overflow detection missing */ |
| 4834 | if (_PyUnicode_Resize(&v, |
| 4835 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4836 | Py_DECREF(x); |
| 4837 | goto onError; |
| 4838 | } |
| 4839 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4840 | } |
| 4841 | Py_UNICODE_COPY(p, |
| 4842 | PyUnicode_AS_UNICODE(x), |
| 4843 | targetsize); |
| 4844 | p += targetsize; |
| 4845 | extrachars -= targetsize; |
| 4846 | } |
| 4847 | /* 1-0 mapping: skip the character */ |
| 4848 | } |
| 4849 | else { |
| 4850 | /* wrong return value */ |
| 4851 | PyErr_SetString(PyExc_TypeError, |
| 4852 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4853 | Py_DECREF(x); |
| 4854 | goto onError; |
| 4855 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4856 | Py_DECREF(x); |
| 4857 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4858 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4859 | } |
| 4860 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4861 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4862 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4863 | Py_XDECREF(errorHandler); |
| 4864 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4865 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4866 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4867 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4868 | Py_XDECREF(errorHandler); |
| 4869 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4870 | Py_XDECREF(v); |
| 4871 | return NULL; |
| 4872 | } |
| 4873 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4874 | /* Charmap encoding: the lookup table */ |
| 4875 | |
| 4876 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4877 | PyObject_HEAD |
| 4878 | unsigned char level1[32]; |
| 4879 | int count2, count3; |
| 4880 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4881 | }; |
| 4882 | |
| 4883 | static PyObject* |
| 4884 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4885 | { |
| 4886 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4887 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4888 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4892 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4893 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4894 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4895 | }; |
| 4896 | |
| 4897 | static void |
| 4898 | encoding_map_dealloc(PyObject* o) |
| 4899 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4900 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4904 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4905 | "EncodingMap", /*tp_name*/ |
| 4906 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4907 | 0, /*tp_itemsize*/ |
| 4908 | /* methods */ |
| 4909 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4910 | 0, /*tp_print*/ |
| 4911 | 0, /*tp_getattr*/ |
| 4912 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4913 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4914 | 0, /*tp_repr*/ |
| 4915 | 0, /*tp_as_number*/ |
| 4916 | 0, /*tp_as_sequence*/ |
| 4917 | 0, /*tp_as_mapping*/ |
| 4918 | 0, /*tp_hash*/ |
| 4919 | 0, /*tp_call*/ |
| 4920 | 0, /*tp_str*/ |
| 4921 | 0, /*tp_getattro*/ |
| 4922 | 0, /*tp_setattro*/ |
| 4923 | 0, /*tp_as_buffer*/ |
| 4924 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4925 | 0, /*tp_doc*/ |
| 4926 | 0, /*tp_traverse*/ |
| 4927 | 0, /*tp_clear*/ |
| 4928 | 0, /*tp_richcompare*/ |
| 4929 | 0, /*tp_weaklistoffset*/ |
| 4930 | 0, /*tp_iter*/ |
| 4931 | 0, /*tp_iternext*/ |
| 4932 | encoding_map_methods, /*tp_methods*/ |
| 4933 | 0, /*tp_members*/ |
| 4934 | 0, /*tp_getset*/ |
| 4935 | 0, /*tp_base*/ |
| 4936 | 0, /*tp_dict*/ |
| 4937 | 0, /*tp_descr_get*/ |
| 4938 | 0, /*tp_descr_set*/ |
| 4939 | 0, /*tp_dictoffset*/ |
| 4940 | 0, /*tp_init*/ |
| 4941 | 0, /*tp_alloc*/ |
| 4942 | 0, /*tp_new*/ |
| 4943 | 0, /*tp_free*/ |
| 4944 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4945 | }; |
| 4946 | |
| 4947 | PyObject* |
| 4948 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4949 | { |
| 4950 | Py_UNICODE *decode; |
| 4951 | PyObject *result; |
| 4952 | struct encoding_map *mresult; |
| 4953 | int i; |
| 4954 | int need_dict = 0; |
| 4955 | unsigned char level1[32]; |
| 4956 | unsigned char level2[512]; |
| 4957 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4958 | int count2 = 0, count3 = 0; |
| 4959 | |
| 4960 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4961 | PyErr_BadArgument(); |
| 4962 | return NULL; |
| 4963 | } |
| 4964 | decode = PyUnicode_AS_UNICODE(string); |
| 4965 | memset(level1, 0xFF, sizeof level1); |
| 4966 | memset(level2, 0xFF, sizeof level2); |
| 4967 | |
| 4968 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4969 | or if there are non-BMP characters, we need to use |
| 4970 | a mapping dictionary. */ |
| 4971 | if (decode[0] != 0) |
| 4972 | need_dict = 1; |
| 4973 | for (i = 1; i < 256; i++) { |
| 4974 | int l1, l2; |
| 4975 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4976 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4977 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4978 | #endif |
| 4979 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4980 | need_dict = 1; |
| 4981 | break; |
| 4982 | } |
| 4983 | if (decode[i] == 0xFFFE) |
| 4984 | /* unmapped character */ |
| 4985 | continue; |
| 4986 | l1 = decode[i] >> 11; |
| 4987 | l2 = decode[i] >> 7; |
| 4988 | if (level1[l1] == 0xFF) |
| 4989 | level1[l1] = count2++; |
| 4990 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4991 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4992 | } |
| 4993 | |
| 4994 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4995 | need_dict = 1; |
| 4996 | |
| 4997 | if (need_dict) { |
| 4998 | PyObject *result = PyDict_New(); |
| 4999 | PyObject *key, *value; |
| 5000 | if (!result) |
| 5001 | return NULL; |
| 5002 | for (i = 0; i < 256; i++) { |
| 5003 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5004 | key = PyLong_FromLong(decode[i]); |
| 5005 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5006 | if (!key || !value) |
| 5007 | goto failed1; |
| 5008 | if (PyDict_SetItem(result, key, value) == -1) |
| 5009 | goto failed1; |
| 5010 | Py_DECREF(key); |
| 5011 | Py_DECREF(value); |
| 5012 | } |
| 5013 | return result; |
| 5014 | failed1: |
| 5015 | Py_XDECREF(key); |
| 5016 | Py_XDECREF(value); |
| 5017 | Py_DECREF(result); |
| 5018 | return NULL; |
| 5019 | } |
| 5020 | |
| 5021 | /* Create a three-level trie */ |
| 5022 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5023 | 16*count2 + 128*count3 - 1); |
| 5024 | if (!result) |
| 5025 | return PyErr_NoMemory(); |
| 5026 | PyObject_Init(result, &EncodingMapType); |
| 5027 | mresult = (struct encoding_map*)result; |
| 5028 | mresult->count2 = count2; |
| 5029 | mresult->count3 = count3; |
| 5030 | mlevel1 = mresult->level1; |
| 5031 | mlevel2 = mresult->level23; |
| 5032 | mlevel3 = mresult->level23 + 16*count2; |
| 5033 | memcpy(mlevel1, level1, 32); |
| 5034 | memset(mlevel2, 0xFF, 16*count2); |
| 5035 | memset(mlevel3, 0, 128*count3); |
| 5036 | count3 = 0; |
| 5037 | for (i = 1; i < 256; i++) { |
| 5038 | int o1, o2, o3, i2, i3; |
| 5039 | if (decode[i] == 0xFFFE) |
| 5040 | /* unmapped character */ |
| 5041 | continue; |
| 5042 | o1 = decode[i]>>11; |
| 5043 | o2 = (decode[i]>>7) & 0xF; |
| 5044 | i2 = 16*mlevel1[o1] + o2; |
| 5045 | if (mlevel2[i2] == 0xFF) |
| 5046 | mlevel2[i2] = count3++; |
| 5047 | o3 = decode[i] & 0x7F; |
| 5048 | i3 = 128*mlevel2[i2] + o3; |
| 5049 | mlevel3[i3] = i; |
| 5050 | } |
| 5051 | return result; |
| 5052 | } |
| 5053 | |
| 5054 | static int |
| 5055 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5056 | { |
| 5057 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5058 | int l1 = c>>11; |
| 5059 | int l2 = (c>>7) & 0xF; |
| 5060 | int l3 = c & 0x7F; |
| 5061 | int i; |
| 5062 | |
| 5063 | #ifdef Py_UNICODE_WIDE |
| 5064 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5066 | } |
| 5067 | #endif |
| 5068 | if (c == 0) |
| 5069 | return 0; |
| 5070 | /* level 1*/ |
| 5071 | i = map->level1[l1]; |
| 5072 | if (i == 0xFF) { |
| 5073 | return -1; |
| 5074 | } |
| 5075 | /* level 2*/ |
| 5076 | i = map->level23[16*i+l2]; |
| 5077 | if (i == 0xFF) { |
| 5078 | return -1; |
| 5079 | } |
| 5080 | /* level 3 */ |
| 5081 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5082 | if (i == 0) { |
| 5083 | return -1; |
| 5084 | } |
| 5085 | return i; |
| 5086 | } |
| 5087 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5088 | /* Lookup the character ch in the mapping. If the character |
| 5089 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5090 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5091 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5092 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5093 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5094 | PyObject *x; |
| 5095 | |
| 5096 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5097 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5098 | x = PyObject_GetItem(mapping, w); |
| 5099 | Py_DECREF(w); |
| 5100 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5101 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5102 | /* No mapping found means: mapping is undefined. */ |
| 5103 | PyErr_Clear(); |
| 5104 | x = Py_None; |
| 5105 | Py_INCREF(x); |
| 5106 | return x; |
| 5107 | } else |
| 5108 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5109 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5110 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5111 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5112 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5113 | long value = PyLong_AS_LONG(x); |
| 5114 | if (value < 0 || value > 255) { |
| 5115 | PyErr_SetString(PyExc_TypeError, |
| 5116 | "character mapping must be in range(256)"); |
| 5117 | Py_DECREF(x); |
| 5118 | return NULL; |
| 5119 | } |
| 5120 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5121 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5122 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5123 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5124 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5125 | /* wrong return value */ |
| 5126 | PyErr_Format(PyExc_TypeError, |
| 5127 | "character mapping must return integer, bytes or None, not %.400s", |
| 5128 | x->ob_type->tp_name); |
| 5129 | Py_DECREF(x); |
| 5130 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5131 | } |
| 5132 | } |
| 5133 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5134 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5135 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5136 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5137 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5138 | /* exponentially overallocate to minimize reallocations */ |
| 5139 | if (requiredsize < 2*outsize) |
| 5140 | requiredsize = 2*outsize; |
| 5141 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5142 | return -1; |
| 5143 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5146 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5147 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5148 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5149 | /* 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] | 5150 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5151 | space is available. Return a new reference to the object that |
| 5152 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5153 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5154 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5155 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5156 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5157 | PyObject **outobj, Py_ssize_t *outpos) |
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 | PyObject *rep; |
| 5160 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5161 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5162 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5163 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5164 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5165 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5166 | if (res == -1) |
| 5167 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5168 | if (outsize<requiredsize) |
| 5169 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5170 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5171 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5172 | outstart[(*outpos)++] = (char)res; |
| 5173 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5177 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5178 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5179 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5180 | Py_DECREF(rep); |
| 5181 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5182 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5183 | if (PyLong_Check(rep)) { |
| 5184 | Py_ssize_t requiredsize = *outpos+1; |
| 5185 | if (outsize<requiredsize) |
| 5186 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5187 | Py_DECREF(rep); |
| 5188 | return enc_EXCEPTION; |
| 5189 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5190 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5191 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5192 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5193 | else { |
| 5194 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5195 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5196 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5197 | if (outsize<requiredsize) |
| 5198 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5199 | Py_DECREF(rep); |
| 5200 | return enc_EXCEPTION; |
| 5201 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5202 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5203 | memcpy(outstart + *outpos, repchars, repsize); |
| 5204 | *outpos += repsize; |
| 5205 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5206 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5207 | Py_DECREF(rep); |
| 5208 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | /* handle an error in PyUnicode_EncodeCharmap |
| 5212 | Return 0 on success, -1 on error */ |
| 5213 | static |
| 5214 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5215 | 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] | 5216 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5217 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5218 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5219 | { |
| 5220 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5221 | Py_ssize_t repsize; |
| 5222 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5223 | Py_UNICODE *uni2; |
| 5224 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5225 | Py_ssize_t collstartpos = *inpos; |
| 5226 | Py_ssize_t collendpos = *inpos+1; |
| 5227 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5228 | char *encoding = "charmap"; |
| 5229 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5230 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5231 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5232 | /* find all unencodable characters */ |
| 5233 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5234 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5235 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5236 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5237 | if (res != -1) |
| 5238 | break; |
| 5239 | ++collendpos; |
| 5240 | continue; |
| 5241 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5242 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5243 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5244 | if (rep==NULL) |
| 5245 | return -1; |
| 5246 | else if (rep!=Py_None) { |
| 5247 | Py_DECREF(rep); |
| 5248 | break; |
| 5249 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5250 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5251 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5252 | } |
| 5253 | /* cache callback name lookup |
| 5254 | * (if not done yet, i.e. it's the first error) */ |
| 5255 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5256 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5257 | *known_errorHandler = 1; |
| 5258 | else if (!strcmp(errors, "replace")) |
| 5259 | *known_errorHandler = 2; |
| 5260 | else if (!strcmp(errors, "ignore")) |
| 5261 | *known_errorHandler = 3; |
| 5262 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5263 | *known_errorHandler = 4; |
| 5264 | else |
| 5265 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5266 | } |
| 5267 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5268 | case 1: /* strict */ |
| 5269 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5270 | return -1; |
| 5271 | case 2: /* replace */ |
| 5272 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5273 | x = charmapencode_output('?', mapping, res, respos); |
| 5274 | if (x==enc_EXCEPTION) { |
| 5275 | return -1; |
| 5276 | } |
| 5277 | else if (x==enc_FAILED) { |
| 5278 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5279 | return -1; |
| 5280 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5281 | } |
| 5282 | /* fall through */ |
| 5283 | case 3: /* ignore */ |
| 5284 | *inpos = collendpos; |
| 5285 | break; |
| 5286 | case 4: /* xmlcharrefreplace */ |
| 5287 | /* generate replacement (temporarily (mis)uses p) */ |
| 5288 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5289 | char buffer[2+29+1+1]; |
| 5290 | char *cp; |
| 5291 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5292 | for (cp = buffer; *cp; ++cp) { |
| 5293 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5294 | if (x==enc_EXCEPTION) |
| 5295 | return -1; |
| 5296 | else if (x==enc_FAILED) { |
| 5297 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5298 | return -1; |
| 5299 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5300 | } |
| 5301 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5302 | *inpos = collendpos; |
| 5303 | break; |
| 5304 | default: |
| 5305 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5306 | encoding, reason, p, size, exceptionObject, |
| 5307 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5308 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5309 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5310 | if (PyBytes_Check(repunicode)) { |
| 5311 | /* Directly copy bytes result to output. */ |
| 5312 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5313 | Py_ssize_t requiredsize; |
| 5314 | repsize = PyBytes_Size(repunicode); |
| 5315 | requiredsize = *respos + repsize; |
| 5316 | if (requiredsize > outsize) |
| 5317 | /* Make room for all additional bytes. */ |
| 5318 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5319 | Py_DECREF(repunicode); |
| 5320 | return -1; |
| 5321 | } |
| 5322 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5323 | PyBytes_AsString(repunicode), repsize); |
| 5324 | *respos += repsize; |
| 5325 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5326 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5327 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5328 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5329 | /* generate replacement */ |
| 5330 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5331 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5332 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5333 | if (x==enc_EXCEPTION) { |
| 5334 | return -1; |
| 5335 | } |
| 5336 | else if (x==enc_FAILED) { |
| 5337 | Py_DECREF(repunicode); |
| 5338 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5339 | return -1; |
| 5340 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5341 | } |
| 5342 | *inpos = newpos; |
| 5343 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5344 | } |
| 5345 | return 0; |
| 5346 | } |
| 5347 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5348 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5349 | Py_ssize_t size, |
| 5350 | PyObject *mapping, |
| 5351 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5352 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5353 | /* output object */ |
| 5354 | PyObject *res = NULL; |
| 5355 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5356 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5357 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5358 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5359 | PyObject *errorHandler = NULL; |
| 5360 | PyObject *exc = NULL; |
| 5361 | /* the following variable is used for caching string comparisons |
| 5362 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5363 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5364 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5365 | |
| 5366 | /* Default to Latin-1 */ |
| 5367 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5368 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5369 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5370 | /* allocate enough for a simple encoding without |
| 5371 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5372 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5373 | if (res == NULL) |
| 5374 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5375 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5376 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5377 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5378 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | /* try to encode it */ |
| 5380 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5381 | if (x==enc_EXCEPTION) /* error */ |
| 5382 | goto onError; |
| 5383 | if (x==enc_FAILED) { /* unencodable character */ |
| 5384 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5385 | &exc, |
| 5386 | &known_errorHandler, &errorHandler, errors, |
| 5387 | &res, &respos)) { |
| 5388 | goto onError; |
| 5389 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5390 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5391 | else |
| 5392 | /* done with this character => adjust input position */ |
| 5393 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5394 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5395 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5396 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5397 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5398 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5399 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5400 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5401 | Py_XDECREF(exc); |
| 5402 | Py_XDECREF(errorHandler); |
| 5403 | return res; |
| 5404 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5405 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5406 | Py_XDECREF(res); |
| 5407 | Py_XDECREF(exc); |
| 5408 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | return NULL; |
| 5410 | } |
| 5411 | |
| 5412 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5413 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5414 | { |
| 5415 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5416 | PyErr_BadArgument(); |
| 5417 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | } |
| 5419 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5420 | PyUnicode_GET_SIZE(unicode), |
| 5421 | mapping, |
| 5422 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5425 | /* create or adjust a UnicodeTranslateError */ |
| 5426 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5427 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5428 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5429 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5431 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5432 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5433 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5434 | } |
| 5435 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5436 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5437 | goto onError; |
| 5438 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5439 | goto onError; |
| 5440 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5441 | goto onError; |
| 5442 | return; |
| 5443 | onError: |
| 5444 | Py_DECREF(*exceptionObject); |
| 5445 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5446 | } |
| 5447 | } |
| 5448 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5449 | /* raises a UnicodeTranslateError */ |
| 5450 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5451 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5452 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5453 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5454 | { |
| 5455 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5456 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5457 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5458 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | /* error handling callback helper: |
| 5462 | build arguments, call the callback and check the arguments, |
| 5463 | put the result into newpos and return the replacement string, which |
| 5464 | has to be freed by the caller */ |
| 5465 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5466 | PyObject **errorHandler, |
| 5467 | const char *reason, |
| 5468 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5469 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5470 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5471 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5472 | 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] | 5473 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5474 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5475 | PyObject *restuple; |
| 5476 | PyObject *resunicode; |
| 5477 | |
| 5478 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5479 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5480 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5481 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5485 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5486 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5487 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5488 | |
| 5489 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5490 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5491 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5492 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5493 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5494 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5495 | Py_DECREF(restuple); |
| 5496 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5497 | } |
| 5498 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5499 | &resunicode, &i_newpos)) { |
| 5500 | Py_DECREF(restuple); |
| 5501 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5502 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5503 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5504 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5505 | else |
| 5506 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5507 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5508 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5509 | Py_DECREF(restuple); |
| 5510 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5511 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5512 | Py_INCREF(resunicode); |
| 5513 | Py_DECREF(restuple); |
| 5514 | return resunicode; |
| 5515 | } |
| 5516 | |
| 5517 | /* Lookup the character ch in the mapping and put the result in result, |
| 5518 | which must be decrefed by the caller. |
| 5519 | Return 0 on success, -1 on error */ |
| 5520 | static |
| 5521 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5522 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5523 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5524 | PyObject *x; |
| 5525 | |
| 5526 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5527 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5528 | x = PyObject_GetItem(mapping, w); |
| 5529 | Py_DECREF(w); |
| 5530 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5531 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5532 | /* No mapping found means: use 1:1 mapping. */ |
| 5533 | PyErr_Clear(); |
| 5534 | *result = NULL; |
| 5535 | return 0; |
| 5536 | } else |
| 5537 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5538 | } |
| 5539 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5540 | *result = x; |
| 5541 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5542 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5543 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5544 | long value = PyLong_AS_LONG(x); |
| 5545 | long max = PyUnicode_GetMax(); |
| 5546 | if (value < 0 || value > max) { |
| 5547 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5548 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5549 | Py_DECREF(x); |
| 5550 | return -1; |
| 5551 | } |
| 5552 | *result = x; |
| 5553 | return 0; |
| 5554 | } |
| 5555 | else if (PyUnicode_Check(x)) { |
| 5556 | *result = x; |
| 5557 | return 0; |
| 5558 | } |
| 5559 | else { |
| 5560 | /* wrong return value */ |
| 5561 | PyErr_SetString(PyExc_TypeError, |
| 5562 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5563 | Py_DECREF(x); |
| 5564 | return -1; |
| 5565 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5566 | } |
| 5567 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5568 | if not reallocate and adjust various state variables. |
| 5569 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5570 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5571 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5572 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5573 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5574 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5575 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5576 | /* remember old output position */ |
| 5577 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5578 | /* exponentially overallocate to minimize reallocations */ |
| 5579 | if (requiredsize < 2 * oldsize) |
| 5580 | requiredsize = 2 * oldsize; |
| 5581 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5582 | return -1; |
| 5583 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5584 | } |
| 5585 | return 0; |
| 5586 | } |
| 5587 | /* lookup the character, put the result in the output string and adjust |
| 5588 | various state variables. Return a new reference to the object that |
| 5589 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5590 | undefined (in which case no character was written). |
| 5591 | The called must decref result. |
| 5592 | Return 0 on success, -1 on error. */ |
| 5593 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5594 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5595 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5596 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5597 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5598 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5599 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5600 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5601 | /* not found => default to 1:1 mapping */ |
| 5602 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5603 | } |
| 5604 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5605 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5606 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5607 | /* no overflow check, because we know that the space is enough */ |
| 5608 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5609 | } |
| 5610 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5611 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5612 | if (repsize==1) { |
| 5613 | /* no overflow check, because we know that the space is enough */ |
| 5614 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5615 | } |
| 5616 | else if (repsize!=0) { |
| 5617 | /* more than one character */ |
| 5618 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5619 | (insize - (curinp-startinp)) + |
| 5620 | repsize - 1; |
| 5621 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5622 | return -1; |
| 5623 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5624 | *outp += repsize; |
| 5625 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5626 | } |
| 5627 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5628 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5629 | return 0; |
| 5630 | } |
| 5631 | |
| 5632 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5633 | Py_ssize_t size, |
| 5634 | PyObject *mapping, |
| 5635 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5636 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5637 | /* output object */ |
| 5638 | PyObject *res = NULL; |
| 5639 | /* pointers to the beginning and end+1 of input */ |
| 5640 | const Py_UNICODE *startp = p; |
| 5641 | const Py_UNICODE *endp = p + size; |
| 5642 | /* pointer into the output */ |
| 5643 | Py_UNICODE *str; |
| 5644 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5645 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5646 | char *reason = "character maps to <undefined>"; |
| 5647 | PyObject *errorHandler = NULL; |
| 5648 | PyObject *exc = NULL; |
| 5649 | /* the following variable is used for caching string comparisons |
| 5650 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5651 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5652 | int known_errorHandler = -1; |
| 5653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5654 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5655 | PyErr_BadArgument(); |
| 5656 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5657 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5658 | |
| 5659 | /* allocate enough for a simple 1:1 translation without |
| 5660 | replacements, if we need more, we'll resize */ |
| 5661 | res = PyUnicode_FromUnicode(NULL, size); |
| 5662 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5663 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5664 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5665 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5666 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5668 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5669 | /* try to encode it */ |
| 5670 | PyObject *x = NULL; |
| 5671 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5672 | Py_XDECREF(x); |
| 5673 | goto onError; |
| 5674 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5675 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5676 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5677 | ++p; |
| 5678 | else { /* untranslatable character */ |
| 5679 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5680 | Py_ssize_t repsize; |
| 5681 | Py_ssize_t newpos; |
| 5682 | Py_UNICODE *uni2; |
| 5683 | /* startpos for collecting untranslatable chars */ |
| 5684 | const Py_UNICODE *collstart = p; |
| 5685 | const Py_UNICODE *collend = p+1; |
| 5686 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5688 | /* find all untranslatable characters */ |
| 5689 | while (collend < endp) { |
| 5690 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5691 | goto onError; |
| 5692 | Py_XDECREF(x); |
| 5693 | if (x!=Py_None) |
| 5694 | break; |
| 5695 | ++collend; |
| 5696 | } |
| 5697 | /* cache callback name lookup |
| 5698 | * (if not done yet, i.e. it's the first error) */ |
| 5699 | if (known_errorHandler==-1) { |
| 5700 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5701 | known_errorHandler = 1; |
| 5702 | else if (!strcmp(errors, "replace")) |
| 5703 | known_errorHandler = 2; |
| 5704 | else if (!strcmp(errors, "ignore")) |
| 5705 | known_errorHandler = 3; |
| 5706 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5707 | known_errorHandler = 4; |
| 5708 | else |
| 5709 | known_errorHandler = 0; |
| 5710 | } |
| 5711 | switch (known_errorHandler) { |
| 5712 | case 1: /* strict */ |
| 5713 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5714 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5715 | case 2: /* replace */ |
| 5716 | /* No need to check for space, this is a 1:1 replacement */ |
| 5717 | for (coll = collstart; coll<collend; ++coll) |
| 5718 | *str++ = '?'; |
| 5719 | /* fall through */ |
| 5720 | case 3: /* ignore */ |
| 5721 | p = collend; |
| 5722 | break; |
| 5723 | case 4: /* xmlcharrefreplace */ |
| 5724 | /* generate replacement (temporarily (mis)uses p) */ |
| 5725 | for (p = collstart; p < collend; ++p) { |
| 5726 | char buffer[2+29+1+1]; |
| 5727 | char *cp; |
| 5728 | sprintf(buffer, "&#%d;", (int)*p); |
| 5729 | if (charmaptranslate_makespace(&res, &str, |
| 5730 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5731 | goto onError; |
| 5732 | for (cp = buffer; *cp; ++cp) |
| 5733 | *str++ = *cp; |
| 5734 | } |
| 5735 | p = collend; |
| 5736 | break; |
| 5737 | default: |
| 5738 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5739 | reason, startp, size, &exc, |
| 5740 | collstart-startp, collend-startp, &newpos); |
| 5741 | if (repunicode == NULL) |
| 5742 | goto onError; |
| 5743 | /* generate replacement */ |
| 5744 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5745 | if (charmaptranslate_makespace(&res, &str, |
| 5746 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5747 | Py_DECREF(repunicode); |
| 5748 | goto onError; |
| 5749 | } |
| 5750 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5751 | *str++ = *uni2; |
| 5752 | p = startp + newpos; |
| 5753 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5754 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5755 | } |
| 5756 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5757 | /* Resize if we allocated to much */ |
| 5758 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5759 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5760 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5761 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5762 | } |
| 5763 | Py_XDECREF(exc); |
| 5764 | Py_XDECREF(errorHandler); |
| 5765 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5766 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5767 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | Py_XDECREF(res); |
| 5769 | Py_XDECREF(exc); |
| 5770 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | return NULL; |
| 5772 | } |
| 5773 | |
| 5774 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5775 | PyObject *mapping, |
| 5776 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5777 | { |
| 5778 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | str = PyUnicode_FromObject(str); |
| 5781 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5782 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5783 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5784 | PyUnicode_GET_SIZE(str), |
| 5785 | mapping, |
| 5786 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5787 | Py_DECREF(str); |
| 5788 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5789 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5790 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5791 | Py_XDECREF(str); |
| 5792 | return NULL; |
| 5793 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5794 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5795 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5796 | |
| 5797 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5798 | Py_ssize_t length, |
| 5799 | char *output, |
| 5800 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5801 | { |
| 5802 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5803 | PyObject *errorHandler = NULL; |
| 5804 | PyObject *exc = NULL; |
| 5805 | const char *encoding = "decimal"; |
| 5806 | const char *reason = "invalid decimal Unicode string"; |
| 5807 | /* the following variable is used for caching string comparisons |
| 5808 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5809 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5810 | |
| 5811 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5812 | PyErr_BadArgument(); |
| 5813 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5814 | } |
| 5815 | |
| 5816 | p = s; |
| 5817 | end = s + length; |
| 5818 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5819 | register Py_UNICODE ch = *p; |
| 5820 | int decimal; |
| 5821 | PyObject *repunicode; |
| 5822 | Py_ssize_t repsize; |
| 5823 | Py_ssize_t newpos; |
| 5824 | Py_UNICODE *uni2; |
| 5825 | Py_UNICODE *collstart; |
| 5826 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5827 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5828 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5829 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5830 | ++p; |
| 5831 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5832 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5833 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5834 | if (decimal >= 0) { |
| 5835 | *output++ = '0' + decimal; |
| 5836 | ++p; |
| 5837 | continue; |
| 5838 | } |
| 5839 | if (0 < ch && ch < 256) { |
| 5840 | *output++ = (char)ch; |
| 5841 | ++p; |
| 5842 | continue; |
| 5843 | } |
| 5844 | /* All other characters are considered unencodable */ |
| 5845 | collstart = p; |
| 5846 | collend = p+1; |
| 5847 | while (collend < end) { |
| 5848 | if ((0 < *collend && *collend < 256) || |
| 5849 | !Py_UNICODE_ISSPACE(*collend) || |
| 5850 | Py_UNICODE_TODECIMAL(*collend)) |
| 5851 | break; |
| 5852 | } |
| 5853 | /* cache callback name lookup |
| 5854 | * (if not done yet, i.e. it's the first error) */ |
| 5855 | if (known_errorHandler==-1) { |
| 5856 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5857 | known_errorHandler = 1; |
| 5858 | else if (!strcmp(errors, "replace")) |
| 5859 | known_errorHandler = 2; |
| 5860 | else if (!strcmp(errors, "ignore")) |
| 5861 | known_errorHandler = 3; |
| 5862 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5863 | known_errorHandler = 4; |
| 5864 | else |
| 5865 | known_errorHandler = 0; |
| 5866 | } |
| 5867 | switch (known_errorHandler) { |
| 5868 | case 1: /* strict */ |
| 5869 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5870 | goto onError; |
| 5871 | case 2: /* replace */ |
| 5872 | for (p = collstart; p < collend; ++p) |
| 5873 | *output++ = '?'; |
| 5874 | /* fall through */ |
| 5875 | case 3: /* ignore */ |
| 5876 | p = collend; |
| 5877 | break; |
| 5878 | case 4: /* xmlcharrefreplace */ |
| 5879 | /* generate replacement (temporarily (mis)uses p) */ |
| 5880 | for (p = collstart; p < collend; ++p) |
| 5881 | output += sprintf(output, "&#%d;", (int)*p); |
| 5882 | p = collend; |
| 5883 | break; |
| 5884 | default: |
| 5885 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5886 | encoding, reason, s, length, &exc, |
| 5887 | collstart-s, collend-s, &newpos); |
| 5888 | if (repunicode == NULL) |
| 5889 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5890 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5891 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5892 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 5893 | Py_DECREF(repunicode); |
| 5894 | goto onError; |
| 5895 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5896 | /* generate replacement */ |
| 5897 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5898 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5899 | Py_UNICODE ch = *uni2; |
| 5900 | if (Py_UNICODE_ISSPACE(ch)) |
| 5901 | *output++ = ' '; |
| 5902 | else { |
| 5903 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5904 | if (decimal >= 0) |
| 5905 | *output++ = '0' + decimal; |
| 5906 | else if (0 < ch && ch < 256) |
| 5907 | *output++ = (char)ch; |
| 5908 | else { |
| 5909 | Py_DECREF(repunicode); |
| 5910 | raise_encode_exception(&exc, encoding, |
| 5911 | s, length, collstart-s, collend-s, reason); |
| 5912 | goto onError; |
| 5913 | } |
| 5914 | } |
| 5915 | } |
| 5916 | p = s + newpos; |
| 5917 | Py_DECREF(repunicode); |
| 5918 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5919 | } |
| 5920 | /* 0-terminate the output string */ |
| 5921 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5922 | Py_XDECREF(exc); |
| 5923 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5924 | return 0; |
| 5925 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5926 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5927 | Py_XDECREF(exc); |
| 5928 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5929 | return -1; |
| 5930 | } |
| 5931 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | /* --- Helpers ------------------------------------------------------------ */ |
| 5933 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5934 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5935 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5936 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5937 | #include "stringlib/count.h" |
| 5938 | #include "stringlib/find.h" |
| 5939 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5940 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5941 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5942 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 5943 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5944 | #include "stringlib/localeutil.h" |
| 5945 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5946 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5947 | #define ADJUST_INDICES(start, end, len) \ |
| 5948 | if (end > len) \ |
| 5949 | end = len; \ |
| 5950 | else if (end < 0) { \ |
| 5951 | end += len; \ |
| 5952 | if (end < 0) \ |
| 5953 | end = 0; \ |
| 5954 | } \ |
| 5955 | if (start < 0) { \ |
| 5956 | start += len; \ |
| 5957 | if (start < 0) \ |
| 5958 | start = 0; \ |
| 5959 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5960 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5961 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5962 | PyObject *substr, |
| 5963 | Py_ssize_t start, |
| 5964 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5965 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5966 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5967 | PyUnicodeObject* str_obj; |
| 5968 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5969 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5970 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5971 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5972 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5973 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5974 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5975 | Py_DECREF(str_obj); |
| 5976 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5977 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5978 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5979 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5980 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5981 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 5982 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5983 | ); |
| 5984 | |
| 5985 | Py_DECREF(sub_obj); |
| 5986 | Py_DECREF(str_obj); |
| 5987 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | return result; |
| 5989 | } |
| 5990 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5991 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5992 | PyObject *sub, |
| 5993 | Py_ssize_t start, |
| 5994 | Py_ssize_t end, |
| 5995 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5996 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5997 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5998 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5999 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6000 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6001 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6002 | sub = PyUnicode_FromObject(sub); |
| 6003 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6004 | Py_DECREF(str); |
| 6005 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6006 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6007 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6008 | if (direction > 0) |
| 6009 | result = stringlib_find_slice( |
| 6010 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6011 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6012 | start, end |
| 6013 | ); |
| 6014 | else |
| 6015 | result = stringlib_rfind_slice( |
| 6016 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6017 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6018 | start, end |
| 6019 | ); |
| 6020 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6022 | Py_DECREF(sub); |
| 6023 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | return result; |
| 6025 | } |
| 6026 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6027 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6029 | PyUnicodeObject *substring, |
| 6030 | Py_ssize_t start, |
| 6031 | Py_ssize_t end, |
| 6032 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | if (substring->length == 0) |
| 6035 | return 1; |
| 6036 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6037 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | end -= substring->length; |
| 6039 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6040 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | |
| 6042 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6043 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6044 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | } else { |
| 6046 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6047 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
| 6050 | return 0; |
| 6051 | } |
| 6052 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6053 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | PyObject *substr, |
| 6055 | Py_ssize_t start, |
| 6056 | Py_ssize_t end, |
| 6057 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6059 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6060 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | str = PyUnicode_FromObject(str); |
| 6062 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6063 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | substr = PyUnicode_FromObject(substr); |
| 6065 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6066 | Py_DECREF(str); |
| 6067 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6071 | (PyUnicodeObject *)substr, |
| 6072 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | Py_DECREF(str); |
| 6074 | Py_DECREF(substr); |
| 6075 | return result; |
| 6076 | } |
| 6077 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | /* Apply fixfct filter to the Unicode object self and return a |
| 6079 | reference to the modified object */ |
| 6080 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6081 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6083 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | { |
| 6085 | |
| 6086 | PyUnicodeObject *u; |
| 6087 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6088 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6090 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6091 | |
| 6092 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6093 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6094 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6095 | /* fixfct should return TRUE if it modified the buffer. If |
| 6096 | FALSE, return a reference to the original buffer instead |
| 6097 | (to save space, not time) */ |
| 6098 | Py_INCREF(self); |
| 6099 | Py_DECREF(u); |
| 6100 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6101 | } |
| 6102 | return (PyObject*) u; |
| 6103 | } |
| 6104 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6105 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6106 | int fixupper(PyUnicodeObject *self) |
| 6107 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6108 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | Py_UNICODE *s = self->str; |
| 6110 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6111 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6112 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6113 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6114 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | ch = Py_UNICODE_TOUPPER(*s); |
| 6116 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | *s = ch; |
| 6119 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6120 | s++; |
| 6121 | } |
| 6122 | |
| 6123 | return status; |
| 6124 | } |
| 6125 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6126 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | int fixlower(PyUnicodeObject *self) |
| 6128 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6129 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | Py_UNICODE *s = self->str; |
| 6131 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6134 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6135 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6136 | ch = Py_UNICODE_TOLOWER(*s); |
| 6137 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6139 | *s = ch; |
| 6140 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6141 | s++; |
| 6142 | } |
| 6143 | |
| 6144 | return status; |
| 6145 | } |
| 6146 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6147 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | int fixswapcase(PyUnicodeObject *self) |
| 6149 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6150 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | Py_UNICODE *s = self->str; |
| 6152 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6154 | while (len-- > 0) { |
| 6155 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6156 | *s = Py_UNICODE_TOLOWER(*s); |
| 6157 | status = 1; |
| 6158 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6159 | *s = Py_UNICODE_TOUPPER(*s); |
| 6160 | status = 1; |
| 6161 | } |
| 6162 | s++; |
| 6163 | } |
| 6164 | |
| 6165 | return status; |
| 6166 | } |
| 6167 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6168 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | int fixcapitalize(PyUnicodeObject *self) |
| 6170 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6171 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6172 | Py_UNICODE *s = self->str; |
| 6173 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6174 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6175 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6176 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6177 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6178 | *s = Py_UNICODE_TOUPPER(*s); |
| 6179 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6180 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6181 | s++; |
| 6182 | while (--len > 0) { |
| 6183 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6184 | *s = Py_UNICODE_TOLOWER(*s); |
| 6185 | status = 1; |
| 6186 | } |
| 6187 | s++; |
| 6188 | } |
| 6189 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | static |
| 6193 | int fixtitle(PyUnicodeObject *self) |
| 6194 | { |
| 6195 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6196 | register Py_UNICODE *e; |
| 6197 | int previous_is_cased; |
| 6198 | |
| 6199 | /* Shortcut for single character strings */ |
| 6200 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6202 | if (*p != ch) { |
| 6203 | *p = ch; |
| 6204 | return 1; |
| 6205 | } |
| 6206 | else |
| 6207 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6208 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6209 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6210 | e = p + PyUnicode_GET_SIZE(self); |
| 6211 | previous_is_cased = 0; |
| 6212 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6213 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6214 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6215 | if (previous_is_cased) |
| 6216 | *p = Py_UNICODE_TOLOWER(ch); |
| 6217 | else |
| 6218 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6219 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6220 | if (Py_UNICODE_ISLOWER(ch) || |
| 6221 | Py_UNICODE_ISUPPER(ch) || |
| 6222 | Py_UNICODE_ISTITLE(ch)) |
| 6223 | previous_is_cased = 1; |
| 6224 | else |
| 6225 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | } |
| 6227 | return 1; |
| 6228 | } |
| 6229 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6230 | PyObject * |
| 6231 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6233 | const Py_UNICODE blank = ' '; |
| 6234 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6235 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6236 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6237 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6238 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6239 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6240 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6241 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6242 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6243 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6244 | fseq = PySequence_Fast(seq, ""); |
| 6245 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6246 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6249 | /* NOTE: the following code can't call back into Python code, |
| 6250 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6251 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6252 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6253 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6254 | /* If empty sequence, return u"". */ |
| 6255 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6256 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6257 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6258 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6259 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6260 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6261 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6262 | item = items[0]; |
| 6263 | if (PyUnicode_CheckExact(item)) { |
| 6264 | Py_INCREF(item); |
| 6265 | res = (PyUnicodeObject *)item; |
| 6266 | goto Done; |
| 6267 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6268 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6269 | else { |
| 6270 | /* Set up sep and seplen */ |
| 6271 | if (separator == NULL) { |
| 6272 | sep = ␣ |
| 6273 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6274 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6275 | else { |
| 6276 | if (!PyUnicode_Check(separator)) { |
| 6277 | PyErr_Format(PyExc_TypeError, |
| 6278 | "separator: expected str instance," |
| 6279 | " %.80s found", |
| 6280 | Py_TYPE(separator)->tp_name); |
| 6281 | goto onError; |
| 6282 | } |
| 6283 | sep = PyUnicode_AS_UNICODE(separator); |
| 6284 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6285 | } |
| 6286 | } |
| 6287 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6288 | /* There are at least two things to join, or else we have a subclass |
| 6289 | * of str in the sequence. |
| 6290 | * Do a pre-pass to figure out the total amount of space we'll |
| 6291 | * need (sz), and see whether all argument are strings. |
| 6292 | */ |
| 6293 | sz = 0; |
| 6294 | for (i = 0; i < seqlen; i++) { |
| 6295 | const Py_ssize_t old_sz = sz; |
| 6296 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | if (!PyUnicode_Check(item)) { |
| 6298 | PyErr_Format(PyExc_TypeError, |
| 6299 | "sequence item %zd: expected str instance," |
| 6300 | " %.80s found", |
| 6301 | i, Py_TYPE(item)->tp_name); |
| 6302 | goto onError; |
| 6303 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6304 | sz += PyUnicode_GET_SIZE(item); |
| 6305 | if (i != 0) |
| 6306 | sz += seplen; |
| 6307 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6308 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6309 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6310 | goto onError; |
| 6311 | } |
| 6312 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6313 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6314 | res = _PyUnicode_New(sz); |
| 6315 | if (res == NULL) |
| 6316 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6317 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6318 | /* Catenate everything. */ |
| 6319 | res_p = PyUnicode_AS_UNICODE(res); |
| 6320 | for (i = 0; i < seqlen; ++i) { |
| 6321 | Py_ssize_t itemlen; |
| 6322 | item = items[i]; |
| 6323 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6324 | /* Copy item, and maybe the separator. */ |
| 6325 | if (i) { |
| 6326 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6327 | res_p += seplen; |
| 6328 | } |
| 6329 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6330 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6331 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6332 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6333 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6334 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6335 | return (PyObject *)res; |
| 6336 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6337 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6338 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6339 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6340 | return NULL; |
| 6341 | } |
| 6342 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6343 | static |
| 6344 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6345 | Py_ssize_t left, |
| 6346 | Py_ssize_t right, |
| 6347 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6348 | { |
| 6349 | PyUnicodeObject *u; |
| 6350 | |
| 6351 | if (left < 0) |
| 6352 | left = 0; |
| 6353 | if (right < 0) |
| 6354 | right = 0; |
| 6355 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6356 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | Py_INCREF(self); |
| 6358 | return self; |
| 6359 | } |
| 6360 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6361 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6362 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6363 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6364 | return NULL; |
| 6365 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6366 | u = _PyUnicode_New(left + self->length + right); |
| 6367 | if (u) { |
| 6368 | if (left) |
| 6369 | Py_UNICODE_FILL(u->str, fill, left); |
| 6370 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6371 | if (right) |
| 6372 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6373 | } |
| 6374 | |
| 6375 | return u; |
| 6376 | } |
| 6377 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6378 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6379 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6380 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6381 | |
| 6382 | string = PyUnicode_FromObject(string); |
| 6383 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6384 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6385 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6386 | list = stringlib_splitlines( |
| 6387 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6388 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6389 | |
| 6390 | Py_DECREF(string); |
| 6391 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6394 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6395 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6396 | PyUnicodeObject *substring, |
| 6397 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6398 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6399 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6400 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6401 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6402 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6403 | return stringlib_split_whitespace( |
| 6404 | (PyObject*) self, self->str, self->length, maxcount |
| 6405 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6406 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6407 | return stringlib_split( |
| 6408 | (PyObject*) self, self->str, self->length, |
| 6409 | substring->str, substring->length, |
| 6410 | maxcount |
| 6411 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | } |
| 6413 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6414 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6415 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | PyUnicodeObject *substring, |
| 6417 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6418 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6419 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6420 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6421 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6422 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6423 | return stringlib_rsplit_whitespace( |
| 6424 | (PyObject*) self, self->str, self->length, maxcount |
| 6425 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6426 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6427 | return stringlib_rsplit( |
| 6428 | (PyObject*) self, self->str, self->length, |
| 6429 | substring->str, substring->length, |
| 6430 | maxcount |
| 6431 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6435 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6436 | PyUnicodeObject *str1, |
| 6437 | PyUnicodeObject *str2, |
| 6438 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 | { |
| 6440 | PyUnicodeObject *u; |
| 6441 | |
| 6442 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6443 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6444 | else if (maxcount == 0 || self->length == 0) |
| 6445 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6447 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6448 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6449 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6450 | if (str1->length == 0) |
| 6451 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6452 | if (str1->length == 1) { |
| 6453 | /* replace characters */ |
| 6454 | Py_UNICODE u1, u2; |
| 6455 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6456 | goto nothing; |
| 6457 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6458 | if (!u) |
| 6459 | return NULL; |
| 6460 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6461 | u1 = str1->str[0]; |
| 6462 | u2 = str2->str[0]; |
| 6463 | for (i = 0; i < u->length; i++) |
| 6464 | if (u->str[i] == u1) { |
| 6465 | if (--maxcount < 0) |
| 6466 | break; |
| 6467 | u->str[i] = u2; |
| 6468 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6470 | i = stringlib_find( |
| 6471 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6472 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6473 | if (i < 0) |
| 6474 | goto nothing; |
| 6475 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6476 | if (!u) |
| 6477 | return NULL; |
| 6478 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6479 | |
| 6480 | /* change everything in-place, starting with this one */ |
| 6481 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6482 | i += str1->length; |
| 6483 | |
| 6484 | while ( --maxcount > 0) { |
| 6485 | i = stringlib_find(self->str+i, self->length-i, |
| 6486 | str1->str, str1->length, |
| 6487 | i); |
| 6488 | if (i == -1) |
| 6489 | break; |
| 6490 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6491 | i += str1->length; |
| 6492 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6493 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6494 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6495 | |
| 6496 | Py_ssize_t n, i, j, e; |
| 6497 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6498 | Py_UNICODE *p; |
| 6499 | |
| 6500 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6501 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6502 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6503 | if (n == 0) |
| 6504 | goto nothing; |
| 6505 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6506 | delta = (str2->length - str1->length); |
| 6507 | if (delta == 0) { |
| 6508 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6510 | product = n * (str2->length - str1->length); |
| 6511 | if ((product / (str2->length - str1->length)) != n) { |
| 6512 | PyErr_SetString(PyExc_OverflowError, |
| 6513 | "replace string is too long"); |
| 6514 | return NULL; |
| 6515 | } |
| 6516 | new_size = self->length + product; |
| 6517 | if (new_size < 0) { |
| 6518 | PyErr_SetString(PyExc_OverflowError, |
| 6519 | "replace string is too long"); |
| 6520 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6521 | } |
| 6522 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6523 | u = _PyUnicode_New(new_size); |
| 6524 | if (!u) |
| 6525 | return NULL; |
| 6526 | i = 0; |
| 6527 | p = u->str; |
| 6528 | e = self->length - str1->length; |
| 6529 | if (str1->length > 0) { |
| 6530 | while (n-- > 0) { |
| 6531 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6532 | j = stringlib_find(self->str+i, self->length-i, |
| 6533 | str1->str, str1->length, |
| 6534 | i); |
| 6535 | if (j == -1) |
| 6536 | break; |
| 6537 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6538 | /* copy unchanged part [i:j] */ |
| 6539 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6540 | p += j - i; |
| 6541 | } |
| 6542 | /* copy substitution string */ |
| 6543 | if (str2->length > 0) { |
| 6544 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6545 | p += str2->length; |
| 6546 | } |
| 6547 | i = j + str1->length; |
| 6548 | } |
| 6549 | if (i < self->length) |
| 6550 | /* copy tail [i:] */ |
| 6551 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6552 | } else { |
| 6553 | /* interleave */ |
| 6554 | while (n > 0) { |
| 6555 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6556 | p += str2->length; |
| 6557 | if (--n <= 0) |
| 6558 | break; |
| 6559 | *p++ = self->str[i++]; |
| 6560 | } |
| 6561 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6562 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6563 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6564 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6565 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6567 | /* nothing to replace; return original string (when possible) */ |
| 6568 | if (PyUnicode_CheckExact(self)) { |
| 6569 | Py_INCREF(self); |
| 6570 | return (PyObject *) self; |
| 6571 | } |
| 6572 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6573 | } |
| 6574 | |
| 6575 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6576 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6577 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6578 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | \n\ |
| 6580 | 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] | 6581 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6582 | |
| 6583 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6584 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6585 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | return fixup(self, fixtitle); |
| 6587 | } |
| 6588 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6589 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6591 | \n\ |
| 6592 | 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] | 6593 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6594 | |
| 6595 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6596 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6597 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | return fixup(self, fixcapitalize); |
| 6599 | } |
| 6600 | |
| 6601 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6602 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6603 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | \n\ |
| 6605 | 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] | 6606 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6607 | |
| 6608 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6609 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6610 | { |
| 6611 | PyObject *list; |
| 6612 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6613 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6614 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6615 | /* Split into words */ |
| 6616 | list = split(self, NULL, -1); |
| 6617 | if (!list) |
| 6618 | return NULL; |
| 6619 | |
| 6620 | /* Capitalize each word */ |
| 6621 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6622 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6623 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6624 | if (item == NULL) |
| 6625 | goto onError; |
| 6626 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6627 | PyList_SET_ITEM(list, i, item); |
| 6628 | } |
| 6629 | |
| 6630 | /* Join the words to form a new string */ |
| 6631 | item = PyUnicode_Join(NULL, list); |
| 6632 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6634 | Py_DECREF(list); |
| 6635 | return (PyObject *)item; |
| 6636 | } |
| 6637 | #endif |
| 6638 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6639 | /* Argument converter. Coerces to a single unicode character */ |
| 6640 | |
| 6641 | static int |
| 6642 | convert_uc(PyObject *obj, void *addr) |
| 6643 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6644 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6645 | PyObject *uniobj; |
| 6646 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6647 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6648 | uniobj = PyUnicode_FromObject(obj); |
| 6649 | if (uniobj == NULL) { |
| 6650 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6651 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6652 | return 0; |
| 6653 | } |
| 6654 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6655 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6656 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6657 | Py_DECREF(uniobj); |
| 6658 | return 0; |
| 6659 | } |
| 6660 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6661 | *fillcharloc = unistr[0]; |
| 6662 | Py_DECREF(uniobj); |
| 6663 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6664 | } |
| 6665 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6666 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6667 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6669 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6670 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | |
| 6672 | static PyObject * |
| 6673 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6674 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6675 | Py_ssize_t marg, left; |
| 6676 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6677 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6679 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6680 | return NULL; |
| 6681 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6682 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | Py_INCREF(self); |
| 6684 | return (PyObject*) self; |
| 6685 | } |
| 6686 | |
| 6687 | marg = width - self->length; |
| 6688 | left = marg / 2 + (marg & width & 1); |
| 6689 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6690 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6693 | #if 0 |
| 6694 | |
| 6695 | /* This code should go into some future Unicode collation support |
| 6696 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6697 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6698 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6699 | /* speedy UTF-16 code point order comparison */ |
| 6700 | /* gleaned from: */ |
| 6701 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6702 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6703 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6704 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6705 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6706 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6707 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6708 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6709 | }; |
| 6710 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6711 | static int |
| 6712 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6713 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6714 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6715 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6716 | Py_UNICODE *s1 = str1->str; |
| 6717 | Py_UNICODE *s2 = str2->str; |
| 6718 | |
| 6719 | len1 = str1->length; |
| 6720 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6721 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6722 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6723 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6724 | |
| 6725 | c1 = *s1++; |
| 6726 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6727 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6728 | if (c1 > (1<<11) * 26) |
| 6729 | c1 += utf16Fixup[c1>>11]; |
| 6730 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6731 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6732 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6733 | |
| 6734 | if (c1 != c2) |
| 6735 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6736 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6737 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | } |
| 6739 | |
| 6740 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6741 | } |
| 6742 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6743 | #else |
| 6744 | |
| 6745 | static int |
| 6746 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6747 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6748 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6749 | |
| 6750 | Py_UNICODE *s1 = str1->str; |
| 6751 | Py_UNICODE *s2 = str2->str; |
| 6752 | |
| 6753 | len1 = str1->length; |
| 6754 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6755 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6756 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6757 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6758 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6759 | c1 = *s1++; |
| 6760 | c2 = *s2++; |
| 6761 | |
| 6762 | if (c1 != c2) |
| 6763 | return (c1 < c2) ? -1 : 1; |
| 6764 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6765 | len1--; len2--; |
| 6766 | } |
| 6767 | |
| 6768 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6769 | } |
| 6770 | |
| 6771 | #endif |
| 6772 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6773 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6774 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6776 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6777 | return unicode_compare((PyUnicodeObject *)left, |
| 6778 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6779 | PyErr_Format(PyExc_TypeError, |
| 6780 | "Can't compare %.100s and %.100s", |
| 6781 | left->ob_type->tp_name, |
| 6782 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | return -1; |
| 6784 | } |
| 6785 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6786 | int |
| 6787 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6788 | { |
| 6789 | int i; |
| 6790 | Py_UNICODE *id; |
| 6791 | assert(PyUnicode_Check(uni)); |
| 6792 | id = PyUnicode_AS_UNICODE(uni); |
| 6793 | /* Compare Unicode string and source character set string */ |
| 6794 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6795 | if (id[i] != str[i]) |
| 6796 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 6797 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 6798 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 6799 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6800 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6801 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6802 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6803 | return 0; |
| 6804 | } |
| 6805 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6806 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6807 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6808 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6809 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6810 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6811 | PyObject *right, |
| 6812 | int op) |
| 6813 | { |
| 6814 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6815 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6816 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6817 | PyObject *v; |
| 6818 | if (((PyUnicodeObject *) left)->length != |
| 6819 | ((PyUnicodeObject *) right)->length) { |
| 6820 | if (op == Py_EQ) { |
| 6821 | Py_INCREF(Py_False); |
| 6822 | return Py_False; |
| 6823 | } |
| 6824 | if (op == Py_NE) { |
| 6825 | Py_INCREF(Py_True); |
| 6826 | return Py_True; |
| 6827 | } |
| 6828 | } |
| 6829 | if (left == right) |
| 6830 | result = 0; |
| 6831 | else |
| 6832 | result = unicode_compare((PyUnicodeObject *)left, |
| 6833 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6834 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6835 | /* Convert the return value to a Boolean */ |
| 6836 | switch (op) { |
| 6837 | case Py_EQ: |
| 6838 | v = TEST_COND(result == 0); |
| 6839 | break; |
| 6840 | case Py_NE: |
| 6841 | v = TEST_COND(result != 0); |
| 6842 | break; |
| 6843 | case Py_LE: |
| 6844 | v = TEST_COND(result <= 0); |
| 6845 | break; |
| 6846 | case Py_GE: |
| 6847 | v = TEST_COND(result >= 0); |
| 6848 | break; |
| 6849 | case Py_LT: |
| 6850 | v = TEST_COND(result == -1); |
| 6851 | break; |
| 6852 | case Py_GT: |
| 6853 | v = TEST_COND(result == 1); |
| 6854 | break; |
| 6855 | default: |
| 6856 | PyErr_BadArgument(); |
| 6857 | return NULL; |
| 6858 | } |
| 6859 | Py_INCREF(v); |
| 6860 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6861 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6862 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6863 | Py_INCREF(Py_NotImplemented); |
| 6864 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6867 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6868 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6869 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6870 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6871 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6872 | |
| 6873 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6874 | sub = PyUnicode_FromObject(element); |
| 6875 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6876 | PyErr_Format(PyExc_TypeError, |
| 6877 | "'in <string>' requires string as left operand, not %s", |
| 6878 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6879 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6880 | } |
| 6881 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6882 | str = PyUnicode_FromObject(container); |
| 6883 | if (!str) { |
| 6884 | Py_DECREF(sub); |
| 6885 | return -1; |
| 6886 | } |
| 6887 | |
| 6888 | result = stringlib_contains_obj(str, sub); |
| 6889 | |
| 6890 | Py_DECREF(str); |
| 6891 | Py_DECREF(sub); |
| 6892 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6893 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6894 | } |
| 6895 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6897 | |
| 6898 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6899 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | { |
| 6901 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6902 | |
| 6903 | /* Coerce the two arguments */ |
| 6904 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6905 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6906 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6908 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6909 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6910 | |
| 6911 | /* Shortcuts */ |
| 6912 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6913 | Py_DECREF(v); |
| 6914 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6915 | } |
| 6916 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6917 | Py_DECREF(u); |
| 6918 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6919 | } |
| 6920 | |
| 6921 | /* Concat the two Unicode strings */ |
| 6922 | w = _PyUnicode_New(u->length + v->length); |
| 6923 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6924 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6925 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6926 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6927 | |
| 6928 | Py_DECREF(u); |
| 6929 | Py_DECREF(v); |
| 6930 | return (PyObject *)w; |
| 6931 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6932 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6933 | Py_XDECREF(u); |
| 6934 | Py_XDECREF(v); |
| 6935 | return NULL; |
| 6936 | } |
| 6937 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6938 | void |
| 6939 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6940 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6941 | PyObject *new; |
| 6942 | if (*pleft == NULL) |
| 6943 | return; |
| 6944 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6945 | Py_DECREF(*pleft); |
| 6946 | *pleft = NULL; |
| 6947 | return; |
| 6948 | } |
| 6949 | new = PyUnicode_Concat(*pleft, right); |
| 6950 | Py_DECREF(*pleft); |
| 6951 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6952 | } |
| 6953 | |
| 6954 | void |
| 6955 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6956 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6957 | PyUnicode_Append(pleft, right); |
| 6958 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6959 | } |
| 6960 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6961 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6962 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6963 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6964 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6965 | 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] | 6966 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6967 | |
| 6968 | static PyObject * |
| 6969 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6970 | { |
| 6971 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6972 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6973 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6974 | PyObject *result; |
| 6975 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6976 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6977 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6978 | return NULL; |
| 6979 | |
| 6980 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6981 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6983 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6984 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6985 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6986 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6987 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6988 | substring->str, substring->length, |
| 6989 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6990 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6991 | |
| 6992 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | return result; |
| 6995 | } |
| 6996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6997 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6998 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6999 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7000 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7001 | 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] | 7002 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7003 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7004 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7005 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7006 | |
| 7007 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7008 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7010 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7011 | char *encoding = NULL; |
| 7012 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7013 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7014 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7015 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7016 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7017 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7018 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7019 | if (v == NULL) |
| 7020 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7021 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7022 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7023 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7024 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7025 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7026 | Py_DECREF(v); |
| 7027 | return NULL; |
| 7028 | } |
| 7029 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7030 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7031 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7032 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7033 | } |
| 7034 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7035 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7036 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7037 | \n\ |
| 7038 | 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] | 7039 | 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] | 7040 | |
| 7041 | static PyObject* |
| 7042 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7043 | { |
| 7044 | Py_UNICODE *e; |
| 7045 | Py_UNICODE *p; |
| 7046 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7047 | Py_UNICODE *qe; |
| 7048 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7049 | PyUnicodeObject *u; |
| 7050 | int tabsize = 8; |
| 7051 | |
| 7052 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7053 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7054 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7055 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7056 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7057 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7058 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7059 | for (p = self->str; p < e; p++) |
| 7060 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7061 | if (tabsize > 0) { |
| 7062 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7063 | if (j > PY_SSIZE_T_MAX - incr) |
| 7064 | goto overflow1; |
| 7065 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7066 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7067 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7068 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7069 | if (j > PY_SSIZE_T_MAX - 1) |
| 7070 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7071 | j++; |
| 7072 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7073 | if (i > PY_SSIZE_T_MAX - j) |
| 7074 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7075 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7076 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7077 | } |
| 7078 | } |
| 7079 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7080 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7081 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7082 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7083 | /* Second pass: create output string and fill it */ |
| 7084 | u = _PyUnicode_New(i + j); |
| 7085 | if (!u) |
| 7086 | return NULL; |
| 7087 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7088 | j = 0; /* same as in first pass */ |
| 7089 | q = u->str; /* next output char */ |
| 7090 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7091 | |
| 7092 | for (p = self->str; p < e; p++) |
| 7093 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7094 | if (tabsize > 0) { |
| 7095 | i = tabsize - (j % tabsize); |
| 7096 | j += i; |
| 7097 | while (i--) { |
| 7098 | if (q >= qe) |
| 7099 | goto overflow2; |
| 7100 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7101 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7102 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7103 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7104 | else { |
| 7105 | if (q >= qe) |
| 7106 | goto overflow2; |
| 7107 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7108 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7109 | if (*p == '\n' || *p == '\r') |
| 7110 | j = 0; |
| 7111 | } |
| 7112 | |
| 7113 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7114 | |
| 7115 | overflow2: |
| 7116 | Py_DECREF(u); |
| 7117 | overflow1: |
| 7118 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7119 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7120 | } |
| 7121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7122 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7123 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7124 | \n\ |
| 7125 | 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] | 7126 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7127 | arguments start and end are interpreted as in slice notation.\n\ |
| 7128 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7129 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | |
| 7131 | static PyObject * |
| 7132 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7133 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7134 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7135 | Py_ssize_t start; |
| 7136 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7137 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7138 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7139 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7140 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7141 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7142 | result = stringlib_find_slice( |
| 7143 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7144 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7145 | start, end |
| 7146 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7147 | |
| 7148 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7149 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7150 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7151 | } |
| 7152 | |
| 7153 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7154 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7155 | { |
| 7156 | if (index < 0 || index >= self->length) { |
| 7157 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7158 | return NULL; |
| 7159 | } |
| 7160 | |
| 7161 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7162 | } |
| 7163 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7164 | /* Believe it or not, this produces the same value for ASCII strings |
| 7165 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7166 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7167 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7168 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7169 | Py_ssize_t len; |
| 7170 | Py_UNICODE *p; |
| 7171 | long x; |
| 7172 | |
| 7173 | if (self->hash != -1) |
| 7174 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7175 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7176 | p = self->str; |
| 7177 | x = *p << 7; |
| 7178 | while (--len >= 0) |
| 7179 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7180 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7181 | if (x == -1) |
| 7182 | x = -2; |
| 7183 | self->hash = x; |
| 7184 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7185 | } |
| 7186 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7187 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7188 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7189 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7190 | 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] | 7191 | |
| 7192 | static PyObject * |
| 7193 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7194 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7195 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7196 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7197 | Py_ssize_t start; |
| 7198 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7199 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7200 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7201 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7202 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7203 | result = stringlib_find_slice( |
| 7204 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7205 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7206 | start, end |
| 7207 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7208 | |
| 7209 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7210 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7211 | if (result < 0) { |
| 7212 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7213 | return NULL; |
| 7214 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7215 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7216 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7217 | } |
| 7218 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7219 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7220 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7221 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7222 | 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] | 7223 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7224 | |
| 7225 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7226 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7227 | { |
| 7228 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7229 | register const Py_UNICODE *e; |
| 7230 | int cased; |
| 7231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7232 | /* Shortcut for single character strings */ |
| 7233 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7234 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7235 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7236 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7237 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7238 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | e = p + PyUnicode_GET_SIZE(self); |
| 7241 | cased = 0; |
| 7242 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7243 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7244 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7245 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7246 | return PyBool_FromLong(0); |
| 7247 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7248 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7249 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7250 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7251 | } |
| 7252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7253 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7254 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7255 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7256 | 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] | 7257 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7258 | |
| 7259 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7260 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7261 | { |
| 7262 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7263 | register const Py_UNICODE *e; |
| 7264 | int cased; |
| 7265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7266 | /* Shortcut for single character strings */ |
| 7267 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7268 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7269 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7270 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7271 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7272 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7274 | e = p + PyUnicode_GET_SIZE(self); |
| 7275 | cased = 0; |
| 7276 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7277 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7278 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7279 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7280 | return PyBool_FromLong(0); |
| 7281 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7282 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7283 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7284 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7285 | } |
| 7286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7287 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7288 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7289 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7290 | Return True if S is a titlecased string and there is at least one\n\ |
| 7291 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7292 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7293 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7294 | |
| 7295 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7296 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7297 | { |
| 7298 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7299 | register const Py_UNICODE *e; |
| 7300 | int cased, previous_is_cased; |
| 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 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7305 | (Py_UNICODE_ISUPPER(*p) != 0)); |
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 | cased = 0; |
| 7313 | previous_is_cased = 0; |
| 7314 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7315 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7316 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7317 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7318 | if (previous_is_cased) |
| 7319 | return PyBool_FromLong(0); |
| 7320 | previous_is_cased = 1; |
| 7321 | cased = 1; |
| 7322 | } |
| 7323 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7324 | if (!previous_is_cased) |
| 7325 | return PyBool_FromLong(0); |
| 7326 | previous_is_cased = 1; |
| 7327 | cased = 1; |
| 7328 | } |
| 7329 | else |
| 7330 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7331 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7332 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7333 | } |
| 7334 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7335 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7336 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7337 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7338 | Return True if all characters in S are whitespace\n\ |
| 7339 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7340 | |
| 7341 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7342 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7343 | { |
| 7344 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7345 | register const Py_UNICODE *e; |
| 7346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7347 | /* Shortcut for single character strings */ |
| 7348 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7349 | Py_UNICODE_ISSPACE(*p)) |
| 7350 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7351 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7352 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7353 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7354 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7355 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7356 | e = p + PyUnicode_GET_SIZE(self); |
| 7357 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7358 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7359 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7360 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7361 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7362 | } |
| 7363 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7364 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7365 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7366 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7367 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7368 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7369 | |
| 7370 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7371 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7372 | { |
| 7373 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7374 | register const Py_UNICODE *e; |
| 7375 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7376 | /* Shortcut for single character strings */ |
| 7377 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7378 | Py_UNICODE_ISALPHA(*p)) |
| 7379 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7380 | |
| 7381 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7382 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7383 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7384 | |
| 7385 | e = p + PyUnicode_GET_SIZE(self); |
| 7386 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7387 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7388 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7389 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7390 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7391 | } |
| 7392 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7393 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7394 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7395 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7396 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7397 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7398 | |
| 7399 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7400 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7401 | { |
| 7402 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7403 | register const Py_UNICODE *e; |
| 7404 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7405 | /* Shortcut for single character strings */ |
| 7406 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7407 | Py_UNICODE_ISALNUM(*p)) |
| 7408 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7409 | |
| 7410 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7411 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7412 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7413 | |
| 7414 | e = p + PyUnicode_GET_SIZE(self); |
| 7415 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7416 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7417 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7418 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7419 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7420 | } |
| 7421 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7422 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7423 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7424 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7425 | 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] | 7426 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7427 | |
| 7428 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7429 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7430 | { |
| 7431 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7432 | register const Py_UNICODE *e; |
| 7433 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7434 | /* Shortcut for single character strings */ |
| 7435 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7436 | Py_UNICODE_ISDECIMAL(*p)) |
| 7437 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7438 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7439 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7440 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7441 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7442 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7443 | e = p + PyUnicode_GET_SIZE(self); |
| 7444 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7445 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7446 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7447 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7448 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7449 | } |
| 7450 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7451 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7452 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7453 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7454 | Return True if all characters in S are digits\n\ |
| 7455 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 | |
| 7457 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7458 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7459 | { |
| 7460 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7461 | register const Py_UNICODE *e; |
| 7462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7463 | /* Shortcut for single character strings */ |
| 7464 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7465 | Py_UNICODE_ISDIGIT(*p)) |
| 7466 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7468 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7469 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7470 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7471 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7472 | e = p + PyUnicode_GET_SIZE(self); |
| 7473 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7474 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7475 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7477 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7478 | } |
| 7479 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7480 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7481 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7482 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7483 | 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] | 7484 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7485 | |
| 7486 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7487 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 | { |
| 7489 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7490 | register const Py_UNICODE *e; |
| 7491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7492 | /* Shortcut for single character strings */ |
| 7493 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7494 | Py_UNICODE_ISNUMERIC(*p)) |
| 7495 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7497 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7498 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7499 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7501 | e = p + PyUnicode_GET_SIZE(self); |
| 7502 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7503 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7504 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7506 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7507 | } |
| 7508 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7509 | int |
| 7510 | PyUnicode_IsIdentifier(PyObject *self) |
| 7511 | { |
| 7512 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7513 | register const Py_UNICODE *e; |
| 7514 | |
| 7515 | /* Special case for empty strings */ |
| 7516 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7517 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7518 | |
| 7519 | /* PEP 3131 says that the first character must be in |
| 7520 | XID_Start and subsequent characters in XID_Continue, |
| 7521 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7522 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7523 | letters, digits, underscore). However, given the current |
| 7524 | definition of XID_Start and XID_Continue, it is sufficient |
| 7525 | to check just for these, except that _ must be allowed |
| 7526 | as starting an identifier. */ |
| 7527 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7528 | return 0; |
| 7529 | |
| 7530 | e = p + PyUnicode_GET_SIZE(self); |
| 7531 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7532 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7533 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7534 | } |
| 7535 | return 1; |
| 7536 | } |
| 7537 | |
| 7538 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7539 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7540 | \n\ |
| 7541 | Return True if S is a valid identifier according\n\ |
| 7542 | to the language definition."); |
| 7543 | |
| 7544 | static PyObject* |
| 7545 | unicode_isidentifier(PyObject *self) |
| 7546 | { |
| 7547 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7548 | } |
| 7549 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7550 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7551 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7552 | \n\ |
| 7553 | Return True if all characters in S are considered\n\ |
| 7554 | printable in repr() or S is empty, False otherwise."); |
| 7555 | |
| 7556 | static PyObject* |
| 7557 | unicode_isprintable(PyObject *self) |
| 7558 | { |
| 7559 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7560 | register const Py_UNICODE *e; |
| 7561 | |
| 7562 | /* Shortcut for single character strings */ |
| 7563 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7564 | Py_RETURN_TRUE; |
| 7565 | } |
| 7566 | |
| 7567 | e = p + PyUnicode_GET_SIZE(self); |
| 7568 | for (; p < e; p++) { |
| 7569 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7570 | Py_RETURN_FALSE; |
| 7571 | } |
| 7572 | } |
| 7573 | Py_RETURN_TRUE; |
| 7574 | } |
| 7575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7576 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7577 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7578 | \n\ |
| 7579 | 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] | 7580 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 | |
| 7582 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7583 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7584 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7585 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7586 | } |
| 7587 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7588 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7589 | unicode_length(PyUnicodeObject *self) |
| 7590 | { |
| 7591 | return self->length; |
| 7592 | } |
| 7593 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7594 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7595 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7596 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7597 | 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] | 7598 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | |
| 7600 | static PyObject * |
| 7601 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7602 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7603 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7604 | Py_UNICODE fillchar = ' '; |
| 7605 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7606 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7607 | return NULL; |
| 7608 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7609 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7610 | Py_INCREF(self); |
| 7611 | return (PyObject*) self; |
| 7612 | } |
| 7613 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7614 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7615 | } |
| 7616 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7617 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7618 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7619 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7620 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7621 | |
| 7622 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7623 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7624 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7625 | return fixup(self, fixlower); |
| 7626 | } |
| 7627 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7628 | #define LEFTSTRIP 0 |
| 7629 | #define RIGHTSTRIP 1 |
| 7630 | #define BOTHSTRIP 2 |
| 7631 | |
| 7632 | /* Arrays indexed by above */ |
| 7633 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7634 | |
| 7635 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7636 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7637 | /* externally visible for str.strip(unicode) */ |
| 7638 | PyObject * |
| 7639 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7640 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7641 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7642 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7643 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7644 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7645 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7646 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7647 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7648 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7649 | i = 0; |
| 7650 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7651 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7652 | i++; |
| 7653 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7654 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7655 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7656 | j = len; |
| 7657 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7658 | do { |
| 7659 | j--; |
| 7660 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7661 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7662 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7663 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7664 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7665 | Py_INCREF(self); |
| 7666 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7667 | } |
| 7668 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7669 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7670 | } |
| 7671 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7672 | |
| 7673 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7674 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7675 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7676 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7677 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7678 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7679 | i = 0; |
| 7680 | if (striptype != RIGHTSTRIP) { |
| 7681 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7682 | i++; |
| 7683 | } |
| 7684 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7685 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7686 | j = len; |
| 7687 | if (striptype != LEFTSTRIP) { |
| 7688 | do { |
| 7689 | j--; |
| 7690 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7691 | j++; |
| 7692 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7693 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7694 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7695 | Py_INCREF(self); |
| 7696 | return (PyObject*)self; |
| 7697 | } |
| 7698 | else |
| 7699 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7700 | } |
| 7701 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7702 | |
| 7703 | static PyObject * |
| 7704 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7705 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7706 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7707 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7708 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7709 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7710 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7711 | if (sep != NULL && sep != Py_None) { |
| 7712 | if (PyUnicode_Check(sep)) |
| 7713 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7714 | else { |
| 7715 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7716 | "%s arg must be None or str", |
| 7717 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7718 | return NULL; |
| 7719 | } |
| 7720 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7721 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7722 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7723 | } |
| 7724 | |
| 7725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7726 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7727 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7728 | \n\ |
| 7729 | Return a copy of the string S with leading and trailing\n\ |
| 7730 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7731 | 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] | 7732 | |
| 7733 | static PyObject * |
| 7734 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7735 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7736 | if (PyTuple_GET_SIZE(args) == 0) |
| 7737 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7738 | else |
| 7739 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7740 | } |
| 7741 | |
| 7742 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7743 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7744 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7745 | \n\ |
| 7746 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7747 | 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] | 7748 | |
| 7749 | static PyObject * |
| 7750 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7751 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7752 | if (PyTuple_GET_SIZE(args) == 0) |
| 7753 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7754 | else |
| 7755 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7756 | } |
| 7757 | |
| 7758 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7759 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7760 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7761 | \n\ |
| 7762 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7763 | 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] | 7764 | |
| 7765 | static PyObject * |
| 7766 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7767 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7768 | if (PyTuple_GET_SIZE(args) == 0) |
| 7769 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7770 | else |
| 7771 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7772 | } |
| 7773 | |
| 7774 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7776 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7777 | { |
| 7778 | PyUnicodeObject *u; |
| 7779 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7780 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7781 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7783 | if (len < 1) { |
| 7784 | Py_INCREF(unicode_empty); |
| 7785 | return (PyObject *)unicode_empty; |
| 7786 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7787 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7788 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | /* no repeat, return original string */ |
| 7790 | Py_INCREF(str); |
| 7791 | return (PyObject*) str; |
| 7792 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7793 | |
| 7794 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7795 | * needed doesn't overflow size_t |
| 7796 | */ |
| 7797 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7798 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7799 | PyErr_SetString(PyExc_OverflowError, |
| 7800 | "repeated string is too long"); |
| 7801 | return NULL; |
| 7802 | } |
| 7803 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7804 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7805 | PyErr_SetString(PyExc_OverflowError, |
| 7806 | "repeated string is too long"); |
| 7807 | return NULL; |
| 7808 | } |
| 7809 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7810 | if (!u) |
| 7811 | return NULL; |
| 7812 | |
| 7813 | p = u->str; |
| 7814 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7815 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7816 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7817 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7818 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7819 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7820 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7821 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7822 | Py_UNICODE_COPY(p+done, p, n); |
| 7823 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7824 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7825 | } |
| 7826 | |
| 7827 | return (PyObject*) u; |
| 7828 | } |
| 7829 | |
| 7830 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7831 | PyObject *subobj, |
| 7832 | PyObject *replobj, |
| 7833 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 | { |
| 7835 | PyObject *self; |
| 7836 | PyObject *str1; |
| 7837 | PyObject *str2; |
| 7838 | PyObject *result; |
| 7839 | |
| 7840 | self = PyUnicode_FromObject(obj); |
| 7841 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7842 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7843 | str1 = PyUnicode_FromObject(subobj); |
| 7844 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7845 | Py_DECREF(self); |
| 7846 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7847 | } |
| 7848 | str2 = PyUnicode_FromObject(replobj); |
| 7849 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7850 | Py_DECREF(self); |
| 7851 | Py_DECREF(str1); |
| 7852 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7853 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7854 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7855 | (PyUnicodeObject *)str1, |
| 7856 | (PyUnicodeObject *)str2, |
| 7857 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7858 | Py_DECREF(self); |
| 7859 | Py_DECREF(str1); |
| 7860 | Py_DECREF(str2); |
| 7861 | return result; |
| 7862 | } |
| 7863 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7864 | PyDoc_STRVAR(replace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7865 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7866 | \n\ |
| 7867 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7868 | old replaced by new. If the optional argument count is\n\ |
| 7869 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7870 | |
| 7871 | static PyObject* |
| 7872 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7873 | { |
| 7874 | PyUnicodeObject *str1; |
| 7875 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7876 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7877 | PyObject *result; |
| 7878 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7879 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7880 | return NULL; |
| 7881 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7882 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7883 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7884 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7885 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7886 | Py_DECREF(str1); |
| 7887 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7888 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7889 | |
| 7890 | result = replace(self, str1, str2, maxcount); |
| 7891 | |
| 7892 | Py_DECREF(str1); |
| 7893 | Py_DECREF(str2); |
| 7894 | return result; |
| 7895 | } |
| 7896 | |
| 7897 | static |
| 7898 | PyObject *unicode_repr(PyObject *unicode) |
| 7899 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7900 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7901 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7902 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7903 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7904 | |
| 7905 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7906 | better to choose a different scheme. Perhaps scan the |
| 7907 | first N-chars of the string and allocate based on that size. |
| 7908 | */ |
| 7909 | /* Initial allocation is based on the longest-possible unichr |
| 7910 | escape. |
| 7911 | |
| 7912 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7913 | unichr, so in this case it's the longest unichr escape. In |
| 7914 | narrow (UTF-16) builds this is five chars per source unichr |
| 7915 | since there are two unichrs in the surrogate pair, so in narrow |
| 7916 | (UTF-16) builds it's not the longest unichr escape. |
| 7917 | |
| 7918 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7919 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7920 | escape. |
| 7921 | */ |
| 7922 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7923 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7924 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7925 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7926 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7927 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7928 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7929 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7930 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7931 | if (repr == NULL) |
| 7932 | return NULL; |
| 7933 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7934 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7935 | |
| 7936 | /* Add quote */ |
| 7937 | *p++ = (findchar(s, size, '\'') && |
| 7938 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7939 | while (size-- > 0) { |
| 7940 | Py_UNICODE ch = *s++; |
| 7941 | |
| 7942 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7943 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7944 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7945 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7946 | continue; |
| 7947 | } |
| 7948 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7949 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7950 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7951 | *p++ = '\\'; |
| 7952 | *p++ = 't'; |
| 7953 | } |
| 7954 | else if (ch == '\n') { |
| 7955 | *p++ = '\\'; |
| 7956 | *p++ = 'n'; |
| 7957 | } |
| 7958 | else if (ch == '\r') { |
| 7959 | *p++ = '\\'; |
| 7960 | *p++ = 'r'; |
| 7961 | } |
| 7962 | |
| 7963 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7964 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7965 | *p++ = '\\'; |
| 7966 | *p++ = 'x'; |
| 7967 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7968 | *p++ = hexdigits[ch & 0x000F]; |
| 7969 | } |
| 7970 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7971 | /* Copy ASCII characters as-is */ |
| 7972 | else if (ch < 0x7F) { |
| 7973 | *p++ = ch; |
| 7974 | } |
| 7975 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7976 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7977 | else { |
| 7978 | Py_UCS4 ucs = ch; |
| 7979 | |
| 7980 | #ifndef Py_UNICODE_WIDE |
| 7981 | Py_UNICODE ch2 = 0; |
| 7982 | /* Get code point from surrogate pair */ |
| 7983 | if (size > 0) { |
| 7984 | ch2 = *s; |
| 7985 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7986 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7987 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7988 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7989 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7990 | size--; |
| 7991 | } |
| 7992 | } |
| 7993 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7994 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7995 | (categories Z* and C* except ASCII space) |
| 7996 | */ |
| 7997 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 7998 | /* Map 8-bit characters to '\xhh' */ |
| 7999 | if (ucs <= 0xff) { |
| 8000 | *p++ = '\\'; |
| 8001 | *p++ = 'x'; |
| 8002 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8003 | *p++ = hexdigits[ch & 0x000F]; |
| 8004 | } |
| 8005 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8006 | else if (ucs >= 0x10000) { |
| 8007 | *p++ = '\\'; |
| 8008 | *p++ = 'U'; |
| 8009 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8010 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8011 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8012 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8013 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8014 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8015 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8016 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8017 | } |
| 8018 | /* Map 16-bit characters to '\uxxxx' */ |
| 8019 | else { |
| 8020 | *p++ = '\\'; |
| 8021 | *p++ = 'u'; |
| 8022 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8023 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8024 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8025 | *p++ = hexdigits[ucs & 0x000F]; |
| 8026 | } |
| 8027 | } |
| 8028 | /* Copy characters as-is */ |
| 8029 | else { |
| 8030 | *p++ = ch; |
| 8031 | #ifndef Py_UNICODE_WIDE |
| 8032 | if (ucs >= 0x10000) |
| 8033 | *p++ = ch2; |
| 8034 | #endif |
| 8035 | } |
| 8036 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8037 | } |
| 8038 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8039 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8040 | |
| 8041 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8042 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8043 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8044 | } |
| 8045 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8046 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8047 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8048 | \n\ |
| 8049 | 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] | 8050 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8051 | arguments start and end are interpreted as in slice notation.\n\ |
| 8052 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8053 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | |
| 8055 | static PyObject * |
| 8056 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8057 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8058 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8059 | Py_ssize_t start; |
| 8060 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8061 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8062 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8063 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8064 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8065 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8066 | result = stringlib_rfind_slice( |
| 8067 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8068 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8069 | start, end |
| 8070 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8071 | |
| 8072 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8073 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8074 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8075 | } |
| 8076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8077 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8078 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8079 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8080 | 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] | 8081 | |
| 8082 | static PyObject * |
| 8083 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8084 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8085 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8086 | Py_ssize_t start; |
| 8087 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8088 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8090 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8091 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8092 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8093 | result = stringlib_rfind_slice( |
| 8094 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8095 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8096 | start, end |
| 8097 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8098 | |
| 8099 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8100 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8101 | if (result < 0) { |
| 8102 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8103 | return NULL; |
| 8104 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8105 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8106 | } |
| 8107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8108 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8109 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8110 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8111 | 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] | 8112 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8113 | |
| 8114 | static PyObject * |
| 8115 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8116 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8117 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8118 | Py_UNICODE fillchar = ' '; |
| 8119 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8120 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8121 | return NULL; |
| 8122 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8123 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8124 | Py_INCREF(self); |
| 8125 | return (PyObject*) self; |
| 8126 | } |
| 8127 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8128 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8129 | } |
| 8130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8131 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | PyObject *sep, |
| 8133 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8134 | { |
| 8135 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | s = PyUnicode_FromObject(s); |
| 8138 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8139 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8140 | if (sep != NULL) { |
| 8141 | sep = PyUnicode_FromObject(sep); |
| 8142 | if (sep == NULL) { |
| 8143 | Py_DECREF(s); |
| 8144 | return NULL; |
| 8145 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8146 | } |
| 8147 | |
| 8148 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8149 | |
| 8150 | Py_DECREF(s); |
| 8151 | Py_XDECREF(sep); |
| 8152 | return result; |
| 8153 | } |
| 8154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8155 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8156 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8157 | \n\ |
| 8158 | Return a list of the words in S, using sep as the\n\ |
| 8159 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8160 | 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] | 8161 | whitespace string is a separator and empty strings are\n\ |
| 8162 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8163 | |
| 8164 | static PyObject* |
| 8165 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8166 | { |
| 8167 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8168 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8169 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8170 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8171 | return NULL; |
| 8172 | |
| 8173 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8175 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8176 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8177 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8178 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8179 | } |
| 8180 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8181 | PyObject * |
| 8182 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8183 | { |
| 8184 | PyObject* str_obj; |
| 8185 | PyObject* sep_obj; |
| 8186 | PyObject* out; |
| 8187 | |
| 8188 | str_obj = PyUnicode_FromObject(str_in); |
| 8189 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8190 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8191 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8192 | if (!sep_obj) { |
| 8193 | Py_DECREF(str_obj); |
| 8194 | return NULL; |
| 8195 | } |
| 8196 | |
| 8197 | out = stringlib_partition( |
| 8198 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8199 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8200 | ); |
| 8201 | |
| 8202 | Py_DECREF(sep_obj); |
| 8203 | Py_DECREF(str_obj); |
| 8204 | |
| 8205 | return out; |
| 8206 | } |
| 8207 | |
| 8208 | |
| 8209 | PyObject * |
| 8210 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8211 | { |
| 8212 | PyObject* str_obj; |
| 8213 | PyObject* sep_obj; |
| 8214 | PyObject* out; |
| 8215 | |
| 8216 | str_obj = PyUnicode_FromObject(str_in); |
| 8217 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8218 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8219 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8220 | if (!sep_obj) { |
| 8221 | Py_DECREF(str_obj); |
| 8222 | return NULL; |
| 8223 | } |
| 8224 | |
| 8225 | out = stringlib_rpartition( |
| 8226 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8227 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8228 | ); |
| 8229 | |
| 8230 | Py_DECREF(sep_obj); |
| 8231 | Py_DECREF(str_obj); |
| 8232 | |
| 8233 | return out; |
| 8234 | } |
| 8235 | |
| 8236 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8238 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8239 | 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] | 8240 | 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] | 8241 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8242 | |
| 8243 | static PyObject* |
| 8244 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8245 | { |
| 8246 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8247 | } |
| 8248 | |
| 8249 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8250 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8251 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8252 | 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] | 8253 | 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] | 8254 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8255 | |
| 8256 | static PyObject* |
| 8257 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8258 | { |
| 8259 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8260 | } |
| 8261 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8262 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8263 | PyObject *sep, |
| 8264 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8265 | { |
| 8266 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8267 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8268 | s = PyUnicode_FromObject(s); |
| 8269 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8270 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | if (sep != NULL) { |
| 8272 | sep = PyUnicode_FromObject(sep); |
| 8273 | if (sep == NULL) { |
| 8274 | Py_DECREF(s); |
| 8275 | return NULL; |
| 8276 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8277 | } |
| 8278 | |
| 8279 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8280 | |
| 8281 | Py_DECREF(s); |
| 8282 | Py_XDECREF(sep); |
| 8283 | return result; |
| 8284 | } |
| 8285 | |
| 8286 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8287 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8288 | \n\ |
| 8289 | Return a list of the words in S, using sep as the\n\ |
| 8290 | delimiter string, starting at the end of the string and\n\ |
| 8291 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8292 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8293 | is a separator."); |
| 8294 | |
| 8295 | static PyObject* |
| 8296 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8297 | { |
| 8298 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8299 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8300 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8301 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8302 | return NULL; |
| 8303 | |
| 8304 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8305 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8306 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8307 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8308 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8309 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8310 | } |
| 8311 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8312 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8313 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8314 | \n\ |
| 8315 | 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] | 8316 | 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] | 8317 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8318 | |
| 8319 | static PyObject* |
| 8320 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8321 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8322 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8323 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8324 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8325 | return NULL; |
| 8326 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8327 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8328 | } |
| 8329 | |
| 8330 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8331 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8332 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8333 | if (PyUnicode_CheckExact(self)) { |
| 8334 | Py_INCREF(self); |
| 8335 | return self; |
| 8336 | } else |
| 8337 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8338 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8339 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8340 | } |
| 8341 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8342 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8343 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8344 | \n\ |
| 8345 | 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] | 8346 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8347 | |
| 8348 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8349 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8350 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | return fixup(self, fixswapcase); |
| 8352 | } |
| 8353 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8354 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8355 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8356 | \n\ |
| 8357 | Return a translation table usable for str.translate().\n\ |
| 8358 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8359 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8360 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8361 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8362 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8363 | character at the same position in y. If there is a third argument, it\n\ |
| 8364 | must be a string, whose characters will be mapped to None in the result."); |
| 8365 | |
| 8366 | static PyObject* |
| 8367 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8368 | { |
| 8369 | PyObject *x, *y = NULL, *z = NULL; |
| 8370 | PyObject *new = NULL, *key, *value; |
| 8371 | Py_ssize_t i = 0; |
| 8372 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8373 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8374 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8375 | return NULL; |
| 8376 | new = PyDict_New(); |
| 8377 | if (!new) |
| 8378 | return NULL; |
| 8379 | if (y != NULL) { |
| 8380 | /* x must be a string too, of equal length */ |
| 8381 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8382 | if (!PyUnicode_Check(x)) { |
| 8383 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8384 | "be a string if there is a second argument"); |
| 8385 | goto err; |
| 8386 | } |
| 8387 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8388 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8389 | "arguments must have equal length"); |
| 8390 | goto err; |
| 8391 | } |
| 8392 | /* create entries for translating chars in x to those in y */ |
| 8393 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8394 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8395 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8396 | if (!key || !value) |
| 8397 | goto err; |
| 8398 | res = PyDict_SetItem(new, key, value); |
| 8399 | Py_DECREF(key); |
| 8400 | Py_DECREF(value); |
| 8401 | if (res < 0) |
| 8402 | goto err; |
| 8403 | } |
| 8404 | /* create entries for deleting chars in z */ |
| 8405 | if (z != NULL) { |
| 8406 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8407 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8408 | if (!key) |
| 8409 | goto err; |
| 8410 | res = PyDict_SetItem(new, key, Py_None); |
| 8411 | Py_DECREF(key); |
| 8412 | if (res < 0) |
| 8413 | goto err; |
| 8414 | } |
| 8415 | } |
| 8416 | } else { |
| 8417 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8418 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8419 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8420 | "to maketrans it must be a dict"); |
| 8421 | goto err; |
| 8422 | } |
| 8423 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8424 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8425 | if (PyUnicode_Check(key)) { |
| 8426 | /* convert string keys to integer keys */ |
| 8427 | PyObject *newkey; |
| 8428 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8429 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8430 | "table must be of length 1"); |
| 8431 | goto err; |
| 8432 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8433 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8434 | if (!newkey) |
| 8435 | goto err; |
| 8436 | res = PyDict_SetItem(new, newkey, value); |
| 8437 | Py_DECREF(newkey); |
| 8438 | if (res < 0) |
| 8439 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8440 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8441 | /* just keep integer keys */ |
| 8442 | if (PyDict_SetItem(new, key, value) < 0) |
| 8443 | goto err; |
| 8444 | } else { |
| 8445 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8446 | "be strings or integers"); |
| 8447 | goto err; |
| 8448 | } |
| 8449 | } |
| 8450 | } |
| 8451 | return new; |
| 8452 | err: |
| 8453 | Py_DECREF(new); |
| 8454 | return NULL; |
| 8455 | } |
| 8456 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8457 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8458 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | \n\ |
| 8460 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8461 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8462 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8463 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8464 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8465 | |
| 8466 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8467 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8468 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8469 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8470 | } |
| 8471 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8472 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8473 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8474 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8475 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8476 | |
| 8477 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8478 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8479 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8480 | return fixup(self, fixupper); |
| 8481 | } |
| 8482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8483 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8484 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8485 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8486 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8487 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8488 | |
| 8489 | static PyObject * |
| 8490 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8491 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8492 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8493 | PyUnicodeObject *u; |
| 8494 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8495 | Py_ssize_t width; |
| 8496 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8497 | return NULL; |
| 8498 | |
| 8499 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8500 | if (PyUnicode_CheckExact(self)) { |
| 8501 | Py_INCREF(self); |
| 8502 | return (PyObject*) self; |
| 8503 | } |
| 8504 | else |
| 8505 | return PyUnicode_FromUnicode( |
| 8506 | PyUnicode_AS_UNICODE(self), |
| 8507 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8508 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8509 | } |
| 8510 | |
| 8511 | fill = width - self->length; |
| 8512 | |
| 8513 | u = pad(self, fill, 0, '0'); |
| 8514 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8515 | if (u == NULL) |
| 8516 | return NULL; |
| 8517 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8518 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8519 | /* move sign to beginning of string */ |
| 8520 | u->str[0] = u->str[fill]; |
| 8521 | u->str[fill] = '0'; |
| 8522 | } |
| 8523 | |
| 8524 | return (PyObject*) u; |
| 8525 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8526 | |
| 8527 | #if 0 |
| 8528 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8529 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8530 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8531 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8532 | } |
| 8533 | #endif |
| 8534 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8535 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8536 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8537 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8538 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8539 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8540 | With optional end, stop comparing S at that position.\n\ |
| 8541 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8542 | |
| 8543 | static PyObject * |
| 8544 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8545 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8546 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8547 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8548 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8549 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8550 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8551 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8552 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8553 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8554 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8555 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8556 | if (PyTuple_Check(subobj)) { |
| 8557 | Py_ssize_t i; |
| 8558 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8559 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8560 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8561 | if (substring == NULL) |
| 8562 | return NULL; |
| 8563 | result = tailmatch(self, substring, start, end, -1); |
| 8564 | Py_DECREF(substring); |
| 8565 | if (result) { |
| 8566 | Py_RETURN_TRUE; |
| 8567 | } |
| 8568 | } |
| 8569 | /* nothing matched */ |
| 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; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8575 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8576 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8577 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8578 | } |
| 8579 | |
| 8580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8581 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8582 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8583 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8584 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8585 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8586 | With optional end, stop comparing S at that position.\n\ |
| 8587 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8588 | |
| 8589 | static PyObject * |
| 8590 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8591 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8592 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8593 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8594 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8595 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8596 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8597 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8598 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8599 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8600 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8601 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8602 | if (PyTuple_Check(subobj)) { |
| 8603 | Py_ssize_t i; |
| 8604 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8605 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8606 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8607 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8608 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8609 | result = tailmatch(self, substring, start, end, +1); |
| 8610 | Py_DECREF(substring); |
| 8611 | if (result) { |
| 8612 | Py_RETURN_TRUE; |
| 8613 | } |
| 8614 | } |
| 8615 | Py_RETURN_FALSE; |
| 8616 | } |
| 8617 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8618 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8619 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8620 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8621 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8622 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8623 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8624 | } |
| 8625 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8626 | #include "stringlib/string_format.h" |
| 8627 | |
| 8628 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8629 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8630 | \n\ |
| 8631 | "); |
| 8632 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8633 | static PyObject * |
| 8634 | unicode__format__(PyObject* self, PyObject* args) |
| 8635 | { |
| 8636 | PyObject *format_spec; |
| 8637 | |
| 8638 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8639 | return NULL; |
| 8640 | |
| 8641 | return _PyUnicode_FormatAdvanced(self, |
| 8642 | PyUnicode_AS_UNICODE(format_spec), |
| 8643 | PyUnicode_GET_SIZE(format_spec)); |
| 8644 | } |
| 8645 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8646 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8647 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8648 | \n\ |
| 8649 | "); |
| 8650 | |
| 8651 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8652 | unicode__sizeof__(PyUnicodeObject *v) |
| 8653 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8654 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8655 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8656 | } |
| 8657 | |
| 8658 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8659 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8660 | |
| 8661 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8662 | unicode_getnewargs(PyUnicodeObject *v) |
| 8663 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8664 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8665 | } |
| 8666 | |
| 8667 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8668 | static PyMethodDef unicode_methods[] = { |
| 8669 | |
| 8670 | /* Order is according to common usage: often used methods should |
| 8671 | appear first, since lookup is done sequentially. */ |
| 8672 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8673 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8674 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8675 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8676 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8677 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8678 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8679 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8680 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8681 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8682 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8683 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8684 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8685 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8686 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8687 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8688 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8689 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8690 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8691 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8692 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8693 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8694 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8695 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8696 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8697 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8698 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8699 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8700 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8701 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8702 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8703 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8704 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8705 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8706 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8707 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8708 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8709 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8710 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8711 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8712 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8713 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8714 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8715 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8716 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8717 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8718 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8719 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8720 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8721 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8722 | #endif |
| 8723 | |
| 8724 | #if 0 |
| 8725 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8726 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8727 | #endif |
| 8728 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8729 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8730 | {NULL, NULL} |
| 8731 | }; |
| 8732 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8733 | static PyObject * |
| 8734 | unicode_mod(PyObject *v, PyObject *w) |
| 8735 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8736 | if (!PyUnicode_Check(v)) { |
| 8737 | Py_INCREF(Py_NotImplemented); |
| 8738 | return Py_NotImplemented; |
| 8739 | } |
| 8740 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8741 | } |
| 8742 | |
| 8743 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8744 | 0, /*nb_add*/ |
| 8745 | 0, /*nb_subtract*/ |
| 8746 | 0, /*nb_multiply*/ |
| 8747 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8748 | }; |
| 8749 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8750 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8751 | (lenfunc) unicode_length, /* sq_length */ |
| 8752 | PyUnicode_Concat, /* sq_concat */ |
| 8753 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8754 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8755 | 0, /* sq_slice */ |
| 8756 | 0, /* sq_ass_item */ |
| 8757 | 0, /* sq_ass_slice */ |
| 8758 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8759 | }; |
| 8760 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8761 | static PyObject* |
| 8762 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8763 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8764 | if (PyIndex_Check(item)) { |
| 8765 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8766 | if (i == -1 && PyErr_Occurred()) |
| 8767 | return NULL; |
| 8768 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8769 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8770 | return unicode_getitem(self, i); |
| 8771 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8772 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8773 | Py_UNICODE* source_buf; |
| 8774 | Py_UNICODE* result_buf; |
| 8775 | PyObject* result; |
| 8776 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8777 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8778 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8779 | return NULL; |
| 8780 | } |
| 8781 | |
| 8782 | if (slicelength <= 0) { |
| 8783 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8784 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8785 | PyUnicode_CheckExact(self)) { |
| 8786 | Py_INCREF(self); |
| 8787 | return (PyObject *)self; |
| 8788 | } else if (step == 1) { |
| 8789 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8790 | } else { |
| 8791 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8792 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8793 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8794 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8795 | if (result_buf == NULL) |
| 8796 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8797 | |
| 8798 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8799 | result_buf[i] = source_buf[cur]; |
| 8800 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8801 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8802 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8803 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8804 | return result; |
| 8805 | } |
| 8806 | } else { |
| 8807 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8808 | return NULL; |
| 8809 | } |
| 8810 | } |
| 8811 | |
| 8812 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8813 | (lenfunc)unicode_length, /* mp_length */ |
| 8814 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8815 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8816 | }; |
| 8817 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8819 | /* Helpers for PyUnicode_Format() */ |
| 8820 | |
| 8821 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8822 | 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] | 8823 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8824 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8825 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8826 | (*p_argidx)++; |
| 8827 | if (arglen < 0) |
| 8828 | return args; |
| 8829 | else |
| 8830 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8831 | } |
| 8832 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8833 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8834 | return NULL; |
| 8835 | } |
| 8836 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8837 | /* 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] | 8838 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8839 | static PyObject * |
| 8840 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8841 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8842 | char *p; |
| 8843 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8844 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8845 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8846 | x = PyFloat_AsDouble(v); |
| 8847 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8848 | return NULL; |
| 8849 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8850 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8851 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8852 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8853 | p = PyOS_double_to_string(x, type, prec, |
| 8854 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8855 | if (p == NULL) |
| 8856 | return NULL; |
| 8857 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8858 | PyMem_Free(p); |
| 8859 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8860 | } |
| 8861 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8862 | static PyObject* |
| 8863 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8864 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8865 | char *buf; |
| 8866 | int len; |
| 8867 | PyObject *str; /* temporary string object. */ |
| 8868 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8869 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8870 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 8871 | if (!str) |
| 8872 | return NULL; |
| 8873 | result = PyUnicode_FromStringAndSize(buf, len); |
| 8874 | Py_DECREF(str); |
| 8875 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8876 | } |
| 8877 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8878 | static int |
| 8879 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8880 | size_t buflen, |
| 8881 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8882 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8883 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8884 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8885 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 8886 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 8887 | buf[1] = '\0'; |
| 8888 | return 1; |
| 8889 | } |
| 8890 | #ifndef Py_UNICODE_WIDE |
| 8891 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 8892 | /* Decode a valid surrogate pair */ |
| 8893 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 8894 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 8895 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 8896 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 8897 | buf[0] = c0; |
| 8898 | buf[1] = c1; |
| 8899 | buf[2] = '\0'; |
| 8900 | return 2; |
| 8901 | } |
| 8902 | } |
| 8903 | #endif |
| 8904 | goto onError; |
| 8905 | } |
| 8906 | else { |
| 8907 | /* Integer input truncated to a character */ |
| 8908 | long x; |
| 8909 | x = PyLong_AsLong(v); |
| 8910 | if (x == -1 && PyErr_Occurred()) |
| 8911 | goto onError; |
| 8912 | |
| 8913 | if (x < 0 || x > 0x10ffff) { |
| 8914 | PyErr_SetString(PyExc_OverflowError, |
| 8915 | "%c arg not in range(0x110000)"); |
| 8916 | return -1; |
| 8917 | } |
| 8918 | |
| 8919 | #ifndef Py_UNICODE_WIDE |
| 8920 | if (x > 0xffff) { |
| 8921 | x -= 0x10000; |
| 8922 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 8923 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 8924 | return 2; |
| 8925 | } |
| 8926 | #endif |
| 8927 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8928 | buf[1] = '\0'; |
| 8929 | return 1; |
| 8930 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8931 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8932 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8933 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8934 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8935 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8936 | } |
| 8937 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8938 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8939 | 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] | 8940 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8941 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8942 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8943 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8944 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8945 | { |
| 8946 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8947 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8948 | int args_owned = 0; |
| 8949 | PyUnicodeObject *result = NULL; |
| 8950 | PyObject *dict = NULL; |
| 8951 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8952 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8953 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8954 | PyErr_BadInternalCall(); |
| 8955 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8956 | } |
| 8957 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8958 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8959 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8960 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8961 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8962 | |
| 8963 | reslen = rescnt = fmtcnt + 100; |
| 8964 | result = _PyUnicode_New(reslen); |
| 8965 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8966 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8967 | res = PyUnicode_AS_UNICODE(result); |
| 8968 | |
| 8969 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8970 | arglen = PyTuple_Size(args); |
| 8971 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8972 | } |
| 8973 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8974 | arglen = -1; |
| 8975 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8976 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8977 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 8978 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8979 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8980 | |
| 8981 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8982 | if (*fmt != '%') { |
| 8983 | if (--rescnt < 0) { |
| 8984 | rescnt = fmtcnt + 100; |
| 8985 | reslen += rescnt; |
| 8986 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 8987 | goto onError; |
| 8988 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8989 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8990 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8991 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8992 | } |
| 8993 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8994 | /* Got a format specifier */ |
| 8995 | int flags = 0; |
| 8996 | Py_ssize_t width = -1; |
| 8997 | int prec = -1; |
| 8998 | Py_UNICODE c = '\0'; |
| 8999 | Py_UNICODE fill; |
| 9000 | int isnumok; |
| 9001 | PyObject *v = NULL; |
| 9002 | PyObject *temp = NULL; |
| 9003 | Py_UNICODE *pbuf; |
| 9004 | Py_UNICODE sign; |
| 9005 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9006 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9007 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9008 | fmt++; |
| 9009 | if (*fmt == '(') { |
| 9010 | Py_UNICODE *keystart; |
| 9011 | Py_ssize_t keylen; |
| 9012 | PyObject *key; |
| 9013 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9015 | if (dict == NULL) { |
| 9016 | PyErr_SetString(PyExc_TypeError, |
| 9017 | "format requires a mapping"); |
| 9018 | goto onError; |
| 9019 | } |
| 9020 | ++fmt; |
| 9021 | --fmtcnt; |
| 9022 | keystart = fmt; |
| 9023 | /* Skip over balanced parentheses */ |
| 9024 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9025 | if (*fmt == ')') |
| 9026 | --pcount; |
| 9027 | else if (*fmt == '(') |
| 9028 | ++pcount; |
| 9029 | fmt++; |
| 9030 | } |
| 9031 | keylen = fmt - keystart - 1; |
| 9032 | if (fmtcnt < 0 || pcount > 0) { |
| 9033 | PyErr_SetString(PyExc_ValueError, |
| 9034 | "incomplete format key"); |
| 9035 | goto onError; |
| 9036 | } |
| 9037 | #if 0 |
| 9038 | /* keys are converted to strings using UTF-8 and |
| 9039 | then looked up since Python uses strings to hold |
| 9040 | variables names etc. in its namespaces and we |
| 9041 | wouldn't want to break common idioms. */ |
| 9042 | key = PyUnicode_EncodeUTF8(keystart, |
| 9043 | keylen, |
| 9044 | NULL); |
| 9045 | #else |
| 9046 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9047 | #endif |
| 9048 | if (key == NULL) |
| 9049 | goto onError; |
| 9050 | if (args_owned) { |
| 9051 | Py_DECREF(args); |
| 9052 | args_owned = 0; |
| 9053 | } |
| 9054 | args = PyObject_GetItem(dict, key); |
| 9055 | Py_DECREF(key); |
| 9056 | if (args == NULL) { |
| 9057 | goto onError; |
| 9058 | } |
| 9059 | args_owned = 1; |
| 9060 | arglen = -1; |
| 9061 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9062 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9063 | while (--fmtcnt >= 0) { |
| 9064 | switch (c = *fmt++) { |
| 9065 | case '-': flags |= F_LJUST; continue; |
| 9066 | case '+': flags |= F_SIGN; continue; |
| 9067 | case ' ': flags |= F_BLANK; continue; |
| 9068 | case '#': flags |= F_ALT; continue; |
| 9069 | case '0': flags |= F_ZERO; continue; |
| 9070 | } |
| 9071 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9072 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9073 | if (c == '*') { |
| 9074 | v = getnextarg(args, arglen, &argidx); |
| 9075 | if (v == NULL) |
| 9076 | goto onError; |
| 9077 | if (!PyLong_Check(v)) { |
| 9078 | PyErr_SetString(PyExc_TypeError, |
| 9079 | "* wants int"); |
| 9080 | goto onError; |
| 9081 | } |
| 9082 | width = PyLong_AsLong(v); |
| 9083 | if (width == -1 && PyErr_Occurred()) |
| 9084 | goto onError; |
| 9085 | if (width < 0) { |
| 9086 | flags |= F_LJUST; |
| 9087 | width = -width; |
| 9088 | } |
| 9089 | if (--fmtcnt >= 0) |
| 9090 | c = *fmt++; |
| 9091 | } |
| 9092 | else if (c >= '0' && c <= '9') { |
| 9093 | width = c - '0'; |
| 9094 | while (--fmtcnt >= 0) { |
| 9095 | c = *fmt++; |
| 9096 | if (c < '0' || c > '9') |
| 9097 | break; |
| 9098 | if ((width*10) / 10 != width) { |
| 9099 | PyErr_SetString(PyExc_ValueError, |
| 9100 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9101 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9102 | } |
| 9103 | width = width*10 + (c - '0'); |
| 9104 | } |
| 9105 | } |
| 9106 | if (c == '.') { |
| 9107 | prec = 0; |
| 9108 | if (--fmtcnt >= 0) |
| 9109 | c = *fmt++; |
| 9110 | if (c == '*') { |
| 9111 | v = getnextarg(args, arglen, &argidx); |
| 9112 | if (v == NULL) |
| 9113 | goto onError; |
| 9114 | if (!PyLong_Check(v)) { |
| 9115 | PyErr_SetString(PyExc_TypeError, |
| 9116 | "* wants int"); |
| 9117 | goto onError; |
| 9118 | } |
| 9119 | prec = PyLong_AsLong(v); |
| 9120 | if (prec == -1 && PyErr_Occurred()) |
| 9121 | goto onError; |
| 9122 | if (prec < 0) |
| 9123 | prec = 0; |
| 9124 | if (--fmtcnt >= 0) |
| 9125 | c = *fmt++; |
| 9126 | } |
| 9127 | else if (c >= '0' && c <= '9') { |
| 9128 | prec = c - '0'; |
| 9129 | while (--fmtcnt >= 0) { |
| 9130 | c = Py_CHARMASK(*fmt++); |
| 9131 | if (c < '0' || c > '9') |
| 9132 | break; |
| 9133 | if ((prec*10) / 10 != prec) { |
| 9134 | PyErr_SetString(PyExc_ValueError, |
| 9135 | "prec too big"); |
| 9136 | goto onError; |
| 9137 | } |
| 9138 | prec = prec*10 + (c - '0'); |
| 9139 | } |
| 9140 | } |
| 9141 | } /* prec */ |
| 9142 | if (fmtcnt >= 0) { |
| 9143 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9144 | if (--fmtcnt >= 0) |
| 9145 | c = *fmt++; |
| 9146 | } |
| 9147 | } |
| 9148 | if (fmtcnt < 0) { |
| 9149 | PyErr_SetString(PyExc_ValueError, |
| 9150 | "incomplete format"); |
| 9151 | goto onError; |
| 9152 | } |
| 9153 | if (c != '%') { |
| 9154 | v = getnextarg(args, arglen, &argidx); |
| 9155 | if (v == NULL) |
| 9156 | goto onError; |
| 9157 | } |
| 9158 | sign = 0; |
| 9159 | fill = ' '; |
| 9160 | switch (c) { |
| 9161 | |
| 9162 | case '%': |
| 9163 | pbuf = formatbuf; |
| 9164 | /* presume that buffer length is at least 1 */ |
| 9165 | pbuf[0] = '%'; |
| 9166 | len = 1; |
| 9167 | break; |
| 9168 | |
| 9169 | case 's': |
| 9170 | case 'r': |
| 9171 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9172 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9173 | temp = v; |
| 9174 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9175 | } |
| 9176 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9177 | if (c == 's') |
| 9178 | temp = PyObject_Str(v); |
| 9179 | else if (c == 'r') |
| 9180 | temp = PyObject_Repr(v); |
| 9181 | else |
| 9182 | temp = PyObject_ASCII(v); |
| 9183 | if (temp == NULL) |
| 9184 | goto onError; |
| 9185 | if (PyUnicode_Check(temp)) |
| 9186 | /* nothing to do */; |
| 9187 | else { |
| 9188 | Py_DECREF(temp); |
| 9189 | PyErr_SetString(PyExc_TypeError, |
| 9190 | "%s argument has non-string str()"); |
| 9191 | goto onError; |
| 9192 | } |
| 9193 | } |
| 9194 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9195 | len = PyUnicode_GET_SIZE(temp); |
| 9196 | if (prec >= 0 && len > prec) |
| 9197 | len = prec; |
| 9198 | break; |
| 9199 | |
| 9200 | case 'i': |
| 9201 | case 'd': |
| 9202 | case 'u': |
| 9203 | case 'o': |
| 9204 | case 'x': |
| 9205 | case 'X': |
| 9206 | if (c == 'i') |
| 9207 | c = 'd'; |
| 9208 | isnumok = 0; |
| 9209 | if (PyNumber_Check(v)) { |
| 9210 | PyObject *iobj=NULL; |
| 9211 | |
| 9212 | if (PyLong_Check(v)) { |
| 9213 | iobj = v; |
| 9214 | Py_INCREF(iobj); |
| 9215 | } |
| 9216 | else { |
| 9217 | iobj = PyNumber_Long(v); |
| 9218 | } |
| 9219 | if (iobj!=NULL) { |
| 9220 | if (PyLong_Check(iobj)) { |
| 9221 | isnumok = 1; |
| 9222 | temp = formatlong(iobj, flags, prec, c); |
| 9223 | Py_DECREF(iobj); |
| 9224 | if (!temp) |
| 9225 | goto onError; |
| 9226 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9227 | len = PyUnicode_GET_SIZE(temp); |
| 9228 | sign = 1; |
| 9229 | } |
| 9230 | else { |
| 9231 | Py_DECREF(iobj); |
| 9232 | } |
| 9233 | } |
| 9234 | } |
| 9235 | if (!isnumok) { |
| 9236 | PyErr_Format(PyExc_TypeError, |
| 9237 | "%%%c format: a number is required, " |
| 9238 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9239 | goto onError; |
| 9240 | } |
| 9241 | if (flags & F_ZERO) |
| 9242 | fill = '0'; |
| 9243 | break; |
| 9244 | |
| 9245 | case 'e': |
| 9246 | case 'E': |
| 9247 | case 'f': |
| 9248 | case 'F': |
| 9249 | case 'g': |
| 9250 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9251 | temp = formatfloat(v, flags, prec, c); |
| 9252 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9253 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9254 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9255 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9256 | sign = 1; |
| 9257 | if (flags & F_ZERO) |
| 9258 | fill = '0'; |
| 9259 | break; |
| 9260 | |
| 9261 | case 'c': |
| 9262 | pbuf = formatbuf; |
| 9263 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9264 | if (len < 0) |
| 9265 | goto onError; |
| 9266 | break; |
| 9267 | |
| 9268 | default: |
| 9269 | PyErr_Format(PyExc_ValueError, |
| 9270 | "unsupported format character '%c' (0x%x) " |
| 9271 | "at index %zd", |
| 9272 | (31<=c && c<=126) ? (char)c : '?', |
| 9273 | (int)c, |
| 9274 | (Py_ssize_t)(fmt - 1 - |
| 9275 | PyUnicode_AS_UNICODE(uformat))); |
| 9276 | goto onError; |
| 9277 | } |
| 9278 | if (sign) { |
| 9279 | if (*pbuf == '-' || *pbuf == '+') { |
| 9280 | sign = *pbuf++; |
| 9281 | len--; |
| 9282 | } |
| 9283 | else if (flags & F_SIGN) |
| 9284 | sign = '+'; |
| 9285 | else if (flags & F_BLANK) |
| 9286 | sign = ' '; |
| 9287 | else |
| 9288 | sign = 0; |
| 9289 | } |
| 9290 | if (width < len) |
| 9291 | width = len; |
| 9292 | if (rescnt - (sign != 0) < width) { |
| 9293 | reslen -= rescnt; |
| 9294 | rescnt = width + fmtcnt + 100; |
| 9295 | reslen += rescnt; |
| 9296 | if (reslen < 0) { |
| 9297 | Py_XDECREF(temp); |
| 9298 | PyErr_NoMemory(); |
| 9299 | goto onError; |
| 9300 | } |
| 9301 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9302 | Py_XDECREF(temp); |
| 9303 | goto onError; |
| 9304 | } |
| 9305 | res = PyUnicode_AS_UNICODE(result) |
| 9306 | + reslen - rescnt; |
| 9307 | } |
| 9308 | if (sign) { |
| 9309 | if (fill != ' ') |
| 9310 | *res++ = sign; |
| 9311 | rescnt--; |
| 9312 | if (width > len) |
| 9313 | width--; |
| 9314 | } |
| 9315 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9316 | assert(pbuf[0] == '0'); |
| 9317 | assert(pbuf[1] == c); |
| 9318 | if (fill != ' ') { |
| 9319 | *res++ = *pbuf++; |
| 9320 | *res++ = *pbuf++; |
| 9321 | } |
| 9322 | rescnt -= 2; |
| 9323 | width -= 2; |
| 9324 | if (width < 0) |
| 9325 | width = 0; |
| 9326 | len -= 2; |
| 9327 | } |
| 9328 | if (width > len && !(flags & F_LJUST)) { |
| 9329 | do { |
| 9330 | --rescnt; |
| 9331 | *res++ = fill; |
| 9332 | } while (--width > len); |
| 9333 | } |
| 9334 | if (fill == ' ') { |
| 9335 | if (sign) |
| 9336 | *res++ = sign; |
| 9337 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9338 | assert(pbuf[0] == '0'); |
| 9339 | assert(pbuf[1] == c); |
| 9340 | *res++ = *pbuf++; |
| 9341 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9342 | } |
| 9343 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9344 | Py_UNICODE_COPY(res, pbuf, len); |
| 9345 | res += len; |
| 9346 | rescnt -= len; |
| 9347 | while (--width >= len) { |
| 9348 | --rescnt; |
| 9349 | *res++ = ' '; |
| 9350 | } |
| 9351 | if (dict && (argidx < arglen) && c != '%') { |
| 9352 | PyErr_SetString(PyExc_TypeError, |
| 9353 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9354 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9355 | goto onError; |
| 9356 | } |
| 9357 | Py_XDECREF(temp); |
| 9358 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9359 | } /* until end */ |
| 9360 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9361 | PyErr_SetString(PyExc_TypeError, |
| 9362 | "not all arguments converted during string formatting"); |
| 9363 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9364 | } |
| 9365 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9366 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9367 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9368 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9369 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9370 | } |
| 9371 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9372 | return (PyObject *)result; |
| 9373 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9374 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9375 | Py_XDECREF(result); |
| 9376 | Py_DECREF(uformat); |
| 9377 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9378 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9379 | } |
| 9380 | return NULL; |
| 9381 | } |
| 9382 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9383 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9384 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9385 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9386 | static PyObject * |
| 9387 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9388 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9389 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9390 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9391 | char *encoding = NULL; |
| 9392 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9393 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9394 | if (type != &PyUnicode_Type) |
| 9395 | return unicode_subtype_new(type, args, kwds); |
| 9396 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9397 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9398 | return NULL; |
| 9399 | if (x == NULL) |
| 9400 | return (PyObject *)_PyUnicode_New(0); |
| 9401 | if (encoding == NULL && errors == NULL) |
| 9402 | return PyObject_Str(x); |
| 9403 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9404 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9405 | } |
| 9406 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9407 | static PyObject * |
| 9408 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9409 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9410 | PyUnicodeObject *tmp, *pnew; |
| 9411 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9412 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9413 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9414 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9415 | if (tmp == NULL) |
| 9416 | return NULL; |
| 9417 | assert(PyUnicode_Check(tmp)); |
| 9418 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9419 | if (pnew == NULL) { |
| 9420 | Py_DECREF(tmp); |
| 9421 | return NULL; |
| 9422 | } |
| 9423 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9424 | if (pnew->str == NULL) { |
| 9425 | _Py_ForgetReference((PyObject *)pnew); |
| 9426 | PyObject_Del(pnew); |
| 9427 | Py_DECREF(tmp); |
| 9428 | return PyErr_NoMemory(); |
| 9429 | } |
| 9430 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9431 | pnew->length = n; |
| 9432 | pnew->hash = tmp->hash; |
| 9433 | Py_DECREF(tmp); |
| 9434 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9435 | } |
| 9436 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9437 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9438 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9439 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9440 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9441 | encoding defaults to the current default string encoding.\n\ |
| 9442 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9443 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9444 | static PyObject *unicode_iter(PyObject *seq); |
| 9445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9446 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9447 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9448 | "str", /* tp_name */ |
| 9449 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9450 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9451 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9452 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9453 | 0, /* tp_print */ |
| 9454 | 0, /* tp_getattr */ |
| 9455 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9456 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9457 | unicode_repr, /* tp_repr */ |
| 9458 | &unicode_as_number, /* tp_as_number */ |
| 9459 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9460 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9461 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9462 | 0, /* tp_call*/ |
| 9463 | (reprfunc) unicode_str, /* tp_str */ |
| 9464 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9465 | 0, /* tp_setattro */ |
| 9466 | 0, /* tp_as_buffer */ |
| 9467 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9468 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9469 | unicode_doc, /* tp_doc */ |
| 9470 | 0, /* tp_traverse */ |
| 9471 | 0, /* tp_clear */ |
| 9472 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9473 | 0, /* tp_weaklistoffset */ |
| 9474 | unicode_iter, /* tp_iter */ |
| 9475 | 0, /* tp_iternext */ |
| 9476 | unicode_methods, /* tp_methods */ |
| 9477 | 0, /* tp_members */ |
| 9478 | 0, /* tp_getset */ |
| 9479 | &PyBaseObject_Type, /* tp_base */ |
| 9480 | 0, /* tp_dict */ |
| 9481 | 0, /* tp_descr_get */ |
| 9482 | 0, /* tp_descr_set */ |
| 9483 | 0, /* tp_dictoffset */ |
| 9484 | 0, /* tp_init */ |
| 9485 | 0, /* tp_alloc */ |
| 9486 | unicode_new, /* tp_new */ |
| 9487 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9488 | }; |
| 9489 | |
| 9490 | /* Initialize the Unicode implementation */ |
| 9491 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9492 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9493 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9494 | int i; |
| 9495 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9496 | /* XXX - move this array to unicodectype.c ? */ |
| 9497 | Py_UNICODE linebreak[] = { |
| 9498 | 0x000A, /* LINE FEED */ |
| 9499 | 0x000D, /* CARRIAGE RETURN */ |
| 9500 | 0x001C, /* FILE SEPARATOR */ |
| 9501 | 0x001D, /* GROUP SEPARATOR */ |
| 9502 | 0x001E, /* RECORD SEPARATOR */ |
| 9503 | 0x0085, /* NEXT LINE */ |
| 9504 | 0x2028, /* LINE SEPARATOR */ |
| 9505 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9506 | }; |
| 9507 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9508 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9509 | free_list = NULL; |
| 9510 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9511 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9512 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9513 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9514 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9515 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9516 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9517 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9518 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9519 | |
| 9520 | /* initialize the linebreak bloom filter */ |
| 9521 | bloom_linebreak = make_bloom_mask( |
| 9522 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9523 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9524 | |
| 9525 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9526 | } |
| 9527 | |
| 9528 | /* Finalize the Unicode implementation */ |
| 9529 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9530 | int |
| 9531 | PyUnicode_ClearFreeList(void) |
| 9532 | { |
| 9533 | int freelist_size = numfree; |
| 9534 | PyUnicodeObject *u; |
| 9535 | |
| 9536 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9537 | PyUnicodeObject *v = u; |
| 9538 | u = *(PyUnicodeObject **)u; |
| 9539 | if (v->str) |
| 9540 | PyObject_DEL(v->str); |
| 9541 | Py_XDECREF(v->defenc); |
| 9542 | PyObject_Del(v); |
| 9543 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9544 | } |
| 9545 | free_list = NULL; |
| 9546 | assert(numfree == 0); |
| 9547 | return freelist_size; |
| 9548 | } |
| 9549 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9550 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9551 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9552 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9553 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9554 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9555 | Py_XDECREF(unicode_empty); |
| 9556 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9557 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9558 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9559 | if (unicode_latin1[i]) { |
| 9560 | Py_DECREF(unicode_latin1[i]); |
| 9561 | unicode_latin1[i] = NULL; |
| 9562 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9563 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9564 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9565 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9566 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9567 | void |
| 9568 | PyUnicode_InternInPlace(PyObject **p) |
| 9569 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9570 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9571 | PyObject *t; |
| 9572 | if (s == NULL || !PyUnicode_Check(s)) |
| 9573 | Py_FatalError( |
| 9574 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9575 | /* If it's a subclass, we don't really know what putting |
| 9576 | it in the interned dict might do. */ |
| 9577 | if (!PyUnicode_CheckExact(s)) |
| 9578 | return; |
| 9579 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9580 | return; |
| 9581 | if (interned == NULL) { |
| 9582 | interned = PyDict_New(); |
| 9583 | if (interned == NULL) { |
| 9584 | PyErr_Clear(); /* Don't leave an exception */ |
| 9585 | return; |
| 9586 | } |
| 9587 | } |
| 9588 | /* It might be that the GetItem call fails even |
| 9589 | though the key is present in the dictionary, |
| 9590 | namely when this happens during a stack overflow. */ |
| 9591 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9592 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9593 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9594 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9595 | if (t) { |
| 9596 | Py_INCREF(t); |
| 9597 | Py_DECREF(*p); |
| 9598 | *p = t; |
| 9599 | return; |
| 9600 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9601 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9602 | PyThreadState_GET()->recursion_critical = 1; |
| 9603 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9604 | PyErr_Clear(); |
| 9605 | PyThreadState_GET()->recursion_critical = 0; |
| 9606 | return; |
| 9607 | } |
| 9608 | PyThreadState_GET()->recursion_critical = 0; |
| 9609 | /* The two references in interned are not counted by refcnt. |
| 9610 | The deallocator will take care of this */ |
| 9611 | Py_REFCNT(s) -= 2; |
| 9612 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9613 | } |
| 9614 | |
| 9615 | void |
| 9616 | PyUnicode_InternImmortal(PyObject **p) |
| 9617 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9618 | PyUnicode_InternInPlace(p); |
| 9619 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9620 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9621 | Py_INCREF(*p); |
| 9622 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9623 | } |
| 9624 | |
| 9625 | PyObject * |
| 9626 | PyUnicode_InternFromString(const char *cp) |
| 9627 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9628 | PyObject *s = PyUnicode_FromString(cp); |
| 9629 | if (s == NULL) |
| 9630 | return NULL; |
| 9631 | PyUnicode_InternInPlace(&s); |
| 9632 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9633 | } |
| 9634 | |
| 9635 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9636 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9637 | PyObject *keys; |
| 9638 | PyUnicodeObject *s; |
| 9639 | Py_ssize_t i, n; |
| 9640 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9641 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9642 | if (interned == NULL || !PyDict_Check(interned)) |
| 9643 | return; |
| 9644 | keys = PyDict_Keys(interned); |
| 9645 | if (keys == NULL || !PyList_Check(keys)) { |
| 9646 | PyErr_Clear(); |
| 9647 | return; |
| 9648 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9649 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9650 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9651 | detector, interned unicode strings are not forcibly deallocated; |
| 9652 | rather, we give them their stolen references back, and then clear |
| 9653 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9654 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9655 | n = PyList_GET_SIZE(keys); |
| 9656 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9657 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9658 | for (i = 0; i < n; i++) { |
| 9659 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9660 | switch (s->state) { |
| 9661 | case SSTATE_NOT_INTERNED: |
| 9662 | /* XXX Shouldn't happen */ |
| 9663 | break; |
| 9664 | case SSTATE_INTERNED_IMMORTAL: |
| 9665 | Py_REFCNT(s) += 1; |
| 9666 | immortal_size += s->length; |
| 9667 | break; |
| 9668 | case SSTATE_INTERNED_MORTAL: |
| 9669 | Py_REFCNT(s) += 2; |
| 9670 | mortal_size += s->length; |
| 9671 | break; |
| 9672 | default: |
| 9673 | Py_FatalError("Inconsistent interned string state."); |
| 9674 | } |
| 9675 | s->state = SSTATE_NOT_INTERNED; |
| 9676 | } |
| 9677 | fprintf(stderr, "total size of all interned strings: " |
| 9678 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9679 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9680 | Py_DECREF(keys); |
| 9681 | PyDict_Clear(interned); |
| 9682 | Py_DECREF(interned); |
| 9683 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9684 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9685 | |
| 9686 | |
| 9687 | /********************* Unicode Iterator **************************/ |
| 9688 | |
| 9689 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9690 | PyObject_HEAD |
| 9691 | Py_ssize_t it_index; |
| 9692 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9693 | } unicodeiterobject; |
| 9694 | |
| 9695 | static void |
| 9696 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9697 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9698 | _PyObject_GC_UNTRACK(it); |
| 9699 | Py_XDECREF(it->it_seq); |
| 9700 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9701 | } |
| 9702 | |
| 9703 | static int |
| 9704 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9705 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9706 | Py_VISIT(it->it_seq); |
| 9707 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9708 | } |
| 9709 | |
| 9710 | static PyObject * |
| 9711 | unicodeiter_next(unicodeiterobject *it) |
| 9712 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9713 | PyUnicodeObject *seq; |
| 9714 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9715 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9716 | assert(it != NULL); |
| 9717 | seq = it->it_seq; |
| 9718 | if (seq == NULL) |
| 9719 | return NULL; |
| 9720 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9721 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9722 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9723 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9724 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9725 | if (item != NULL) |
| 9726 | ++it->it_index; |
| 9727 | return item; |
| 9728 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9729 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9730 | Py_DECREF(seq); |
| 9731 | it->it_seq = NULL; |
| 9732 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9733 | } |
| 9734 | |
| 9735 | static PyObject * |
| 9736 | unicodeiter_len(unicodeiterobject *it) |
| 9737 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9738 | Py_ssize_t len = 0; |
| 9739 | if (it->it_seq) |
| 9740 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9741 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9742 | } |
| 9743 | |
| 9744 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9745 | |
| 9746 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9747 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9748 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9749 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9750 | }; |
| 9751 | |
| 9752 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9753 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9754 | "str_iterator", /* tp_name */ |
| 9755 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9756 | 0, /* tp_itemsize */ |
| 9757 | /* methods */ |
| 9758 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9759 | 0, /* tp_print */ |
| 9760 | 0, /* tp_getattr */ |
| 9761 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9762 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9763 | 0, /* tp_repr */ |
| 9764 | 0, /* tp_as_number */ |
| 9765 | 0, /* tp_as_sequence */ |
| 9766 | 0, /* tp_as_mapping */ |
| 9767 | 0, /* tp_hash */ |
| 9768 | 0, /* tp_call */ |
| 9769 | 0, /* tp_str */ |
| 9770 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9771 | 0, /* tp_setattro */ |
| 9772 | 0, /* tp_as_buffer */ |
| 9773 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9774 | 0, /* tp_doc */ |
| 9775 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9776 | 0, /* tp_clear */ |
| 9777 | 0, /* tp_richcompare */ |
| 9778 | 0, /* tp_weaklistoffset */ |
| 9779 | PyObject_SelfIter, /* tp_iter */ |
| 9780 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9781 | unicodeiter_methods, /* tp_methods */ |
| 9782 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9783 | }; |
| 9784 | |
| 9785 | static PyObject * |
| 9786 | unicode_iter(PyObject *seq) |
| 9787 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9788 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9789 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9790 | if (!PyUnicode_Check(seq)) { |
| 9791 | PyErr_BadInternalCall(); |
| 9792 | return NULL; |
| 9793 | } |
| 9794 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9795 | if (it == NULL) |
| 9796 | return NULL; |
| 9797 | it->it_index = 0; |
| 9798 | Py_INCREF(seq); |
| 9799 | it->it_seq = (PyUnicodeObject *)seq; |
| 9800 | _PyObject_GC_TRACK(it); |
| 9801 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9802 | } |
| 9803 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9804 | size_t |
| 9805 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9806 | { |
| 9807 | int res = 0; |
| 9808 | while(*u++) |
| 9809 | res++; |
| 9810 | return res; |
| 9811 | } |
| 9812 | |
| 9813 | Py_UNICODE* |
| 9814 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9815 | { |
| 9816 | Py_UNICODE *u = s1; |
| 9817 | while ((*u++ = *s2++)); |
| 9818 | return s1; |
| 9819 | } |
| 9820 | |
| 9821 | Py_UNICODE* |
| 9822 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9823 | { |
| 9824 | Py_UNICODE *u = s1; |
| 9825 | while ((*u++ = *s2++)) |
| 9826 | if (n-- == 0) |
| 9827 | break; |
| 9828 | return s1; |
| 9829 | } |
| 9830 | |
| 9831 | int |
| 9832 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9833 | { |
| 9834 | while (*s1 && *s2 && *s1 == *s2) |
| 9835 | s1++, s2++; |
| 9836 | if (*s1 && *s2) |
| 9837 | return (*s1 < *s2) ? -1 : +1; |
| 9838 | if (*s1) |
| 9839 | return 1; |
| 9840 | if (*s2) |
| 9841 | return -1; |
| 9842 | return 0; |
| 9843 | } |
| 9844 | |
| 9845 | Py_UNICODE* |
| 9846 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9847 | { |
| 9848 | const Py_UNICODE *p; |
| 9849 | for (p = s; *p; p++) |
| 9850 | if (*p == c) |
| 9851 | return (Py_UNICODE*)p; |
| 9852 | return NULL; |
| 9853 | } |
| 9854 | |
| 9855 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9856 | #ifdef __cplusplus |
| 9857 | } |
| 9858 | #endif |