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 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1296 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1297 | catch e.g. UTF_8. Truncate the string if it is longer than lower_len-1 |
| 1298 | characters. */ |
| 1299 | static void normalize_encoding(const char *encoding, |
| 1300 | char *lower, |
| 1301 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1302 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1303 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1304 | char *l; |
| 1305 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1306 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1307 | e = encoding; |
| 1308 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1309 | l_end = &lower[lower_len - 1]; |
| 1310 | while (*e && l < l_end) { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1311 | if (ISUPPER(*e)) { |
| 1312 | *l++ = TOLOWER(*e++); |
| 1313 | } |
| 1314 | else if (*e == '_') { |
| 1315 | *l++ = '-'; |
| 1316 | e++; |
| 1317 | } |
| 1318 | else { |
| 1319 | *l++ = *e++; |
| 1320 | } |
| 1321 | } |
| 1322 | *l = '\0'; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1323 | } |
| 1324 | |
| 1325 | PyObject *PyUnicode_Decode(const char *s, |
| 1326 | Py_ssize_t size, |
| 1327 | const char *encoding, |
| 1328 | const char *errors) |
| 1329 | { |
| 1330 | PyObject *buffer = NULL, *unicode; |
| 1331 | Py_buffer info; |
| 1332 | char lower[11]; /* Enough for any encoding shortcut */ |
| 1333 | |
| 1334 | if (encoding == NULL) |
| 1335 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1336 | |
| 1337 | /* Shortcuts for common default encodings */ |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1338 | normalize_encoding(encoding, lower, sizeof(lower)); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1339 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1340 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1341 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1342 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1343 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1344 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1345 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1346 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1347 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1348 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1349 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1350 | else if (strcmp(lower, "utf-16") == 0) |
| 1351 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1352 | else if (strcmp(lower, "utf-32") == 0) |
| 1353 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1354 | |
| 1355 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1356 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1357 | 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] | 1358 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1359 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1360 | if (buffer == NULL) |
| 1361 | goto onError; |
| 1362 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1363 | if (unicode == NULL) |
| 1364 | goto onError; |
| 1365 | if (!PyUnicode_Check(unicode)) { |
| 1366 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1367 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1368 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1369 | Py_DECREF(unicode); |
| 1370 | goto onError; |
| 1371 | } |
| 1372 | Py_DECREF(buffer); |
| 1373 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1374 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1375 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1376 | Py_XDECREF(buffer); |
| 1377 | return NULL; |
| 1378 | } |
| 1379 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1380 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1381 | const char *encoding, |
| 1382 | const char *errors) |
| 1383 | { |
| 1384 | PyObject *v; |
| 1385 | |
| 1386 | if (!PyUnicode_Check(unicode)) { |
| 1387 | PyErr_BadArgument(); |
| 1388 | goto onError; |
| 1389 | } |
| 1390 | |
| 1391 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1392 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1393 | |
| 1394 | /* Decode via the codec registry */ |
| 1395 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1396 | if (v == NULL) |
| 1397 | goto onError; |
| 1398 | return v; |
| 1399 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1400 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1401 | return NULL; |
| 1402 | } |
| 1403 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1404 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1405 | const char *encoding, |
| 1406 | const char *errors) |
| 1407 | { |
| 1408 | PyObject *v; |
| 1409 | |
| 1410 | if (!PyUnicode_Check(unicode)) { |
| 1411 | PyErr_BadArgument(); |
| 1412 | goto onError; |
| 1413 | } |
| 1414 | |
| 1415 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1416 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1417 | |
| 1418 | /* Decode via the codec registry */ |
| 1419 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1420 | if (v == NULL) |
| 1421 | goto onError; |
| 1422 | if (!PyUnicode_Check(v)) { |
| 1423 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1424 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1425 | Py_TYPE(v)->tp_name); |
| 1426 | Py_DECREF(v); |
| 1427 | goto onError; |
| 1428 | } |
| 1429 | return v; |
| 1430 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1431 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1432 | return NULL; |
| 1433 | } |
| 1434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1435 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1436 | Py_ssize_t size, |
| 1437 | const char *encoding, |
| 1438 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1439 | { |
| 1440 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1441 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1442 | unicode = PyUnicode_FromUnicode(s, size); |
| 1443 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1444 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1445 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1446 | Py_DECREF(unicode); |
| 1447 | return v; |
| 1448 | } |
| 1449 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1450 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1451 | const char *encoding, |
| 1452 | const char *errors) |
| 1453 | { |
| 1454 | PyObject *v; |
| 1455 | |
| 1456 | if (!PyUnicode_Check(unicode)) { |
| 1457 | PyErr_BadArgument(); |
| 1458 | goto onError; |
| 1459 | } |
| 1460 | |
| 1461 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1462 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1463 | |
| 1464 | /* Encode via the codec registry */ |
| 1465 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1466 | if (v == NULL) |
| 1467 | goto onError; |
| 1468 | return v; |
| 1469 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1470 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1471 | return NULL; |
| 1472 | } |
| 1473 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1474 | PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode) |
| 1475 | { |
| 1476 | if (Py_FileSystemDefaultEncoding) |
| 1477 | return PyUnicode_AsEncodedString(unicode, |
| 1478 | Py_FileSystemDefaultEncoding, |
| 1479 | "surrogateescape"); |
| 1480 | else |
| 1481 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1482 | PyUnicode_GET_SIZE(unicode), |
| 1483 | "surrogateescape"); |
| 1484 | } |
| 1485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1486 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1487 | const char *encoding, |
| 1488 | const char *errors) |
| 1489 | { |
| 1490 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1491 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1493 | if (!PyUnicode_Check(unicode)) { |
| 1494 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1495 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1496 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1497 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1498 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1499 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1500 | |
| 1501 | /* Shortcuts for common default encodings */ |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1502 | normalize_encoding(encoding, lower, sizeof(lower)); |
| 1503 | if (strcmp(lower, "utf-8") == 0) |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1504 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1505 | PyUnicode_GET_SIZE(unicode), |
| 1506 | errors); |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1507 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1508 | (strcmp(lower, "iso-8859-1") == 0)) |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1509 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1510 | PyUnicode_GET_SIZE(unicode), |
| 1511 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1512 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1513 | else if (strcmp(lower, "mbcs") == 0) |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1514 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1515 | PyUnicode_GET_SIZE(unicode), |
| 1516 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1517 | #endif |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame^] | 1518 | else if (strcmp(lower, "ascii") == 0) |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1519 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1520 | PyUnicode_GET_SIZE(unicode), |
| 1521 | errors); |
| 1522 | /* During bootstrap, we may need to find the encodings |
| 1523 | package, to load the file system encoding, and require the |
| 1524 | file system encoding in order to load the encodings |
| 1525 | package. |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1526 | |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1527 | Break out of this dependency by assuming that the path to |
| 1528 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1529 | instead, if the file system encoding is the locale's |
| 1530 | encoding. */ |
| 1531 | else if (Py_FileSystemDefaultEncoding && |
| 1532 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1533 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1534 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1535 | PyUnicode_GET_SIZE(unicode), |
| 1536 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1537 | |
| 1538 | /* Encode via the codec registry */ |
| 1539 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1540 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1541 | return NULL; |
| 1542 | |
| 1543 | /* The normal path */ |
| 1544 | if (PyBytes_Check(v)) |
| 1545 | return v; |
| 1546 | |
| 1547 | /* 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] | 1548 | if (PyByteArray_Check(v)) { |
| 1549 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1550 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1551 | PyOS_snprintf(msg, sizeof(msg), |
| 1552 | "encoder %s returned buffer instead of bytes", |
| 1553 | encoding); |
| 1554 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1555 | Py_DECREF(v); |
| 1556 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1557 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1558 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1559 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1560 | Py_DECREF(v); |
| 1561 | return b; |
| 1562 | } |
| 1563 | |
| 1564 | PyErr_Format(PyExc_TypeError, |
| 1565 | "encoder did not return a bytes object (type=%.400s)", |
| 1566 | Py_TYPE(v)->tp_name); |
| 1567 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1568 | return NULL; |
| 1569 | } |
| 1570 | |
| 1571 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1572 | const char *encoding, |
| 1573 | const char *errors) |
| 1574 | { |
| 1575 | PyObject *v; |
| 1576 | |
| 1577 | if (!PyUnicode_Check(unicode)) { |
| 1578 | PyErr_BadArgument(); |
| 1579 | goto onError; |
| 1580 | } |
| 1581 | |
| 1582 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1583 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1584 | |
| 1585 | /* Encode via the codec registry */ |
| 1586 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1587 | if (v == NULL) |
| 1588 | goto onError; |
| 1589 | if (!PyUnicode_Check(v)) { |
| 1590 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1591 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1592 | Py_TYPE(v)->tp_name); |
| 1593 | Py_DECREF(v); |
| 1594 | goto onError; |
| 1595 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1596 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1597 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1598 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1599 | return NULL; |
| 1600 | } |
| 1601 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1602 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1603 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1604 | { |
| 1605 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1606 | if (v) |
| 1607 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1608 | if (errors != NULL) |
| 1609 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1610 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1611 | PyUnicode_GET_SIZE(unicode), |
| 1612 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1613 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1614 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1615 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1616 | return v; |
| 1617 | } |
| 1618 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1619 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1620 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1621 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1622 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1623 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1624 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1625 | PyObject* |
| 1626 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1627 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1628 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1629 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1630 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1631 | bootstrapping process where the codecs aren't ready yet. |
| 1632 | */ |
| 1633 | if (Py_FileSystemDefaultEncoding) { |
| 1634 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1635 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1636 | return PyUnicode_DecodeMBCS(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1637 | } |
| 1638 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1639 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1640 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1641 | } |
| 1642 | #endif |
| 1643 | return PyUnicode_Decode(s, size, |
| 1644 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1645 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1646 | } |
| 1647 | else { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1648 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1652 | /* Convert the argument to a bytes object, according to the file |
Gregory P. Smith | cc47d8c | 2010-02-27 08:33:11 +0000 | [diff] [blame] | 1653 | system encoding. The addr param must be a PyObject**. |
| 1654 | 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] | 1655 | |
| 1656 | int |
| 1657 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1658 | { |
| 1659 | PyObject *output = NULL; |
| 1660 | Py_ssize_t size; |
| 1661 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1662 | if (arg == NULL) { |
| 1663 | Py_DECREF(*(PyObject**)addr); |
| 1664 | return 1; |
| 1665 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1666 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1667 | output = arg; |
| 1668 | Py_INCREF(output); |
| 1669 | } |
| 1670 | else { |
| 1671 | arg = PyUnicode_FromObject(arg); |
| 1672 | if (!arg) |
| 1673 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1674 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1675 | Py_DECREF(arg); |
| 1676 | if (!output) |
| 1677 | return 0; |
| 1678 | if (!PyBytes_Check(output)) { |
| 1679 | Py_DECREF(output); |
| 1680 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1681 | return 0; |
| 1682 | } |
| 1683 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1684 | size = PyBytes_GET_SIZE(output); |
| 1685 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1686 | if (size != strlen(data)) { |
| 1687 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1688 | Py_DECREF(output); |
| 1689 | return 0; |
| 1690 | } |
| 1691 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1692 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1696 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1697 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1698 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1699 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1700 | if (!PyUnicode_Check(unicode)) { |
| 1701 | PyErr_BadArgument(); |
| 1702 | return NULL; |
| 1703 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1704 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1705 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1706 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1707 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1708 | *psize = PyBytes_GET_SIZE(bytes); |
| 1709 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1713 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1714 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1715 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1718 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1719 | { |
| 1720 | if (!PyUnicode_Check(unicode)) { |
| 1721 | PyErr_BadArgument(); |
| 1722 | goto onError; |
| 1723 | } |
| 1724 | return PyUnicode_AS_UNICODE(unicode); |
| 1725 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1726 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1727 | return NULL; |
| 1728 | } |
| 1729 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1730 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1731 | { |
| 1732 | if (!PyUnicode_Check(unicode)) { |
| 1733 | PyErr_BadArgument(); |
| 1734 | goto onError; |
| 1735 | } |
| 1736 | return PyUnicode_GET_SIZE(unicode); |
| 1737 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1738 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1739 | return -1; |
| 1740 | } |
| 1741 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1742 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1743 | { |
| 1744 | return unicode_default_encoding; |
| 1745 | } |
| 1746 | |
| 1747 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1748 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1749 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1750 | PyErr_Format(PyExc_ValueError, |
| 1751 | "Can only set default encoding to %s", |
| 1752 | unicode_default_encoding); |
| 1753 | return -1; |
| 1754 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1755 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1758 | /* error handling callback helper: |
| 1759 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1760 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1761 | and adjust various state variables. |
| 1762 | return 0 on success, -1 on error |
| 1763 | */ |
| 1764 | |
| 1765 | static |
| 1766 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1767 | const char *encoding, const char *reason, |
| 1768 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1769 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1770 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1771 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1772 | 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] | 1773 | |
| 1774 | PyObject *restuple = NULL; |
| 1775 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1776 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1777 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1778 | Py_ssize_t requiredsize; |
| 1779 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1780 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1781 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1782 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1783 | int res = -1; |
| 1784 | |
| 1785 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1786 | *errorHandler = PyCodec_LookupError(errors); |
| 1787 | if (*errorHandler == NULL) |
| 1788 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1792 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1793 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
| 1794 | if (*exceptionObject == NULL) |
| 1795 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1796 | } |
| 1797 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1798 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1799 | goto onError; |
| 1800 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1801 | goto onError; |
| 1802 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1803 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1807 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1808 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1809 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1810 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1811 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1812 | } |
| 1813 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1814 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1815 | |
| 1816 | /* Copy back the bytes variables, which might have been modified by the |
| 1817 | callback */ |
| 1818 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1819 | if (!inputobj) |
| 1820 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1821 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1822 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1823 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1824 | *input = PyBytes_AS_STRING(inputobj); |
| 1825 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1826 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1827 | /* we can DECREF safely, as the exception has another reference, |
| 1828 | so the object won't go away. */ |
| 1829 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1830 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1831 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1832 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1833 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1834 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1835 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1836 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1837 | |
| 1838 | /* need more space? (at least enough for what we |
| 1839 | have+the replacement+the rest of the string (starting |
| 1840 | at the new input position), so we won't have to check space |
| 1841 | when there are no errors in the rest of the string) */ |
| 1842 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1843 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1844 | requiredsize = *outpos + repsize + insize-newpos; |
| 1845 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1846 | if (requiredsize<2*outsize) |
| 1847 | requiredsize = 2*outsize; |
| 1848 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1849 | goto onError; |
| 1850 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1851 | } |
| 1852 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1853 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1854 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1855 | *outptr += repsize; |
| 1856 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1857 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1858 | /* we made it! */ |
| 1859 | res = 0; |
| 1860 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1861 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1862 | Py_XDECREF(restuple); |
| 1863 | return res; |
| 1864 | } |
| 1865 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1866 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1867 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1868 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1869 | |
| 1870 | /* Three simple macros defining base-64. */ |
| 1871 | |
| 1872 | /* Is c a base-64 character? */ |
| 1873 | |
| 1874 | #define IS_BASE64(c) \ |
| 1875 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1876 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1877 | ((c) >= '0' && (c) <= '9') || \ |
| 1878 | (c) == '+' || (c) == '/') |
| 1879 | |
| 1880 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1881 | |
| 1882 | #define FROM_BASE64(c) \ |
| 1883 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1884 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1885 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1886 | (c) == '+' ? 62 : 63) |
| 1887 | |
| 1888 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1889 | |
| 1890 | #define TO_BASE64(n) \ |
| 1891 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1892 | |
| 1893 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1894 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1895 | * byte not decoding to itself is the + which begins a base64 |
| 1896 | * string. */ |
| 1897 | |
| 1898 | #define DECODE_DIRECT(c) \ |
| 1899 | ((c) <= 127 && (c) != '+') |
| 1900 | |
| 1901 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1902 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1903 | * the above). See RFC2152. This array identifies these different |
| 1904 | * sets: |
| 1905 | * 0 : "Set D" |
| 1906 | * alphanumeric and '(),-./:? |
| 1907 | * 1 : "Set O" |
| 1908 | * !"#$%&*;<=>@[]^_`{|} |
| 1909 | * 2 : "whitespace" |
| 1910 | * ht nl cr sp |
| 1911 | * 3 : special (must be base64 encoded) |
| 1912 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1913 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1914 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1915 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1916 | char utf7_category[128] = { |
| 1917 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 1918 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 1919 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 1920 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1921 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 1922 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 1923 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 1924 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 1925 | /* @ A B C D E F G H I J K L M N O */ |
| 1926 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1927 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 1928 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 1929 | /* ` a b c d e f g h i j k l m n o */ |
| 1930 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1931 | /* p q r s t u v w x y z { | } ~ del */ |
| 1932 | 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] | 1933 | }; |
| 1934 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1935 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 1936 | * answer depends on whether we are encoding set O as itself, and also |
| 1937 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 1938 | * clear that the answers to these questions vary between |
| 1939 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1940 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1941 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 1942 | ((c) < 128 && (c) > 0 && \ |
| 1943 | ((utf7_category[(c)] == 0) || \ |
| 1944 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 1945 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1946 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1947 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1948 | Py_ssize_t size, |
| 1949 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1950 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1951 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1952 | } |
| 1953 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1954 | /* The decoder. The only state we preserve is our read position, |
| 1955 | * i.e. how many characters we have consumed. So if we end in the |
| 1956 | * middle of a shift sequence we have to back off the read position |
| 1957 | * and the output to the beginning of the sequence, otherwise we lose |
| 1958 | * all the shift state (seen bits, number of bits seen, high |
| 1959 | * surrogate). */ |
| 1960 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1961 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1962 | Py_ssize_t size, |
| 1963 | const char *errors, |
| 1964 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1965 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1966 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1967 | Py_ssize_t startinpos; |
| 1968 | Py_ssize_t endinpos; |
| 1969 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1970 | const char *e; |
| 1971 | PyUnicodeObject *unicode; |
| 1972 | Py_UNICODE *p; |
| 1973 | const char *errmsg = ""; |
| 1974 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1975 | Py_UNICODE *shiftOutStart; |
| 1976 | unsigned int base64bits = 0; |
| 1977 | unsigned long base64buffer = 0; |
| 1978 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1979 | PyObject *errorHandler = NULL; |
| 1980 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1981 | |
| 1982 | unicode = _PyUnicode_New(size); |
| 1983 | if (!unicode) |
| 1984 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1985 | if (size == 0) { |
| 1986 | if (consumed) |
| 1987 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1988 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1989 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1990 | |
| 1991 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1992 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1993 | e = s + size; |
| 1994 | |
| 1995 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1996 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1997 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1998 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1999 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2000 | if (inShift) { /* in a base-64 section */ |
| 2001 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 2002 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 2003 | base64bits += 6; |
| 2004 | s++; |
| 2005 | if (base64bits >= 16) { |
| 2006 | /* we have enough bits for a UTF-16 value */ |
| 2007 | Py_UNICODE outCh = (Py_UNICODE) |
| 2008 | (base64buffer >> (base64bits-16)); |
| 2009 | base64bits -= 16; |
| 2010 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 2011 | if (surrogate) { |
| 2012 | /* expecting a second surrogate */ |
| 2013 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2014 | #ifdef Py_UNICODE_WIDE |
| 2015 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2016 | | (outCh & 0x3FF)) + 0x10000; |
| 2017 | #else |
| 2018 | *p++ = surrogate; |
| 2019 | *p++ = outCh; |
| 2020 | #endif |
| 2021 | surrogate = 0; |
| 2022 | } |
| 2023 | else { |
| 2024 | surrogate = 0; |
| 2025 | errmsg = "second surrogate missing"; |
| 2026 | goto utf7Error; |
| 2027 | } |
| 2028 | } |
| 2029 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2030 | /* first surrogate */ |
| 2031 | surrogate = outCh; |
| 2032 | } |
| 2033 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2034 | errmsg = "unexpected second surrogate"; |
| 2035 | goto utf7Error; |
| 2036 | } |
| 2037 | else { |
| 2038 | *p++ = outCh; |
| 2039 | } |
| 2040 | } |
| 2041 | } |
| 2042 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2043 | inShift = 0; |
| 2044 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2045 | if (surrogate) { |
| 2046 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2047 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2048 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2049 | if (base64bits > 0) { /* left-over bits */ |
| 2050 | if (base64bits >= 6) { |
| 2051 | /* We've seen at least one base-64 character */ |
| 2052 | errmsg = "partial character in shift sequence"; |
| 2053 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2054 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2055 | else { |
| 2056 | /* Some bits remain; they should be zero */ |
| 2057 | if (base64buffer != 0) { |
| 2058 | errmsg = "non-zero padding bits in shift sequence"; |
| 2059 | goto utf7Error; |
| 2060 | } |
| 2061 | } |
| 2062 | } |
| 2063 | if (ch != '-') { |
| 2064 | /* '-' is absorbed; other terminating |
| 2065 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2066 | *p++ = ch; |
| 2067 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2068 | } |
| 2069 | } |
| 2070 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2071 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2072 | s++; /* consume '+' */ |
| 2073 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2074 | s++; |
| 2075 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2076 | } |
| 2077 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2078 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2079 | shiftOutStart = p; |
| 2080 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2081 | } |
| 2082 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2083 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2084 | *p++ = ch; |
| 2085 | s++; |
| 2086 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2087 | else { |
| 2088 | startinpos = s-starts; |
| 2089 | s++; |
| 2090 | errmsg = "unexpected special character"; |
| 2091 | goto utf7Error; |
| 2092 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2093 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2094 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2095 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2096 | endinpos = s-starts; |
| 2097 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2098 | errors, &errorHandler, |
| 2099 | "utf7", errmsg, |
| 2100 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2101 | &unicode, &outpos, &p)) |
| 2102 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2105 | /* end of string */ |
| 2106 | |
| 2107 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2108 | /* if we're in an inconsistent state, that's an error */ |
| 2109 | if (surrogate || |
| 2110 | (base64bits >= 6) || |
| 2111 | (base64bits > 0 && base64buffer != 0)) { |
| 2112 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2113 | endinpos = size; |
| 2114 | if (unicode_decode_call_errorhandler( |
| 2115 | errors, &errorHandler, |
| 2116 | "utf7", "unterminated shift sequence", |
| 2117 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2118 | &unicode, &outpos, &p)) |
| 2119 | goto onError; |
| 2120 | if (s < e) |
| 2121 | goto restart; |
| 2122 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2123 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2124 | |
| 2125 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2126 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2127 | if (inShift) { |
| 2128 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2129 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2130 | } |
| 2131 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2132 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2133 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2134 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2135 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2136 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2137 | goto onError; |
| 2138 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2139 | Py_XDECREF(errorHandler); |
| 2140 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2141 | return (PyObject *)unicode; |
| 2142 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2143 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2144 | Py_XDECREF(errorHandler); |
| 2145 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2146 | Py_DECREF(unicode); |
| 2147 | return NULL; |
| 2148 | } |
| 2149 | |
| 2150 | |
| 2151 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2152 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2153 | int base64SetO, |
| 2154 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2155 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2156 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2157 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2158 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2159 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2160 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2161 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2162 | unsigned int base64bits = 0; |
| 2163 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2164 | char * out; |
| 2165 | char * start; |
| 2166 | |
| 2167 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2168 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2169 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2170 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2171 | return PyErr_NoMemory(); |
| 2172 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2173 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2174 | if (v == NULL) |
| 2175 | return NULL; |
| 2176 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2177 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2178 | for (;i < size; ++i) { |
| 2179 | Py_UNICODE ch = s[i]; |
| 2180 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2181 | if (inShift) { |
| 2182 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2183 | /* shifting out */ |
| 2184 | if (base64bits) { /* output remaining bits */ |
| 2185 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2186 | base64buffer = 0; |
| 2187 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2188 | } |
| 2189 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2190 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2191 | so no '-' is required, except if the character is itself a '-' */ |
| 2192 | if (IS_BASE64(ch) || ch == '-') { |
| 2193 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2194 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2195 | *out++ = (char) ch; |
| 2196 | } |
| 2197 | else { |
| 2198 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2199 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2200 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2201 | else { /* not in a shift sequence */ |
| 2202 | if (ch == '+') { |
| 2203 | *out++ = '+'; |
| 2204 | *out++ = '-'; |
| 2205 | } |
| 2206 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2207 | *out++ = (char) ch; |
| 2208 | } |
| 2209 | else { |
| 2210 | *out++ = '+'; |
| 2211 | inShift = 1; |
| 2212 | goto encode_char; |
| 2213 | } |
| 2214 | } |
| 2215 | continue; |
| 2216 | encode_char: |
| 2217 | #ifdef Py_UNICODE_WIDE |
| 2218 | if (ch >= 0x10000) { |
| 2219 | /* code first surrogate */ |
| 2220 | base64bits += 16; |
| 2221 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2222 | while (base64bits >= 6) { |
| 2223 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2224 | base64bits -= 6; |
| 2225 | } |
| 2226 | /* prepare second surrogate */ |
| 2227 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2228 | } |
| 2229 | #endif |
| 2230 | base64bits += 16; |
| 2231 | base64buffer = (base64buffer << 16) | ch; |
| 2232 | while (base64bits >= 6) { |
| 2233 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2234 | base64bits -= 6; |
| 2235 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2236 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2237 | if (base64bits) |
| 2238 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2239 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2240 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2241 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2242 | return NULL; |
| 2243 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2246 | #undef IS_BASE64 |
| 2247 | #undef FROM_BASE64 |
| 2248 | #undef TO_BASE64 |
| 2249 | #undef DECODE_DIRECT |
| 2250 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2251 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2252 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2253 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2254 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2255 | char utf8_code_length[256] = { |
| 2256 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 2257 | illegal prefix. see RFC 2279 for details */ |
| 2258 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2259 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2260 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2261 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2262 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2263 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2264 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2265 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2266 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2267 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2268 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2269 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2270 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2271 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2272 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2273 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 2274 | }; |
| 2275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2276 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2277 | Py_ssize_t size, |
| 2278 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2279 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2280 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2281 | } |
| 2282 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2283 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2284 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2285 | |
| 2286 | /* Mask to quickly check whether a C 'long' contains a |
| 2287 | non-ASCII, UTF8-encoded char. */ |
| 2288 | #if (SIZEOF_LONG == 8) |
| 2289 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2290 | #elif (SIZEOF_LONG == 4) |
| 2291 | # define ASCII_CHAR_MASK 0x80808080L |
| 2292 | #else |
| 2293 | # error C 'long' size should be either 4 or 8! |
| 2294 | #endif |
| 2295 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2296 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2297 | Py_ssize_t size, |
| 2298 | const char *errors, |
| 2299 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2300 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2301 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2302 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2303 | Py_ssize_t startinpos; |
| 2304 | Py_ssize_t endinpos; |
| 2305 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2306 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2307 | PyUnicodeObject *unicode; |
| 2308 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2309 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2310 | PyObject *errorHandler = NULL; |
| 2311 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2312 | |
| 2313 | /* Note: size will always be longer than the resulting Unicode |
| 2314 | character count */ |
| 2315 | unicode = _PyUnicode_New(size); |
| 2316 | if (!unicode) |
| 2317 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2318 | if (size == 0) { |
| 2319 | if (consumed) |
| 2320 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2321 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2322 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2323 | |
| 2324 | /* Unpack UTF-8 encoded data */ |
| 2325 | p = unicode->str; |
| 2326 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2327 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2328 | |
| 2329 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2330 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2331 | |
| 2332 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2333 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2334 | input will consist of an overwhelming majority of ASCII |
| 2335 | characters, we try to optimize for this case by checking |
| 2336 | as many characters as a C 'long' can contain. |
| 2337 | First, check if we can do an aligned read, as most CPUs have |
| 2338 | a penalty for unaligned reads. |
| 2339 | */ |
| 2340 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2341 | /* Help register allocation */ |
| 2342 | register const char *_s = s; |
| 2343 | register Py_UNICODE *_p = p; |
| 2344 | while (_s < aligned_end) { |
| 2345 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2346 | and do a fast unrolled copy if it only contains ASCII |
| 2347 | characters. */ |
| 2348 | unsigned long data = *(unsigned long *) _s; |
| 2349 | if (data & ASCII_CHAR_MASK) |
| 2350 | break; |
| 2351 | _p[0] = (unsigned char) _s[0]; |
| 2352 | _p[1] = (unsigned char) _s[1]; |
| 2353 | _p[2] = (unsigned char) _s[2]; |
| 2354 | _p[3] = (unsigned char) _s[3]; |
| 2355 | #if (SIZEOF_LONG == 8) |
| 2356 | _p[4] = (unsigned char) _s[4]; |
| 2357 | _p[5] = (unsigned char) _s[5]; |
| 2358 | _p[6] = (unsigned char) _s[6]; |
| 2359 | _p[7] = (unsigned char) _s[7]; |
| 2360 | #endif |
| 2361 | _s += SIZEOF_LONG; |
| 2362 | _p += SIZEOF_LONG; |
| 2363 | } |
| 2364 | s = _s; |
| 2365 | p = _p; |
| 2366 | if (s == e) |
| 2367 | break; |
| 2368 | ch = (unsigned char)*s; |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2373 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2374 | s++; |
| 2375 | continue; |
| 2376 | } |
| 2377 | |
| 2378 | n = utf8_code_length[ch]; |
| 2379 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2380 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2381 | if (consumed) |
| 2382 | break; |
| 2383 | else { |
| 2384 | errmsg = "unexpected end of data"; |
| 2385 | startinpos = s-starts; |
| 2386 | endinpos = size; |
| 2387 | goto utf8Error; |
| 2388 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2389 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2390 | |
| 2391 | switch (n) { |
| 2392 | |
| 2393 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2394 | errmsg = "unexpected code byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2395 | startinpos = s-starts; |
| 2396 | endinpos = startinpos+1; |
| 2397 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2398 | |
| 2399 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2400 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2401 | startinpos = s-starts; |
| 2402 | endinpos = startinpos+1; |
| 2403 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2404 | |
| 2405 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2406 | if ((s[1] & 0xc0) != 0x80) { |
| 2407 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2408 | startinpos = s-starts; |
| 2409 | endinpos = startinpos+2; |
| 2410 | goto utf8Error; |
| 2411 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2412 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2413 | if (ch < 0x80) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2414 | startinpos = s-starts; |
| 2415 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2416 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2417 | goto utf8Error; |
| 2418 | } |
| 2419 | else |
| 2420 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2421 | break; |
| 2422 | |
| 2423 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2424 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2425 | (s[2] & 0xc0) != 0x80) { |
| 2426 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2427 | startinpos = s-starts; |
| 2428 | endinpos = startinpos+3; |
| 2429 | goto utf8Error; |
| 2430 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2431 | 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] | 2432 | if (ch < 0x0800 || (ch >= 0xd800 && ch <= 0xDFFF)) { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2433 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2434 | startinpos = s-starts; |
| 2435 | endinpos = startinpos+3; |
| 2436 | goto utf8Error; |
| 2437 | } |
| 2438 | else |
| 2439 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2440 | break; |
| 2441 | |
| 2442 | case 4: |
| 2443 | if ((s[1] & 0xc0) != 0x80 || |
| 2444 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2445 | (s[3] & 0xc0) != 0x80) { |
| 2446 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2447 | startinpos = s-starts; |
| 2448 | endinpos = startinpos+4; |
| 2449 | goto utf8Error; |
| 2450 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2451 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2452 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2453 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2454 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2455 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2456 | || (ch > 0x10ffff)) /* maximum value allowed for |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2457 | UTF-16 */ |
| 2458 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2459 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2460 | startinpos = s-starts; |
| 2461 | endinpos = startinpos+4; |
| 2462 | goto utf8Error; |
| 2463 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2464 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2465 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2466 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2467 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2468 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2469 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2470 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2471 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2472 | /* high surrogate = top 10 bits added to D800 */ |
| 2473 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2474 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2475 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2476 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2477 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2478 | break; |
| 2479 | |
| 2480 | default: |
| 2481 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2482 | errmsg = "unsupported Unicode code range"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2483 | startinpos = s-starts; |
| 2484 | endinpos = startinpos+n; |
| 2485 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2486 | } |
| 2487 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2488 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2489 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2490 | utf8Error: |
| 2491 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2492 | if (unicode_decode_call_errorhandler( |
| 2493 | errors, &errorHandler, |
| 2494 | "utf8", errmsg, |
| 2495 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2496 | &unicode, &outpos, &p)) |
| 2497 | goto onError; |
| 2498 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2499 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2500 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2501 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2502 | |
| 2503 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2504 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2505 | goto onError; |
| 2506 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2507 | Py_XDECREF(errorHandler); |
| 2508 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2509 | return (PyObject *)unicode; |
| 2510 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2511 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2512 | Py_XDECREF(errorHandler); |
| 2513 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2514 | Py_DECREF(unicode); |
| 2515 | return NULL; |
| 2516 | } |
| 2517 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2518 | #undef ASCII_CHAR_MASK |
| 2519 | |
| 2520 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2521 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2522 | and allocate exactly as much space needed at the end. Else allocate the |
| 2523 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2524 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2525 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2526 | PyObject * |
| 2527 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2528 | Py_ssize_t size, |
| 2529 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2530 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2531 | #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] | 2532 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2533 | Py_ssize_t i; /* index into s of next input byte */ |
| 2534 | PyObject *result; /* result string object */ |
| 2535 | char *p; /* next free byte in output buffer */ |
| 2536 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2537 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2538 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2539 | PyObject *errorHandler = NULL; |
| 2540 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2541 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2542 | assert(s != NULL); |
| 2543 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2544 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2545 | if (size <= MAX_SHORT_UNICHARS) { |
| 2546 | /* Write into the stack buffer; nallocated can't overflow. |
| 2547 | * At the end, we'll allocate exactly as much heap space as it |
| 2548 | * turns out we need. |
| 2549 | */ |
| 2550 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2551 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2552 | p = stackbuf; |
| 2553 | } |
| 2554 | else { |
| 2555 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2556 | nallocated = size * 4; |
| 2557 | if (nallocated / 4 != size) /* overflow! */ |
| 2558 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2559 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2560 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2561 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2562 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2563 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2564 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2565 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2566 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2567 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2568 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2569 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2570 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2572 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2573 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2574 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2575 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2576 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2577 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2578 | /* Special case: check for high and low surrogate */ |
| 2579 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2580 | Py_UCS4 ch2 = s[i]; |
| 2581 | /* Combine the two surrogates to form a UCS4 value */ |
| 2582 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2583 | i++; |
| 2584 | |
| 2585 | /* Encode UCS4 Unicode ordinals */ |
| 2586 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2587 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2588 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2589 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2590 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2591 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2592 | Py_ssize_t newpos; |
| 2593 | PyObject *rep; |
| 2594 | Py_ssize_t repsize, k; |
| 2595 | rep = unicode_encode_call_errorhandler |
| 2596 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2597 | s, size, &exc, i-1, i, &newpos); |
| 2598 | if (!rep) |
| 2599 | goto error; |
| 2600 | |
| 2601 | if (PyBytes_Check(rep)) |
| 2602 | repsize = PyBytes_GET_SIZE(rep); |
| 2603 | else |
| 2604 | repsize = PyUnicode_GET_SIZE(rep); |
| 2605 | |
| 2606 | if (repsize > 4) { |
| 2607 | Py_ssize_t offset; |
| 2608 | |
| 2609 | if (result == NULL) |
| 2610 | offset = p - stackbuf; |
| 2611 | else |
| 2612 | offset = p - PyBytes_AS_STRING(result); |
| 2613 | |
| 2614 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2615 | /* integer overflow */ |
| 2616 | PyErr_NoMemory(); |
| 2617 | goto error; |
| 2618 | } |
| 2619 | nallocated += repsize - 4; |
| 2620 | if (result != NULL) { |
| 2621 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2622 | goto error; |
| 2623 | } else { |
| 2624 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2625 | if (result == NULL) |
| 2626 | goto error; |
| 2627 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2628 | } |
| 2629 | p = PyBytes_AS_STRING(result) + offset; |
| 2630 | } |
| 2631 | |
| 2632 | if (PyBytes_Check(rep)) { |
| 2633 | char *prep = PyBytes_AS_STRING(rep); |
| 2634 | for(k = repsize; k > 0; k--) |
| 2635 | *p++ = *prep++; |
| 2636 | } else /* rep is unicode */ { |
| 2637 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2638 | Py_UNICODE c; |
| 2639 | |
| 2640 | for(k=0; k<repsize; k++) { |
| 2641 | c = prep[k]; |
| 2642 | if (0x80 <= c) { |
| 2643 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2644 | i-1, i, "surrogates not allowed"); |
| 2645 | goto error; |
| 2646 | } |
| 2647 | *p++ = (char)prep[k]; |
| 2648 | } |
| 2649 | } |
| 2650 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2651 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2652 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2653 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2654 | } else if (ch < 0x10000) { |
| 2655 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2656 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2657 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2658 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2659 | /* Encode UCS4 Unicode ordinals */ |
| 2660 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2661 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2662 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2663 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2664 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2665 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2666 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2667 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2668 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2669 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2670 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2671 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2672 | } |
| 2673 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2674 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2675 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2676 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2677 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2678 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2679 | Py_XDECREF(errorHandler); |
| 2680 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2681 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2682 | error: |
| 2683 | Py_XDECREF(errorHandler); |
| 2684 | Py_XDECREF(exc); |
| 2685 | Py_XDECREF(result); |
| 2686 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2687 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2688 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2691 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2692 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2693 | if (!PyUnicode_Check(unicode)) { |
| 2694 | PyErr_BadArgument(); |
| 2695 | return NULL; |
| 2696 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2697 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2698 | PyUnicode_GET_SIZE(unicode), |
| 2699 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2702 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2703 | |
| 2704 | PyObject * |
| 2705 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2706 | Py_ssize_t size, |
| 2707 | const char *errors, |
| 2708 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2709 | { |
| 2710 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2711 | } |
| 2712 | |
| 2713 | PyObject * |
| 2714 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2715 | Py_ssize_t size, |
| 2716 | const char *errors, |
| 2717 | int *byteorder, |
| 2718 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2719 | { |
| 2720 | const char *starts = s; |
| 2721 | Py_ssize_t startinpos; |
| 2722 | Py_ssize_t endinpos; |
| 2723 | Py_ssize_t outpos; |
| 2724 | PyUnicodeObject *unicode; |
| 2725 | Py_UNICODE *p; |
| 2726 | #ifndef Py_UNICODE_WIDE |
| 2727 | int i, pairs; |
| 2728 | #else |
| 2729 | const int pairs = 0; |
| 2730 | #endif |
| 2731 | const unsigned char *q, *e; |
| 2732 | int bo = 0; /* assume native ordering by default */ |
| 2733 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2734 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2735 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2736 | int iorder[] = {0, 1, 2, 3}; |
| 2737 | #else |
| 2738 | int iorder[] = {3, 2, 1, 0}; |
| 2739 | #endif |
| 2740 | PyObject *errorHandler = NULL; |
| 2741 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2742 | /* On narrow builds we split characters outside the BMP into two |
| 2743 | codepoints => count how much extra space we need. */ |
| 2744 | #ifndef Py_UNICODE_WIDE |
| 2745 | for (i = pairs = 0; i < size/4; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2746 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2747 | pairs++; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2748 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2749 | |
| 2750 | /* This might be one to much, because of a BOM */ |
| 2751 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2752 | if (!unicode) |
| 2753 | return NULL; |
| 2754 | if (size == 0) |
| 2755 | return (PyObject *)unicode; |
| 2756 | |
| 2757 | /* Unpack UTF-32 encoded data */ |
| 2758 | p = unicode->str; |
| 2759 | q = (unsigned char *)s; |
| 2760 | e = q + size; |
| 2761 | |
| 2762 | if (byteorder) |
| 2763 | bo = *byteorder; |
| 2764 | |
| 2765 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2766 | byte order setting accordingly. In native mode, the leading BOM |
| 2767 | mark is skipped, in all other modes, it is copied to the output |
| 2768 | stream as-is (giving a ZWNBSP character). */ |
| 2769 | if (bo == 0) { |
| 2770 | if (size >= 4) { |
| 2771 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2772 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2773 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2774 | if (bom == 0x0000FEFF) { |
| 2775 | q += 4; |
| 2776 | bo = -1; |
| 2777 | } |
| 2778 | else if (bom == 0xFFFE0000) { |
| 2779 | q += 4; |
| 2780 | bo = 1; |
| 2781 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2782 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2783 | if (bom == 0x0000FEFF) { |
| 2784 | q += 4; |
| 2785 | bo = 1; |
| 2786 | } |
| 2787 | else if (bom == 0xFFFE0000) { |
| 2788 | q += 4; |
| 2789 | bo = -1; |
| 2790 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2791 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2792 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | if (bo == -1) { |
| 2796 | /* force LE */ |
| 2797 | iorder[0] = 0; |
| 2798 | iorder[1] = 1; |
| 2799 | iorder[2] = 2; |
| 2800 | iorder[3] = 3; |
| 2801 | } |
| 2802 | else if (bo == 1) { |
| 2803 | /* force BE */ |
| 2804 | iorder[0] = 3; |
| 2805 | iorder[1] = 2; |
| 2806 | iorder[2] = 1; |
| 2807 | iorder[3] = 0; |
| 2808 | } |
| 2809 | |
| 2810 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2811 | Py_UCS4 ch; |
| 2812 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2813 | if (e-q<4) { |
| 2814 | if (consumed) |
| 2815 | break; |
| 2816 | errmsg = "truncated data"; |
| 2817 | startinpos = ((const char *)q)-starts; |
| 2818 | endinpos = ((const char *)e)-starts; |
| 2819 | goto utf32Error; |
| 2820 | /* The remaining input chars are ignored if the callback |
| 2821 | chooses to skip the input */ |
| 2822 | } |
| 2823 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2824 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2825 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2826 | if (ch >= 0x110000) |
| 2827 | { |
| 2828 | errmsg = "codepoint not in range(0x110000)"; |
| 2829 | startinpos = ((const char *)q)-starts; |
| 2830 | endinpos = startinpos+4; |
| 2831 | goto utf32Error; |
| 2832 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2833 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2834 | if (ch >= 0x10000) |
| 2835 | { |
| 2836 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2837 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2838 | } |
| 2839 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2840 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2841 | *p++ = ch; |
| 2842 | q += 4; |
| 2843 | continue; |
| 2844 | utf32Error: |
| 2845 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2846 | if (unicode_decode_call_errorhandler( |
| 2847 | errors, &errorHandler, |
| 2848 | "utf32", errmsg, |
| 2849 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2850 | &unicode, &outpos, &p)) |
| 2851 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | if (byteorder) |
| 2855 | *byteorder = bo; |
| 2856 | |
| 2857 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2858 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2859 | |
| 2860 | /* Adjust length */ |
| 2861 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2862 | goto onError; |
| 2863 | |
| 2864 | Py_XDECREF(errorHandler); |
| 2865 | Py_XDECREF(exc); |
| 2866 | return (PyObject *)unicode; |
| 2867 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2868 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2869 | Py_DECREF(unicode); |
| 2870 | Py_XDECREF(errorHandler); |
| 2871 | Py_XDECREF(exc); |
| 2872 | return NULL; |
| 2873 | } |
| 2874 | |
| 2875 | PyObject * |
| 2876 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2877 | Py_ssize_t size, |
| 2878 | const char *errors, |
| 2879 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2880 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2881 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2882 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2883 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2884 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2885 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2886 | #else |
| 2887 | const int pairs = 0; |
| 2888 | #endif |
| 2889 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2890 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2891 | int iorder[] = {0, 1, 2, 3}; |
| 2892 | #else |
| 2893 | int iorder[] = {3, 2, 1, 0}; |
| 2894 | #endif |
| 2895 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2896 | #define STORECHAR(CH) \ |
| 2897 | do { \ |
| 2898 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2899 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2900 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2901 | p[iorder[0]] = (CH) & 0xff; \ |
| 2902 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2903 | } while(0) |
| 2904 | |
| 2905 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2906 | so we need less space. */ |
| 2907 | #ifndef Py_UNICODE_WIDE |
| 2908 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2909 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2910 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2911 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2912 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2913 | nsize = (size - pairs + (byteorder == 0)); |
| 2914 | bytesize = nsize * 4; |
| 2915 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2916 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2917 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2918 | if (v == NULL) |
| 2919 | return NULL; |
| 2920 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2921 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2922 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2923 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2924 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2925 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2926 | |
| 2927 | if (byteorder == -1) { |
| 2928 | /* force LE */ |
| 2929 | iorder[0] = 0; |
| 2930 | iorder[1] = 1; |
| 2931 | iorder[2] = 2; |
| 2932 | iorder[3] = 3; |
| 2933 | } |
| 2934 | else if (byteorder == 1) { |
| 2935 | /* force BE */ |
| 2936 | iorder[0] = 3; |
| 2937 | iorder[1] = 2; |
| 2938 | iorder[2] = 1; |
| 2939 | iorder[3] = 0; |
| 2940 | } |
| 2941 | |
| 2942 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2943 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2944 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2945 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2946 | Py_UCS4 ch2 = *s; |
| 2947 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2948 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2949 | s++; |
| 2950 | size--; |
| 2951 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2952 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2953 | #endif |
| 2954 | STORECHAR(ch); |
| 2955 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2956 | |
| 2957 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2958 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2959 | #undef STORECHAR |
| 2960 | } |
| 2961 | |
| 2962 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2963 | { |
| 2964 | if (!PyUnicode_Check(unicode)) { |
| 2965 | PyErr_BadArgument(); |
| 2966 | return NULL; |
| 2967 | } |
| 2968 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2969 | PyUnicode_GET_SIZE(unicode), |
| 2970 | NULL, |
| 2971 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2974 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2975 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2976 | PyObject * |
| 2977 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2978 | Py_ssize_t size, |
| 2979 | const char *errors, |
| 2980 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2981 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2982 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2983 | } |
| 2984 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2985 | /* Two masks for fast checking of whether a C 'long' may contain |
| 2986 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 2987 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 2988 | rare in most input. |
| 2989 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 2990 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2991 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2992 | #if (SIZEOF_LONG == 8) |
| 2993 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 2994 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 2995 | #elif (SIZEOF_LONG == 4) |
| 2996 | # define FAST_CHAR_MASK 0x80008000L |
| 2997 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 2998 | #else |
| 2999 | # error C 'long' size should be either 4 or 8! |
| 3000 | #endif |
| 3001 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3002 | PyObject * |
| 3003 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3004 | Py_ssize_t size, |
| 3005 | const char *errors, |
| 3006 | int *byteorder, |
| 3007 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3008 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3009 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3010 | Py_ssize_t startinpos; |
| 3011 | Py_ssize_t endinpos; |
| 3012 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3013 | PyUnicodeObject *unicode; |
| 3014 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3015 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3016 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3017 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3018 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3019 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3020 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3021 | int ihi = 1, ilo = 0; |
| 3022 | #else |
| 3023 | int ihi = 0, ilo = 1; |
| 3024 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3025 | PyObject *errorHandler = NULL; |
| 3026 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3027 | |
| 3028 | /* Note: size will always be longer than the resulting Unicode |
| 3029 | character count */ |
| 3030 | unicode = _PyUnicode_New(size); |
| 3031 | if (!unicode) |
| 3032 | return NULL; |
| 3033 | if (size == 0) |
| 3034 | return (PyObject *)unicode; |
| 3035 | |
| 3036 | /* Unpack UTF-16 encoded data */ |
| 3037 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3038 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3039 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3040 | |
| 3041 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3042 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3043 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3044 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3045 | byte order setting accordingly. In native mode, the leading BOM |
| 3046 | mark is skipped, in all other modes, it is copied to the output |
| 3047 | stream as-is (giving a ZWNBSP character). */ |
| 3048 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3049 | if (size >= 2) { |
| 3050 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3051 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3052 | if (bom == 0xFEFF) { |
| 3053 | q += 2; |
| 3054 | bo = -1; |
| 3055 | } |
| 3056 | else if (bom == 0xFFFE) { |
| 3057 | q += 2; |
| 3058 | bo = 1; |
| 3059 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3060 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3061 | if (bom == 0xFEFF) { |
| 3062 | q += 2; |
| 3063 | bo = 1; |
| 3064 | } |
| 3065 | else if (bom == 0xFFFE) { |
| 3066 | q += 2; |
| 3067 | bo = -1; |
| 3068 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3069 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3070 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3071 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3072 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3073 | if (bo == -1) { |
| 3074 | /* force LE */ |
| 3075 | ihi = 1; |
| 3076 | ilo = 0; |
| 3077 | } |
| 3078 | else if (bo == 1) { |
| 3079 | /* force BE */ |
| 3080 | ihi = 0; |
| 3081 | ilo = 1; |
| 3082 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3083 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3084 | native_ordering = ilo < ihi; |
| 3085 | #else |
| 3086 | native_ordering = ilo > ihi; |
| 3087 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3088 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3089 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3090 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3091 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3092 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3093 | reads are more expensive, better to defer to another iteration. */ |
| 3094 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3095 | /* Fast path for runs of non-surrogate chars. */ |
| 3096 | register const unsigned char *_q = q; |
| 3097 | Py_UNICODE *_p = p; |
| 3098 | if (native_ordering) { |
| 3099 | /* Native ordering is simple: as long as the input cannot |
| 3100 | possibly contain a surrogate char, do an unrolled copy |
| 3101 | of several 16-bit code points to the target object. |
| 3102 | The non-surrogate check is done on several input bytes |
| 3103 | at a time (as many as a C 'long' can contain). */ |
| 3104 | while (_q < aligned_end) { |
| 3105 | unsigned long data = * (unsigned long *) _q; |
| 3106 | if (data & FAST_CHAR_MASK) |
| 3107 | break; |
| 3108 | _p[0] = ((unsigned short *) _q)[0]; |
| 3109 | _p[1] = ((unsigned short *) _q)[1]; |
| 3110 | #if (SIZEOF_LONG == 8) |
| 3111 | _p[2] = ((unsigned short *) _q)[2]; |
| 3112 | _p[3] = ((unsigned short *) _q)[3]; |
| 3113 | #endif |
| 3114 | _q += SIZEOF_LONG; |
| 3115 | _p += SIZEOF_LONG / 2; |
| 3116 | } |
| 3117 | } |
| 3118 | else { |
| 3119 | /* Byteswapped ordering is similar, but we must decompose |
| 3120 | the copy bytewise, and take care of zero'ing out the |
| 3121 | upper bytes if the target object is in 32-bit units |
| 3122 | (that is, in UCS-4 builds). */ |
| 3123 | while (_q < aligned_end) { |
| 3124 | unsigned long data = * (unsigned long *) _q; |
| 3125 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3126 | break; |
| 3127 | /* Zero upper bytes in UCS-4 builds */ |
| 3128 | #if (Py_UNICODE_SIZE > 2) |
| 3129 | _p[0] = 0; |
| 3130 | _p[1] = 0; |
| 3131 | #if (SIZEOF_LONG == 8) |
| 3132 | _p[2] = 0; |
| 3133 | _p[3] = 0; |
| 3134 | #endif |
| 3135 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3136 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3137 | fill the two last bytes of each 4-byte unit. */ |
| 3138 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3139 | # define OFF 2 |
| 3140 | #else |
| 3141 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3142 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3143 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3144 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3145 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3146 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3147 | #if (SIZEOF_LONG == 8) |
| 3148 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3149 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3150 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3151 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3152 | #endif |
| 3153 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3154 | _q += SIZEOF_LONG; |
| 3155 | _p += SIZEOF_LONG / 2; |
| 3156 | } |
| 3157 | } |
| 3158 | p = _p; |
| 3159 | q = _q; |
| 3160 | if (q >= e) |
| 3161 | break; |
| 3162 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3163 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3164 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3165 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3166 | |
| 3167 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3168 | *p++ = ch; |
| 3169 | continue; |
| 3170 | } |
| 3171 | |
| 3172 | /* UTF-16 code pair: */ |
| 3173 | if (q > e) { |
| 3174 | errmsg = "unexpected end of data"; |
| 3175 | startinpos = (((const char *)q) - 2) - starts; |
| 3176 | endinpos = ((const char *)e) + 1 - starts; |
| 3177 | goto utf16Error; |
| 3178 | } |
| 3179 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3180 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3181 | q += 2; |
| 3182 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3183 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3184 | *p++ = ch; |
| 3185 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3186 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3187 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3188 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3189 | continue; |
| 3190 | } |
| 3191 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3192 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3193 | startinpos = (((const char *)q)-4)-starts; |
| 3194 | endinpos = startinpos+2; |
| 3195 | goto utf16Error; |
| 3196 | } |
| 3197 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3198 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3199 | errmsg = "illegal encoding"; |
| 3200 | startinpos = (((const char *)q)-2)-starts; |
| 3201 | endinpos = startinpos+2; |
| 3202 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3203 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3204 | utf16Error: |
| 3205 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3206 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3207 | errors, |
| 3208 | &errorHandler, |
| 3209 | "utf16", errmsg, |
| 3210 | &starts, |
| 3211 | (const char **)&e, |
| 3212 | &startinpos, |
| 3213 | &endinpos, |
| 3214 | &exc, |
| 3215 | (const char **)&q, |
| 3216 | &unicode, |
| 3217 | &outpos, |
| 3218 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3219 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3220 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3221 | /* remaining byte at the end? (size should be even) */ |
| 3222 | if (e == q) { |
| 3223 | if (!consumed) { |
| 3224 | errmsg = "truncated data"; |
| 3225 | startinpos = ((const char *)q) - starts; |
| 3226 | endinpos = ((const char *)e) + 1 - starts; |
| 3227 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3228 | if (unicode_decode_call_errorhandler( |
| 3229 | errors, |
| 3230 | &errorHandler, |
| 3231 | "utf16", errmsg, |
| 3232 | &starts, |
| 3233 | (const char **)&e, |
| 3234 | &startinpos, |
| 3235 | &endinpos, |
| 3236 | &exc, |
| 3237 | (const char **)&q, |
| 3238 | &unicode, |
| 3239 | &outpos, |
| 3240 | &p)) |
| 3241 | goto onError; |
| 3242 | /* The remaining input chars are ignored if the callback |
| 3243 | chooses to skip the input */ |
| 3244 | } |
| 3245 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3246 | |
| 3247 | if (byteorder) |
| 3248 | *byteorder = bo; |
| 3249 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3250 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3251 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3253 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3254 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3255 | goto onError; |
| 3256 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3257 | Py_XDECREF(errorHandler); |
| 3258 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3259 | return (PyObject *)unicode; |
| 3260 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3261 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3262 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3263 | Py_XDECREF(errorHandler); |
| 3264 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3265 | return NULL; |
| 3266 | } |
| 3267 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3268 | #undef FAST_CHAR_MASK |
| 3269 | #undef SWAPPED_FAST_CHAR_MASK |
| 3270 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3271 | PyObject * |
| 3272 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3273 | Py_ssize_t size, |
| 3274 | const char *errors, |
| 3275 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3276 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3277 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3278 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3279 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3280 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3281 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3282 | #else |
| 3283 | const int pairs = 0; |
| 3284 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3285 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3286 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3287 | int ihi = 1, ilo = 0; |
| 3288 | #else |
| 3289 | int ihi = 0, ilo = 1; |
| 3290 | #endif |
| 3291 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3292 | #define STORECHAR(CH) \ |
| 3293 | do { \ |
| 3294 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3295 | p[ilo] = (CH) & 0xff; \ |
| 3296 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3297 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3298 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3299 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3300 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3301 | if (s[i] >= 0x10000) |
| 3302 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3303 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3304 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3305 | if (size > PY_SSIZE_T_MAX || |
| 3306 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3307 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3308 | nsize = size + pairs + (byteorder == 0); |
| 3309 | bytesize = nsize * 2; |
| 3310 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3311 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3312 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3313 | if (v == NULL) |
| 3314 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3315 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3316 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3318 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3319 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3320 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3321 | |
| 3322 | if (byteorder == -1) { |
| 3323 | /* force LE */ |
| 3324 | ihi = 1; |
| 3325 | ilo = 0; |
| 3326 | } |
| 3327 | else if (byteorder == 1) { |
| 3328 | /* force BE */ |
| 3329 | ihi = 0; |
| 3330 | ilo = 1; |
| 3331 | } |
| 3332 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3333 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3334 | Py_UNICODE ch = *s++; |
| 3335 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3336 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3337 | if (ch >= 0x10000) { |
| 3338 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3339 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3340 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3341 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3342 | STORECHAR(ch); |
| 3343 | if (ch2) |
| 3344 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3345 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3346 | |
| 3347 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3348 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3349 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3353 | { |
| 3354 | if (!PyUnicode_Check(unicode)) { |
| 3355 | PyErr_BadArgument(); |
| 3356 | return NULL; |
| 3357 | } |
| 3358 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3359 | PyUnicode_GET_SIZE(unicode), |
| 3360 | NULL, |
| 3361 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3365 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3366 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3367 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3368 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3369 | Py_ssize_t size, |
| 3370 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3371 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3372 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3373 | Py_ssize_t startinpos; |
| 3374 | Py_ssize_t endinpos; |
| 3375 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3376 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3377 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3378 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3379 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3380 | char* message; |
| 3381 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3382 | PyObject *errorHandler = NULL; |
| 3383 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3384 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3385 | /* Escaped strings will always be longer than the resulting |
| 3386 | 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] | 3387 | length after conversion to the true value. |
| 3388 | (but if the error callback returns a long replacement string |
| 3389 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3390 | v = _PyUnicode_New(size); |
| 3391 | if (v == NULL) |
| 3392 | goto onError; |
| 3393 | if (size == 0) |
| 3394 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3395 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3396 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3397 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3399 | while (s < end) { |
| 3400 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3401 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3402 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3403 | |
| 3404 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3405 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3406 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3407 | continue; |
| 3408 | } |
| 3409 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3410 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3411 | /* \ - Escapes */ |
| 3412 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3413 | c = *s++; |
| 3414 | if (s > end) |
| 3415 | c = '\0'; /* Invalid after \ */ |
| 3416 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3417 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3418 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3419 | case '\n': break; |
| 3420 | case '\\': *p++ = '\\'; break; |
| 3421 | case '\'': *p++ = '\''; break; |
| 3422 | case '\"': *p++ = '\"'; break; |
| 3423 | case 'b': *p++ = '\b'; break; |
| 3424 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3425 | case 't': *p++ = '\t'; break; |
| 3426 | case 'n': *p++ = '\n'; break; |
| 3427 | case 'r': *p++ = '\r'; break; |
| 3428 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3429 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3430 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3431 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3432 | case '0': case '1': case '2': case '3': |
| 3433 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3434 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3435 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3436 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3437 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3438 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3439 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3440 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3441 | break; |
| 3442 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3443 | /* hex escapes */ |
| 3444 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3445 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3446 | digits = 2; |
| 3447 | message = "truncated \\xXX escape"; |
| 3448 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3449 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3450 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3451 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3452 | digits = 4; |
| 3453 | message = "truncated \\uXXXX escape"; |
| 3454 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3455 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3456 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3457 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3458 | digits = 8; |
| 3459 | message = "truncated \\UXXXXXXXX escape"; |
| 3460 | hexescape: |
| 3461 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3462 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3463 | if (s+digits>end) { |
| 3464 | endinpos = size; |
| 3465 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3466 | errors, &errorHandler, |
| 3467 | "unicodeescape", "end of string in escape sequence", |
| 3468 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3469 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3470 | goto onError; |
| 3471 | goto nextByte; |
| 3472 | } |
| 3473 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3474 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3475 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3476 | endinpos = (s+i+1)-starts; |
| 3477 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3478 | errors, &errorHandler, |
| 3479 | "unicodeescape", message, |
| 3480 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3481 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3482 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3483 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3484 | } |
| 3485 | chr = (chr<<4) & ~0xF; |
| 3486 | if (c >= '0' && c <= '9') |
| 3487 | chr += c - '0'; |
| 3488 | else if (c >= 'a' && c <= 'f') |
| 3489 | chr += 10 + c - 'a'; |
| 3490 | else |
| 3491 | chr += 10 + c - 'A'; |
| 3492 | } |
| 3493 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3494 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3495 | /* _decoding_error will have already written into the |
| 3496 | target buffer. */ |
| 3497 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3498 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3499 | /* when we get here, chr is a 32-bit unicode character */ |
| 3500 | if (chr <= 0xffff) |
| 3501 | /* UCS-2 character */ |
| 3502 | *p++ = (Py_UNICODE) chr; |
| 3503 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3504 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3505 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3506 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3507 | *p++ = chr; |
| 3508 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3509 | chr -= 0x10000L; |
| 3510 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3511 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3512 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3513 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3514 | endinpos = s-starts; |
| 3515 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3516 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3517 | errors, &errorHandler, |
| 3518 | "unicodeescape", "illegal Unicode character", |
| 3519 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3520 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3521 | goto onError; |
| 3522 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3523 | break; |
| 3524 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3525 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3526 | case 'N': |
| 3527 | message = "malformed \\N character escape"; |
| 3528 | if (ucnhash_CAPI == NULL) { |
| 3529 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3530 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3531 | if (ucnhash_CAPI == NULL) |
| 3532 | goto ucnhashError; |
| 3533 | } |
| 3534 | if (*s == '{') { |
| 3535 | const char *start = s+1; |
| 3536 | /* look for the closing brace */ |
| 3537 | while (*s != '}' && s < end) |
| 3538 | s++; |
| 3539 | if (s > start && s < end && *s == '}') { |
| 3540 | /* found a name. look it up in the unicode database */ |
| 3541 | message = "unknown Unicode character name"; |
| 3542 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3543 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3544 | goto store; |
| 3545 | } |
| 3546 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3547 | endinpos = s-starts; |
| 3548 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3549 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3550 | errors, &errorHandler, |
| 3551 | "unicodeescape", message, |
| 3552 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3553 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3554 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3555 | break; |
| 3556 | |
| 3557 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3558 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3559 | message = "\\ at end of string"; |
| 3560 | s--; |
| 3561 | endinpos = s-starts; |
| 3562 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3563 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3564 | errors, &errorHandler, |
| 3565 | "unicodeescape", message, |
| 3566 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3567 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3568 | goto onError; |
| 3569 | } |
| 3570 | else { |
| 3571 | *p++ = '\\'; |
| 3572 | *p++ = (unsigned char)s[-1]; |
| 3573 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3574 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3575 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3576 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3577 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3578 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3579 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3580 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3581 | Py_XDECREF(errorHandler); |
| 3582 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3583 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3584 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3585 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3586 | PyErr_SetString( |
| 3587 | PyExc_UnicodeError, |
| 3588 | "\\N escapes not supported (can't load unicodedata module)" |
| 3589 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3590 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3591 | Py_XDECREF(errorHandler); |
| 3592 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3593 | return NULL; |
| 3594 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3595 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3596 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3597 | Py_XDECREF(errorHandler); |
| 3598 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3599 | return NULL; |
| 3600 | } |
| 3601 | |
| 3602 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3603 | |
| 3604 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3605 | appropriate. |
| 3606 | |
| 3607 | */ |
| 3608 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3609 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3610 | Py_ssize_t size, |
| 3611 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3612 | { |
| 3613 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3614 | |
| 3615 | while (size-- > 0) { |
| 3616 | if (*s == ch) |
| 3617 | return s; |
| 3618 | s++; |
| 3619 | } |
| 3620 | |
| 3621 | return NULL; |
| 3622 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3623 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3624 | static const char *hexdigits = "0123456789abcdef"; |
| 3625 | |
| 3626 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3627 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3628 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3629 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3630 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3631 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3632 | #ifdef Py_UNICODE_WIDE |
| 3633 | const Py_ssize_t expandsize = 10; |
| 3634 | #else |
| 3635 | const Py_ssize_t expandsize = 6; |
| 3636 | #endif |
| 3637 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3638 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3639 | better to choose a different scheme. Perhaps scan the |
| 3640 | first N-chars of the string and allocate based on that size. |
| 3641 | */ |
| 3642 | /* Initial allocation is based on the longest-possible unichr |
| 3643 | escape. |
| 3644 | |
| 3645 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3646 | unichr, so in this case it's the longest unichr escape. In |
| 3647 | narrow (UTF-16) builds this is five chars per source unichr |
| 3648 | since there are two unichrs in the surrogate pair, so in narrow |
| 3649 | (UTF-16) builds it's not the longest unichr escape. |
| 3650 | |
| 3651 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3652 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3653 | escape. |
| 3654 | */ |
| 3655 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3656 | if (size == 0) |
| 3657 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3658 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3659 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3660 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3661 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3662 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3663 | 2 |
| 3664 | + expandsize*size |
| 3665 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3666 | if (repr == NULL) |
| 3667 | return NULL; |
| 3668 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3669 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3670 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3671 | while (size-- > 0) { |
| 3672 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3673 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3674 | /* Escape backslashes */ |
| 3675 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3676 | *p++ = '\\'; |
| 3677 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3678 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3679 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3680 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3681 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3682 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3683 | else if (ch >= 0x10000) { |
| 3684 | *p++ = '\\'; |
| 3685 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3686 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3687 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3688 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3689 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3690 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3691 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3692 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3693 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3694 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3695 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3696 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3697 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3698 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3699 | Py_UNICODE ch2; |
| 3700 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3701 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3702 | ch2 = *s++; |
| 3703 | size--; |
| 3704 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3705 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3706 | *p++ = '\\'; |
| 3707 | *p++ = 'U'; |
| 3708 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3709 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3710 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3711 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3712 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3713 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3714 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3715 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3716 | continue; |
| 3717 | } |
| 3718 | /* Fall through: isolated surrogates are copied as-is */ |
| 3719 | s--; |
| 3720 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3721 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3722 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3724 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3725 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3726 | *p++ = '\\'; |
| 3727 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3728 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3729 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3730 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3731 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3732 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3733 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3734 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3735 | else if (ch == '\t') { |
| 3736 | *p++ = '\\'; |
| 3737 | *p++ = 't'; |
| 3738 | } |
| 3739 | else if (ch == '\n') { |
| 3740 | *p++ = '\\'; |
| 3741 | *p++ = 'n'; |
| 3742 | } |
| 3743 | else if (ch == '\r') { |
| 3744 | *p++ = '\\'; |
| 3745 | *p++ = 'r'; |
| 3746 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3747 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3748 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3749 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3750 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3751 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3752 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3753 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3754 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3756 | /* Copy everything else as-is */ |
| 3757 | else |
| 3758 | *p++ = (char) ch; |
| 3759 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3760 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3761 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3762 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3763 | return NULL; |
| 3764 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3767 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3768 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3769 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3770 | if (!PyUnicode_Check(unicode)) { |
| 3771 | PyErr_BadArgument(); |
| 3772 | return NULL; |
| 3773 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3774 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3775 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3776 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3780 | |
| 3781 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3782 | Py_ssize_t size, |
| 3783 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3784 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3785 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3786 | Py_ssize_t startinpos; |
| 3787 | Py_ssize_t endinpos; |
| 3788 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3789 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3790 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3791 | const char *end; |
| 3792 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3793 | PyObject *errorHandler = NULL; |
| 3794 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3795 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3796 | /* Escaped strings will always be longer than the resulting |
| 3797 | 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] | 3798 | length after conversion to the true value. (But decoding error |
| 3799 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3800 | v = _PyUnicode_New(size); |
| 3801 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3802 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3803 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3804 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3805 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3806 | end = s + size; |
| 3807 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3808 | unsigned char c; |
| 3809 | Py_UCS4 x; |
| 3810 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3811 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3812 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3813 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3814 | if (*s != '\\') { |
| 3815 | *p++ = (unsigned char)*s++; |
| 3816 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3817 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3818 | startinpos = s-starts; |
| 3819 | |
| 3820 | /* \u-escapes are only interpreted iff the number of leading |
| 3821 | backslashes if odd */ |
| 3822 | bs = s; |
| 3823 | for (;s < end;) { |
| 3824 | if (*s != '\\') |
| 3825 | break; |
| 3826 | *p++ = (unsigned char)*s++; |
| 3827 | } |
| 3828 | if (((s - bs) & 1) == 0 || |
| 3829 | s >= end || |
| 3830 | (*s != 'u' && *s != 'U')) { |
| 3831 | continue; |
| 3832 | } |
| 3833 | p--; |
| 3834 | count = *s=='u' ? 4 : 8; |
| 3835 | s++; |
| 3836 | |
| 3837 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3838 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3839 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3840 | c = (unsigned char)*s; |
| 3841 | if (!ISXDIGIT(c)) { |
| 3842 | endinpos = s-starts; |
| 3843 | if (unicode_decode_call_errorhandler( |
| 3844 | errors, &errorHandler, |
| 3845 | "rawunicodeescape", "truncated \\uXXXX", |
| 3846 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3847 | &v, &outpos, &p)) |
| 3848 | goto onError; |
| 3849 | goto nextByte; |
| 3850 | } |
| 3851 | x = (x<<4) & ~0xF; |
| 3852 | if (c >= '0' && c <= '9') |
| 3853 | x += c - '0'; |
| 3854 | else if (c >= 'a' && c <= 'f') |
| 3855 | x += 10 + c - 'a'; |
| 3856 | else |
| 3857 | x += 10 + c - 'A'; |
| 3858 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3859 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3860 | /* UCS-2 character */ |
| 3861 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3862 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3863 | /* UCS-4 character. Either store directly, or as |
| 3864 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3865 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3866 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3867 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3868 | x -= 0x10000L; |
| 3869 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3870 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3871 | #endif |
| 3872 | } else { |
| 3873 | endinpos = s-starts; |
| 3874 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3875 | if (unicode_decode_call_errorhandler( |
| 3876 | errors, &errorHandler, |
| 3877 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3878 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3879 | &v, &outpos, &p)) |
| 3880 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3881 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3882 | nextByte: |
| 3883 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3884 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3885 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3886 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3887 | Py_XDECREF(errorHandler); |
| 3888 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3889 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3890 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3891 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3892 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3893 | Py_XDECREF(errorHandler); |
| 3894 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3895 | return NULL; |
| 3896 | } |
| 3897 | |
| 3898 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3899 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3900 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3901 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3902 | char *p; |
| 3903 | char *q; |
| 3904 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3905 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3906 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3907 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3908 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3909 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3910 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3911 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3912 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3913 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3914 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3915 | if (repr == NULL) |
| 3916 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3917 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3918 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3919 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3920 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3921 | while (size-- > 0) { |
| 3922 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3923 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3924 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3925 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3926 | *p++ = '\\'; |
| 3927 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3928 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3929 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3930 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3931 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3932 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3933 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3934 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3935 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3936 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3937 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3938 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3939 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3940 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3941 | Py_UNICODE ch2; |
| 3942 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3943 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3944 | ch2 = *s++; |
| 3945 | size--; |
| 3946 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3947 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3948 | *p++ = '\\'; |
| 3949 | *p++ = 'U'; |
| 3950 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3951 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3952 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3953 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3954 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3955 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3956 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3957 | *p++ = hexdigits[ucs & 0xf]; |
| 3958 | continue; |
| 3959 | } |
| 3960 | /* Fall through: isolated surrogates are copied as-is */ |
| 3961 | s--; |
| 3962 | size++; |
| 3963 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3964 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3965 | /* Map 16-bit characters to '\uxxxx' */ |
| 3966 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3967 | *p++ = '\\'; |
| 3968 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3969 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3970 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3971 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3972 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3973 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3974 | /* Copy everything else as-is */ |
| 3975 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3976 | *p++ = (char) ch; |
| 3977 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3978 | size = p - q; |
| 3979 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3980 | assert(size > 0); |
| 3981 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3982 | return NULL; |
| 3983 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3987 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3988 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3989 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3990 | PyErr_BadArgument(); |
| 3991 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3992 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3993 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3994 | PyUnicode_GET_SIZE(unicode)); |
| 3995 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3996 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3999 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 4000 | |
| 4001 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4002 | Py_ssize_t size, |
| 4003 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4004 | { |
| 4005 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4006 | Py_ssize_t startinpos; |
| 4007 | Py_ssize_t endinpos; |
| 4008 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4009 | PyUnicodeObject *v; |
| 4010 | Py_UNICODE *p; |
| 4011 | const char *end; |
| 4012 | const char *reason; |
| 4013 | PyObject *errorHandler = NULL; |
| 4014 | PyObject *exc = NULL; |
| 4015 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4016 | #ifdef Py_UNICODE_WIDE |
| 4017 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4018 | #endif |
| 4019 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4020 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4021 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4022 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4023 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4024 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4025 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4026 | p = PyUnicode_AS_UNICODE(v); |
| 4027 | end = s + size; |
| 4028 | |
| 4029 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4030 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4031 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4032 | some malformed UCS-4 data. */ |
| 4033 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4034 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4035 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4036 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4037 | end-s < Py_UNICODE_SIZE |
| 4038 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4039 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4040 | startinpos = s - starts; |
| 4041 | if (end-s < Py_UNICODE_SIZE) { |
| 4042 | endinpos = end-starts; |
| 4043 | reason = "truncated input"; |
| 4044 | } |
| 4045 | else { |
| 4046 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4047 | reason = "illegal code point (> 0x10FFFF)"; |
| 4048 | } |
| 4049 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4050 | if (unicode_decode_call_errorhandler( |
| 4051 | errors, &errorHandler, |
| 4052 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4053 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4054 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4055 | goto onError; |
| 4056 | } |
| 4057 | } |
| 4058 | else { |
| 4059 | p++; |
| 4060 | s += Py_UNICODE_SIZE; |
| 4061 | } |
| 4062 | } |
| 4063 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4064 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4065 | goto onError; |
| 4066 | Py_XDECREF(errorHandler); |
| 4067 | Py_XDECREF(exc); |
| 4068 | return (PyObject *)v; |
| 4069 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4070 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4071 | Py_XDECREF(v); |
| 4072 | Py_XDECREF(errorHandler); |
| 4073 | Py_XDECREF(exc); |
| 4074 | return NULL; |
| 4075 | } |
| 4076 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4077 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4078 | |
| 4079 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4080 | Py_ssize_t size, |
| 4081 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4082 | { |
| 4083 | PyUnicodeObject *v; |
| 4084 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4085 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4086 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4087 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4088 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4089 | Py_UNICODE r = *(unsigned char*)s; |
| 4090 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4093 | v = _PyUnicode_New(size); |
| 4094 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4095 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4096 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4097 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4098 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4099 | e = s + size; |
| 4100 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4101 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4102 | unrolled_end = e - 4; |
| 4103 | while (s < unrolled_end) { |
| 4104 | p[0] = (unsigned char) s[0]; |
| 4105 | p[1] = (unsigned char) s[1]; |
| 4106 | p[2] = (unsigned char) s[2]; |
| 4107 | p[3] = (unsigned char) s[3]; |
| 4108 | s += 4; |
| 4109 | p += 4; |
| 4110 | } |
| 4111 | while (s < e) |
| 4112 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4113 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4114 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4115 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4116 | Py_XDECREF(v); |
| 4117 | return NULL; |
| 4118 | } |
| 4119 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4120 | /* create or adjust a UnicodeEncodeError */ |
| 4121 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4122 | const char *encoding, |
| 4123 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4124 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4125 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4126 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4127 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4128 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4129 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4130 | } |
| 4131 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4132 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4133 | goto onError; |
| 4134 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4135 | goto onError; |
| 4136 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4137 | goto onError; |
| 4138 | return; |
| 4139 | onError: |
| 4140 | Py_DECREF(*exceptionObject); |
| 4141 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4142 | } |
| 4143 | } |
| 4144 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4145 | /* raises a UnicodeEncodeError */ |
| 4146 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4147 | const char *encoding, |
| 4148 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4149 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4150 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4151 | { |
| 4152 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4153 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4154 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4155 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | /* error handling callback helper: |
| 4159 | build arguments, call the callback and check the arguments, |
| 4160 | put the result into newpos and return the replacement string, which |
| 4161 | has to be freed by the caller */ |
| 4162 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4163 | PyObject **errorHandler, |
| 4164 | const char *encoding, const char *reason, |
| 4165 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4166 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4167 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4168 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4169 | 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] | 4170 | |
| 4171 | PyObject *restuple; |
| 4172 | PyObject *resunicode; |
| 4173 | |
| 4174 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4175 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4176 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4177 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4181 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4182 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4183 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4184 | |
| 4185 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4186 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4187 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4188 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4189 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4190 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4191 | Py_DECREF(restuple); |
| 4192 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4193 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4194 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4195 | &resunicode, newpos)) { |
| 4196 | Py_DECREF(restuple); |
| 4197 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4198 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4199 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4200 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4201 | Py_DECREF(restuple); |
| 4202 | return NULL; |
| 4203 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4204 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4205 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4206 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4207 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4208 | Py_DECREF(restuple); |
| 4209 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4210 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4211 | Py_INCREF(resunicode); |
| 4212 | Py_DECREF(restuple); |
| 4213 | return resunicode; |
| 4214 | } |
| 4215 | |
| 4216 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4217 | Py_ssize_t size, |
| 4218 | const char *errors, |
| 4219 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4220 | { |
| 4221 | /* output object */ |
| 4222 | PyObject *res; |
| 4223 | /* pointers to the beginning and end+1 of input */ |
| 4224 | const Py_UNICODE *startp = p; |
| 4225 | const Py_UNICODE *endp = p + size; |
| 4226 | /* pointer to the beginning of the unencodable characters */ |
| 4227 | /* const Py_UNICODE *badp = NULL; */ |
| 4228 | /* pointer into the output */ |
| 4229 | char *str; |
| 4230 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4231 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4232 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4233 | 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] | 4234 | PyObject *errorHandler = NULL; |
| 4235 | PyObject *exc = NULL; |
| 4236 | /* the following variable is used for caching string comparisons |
| 4237 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4238 | int known_errorHandler = -1; |
| 4239 | |
| 4240 | /* allocate enough for a simple encoding without |
| 4241 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4242 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4243 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4244 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4245 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4246 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4247 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4248 | ressize = size; |
| 4249 | |
| 4250 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4251 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4252 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4253 | /* can we encode this? */ |
| 4254 | if (c<limit) { |
| 4255 | /* no overflow check, because we know that the space is enough */ |
| 4256 | *str++ = (char)c; |
| 4257 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4258 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4259 | else { |
| 4260 | Py_ssize_t unicodepos = p-startp; |
| 4261 | Py_ssize_t requiredsize; |
| 4262 | PyObject *repunicode; |
| 4263 | Py_ssize_t repsize; |
| 4264 | Py_ssize_t newpos; |
| 4265 | Py_ssize_t respos; |
| 4266 | Py_UNICODE *uni2; |
| 4267 | /* startpos for collecting unencodable chars */ |
| 4268 | const Py_UNICODE *collstart = p; |
| 4269 | const Py_UNICODE *collend = p; |
| 4270 | /* find all unecodable characters */ |
| 4271 | while ((collend < endp) && ((*collend)>=limit)) |
| 4272 | ++collend; |
| 4273 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4274 | if (known_errorHandler==-1) { |
| 4275 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4276 | known_errorHandler = 1; |
| 4277 | else if (!strcmp(errors, "replace")) |
| 4278 | known_errorHandler = 2; |
| 4279 | else if (!strcmp(errors, "ignore")) |
| 4280 | known_errorHandler = 3; |
| 4281 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4282 | known_errorHandler = 4; |
| 4283 | else |
| 4284 | known_errorHandler = 0; |
| 4285 | } |
| 4286 | switch (known_errorHandler) { |
| 4287 | case 1: /* strict */ |
| 4288 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4289 | goto onError; |
| 4290 | case 2: /* replace */ |
| 4291 | while (collstart++<collend) |
| 4292 | *str++ = '?'; /* fall through */ |
| 4293 | case 3: /* ignore */ |
| 4294 | p = collend; |
| 4295 | break; |
| 4296 | case 4: /* xmlcharrefreplace */ |
| 4297 | respos = str - PyBytes_AS_STRING(res); |
| 4298 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4299 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4300 | if (*p<10) |
| 4301 | repsize += 2+1+1; |
| 4302 | else if (*p<100) |
| 4303 | repsize += 2+2+1; |
| 4304 | else if (*p<1000) |
| 4305 | repsize += 2+3+1; |
| 4306 | else if (*p<10000) |
| 4307 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4308 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4309 | else |
| 4310 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4311 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4312 | else if (*p<100000) |
| 4313 | repsize += 2+5+1; |
| 4314 | else if (*p<1000000) |
| 4315 | repsize += 2+6+1; |
| 4316 | else |
| 4317 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4318 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4319 | } |
| 4320 | requiredsize = respos+repsize+(endp-collend); |
| 4321 | if (requiredsize > ressize) { |
| 4322 | if (requiredsize<2*ressize) |
| 4323 | requiredsize = 2*ressize; |
| 4324 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4325 | goto onError; |
| 4326 | str = PyBytes_AS_STRING(res) + respos; |
| 4327 | ressize = requiredsize; |
| 4328 | } |
| 4329 | /* generate replacement (temporarily (mis)uses p) */ |
| 4330 | for (p = collstart; p < collend; ++p) { |
| 4331 | str += sprintf(str, "&#%d;", (int)*p); |
| 4332 | } |
| 4333 | p = collend; |
| 4334 | break; |
| 4335 | default: |
| 4336 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4337 | encoding, reason, startp, size, &exc, |
| 4338 | collstart-startp, collend-startp, &newpos); |
| 4339 | if (repunicode == NULL) |
| 4340 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4341 | if (PyBytes_Check(repunicode)) { |
| 4342 | /* Directly copy bytes result to output. */ |
| 4343 | repsize = PyBytes_Size(repunicode); |
| 4344 | if (repsize > 1) { |
| 4345 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4346 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4347 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4348 | Py_DECREF(repunicode); |
| 4349 | goto onError; |
| 4350 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4351 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4352 | ressize += repsize-1; |
| 4353 | } |
| 4354 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4355 | str += repsize; |
| 4356 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4357 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4358 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4359 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4360 | /* need more space? (at least enough for what we |
| 4361 | have+the replacement+the rest of the string, so |
| 4362 | we won't have to check space for encodable characters) */ |
| 4363 | respos = str - PyBytes_AS_STRING(res); |
| 4364 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4365 | requiredsize = respos+repsize+(endp-collend); |
| 4366 | if (requiredsize > ressize) { |
| 4367 | if (requiredsize<2*ressize) |
| 4368 | requiredsize = 2*ressize; |
| 4369 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4370 | Py_DECREF(repunicode); |
| 4371 | goto onError; |
| 4372 | } |
| 4373 | str = PyBytes_AS_STRING(res) + respos; |
| 4374 | ressize = requiredsize; |
| 4375 | } |
| 4376 | /* check if there is anything unencodable in the replacement |
| 4377 | and copy it to the output */ |
| 4378 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4379 | c = *uni2; |
| 4380 | if (c >= limit) { |
| 4381 | raise_encode_exception(&exc, encoding, startp, size, |
| 4382 | unicodepos, unicodepos+1, reason); |
| 4383 | Py_DECREF(repunicode); |
| 4384 | goto onError; |
| 4385 | } |
| 4386 | *str = (char)c; |
| 4387 | } |
| 4388 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4389 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4390 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4391 | } |
| 4392 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4393 | /* Resize if we allocated to much */ |
| 4394 | size = str - PyBytes_AS_STRING(res); |
| 4395 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4396 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4397 | if (_PyBytes_Resize(&res, size) < 0) |
| 4398 | goto onError; |
| 4399 | } |
| 4400 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4401 | Py_XDECREF(errorHandler); |
| 4402 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4403 | return res; |
| 4404 | |
| 4405 | onError: |
| 4406 | Py_XDECREF(res); |
| 4407 | Py_XDECREF(errorHandler); |
| 4408 | Py_XDECREF(exc); |
| 4409 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4412 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4413 | Py_ssize_t size, |
| 4414 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4415 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4416 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4420 | { |
| 4421 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4422 | PyErr_BadArgument(); |
| 4423 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4424 | } |
| 4425 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4426 | PyUnicode_GET_SIZE(unicode), |
| 4427 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4432 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4433 | Py_ssize_t size, |
| 4434 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4435 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4436 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4437 | PyUnicodeObject *v; |
| 4438 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4439 | Py_ssize_t startinpos; |
| 4440 | Py_ssize_t endinpos; |
| 4441 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4442 | const char *e; |
| 4443 | PyObject *errorHandler = NULL; |
| 4444 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4446 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4447 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4448 | Py_UNICODE r = *(unsigned char*)s; |
| 4449 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4450 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4452 | v = _PyUnicode_New(size); |
| 4453 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4454 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4455 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4456 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4457 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4458 | e = s + size; |
| 4459 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4460 | register unsigned char c = (unsigned char)*s; |
| 4461 | if (c < 128) { |
| 4462 | *p++ = c; |
| 4463 | ++s; |
| 4464 | } |
| 4465 | else { |
| 4466 | startinpos = s-starts; |
| 4467 | endinpos = startinpos + 1; |
| 4468 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4469 | if (unicode_decode_call_errorhandler( |
| 4470 | errors, &errorHandler, |
| 4471 | "ascii", "ordinal not in range(128)", |
| 4472 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4473 | &v, &outpos, &p)) |
| 4474 | goto onError; |
| 4475 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4477 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4478 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4479 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4480 | Py_XDECREF(errorHandler); |
| 4481 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4482 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4484 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4485 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4486 | Py_XDECREF(errorHandler); |
| 4487 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4488 | return NULL; |
| 4489 | } |
| 4490 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4491 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4492 | Py_ssize_t size, |
| 4493 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4494 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4495 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4499 | { |
| 4500 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4501 | PyErr_BadArgument(); |
| 4502 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4503 | } |
| 4504 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4505 | PyUnicode_GET_SIZE(unicode), |
| 4506 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4509 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4510 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4511 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4512 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4513 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4514 | #define NEED_RETRY |
| 4515 | #endif |
| 4516 | |
| 4517 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4518 | a) it assumes an incomplete character consists of a single byte, and |
| 4519 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4520 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4521 | |
| 4522 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4523 | { |
| 4524 | const char *curr = s + offset; |
| 4525 | |
| 4526 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4527 | const char *prev = CharPrev(s, curr); |
| 4528 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4529 | } |
| 4530 | return 0; |
| 4531 | } |
| 4532 | |
| 4533 | /* |
| 4534 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4535 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4536 | */ |
| 4537 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4538 | const char *s, /* MBCS string */ |
| 4539 | int size, /* sizeof MBCS string */ |
| 4540 | int final) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4541 | { |
| 4542 | Py_UNICODE *p; |
| 4543 | Py_ssize_t n = 0; |
| 4544 | int usize = 0; |
| 4545 | |
| 4546 | assert(size >= 0); |
| 4547 | |
| 4548 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4549 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4550 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4551 | |
| 4552 | /* First get the size of the result */ |
| 4553 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4554 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4555 | if (usize == 0) { |
| 4556 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4557 | return -1; |
| 4558 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4562 | /* Create unicode object */ |
| 4563 | *v = _PyUnicode_New(usize); |
| 4564 | if (*v == NULL) |
| 4565 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4566 | } |
| 4567 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4568 | /* Extend unicode object */ |
| 4569 | n = PyUnicode_GET_SIZE(*v); |
| 4570 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4571 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | /* Do the conversion */ |
| 4575 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4576 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4577 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4578 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4579 | return -1; |
| 4580 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | return size; |
| 4584 | } |
| 4585 | |
| 4586 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4587 | Py_ssize_t size, |
| 4588 | const char *errors, |
| 4589 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4590 | { |
| 4591 | PyUnicodeObject *v = NULL; |
| 4592 | int done; |
| 4593 | |
| 4594 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4595 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4596 | |
| 4597 | #ifdef NEED_RETRY |
| 4598 | retry: |
| 4599 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4600 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4601 | else |
| 4602 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4603 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4604 | |
| 4605 | if (done < 0) { |
| 4606 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4607 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4611 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4612 | |
| 4613 | #ifdef NEED_RETRY |
| 4614 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4615 | s += done; |
| 4616 | size -= done; |
| 4617 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4618 | } |
| 4619 | #endif |
| 4620 | |
| 4621 | return (PyObject *)v; |
| 4622 | } |
| 4623 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4624 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4625 | Py_ssize_t size, |
| 4626 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4627 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4628 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4629 | } |
| 4630 | |
| 4631 | /* |
| 4632 | * Convert unicode into string object (MBCS). |
| 4633 | * Returns 0 if succeed, -1 otherwise. |
| 4634 | */ |
| 4635 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4636 | const Py_UNICODE *p, /* unicode */ |
| 4637 | int size) /* size of unicode */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4638 | { |
| 4639 | int mbcssize = 0; |
| 4640 | Py_ssize_t n = 0; |
| 4641 | |
| 4642 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4643 | |
| 4644 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4645 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4646 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4647 | if (mbcssize == 0) { |
| 4648 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4649 | return -1; |
| 4650 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4653 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4654 | /* Create string object */ |
| 4655 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4656 | if (*repr == NULL) |
| 4657 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4658 | } |
| 4659 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4660 | /* Extend string object */ |
| 4661 | n = PyBytes_Size(*repr); |
| 4662 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4663 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4664 | } |
| 4665 | |
| 4666 | /* Do the conversion */ |
| 4667 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4668 | char *s = PyBytes_AS_STRING(*repr) + n; |
| 4669 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4670 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4671 | return -1; |
| 4672 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4679 | Py_ssize_t size, |
| 4680 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4681 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4682 | PyObject *repr = NULL; |
| 4683 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4684 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4685 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4686 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4687 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4688 | ret = encode_mbcs(&repr, p, INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4689 | else |
| 4690 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4691 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4692 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4693 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4694 | Py_XDECREF(repr); |
| 4695 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4696 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4697 | |
| 4698 | #ifdef NEED_RETRY |
| 4699 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4700 | p += INT_MAX; |
| 4701 | size -= INT_MAX; |
| 4702 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4703 | } |
| 4704 | #endif |
| 4705 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4706 | return repr; |
| 4707 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4708 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4709 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4710 | { |
| 4711 | if (!PyUnicode_Check(unicode)) { |
| 4712 | PyErr_BadArgument(); |
| 4713 | return NULL; |
| 4714 | } |
| 4715 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4716 | PyUnicode_GET_SIZE(unicode), |
| 4717 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4720 | #undef NEED_RETRY |
| 4721 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4722 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4724 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4725 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4726 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4727 | Py_ssize_t size, |
| 4728 | PyObject *mapping, |
| 4729 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4730 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4731 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4732 | Py_ssize_t startinpos; |
| 4733 | Py_ssize_t endinpos; |
| 4734 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4735 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4736 | PyUnicodeObject *v; |
| 4737 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4738 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4739 | PyObject *errorHandler = NULL; |
| 4740 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4741 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4742 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4743 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4744 | /* Default to Latin-1 */ |
| 4745 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4746 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4747 | |
| 4748 | v = _PyUnicode_New(size); |
| 4749 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4750 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4751 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4752 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4753 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4754 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4755 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4756 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4757 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4758 | while (s < e) { |
| 4759 | unsigned char ch = *s; |
| 4760 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4761 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4762 | if (ch < maplen) |
| 4763 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4764 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4765 | if (x == 0xfffe) { |
| 4766 | /* undefined mapping */ |
| 4767 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4768 | startinpos = s-starts; |
| 4769 | endinpos = startinpos+1; |
| 4770 | if (unicode_decode_call_errorhandler( |
| 4771 | errors, &errorHandler, |
| 4772 | "charmap", "character maps to <undefined>", |
| 4773 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4774 | &v, &outpos, &p)) { |
| 4775 | goto onError; |
| 4776 | } |
| 4777 | continue; |
| 4778 | } |
| 4779 | *p++ = x; |
| 4780 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4781 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4782 | } |
| 4783 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4784 | while (s < e) { |
| 4785 | unsigned char ch = *s; |
| 4786 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4787 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4788 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4789 | w = PyLong_FromLong((long)ch); |
| 4790 | if (w == NULL) |
| 4791 | goto onError; |
| 4792 | x = PyObject_GetItem(mapping, w); |
| 4793 | Py_DECREF(w); |
| 4794 | if (x == NULL) { |
| 4795 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4796 | /* No mapping found means: mapping is undefined. */ |
| 4797 | PyErr_Clear(); |
| 4798 | x = Py_None; |
| 4799 | Py_INCREF(x); |
| 4800 | } else |
| 4801 | goto onError; |
| 4802 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4803 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4804 | /* Apply mapping */ |
| 4805 | if (PyLong_Check(x)) { |
| 4806 | long value = PyLong_AS_LONG(x); |
| 4807 | if (value < 0 || value > 65535) { |
| 4808 | PyErr_SetString(PyExc_TypeError, |
| 4809 | "character mapping must be in range(65536)"); |
| 4810 | Py_DECREF(x); |
| 4811 | goto onError; |
| 4812 | } |
| 4813 | *p++ = (Py_UNICODE)value; |
| 4814 | } |
| 4815 | else if (x == Py_None) { |
| 4816 | /* undefined mapping */ |
| 4817 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4818 | startinpos = s-starts; |
| 4819 | endinpos = startinpos+1; |
| 4820 | if (unicode_decode_call_errorhandler( |
| 4821 | errors, &errorHandler, |
| 4822 | "charmap", "character maps to <undefined>", |
| 4823 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4824 | &v, &outpos, &p)) { |
| 4825 | Py_DECREF(x); |
| 4826 | goto onError; |
| 4827 | } |
| 4828 | Py_DECREF(x); |
| 4829 | continue; |
| 4830 | } |
| 4831 | else if (PyUnicode_Check(x)) { |
| 4832 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4833 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4834 | if (targetsize == 1) |
| 4835 | /* 1-1 mapping */ |
| 4836 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4837 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4838 | else if (targetsize > 1) { |
| 4839 | /* 1-n mapping */ |
| 4840 | if (targetsize > extrachars) { |
| 4841 | /* resize first */ |
| 4842 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4843 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4844 | (targetsize << 2); |
| 4845 | extrachars += needed; |
| 4846 | /* XXX overflow detection missing */ |
| 4847 | if (_PyUnicode_Resize(&v, |
| 4848 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4849 | Py_DECREF(x); |
| 4850 | goto onError; |
| 4851 | } |
| 4852 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4853 | } |
| 4854 | Py_UNICODE_COPY(p, |
| 4855 | PyUnicode_AS_UNICODE(x), |
| 4856 | targetsize); |
| 4857 | p += targetsize; |
| 4858 | extrachars -= targetsize; |
| 4859 | } |
| 4860 | /* 1-0 mapping: skip the character */ |
| 4861 | } |
| 4862 | else { |
| 4863 | /* wrong return value */ |
| 4864 | PyErr_SetString(PyExc_TypeError, |
| 4865 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4866 | Py_DECREF(x); |
| 4867 | goto onError; |
| 4868 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4869 | Py_DECREF(x); |
| 4870 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4871 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4872 | } |
| 4873 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4874 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4875 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4876 | Py_XDECREF(errorHandler); |
| 4877 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4878 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4879 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4880 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4881 | Py_XDECREF(errorHandler); |
| 4882 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4883 | Py_XDECREF(v); |
| 4884 | return NULL; |
| 4885 | } |
| 4886 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4887 | /* Charmap encoding: the lookup table */ |
| 4888 | |
| 4889 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4890 | PyObject_HEAD |
| 4891 | unsigned char level1[32]; |
| 4892 | int count2, count3; |
| 4893 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4894 | }; |
| 4895 | |
| 4896 | static PyObject* |
| 4897 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4898 | { |
| 4899 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4900 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4901 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4905 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4906 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4907 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4908 | }; |
| 4909 | |
| 4910 | static void |
| 4911 | encoding_map_dealloc(PyObject* o) |
| 4912 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4913 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4917 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4918 | "EncodingMap", /*tp_name*/ |
| 4919 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4920 | 0, /*tp_itemsize*/ |
| 4921 | /* methods */ |
| 4922 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4923 | 0, /*tp_print*/ |
| 4924 | 0, /*tp_getattr*/ |
| 4925 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4926 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4927 | 0, /*tp_repr*/ |
| 4928 | 0, /*tp_as_number*/ |
| 4929 | 0, /*tp_as_sequence*/ |
| 4930 | 0, /*tp_as_mapping*/ |
| 4931 | 0, /*tp_hash*/ |
| 4932 | 0, /*tp_call*/ |
| 4933 | 0, /*tp_str*/ |
| 4934 | 0, /*tp_getattro*/ |
| 4935 | 0, /*tp_setattro*/ |
| 4936 | 0, /*tp_as_buffer*/ |
| 4937 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4938 | 0, /*tp_doc*/ |
| 4939 | 0, /*tp_traverse*/ |
| 4940 | 0, /*tp_clear*/ |
| 4941 | 0, /*tp_richcompare*/ |
| 4942 | 0, /*tp_weaklistoffset*/ |
| 4943 | 0, /*tp_iter*/ |
| 4944 | 0, /*tp_iternext*/ |
| 4945 | encoding_map_methods, /*tp_methods*/ |
| 4946 | 0, /*tp_members*/ |
| 4947 | 0, /*tp_getset*/ |
| 4948 | 0, /*tp_base*/ |
| 4949 | 0, /*tp_dict*/ |
| 4950 | 0, /*tp_descr_get*/ |
| 4951 | 0, /*tp_descr_set*/ |
| 4952 | 0, /*tp_dictoffset*/ |
| 4953 | 0, /*tp_init*/ |
| 4954 | 0, /*tp_alloc*/ |
| 4955 | 0, /*tp_new*/ |
| 4956 | 0, /*tp_free*/ |
| 4957 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4958 | }; |
| 4959 | |
| 4960 | PyObject* |
| 4961 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4962 | { |
| 4963 | Py_UNICODE *decode; |
| 4964 | PyObject *result; |
| 4965 | struct encoding_map *mresult; |
| 4966 | int i; |
| 4967 | int need_dict = 0; |
| 4968 | unsigned char level1[32]; |
| 4969 | unsigned char level2[512]; |
| 4970 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4971 | int count2 = 0, count3 = 0; |
| 4972 | |
| 4973 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4974 | PyErr_BadArgument(); |
| 4975 | return NULL; |
| 4976 | } |
| 4977 | decode = PyUnicode_AS_UNICODE(string); |
| 4978 | memset(level1, 0xFF, sizeof level1); |
| 4979 | memset(level2, 0xFF, sizeof level2); |
| 4980 | |
| 4981 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4982 | or if there are non-BMP characters, we need to use |
| 4983 | a mapping dictionary. */ |
| 4984 | if (decode[0] != 0) |
| 4985 | need_dict = 1; |
| 4986 | for (i = 1; i < 256; i++) { |
| 4987 | int l1, l2; |
| 4988 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4989 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4990 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4991 | #endif |
| 4992 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4993 | need_dict = 1; |
| 4994 | break; |
| 4995 | } |
| 4996 | if (decode[i] == 0xFFFE) |
| 4997 | /* unmapped character */ |
| 4998 | continue; |
| 4999 | l1 = decode[i] >> 11; |
| 5000 | l2 = decode[i] >> 7; |
| 5001 | if (level1[l1] == 0xFF) |
| 5002 | level1[l1] = count2++; |
| 5003 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5004 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 5008 | need_dict = 1; |
| 5009 | |
| 5010 | if (need_dict) { |
| 5011 | PyObject *result = PyDict_New(); |
| 5012 | PyObject *key, *value; |
| 5013 | if (!result) |
| 5014 | return NULL; |
| 5015 | for (i = 0; i < 256; i++) { |
| 5016 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5017 | key = PyLong_FromLong(decode[i]); |
| 5018 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5019 | if (!key || !value) |
| 5020 | goto failed1; |
| 5021 | if (PyDict_SetItem(result, key, value) == -1) |
| 5022 | goto failed1; |
| 5023 | Py_DECREF(key); |
| 5024 | Py_DECREF(value); |
| 5025 | } |
| 5026 | return result; |
| 5027 | failed1: |
| 5028 | Py_XDECREF(key); |
| 5029 | Py_XDECREF(value); |
| 5030 | Py_DECREF(result); |
| 5031 | return NULL; |
| 5032 | } |
| 5033 | |
| 5034 | /* Create a three-level trie */ |
| 5035 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5036 | 16*count2 + 128*count3 - 1); |
| 5037 | if (!result) |
| 5038 | return PyErr_NoMemory(); |
| 5039 | PyObject_Init(result, &EncodingMapType); |
| 5040 | mresult = (struct encoding_map*)result; |
| 5041 | mresult->count2 = count2; |
| 5042 | mresult->count3 = count3; |
| 5043 | mlevel1 = mresult->level1; |
| 5044 | mlevel2 = mresult->level23; |
| 5045 | mlevel3 = mresult->level23 + 16*count2; |
| 5046 | memcpy(mlevel1, level1, 32); |
| 5047 | memset(mlevel2, 0xFF, 16*count2); |
| 5048 | memset(mlevel3, 0, 128*count3); |
| 5049 | count3 = 0; |
| 5050 | for (i = 1; i < 256; i++) { |
| 5051 | int o1, o2, o3, i2, i3; |
| 5052 | if (decode[i] == 0xFFFE) |
| 5053 | /* unmapped character */ |
| 5054 | continue; |
| 5055 | o1 = decode[i]>>11; |
| 5056 | o2 = (decode[i]>>7) & 0xF; |
| 5057 | i2 = 16*mlevel1[o1] + o2; |
| 5058 | if (mlevel2[i2] == 0xFF) |
| 5059 | mlevel2[i2] = count3++; |
| 5060 | o3 = decode[i] & 0x7F; |
| 5061 | i3 = 128*mlevel2[i2] + o3; |
| 5062 | mlevel3[i3] = i; |
| 5063 | } |
| 5064 | return result; |
| 5065 | } |
| 5066 | |
| 5067 | static int |
| 5068 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5069 | { |
| 5070 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5071 | int l1 = c>>11; |
| 5072 | int l2 = (c>>7) & 0xF; |
| 5073 | int l3 = c & 0x7F; |
| 5074 | int i; |
| 5075 | |
| 5076 | #ifdef Py_UNICODE_WIDE |
| 5077 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5078 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5079 | } |
| 5080 | #endif |
| 5081 | if (c == 0) |
| 5082 | return 0; |
| 5083 | /* level 1*/ |
| 5084 | i = map->level1[l1]; |
| 5085 | if (i == 0xFF) { |
| 5086 | return -1; |
| 5087 | } |
| 5088 | /* level 2*/ |
| 5089 | i = map->level23[16*i+l2]; |
| 5090 | if (i == 0xFF) { |
| 5091 | return -1; |
| 5092 | } |
| 5093 | /* level 3 */ |
| 5094 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5095 | if (i == 0) { |
| 5096 | return -1; |
| 5097 | } |
| 5098 | return i; |
| 5099 | } |
| 5100 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5101 | /* Lookup the character ch in the mapping. If the character |
| 5102 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5103 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5104 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5105 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5106 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5107 | PyObject *x; |
| 5108 | |
| 5109 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5110 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5111 | x = PyObject_GetItem(mapping, w); |
| 5112 | Py_DECREF(w); |
| 5113 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5114 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5115 | /* No mapping found means: mapping is undefined. */ |
| 5116 | PyErr_Clear(); |
| 5117 | x = Py_None; |
| 5118 | Py_INCREF(x); |
| 5119 | return x; |
| 5120 | } else |
| 5121 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5122 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5123 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5124 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5125 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5126 | long value = PyLong_AS_LONG(x); |
| 5127 | if (value < 0 || value > 255) { |
| 5128 | PyErr_SetString(PyExc_TypeError, |
| 5129 | "character mapping must be in range(256)"); |
| 5130 | Py_DECREF(x); |
| 5131 | return NULL; |
| 5132 | } |
| 5133 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5134 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5135 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5136 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5138 | /* wrong return value */ |
| 5139 | PyErr_Format(PyExc_TypeError, |
| 5140 | "character mapping must return integer, bytes or None, not %.400s", |
| 5141 | x->ob_type->tp_name); |
| 5142 | Py_DECREF(x); |
| 5143 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5144 | } |
| 5145 | } |
| 5146 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5147 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5148 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5149 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5150 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5151 | /* exponentially overallocate to minimize reallocations */ |
| 5152 | if (requiredsize < 2*outsize) |
| 5153 | requiredsize = 2*outsize; |
| 5154 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5155 | return -1; |
| 5156 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5159 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5160 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5161 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5162 | /* 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] | 5163 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5164 | space is available. Return a new reference to the object that |
| 5165 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5166 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5167 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5168 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5169 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5170 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5171 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5172 | PyObject *rep; |
| 5173 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5174 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5175 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5176 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5177 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5178 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5179 | if (res == -1) |
| 5180 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5181 | if (outsize<requiredsize) |
| 5182 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5183 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5184 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5185 | outstart[(*outpos)++] = (char)res; |
| 5186 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5190 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5191 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5192 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5193 | Py_DECREF(rep); |
| 5194 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5195 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5196 | if (PyLong_Check(rep)) { |
| 5197 | Py_ssize_t requiredsize = *outpos+1; |
| 5198 | if (outsize<requiredsize) |
| 5199 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5200 | Py_DECREF(rep); |
| 5201 | return enc_EXCEPTION; |
| 5202 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5203 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5204 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5205 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5206 | else { |
| 5207 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5208 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5209 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5210 | if (outsize<requiredsize) |
| 5211 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5212 | Py_DECREF(rep); |
| 5213 | return enc_EXCEPTION; |
| 5214 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5215 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5216 | memcpy(outstart + *outpos, repchars, repsize); |
| 5217 | *outpos += repsize; |
| 5218 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5219 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5220 | Py_DECREF(rep); |
| 5221 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
| 5224 | /* handle an error in PyUnicode_EncodeCharmap |
| 5225 | Return 0 on success, -1 on error */ |
| 5226 | static |
| 5227 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5228 | 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] | 5229 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5230 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5231 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5232 | { |
| 5233 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5234 | Py_ssize_t repsize; |
| 5235 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5236 | Py_UNICODE *uni2; |
| 5237 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5238 | Py_ssize_t collstartpos = *inpos; |
| 5239 | Py_ssize_t collendpos = *inpos+1; |
| 5240 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5241 | char *encoding = "charmap"; |
| 5242 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5243 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5244 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5245 | /* find all unencodable characters */ |
| 5246 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5247 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5248 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5249 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5250 | if (res != -1) |
| 5251 | break; |
| 5252 | ++collendpos; |
| 5253 | continue; |
| 5254 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5255 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5256 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5257 | if (rep==NULL) |
| 5258 | return -1; |
| 5259 | else if (rep!=Py_None) { |
| 5260 | Py_DECREF(rep); |
| 5261 | break; |
| 5262 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5263 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5264 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5265 | } |
| 5266 | /* cache callback name lookup |
| 5267 | * (if not done yet, i.e. it's the first error) */ |
| 5268 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5269 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5270 | *known_errorHandler = 1; |
| 5271 | else if (!strcmp(errors, "replace")) |
| 5272 | *known_errorHandler = 2; |
| 5273 | else if (!strcmp(errors, "ignore")) |
| 5274 | *known_errorHandler = 3; |
| 5275 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5276 | *known_errorHandler = 4; |
| 5277 | else |
| 5278 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5279 | } |
| 5280 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5281 | case 1: /* strict */ |
| 5282 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5283 | return -1; |
| 5284 | case 2: /* replace */ |
| 5285 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5286 | x = charmapencode_output('?', mapping, res, respos); |
| 5287 | if (x==enc_EXCEPTION) { |
| 5288 | return -1; |
| 5289 | } |
| 5290 | else if (x==enc_FAILED) { |
| 5291 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5292 | return -1; |
| 5293 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5294 | } |
| 5295 | /* fall through */ |
| 5296 | case 3: /* ignore */ |
| 5297 | *inpos = collendpos; |
| 5298 | break; |
| 5299 | case 4: /* xmlcharrefreplace */ |
| 5300 | /* generate replacement (temporarily (mis)uses p) */ |
| 5301 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5302 | char buffer[2+29+1+1]; |
| 5303 | char *cp; |
| 5304 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5305 | for (cp = buffer; *cp; ++cp) { |
| 5306 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5307 | if (x==enc_EXCEPTION) |
| 5308 | return -1; |
| 5309 | else if (x==enc_FAILED) { |
| 5310 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5311 | return -1; |
| 5312 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5313 | } |
| 5314 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5315 | *inpos = collendpos; |
| 5316 | break; |
| 5317 | default: |
| 5318 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5319 | encoding, reason, p, size, exceptionObject, |
| 5320 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5321 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5322 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5323 | if (PyBytes_Check(repunicode)) { |
| 5324 | /* Directly copy bytes result to output. */ |
| 5325 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5326 | Py_ssize_t requiredsize; |
| 5327 | repsize = PyBytes_Size(repunicode); |
| 5328 | requiredsize = *respos + repsize; |
| 5329 | if (requiredsize > outsize) |
| 5330 | /* Make room for all additional bytes. */ |
| 5331 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5332 | Py_DECREF(repunicode); |
| 5333 | return -1; |
| 5334 | } |
| 5335 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5336 | PyBytes_AsString(repunicode), repsize); |
| 5337 | *respos += repsize; |
| 5338 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5339 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5340 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5341 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5342 | /* generate replacement */ |
| 5343 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5344 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5345 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5346 | if (x==enc_EXCEPTION) { |
| 5347 | return -1; |
| 5348 | } |
| 5349 | else if (x==enc_FAILED) { |
| 5350 | Py_DECREF(repunicode); |
| 5351 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5352 | return -1; |
| 5353 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5354 | } |
| 5355 | *inpos = newpos; |
| 5356 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5357 | } |
| 5358 | return 0; |
| 5359 | } |
| 5360 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5361 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5362 | Py_ssize_t size, |
| 5363 | PyObject *mapping, |
| 5364 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5365 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5366 | /* output object */ |
| 5367 | PyObject *res = NULL; |
| 5368 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5369 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5370 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5371 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5372 | PyObject *errorHandler = NULL; |
| 5373 | PyObject *exc = NULL; |
| 5374 | /* the following variable is used for caching string comparisons |
| 5375 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5376 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5377 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5378 | |
| 5379 | /* Default to Latin-1 */ |
| 5380 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5381 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5383 | /* allocate enough for a simple encoding without |
| 5384 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5385 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5386 | if (res == NULL) |
| 5387 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5388 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5389 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5391 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5392 | /* try to encode it */ |
| 5393 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5394 | if (x==enc_EXCEPTION) /* error */ |
| 5395 | goto onError; |
| 5396 | if (x==enc_FAILED) { /* unencodable character */ |
| 5397 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5398 | &exc, |
| 5399 | &known_errorHandler, &errorHandler, errors, |
| 5400 | &res, &respos)) { |
| 5401 | goto onError; |
| 5402 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5403 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5404 | else |
| 5405 | /* done with this character => adjust input position */ |
| 5406 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5407 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5409 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5410 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5411 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5412 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5413 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5414 | Py_XDECREF(exc); |
| 5415 | Py_XDECREF(errorHandler); |
| 5416 | return res; |
| 5417 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5418 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5419 | Py_XDECREF(res); |
| 5420 | Py_XDECREF(exc); |
| 5421 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | return NULL; |
| 5423 | } |
| 5424 | |
| 5425 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5426 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5427 | { |
| 5428 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5429 | PyErr_BadArgument(); |
| 5430 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | } |
| 5432 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5433 | PyUnicode_GET_SIZE(unicode), |
| 5434 | mapping, |
| 5435 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5438 | /* create or adjust a UnicodeTranslateError */ |
| 5439 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5440 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5441 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5442 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5444 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5445 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5446 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | } |
| 5448 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5449 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5450 | goto onError; |
| 5451 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5452 | goto onError; |
| 5453 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5454 | goto onError; |
| 5455 | return; |
| 5456 | onError: |
| 5457 | Py_DECREF(*exceptionObject); |
| 5458 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | } |
| 5460 | } |
| 5461 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5462 | /* raises a UnicodeTranslateError */ |
| 5463 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5464 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5465 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5466 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5467 | { |
| 5468 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5469 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5470 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5471 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
| 5474 | /* error handling callback helper: |
| 5475 | build arguments, call the callback and check the arguments, |
| 5476 | put the result into newpos and return the replacement string, which |
| 5477 | has to be freed by the caller */ |
| 5478 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5479 | PyObject **errorHandler, |
| 5480 | const char *reason, |
| 5481 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5482 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5483 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5484 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5485 | 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] | 5486 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5487 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5488 | PyObject *restuple; |
| 5489 | PyObject *resunicode; |
| 5490 | |
| 5491 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5492 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5493 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5494 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
| 5497 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5498 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5499 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5500 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5501 | |
| 5502 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5503 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5504 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5505 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5506 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5507 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5508 | Py_DECREF(restuple); |
| 5509 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5510 | } |
| 5511 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5512 | &resunicode, &i_newpos)) { |
| 5513 | Py_DECREF(restuple); |
| 5514 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5515 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5516 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5517 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5518 | else |
| 5519 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5520 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5521 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5522 | Py_DECREF(restuple); |
| 5523 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5524 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5525 | Py_INCREF(resunicode); |
| 5526 | Py_DECREF(restuple); |
| 5527 | return resunicode; |
| 5528 | } |
| 5529 | |
| 5530 | /* Lookup the character ch in the mapping and put the result in result, |
| 5531 | which must be decrefed by the caller. |
| 5532 | Return 0 on success, -1 on error */ |
| 5533 | static |
| 5534 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5535 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5536 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5537 | PyObject *x; |
| 5538 | |
| 5539 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5540 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5541 | x = PyObject_GetItem(mapping, w); |
| 5542 | Py_DECREF(w); |
| 5543 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5544 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5545 | /* No mapping found means: use 1:1 mapping. */ |
| 5546 | PyErr_Clear(); |
| 5547 | *result = NULL; |
| 5548 | return 0; |
| 5549 | } else |
| 5550 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5551 | } |
| 5552 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5553 | *result = x; |
| 5554 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5555 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5556 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5557 | long value = PyLong_AS_LONG(x); |
| 5558 | long max = PyUnicode_GetMax(); |
| 5559 | if (value < 0 || value > max) { |
| 5560 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5561 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5562 | Py_DECREF(x); |
| 5563 | return -1; |
| 5564 | } |
| 5565 | *result = x; |
| 5566 | return 0; |
| 5567 | } |
| 5568 | else if (PyUnicode_Check(x)) { |
| 5569 | *result = x; |
| 5570 | return 0; |
| 5571 | } |
| 5572 | else { |
| 5573 | /* wrong return value */ |
| 5574 | PyErr_SetString(PyExc_TypeError, |
| 5575 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5576 | Py_DECREF(x); |
| 5577 | return -1; |
| 5578 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5579 | } |
| 5580 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5581 | if not reallocate and adjust various state variables. |
| 5582 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5583 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5584 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5585 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5586 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5587 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5588 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5589 | /* remember old output position */ |
| 5590 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5591 | /* exponentially overallocate to minimize reallocations */ |
| 5592 | if (requiredsize < 2 * oldsize) |
| 5593 | requiredsize = 2 * oldsize; |
| 5594 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5595 | return -1; |
| 5596 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5597 | } |
| 5598 | return 0; |
| 5599 | } |
| 5600 | /* lookup the character, put the result in the output string and adjust |
| 5601 | various state variables. Return a new reference to the object that |
| 5602 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5603 | undefined (in which case no character was written). |
| 5604 | The called must decref result. |
| 5605 | Return 0 on success, -1 on error. */ |
| 5606 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5607 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5608 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5609 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5610 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5611 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5612 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5613 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5614 | /* not found => default to 1:1 mapping */ |
| 5615 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5616 | } |
| 5617 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5618 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5619 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5620 | /* no overflow check, because we know that the space is enough */ |
| 5621 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5622 | } |
| 5623 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5624 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5625 | if (repsize==1) { |
| 5626 | /* no overflow check, because we know that the space is enough */ |
| 5627 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5628 | } |
| 5629 | else if (repsize!=0) { |
| 5630 | /* more than one character */ |
| 5631 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5632 | (insize - (curinp-startinp)) + |
| 5633 | repsize - 1; |
| 5634 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5635 | return -1; |
| 5636 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5637 | *outp += repsize; |
| 5638 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5639 | } |
| 5640 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5641 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5642 | return 0; |
| 5643 | } |
| 5644 | |
| 5645 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5646 | Py_ssize_t size, |
| 5647 | PyObject *mapping, |
| 5648 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5649 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5650 | /* output object */ |
| 5651 | PyObject *res = NULL; |
| 5652 | /* pointers to the beginning and end+1 of input */ |
| 5653 | const Py_UNICODE *startp = p; |
| 5654 | const Py_UNICODE *endp = p + size; |
| 5655 | /* pointer into the output */ |
| 5656 | Py_UNICODE *str; |
| 5657 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5658 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5659 | char *reason = "character maps to <undefined>"; |
| 5660 | PyObject *errorHandler = NULL; |
| 5661 | PyObject *exc = NULL; |
| 5662 | /* the following variable is used for caching string comparisons |
| 5663 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5664 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5665 | int known_errorHandler = -1; |
| 5666 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5668 | PyErr_BadArgument(); |
| 5669 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5671 | |
| 5672 | /* allocate enough for a simple 1:1 translation without |
| 5673 | replacements, if we need more, we'll resize */ |
| 5674 | res = PyUnicode_FromUnicode(NULL, size); |
| 5675 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5676 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5678 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5679 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5680 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5681 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5682 | /* try to encode it */ |
| 5683 | PyObject *x = NULL; |
| 5684 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5685 | Py_XDECREF(x); |
| 5686 | goto onError; |
| 5687 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5688 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5689 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5690 | ++p; |
| 5691 | else { /* untranslatable character */ |
| 5692 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5693 | Py_ssize_t repsize; |
| 5694 | Py_ssize_t newpos; |
| 5695 | Py_UNICODE *uni2; |
| 5696 | /* startpos for collecting untranslatable chars */ |
| 5697 | const Py_UNICODE *collstart = p; |
| 5698 | const Py_UNICODE *collend = p+1; |
| 5699 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5700 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5701 | /* find all untranslatable characters */ |
| 5702 | while (collend < endp) { |
| 5703 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5704 | goto onError; |
| 5705 | Py_XDECREF(x); |
| 5706 | if (x!=Py_None) |
| 5707 | break; |
| 5708 | ++collend; |
| 5709 | } |
| 5710 | /* cache callback name lookup |
| 5711 | * (if not done yet, i.e. it's the first error) */ |
| 5712 | if (known_errorHandler==-1) { |
| 5713 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5714 | known_errorHandler = 1; |
| 5715 | else if (!strcmp(errors, "replace")) |
| 5716 | known_errorHandler = 2; |
| 5717 | else if (!strcmp(errors, "ignore")) |
| 5718 | known_errorHandler = 3; |
| 5719 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5720 | known_errorHandler = 4; |
| 5721 | else |
| 5722 | known_errorHandler = 0; |
| 5723 | } |
| 5724 | switch (known_errorHandler) { |
| 5725 | case 1: /* strict */ |
| 5726 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5727 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5728 | case 2: /* replace */ |
| 5729 | /* No need to check for space, this is a 1:1 replacement */ |
| 5730 | for (coll = collstart; coll<collend; ++coll) |
| 5731 | *str++ = '?'; |
| 5732 | /* fall through */ |
| 5733 | case 3: /* ignore */ |
| 5734 | p = collend; |
| 5735 | break; |
| 5736 | case 4: /* xmlcharrefreplace */ |
| 5737 | /* generate replacement (temporarily (mis)uses p) */ |
| 5738 | for (p = collstart; p < collend; ++p) { |
| 5739 | char buffer[2+29+1+1]; |
| 5740 | char *cp; |
| 5741 | sprintf(buffer, "&#%d;", (int)*p); |
| 5742 | if (charmaptranslate_makespace(&res, &str, |
| 5743 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5744 | goto onError; |
| 5745 | for (cp = buffer; *cp; ++cp) |
| 5746 | *str++ = *cp; |
| 5747 | } |
| 5748 | p = collend; |
| 5749 | break; |
| 5750 | default: |
| 5751 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5752 | reason, startp, size, &exc, |
| 5753 | collstart-startp, collend-startp, &newpos); |
| 5754 | if (repunicode == NULL) |
| 5755 | goto onError; |
| 5756 | /* generate replacement */ |
| 5757 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5758 | if (charmaptranslate_makespace(&res, &str, |
| 5759 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5760 | Py_DECREF(repunicode); |
| 5761 | goto onError; |
| 5762 | } |
| 5763 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5764 | *str++ = *uni2; |
| 5765 | p = startp + newpos; |
| 5766 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5767 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5768 | } |
| 5769 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5770 | /* Resize if we allocated to much */ |
| 5771 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5772 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5773 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5774 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5775 | } |
| 5776 | Py_XDECREF(exc); |
| 5777 | Py_XDECREF(errorHandler); |
| 5778 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5779 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5780 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5781 | Py_XDECREF(res); |
| 5782 | Py_XDECREF(exc); |
| 5783 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5784 | return NULL; |
| 5785 | } |
| 5786 | |
| 5787 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5788 | PyObject *mapping, |
| 5789 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | { |
| 5791 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5792 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5793 | str = PyUnicode_FromObject(str); |
| 5794 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5795 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5797 | PyUnicode_GET_SIZE(str), |
| 5798 | mapping, |
| 5799 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5800 | Py_DECREF(str); |
| 5801 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5802 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5803 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5804 | Py_XDECREF(str); |
| 5805 | return NULL; |
| 5806 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5807 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5808 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5809 | |
| 5810 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5811 | Py_ssize_t length, |
| 5812 | char *output, |
| 5813 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5814 | { |
| 5815 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5816 | PyObject *errorHandler = NULL; |
| 5817 | PyObject *exc = NULL; |
| 5818 | const char *encoding = "decimal"; |
| 5819 | const char *reason = "invalid decimal Unicode string"; |
| 5820 | /* the following variable is used for caching string comparisons |
| 5821 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5822 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5823 | |
| 5824 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5825 | PyErr_BadArgument(); |
| 5826 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | p = s; |
| 5830 | end = s + length; |
| 5831 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | register Py_UNICODE ch = *p; |
| 5833 | int decimal; |
| 5834 | PyObject *repunicode; |
| 5835 | Py_ssize_t repsize; |
| 5836 | Py_ssize_t newpos; |
| 5837 | Py_UNICODE *uni2; |
| 5838 | Py_UNICODE *collstart; |
| 5839 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5840 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5841 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5842 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5843 | ++p; |
| 5844 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5845 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5846 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5847 | if (decimal >= 0) { |
| 5848 | *output++ = '0' + decimal; |
| 5849 | ++p; |
| 5850 | continue; |
| 5851 | } |
| 5852 | if (0 < ch && ch < 256) { |
| 5853 | *output++ = (char)ch; |
| 5854 | ++p; |
| 5855 | continue; |
| 5856 | } |
| 5857 | /* All other characters are considered unencodable */ |
| 5858 | collstart = p; |
| 5859 | collend = p+1; |
| 5860 | while (collend < end) { |
| 5861 | if ((0 < *collend && *collend < 256) || |
| 5862 | !Py_UNICODE_ISSPACE(*collend) || |
| 5863 | Py_UNICODE_TODECIMAL(*collend)) |
| 5864 | break; |
| 5865 | } |
| 5866 | /* cache callback name lookup |
| 5867 | * (if not done yet, i.e. it's the first error) */ |
| 5868 | if (known_errorHandler==-1) { |
| 5869 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5870 | known_errorHandler = 1; |
| 5871 | else if (!strcmp(errors, "replace")) |
| 5872 | known_errorHandler = 2; |
| 5873 | else if (!strcmp(errors, "ignore")) |
| 5874 | known_errorHandler = 3; |
| 5875 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5876 | known_errorHandler = 4; |
| 5877 | else |
| 5878 | known_errorHandler = 0; |
| 5879 | } |
| 5880 | switch (known_errorHandler) { |
| 5881 | case 1: /* strict */ |
| 5882 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5883 | goto onError; |
| 5884 | case 2: /* replace */ |
| 5885 | for (p = collstart; p < collend; ++p) |
| 5886 | *output++ = '?'; |
| 5887 | /* fall through */ |
| 5888 | case 3: /* ignore */ |
| 5889 | p = collend; |
| 5890 | break; |
| 5891 | case 4: /* xmlcharrefreplace */ |
| 5892 | /* generate replacement (temporarily (mis)uses p) */ |
| 5893 | for (p = collstart; p < collend; ++p) |
| 5894 | output += sprintf(output, "&#%d;", (int)*p); |
| 5895 | p = collend; |
| 5896 | break; |
| 5897 | default: |
| 5898 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5899 | encoding, reason, s, length, &exc, |
| 5900 | collstart-s, collend-s, &newpos); |
| 5901 | if (repunicode == NULL) |
| 5902 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5903 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5904 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5905 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 5906 | Py_DECREF(repunicode); |
| 5907 | goto onError; |
| 5908 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5909 | /* generate replacement */ |
| 5910 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5911 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5912 | Py_UNICODE ch = *uni2; |
| 5913 | if (Py_UNICODE_ISSPACE(ch)) |
| 5914 | *output++ = ' '; |
| 5915 | else { |
| 5916 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5917 | if (decimal >= 0) |
| 5918 | *output++ = '0' + decimal; |
| 5919 | else if (0 < ch && ch < 256) |
| 5920 | *output++ = (char)ch; |
| 5921 | else { |
| 5922 | Py_DECREF(repunicode); |
| 5923 | raise_encode_exception(&exc, encoding, |
| 5924 | s, length, collstart-s, collend-s, reason); |
| 5925 | goto onError; |
| 5926 | } |
| 5927 | } |
| 5928 | } |
| 5929 | p = s + newpos; |
| 5930 | Py_DECREF(repunicode); |
| 5931 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5932 | } |
| 5933 | /* 0-terminate the output string */ |
| 5934 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5935 | Py_XDECREF(exc); |
| 5936 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5937 | return 0; |
| 5938 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5939 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5940 | Py_XDECREF(exc); |
| 5941 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5942 | return -1; |
| 5943 | } |
| 5944 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5945 | /* --- Helpers ------------------------------------------------------------ */ |
| 5946 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5947 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5948 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5949 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5950 | #include "stringlib/count.h" |
| 5951 | #include "stringlib/find.h" |
| 5952 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5953 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5954 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5955 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 5956 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5957 | #include "stringlib/localeutil.h" |
| 5958 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5959 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5960 | #define ADJUST_INDICES(start, end, len) \ |
| 5961 | if (end > len) \ |
| 5962 | end = len; \ |
| 5963 | else if (end < 0) { \ |
| 5964 | end += len; \ |
| 5965 | if (end < 0) \ |
| 5966 | end = 0; \ |
| 5967 | } \ |
| 5968 | if (start < 0) { \ |
| 5969 | start += len; \ |
| 5970 | if (start < 0) \ |
| 5971 | start = 0; \ |
| 5972 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5973 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5974 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5975 | PyObject *substr, |
| 5976 | Py_ssize_t start, |
| 5977 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5979 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5980 | PyUnicodeObject* str_obj; |
| 5981 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5982 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5983 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5984 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5985 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5986 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5987 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5988 | Py_DECREF(str_obj); |
| 5989 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5990 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5991 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5992 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5993 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5994 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 5995 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5996 | ); |
| 5997 | |
| 5998 | Py_DECREF(sub_obj); |
| 5999 | Py_DECREF(str_obj); |
| 6000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6001 | return result; |
| 6002 | } |
| 6003 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6004 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6005 | PyObject *sub, |
| 6006 | Py_ssize_t start, |
| 6007 | Py_ssize_t end, |
| 6008 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6010 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6011 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6013 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6014 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6015 | sub = PyUnicode_FromObject(sub); |
| 6016 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6017 | Py_DECREF(str); |
| 6018 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6019 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6020 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6021 | if (direction > 0) |
| 6022 | result = stringlib_find_slice( |
| 6023 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6024 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6025 | start, end |
| 6026 | ); |
| 6027 | else |
| 6028 | result = stringlib_rfind_slice( |
| 6029 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6030 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6031 | start, end |
| 6032 | ); |
| 6033 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6035 | Py_DECREF(sub); |
| 6036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 | return result; |
| 6038 | } |
| 6039 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6040 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | PyUnicodeObject *substring, |
| 6043 | Py_ssize_t start, |
| 6044 | Py_ssize_t end, |
| 6045 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6047 | if (substring->length == 0) |
| 6048 | return 1; |
| 6049 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6050 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | end -= substring->length; |
| 6052 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6053 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6054 | |
| 6055 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6056 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6057 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | } else { |
| 6059 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6060 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | } |
| 6062 | |
| 6063 | return 0; |
| 6064 | } |
| 6065 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6066 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6067 | PyObject *substr, |
| 6068 | Py_ssize_t start, |
| 6069 | Py_ssize_t end, |
| 6070 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6072 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | str = PyUnicode_FromObject(str); |
| 6075 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6076 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6077 | substr = PyUnicode_FromObject(substr); |
| 6078 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6079 | Py_DECREF(str); |
| 6080 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6082 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6083 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6084 | (PyUnicodeObject *)substr, |
| 6085 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6086 | Py_DECREF(str); |
| 6087 | Py_DECREF(substr); |
| 6088 | return result; |
| 6089 | } |
| 6090 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6091 | /* Apply fixfct filter to the Unicode object self and return a |
| 6092 | reference to the modified object */ |
| 6093 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6094 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6095 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6096 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | { |
| 6098 | |
| 6099 | PyUnicodeObject *u; |
| 6100 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6101 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6103 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6104 | |
| 6105 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6106 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6107 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6108 | /* fixfct should return TRUE if it modified the buffer. If |
| 6109 | FALSE, return a reference to the original buffer instead |
| 6110 | (to save space, not time) */ |
| 6111 | Py_INCREF(self); |
| 6112 | Py_DECREF(u); |
| 6113 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | } |
| 6115 | return (PyObject*) u; |
| 6116 | } |
| 6117 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6118 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6119 | int fixupper(PyUnicodeObject *self) |
| 6120 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6121 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | Py_UNICODE *s = self->str; |
| 6123 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6125 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6126 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6127 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6128 | ch = Py_UNICODE_TOUPPER(*s); |
| 6129 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6131 | *s = ch; |
| 6132 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | s++; |
| 6134 | } |
| 6135 | |
| 6136 | return status; |
| 6137 | } |
| 6138 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6139 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6140 | int fixlower(PyUnicodeObject *self) |
| 6141 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6142 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | Py_UNICODE *s = self->str; |
| 6144 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6145 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6147 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6148 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6149 | ch = Py_UNICODE_TOLOWER(*s); |
| 6150 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6152 | *s = ch; |
| 6153 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6154 | s++; |
| 6155 | } |
| 6156 | |
| 6157 | return status; |
| 6158 | } |
| 6159 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6160 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6161 | int fixswapcase(PyUnicodeObject *self) |
| 6162 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6163 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6164 | Py_UNICODE *s = self->str; |
| 6165 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6167 | while (len-- > 0) { |
| 6168 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6169 | *s = Py_UNICODE_TOLOWER(*s); |
| 6170 | status = 1; |
| 6171 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6172 | *s = Py_UNICODE_TOUPPER(*s); |
| 6173 | status = 1; |
| 6174 | } |
| 6175 | s++; |
| 6176 | } |
| 6177 | |
| 6178 | return status; |
| 6179 | } |
| 6180 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6181 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6182 | int fixcapitalize(PyUnicodeObject *self) |
| 6183 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6184 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6185 | Py_UNICODE *s = self->str; |
| 6186 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6187 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6188 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6189 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6190 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6191 | *s = Py_UNICODE_TOUPPER(*s); |
| 6192 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6194 | s++; |
| 6195 | while (--len > 0) { |
| 6196 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6197 | *s = Py_UNICODE_TOLOWER(*s); |
| 6198 | status = 1; |
| 6199 | } |
| 6200 | s++; |
| 6201 | } |
| 6202 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | static |
| 6206 | int fixtitle(PyUnicodeObject *self) |
| 6207 | { |
| 6208 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6209 | register Py_UNICODE *e; |
| 6210 | int previous_is_cased; |
| 6211 | |
| 6212 | /* Shortcut for single character strings */ |
| 6213 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6215 | if (*p != ch) { |
| 6216 | *p = ch; |
| 6217 | return 1; |
| 6218 | } |
| 6219 | else |
| 6220 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | e = p + PyUnicode_GET_SIZE(self); |
| 6224 | previous_is_cased = 0; |
| 6225 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6226 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6227 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6228 | if (previous_is_cased) |
| 6229 | *p = Py_UNICODE_TOLOWER(ch); |
| 6230 | else |
| 6231 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6232 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6233 | if (Py_UNICODE_ISLOWER(ch) || |
| 6234 | Py_UNICODE_ISUPPER(ch) || |
| 6235 | Py_UNICODE_ISTITLE(ch)) |
| 6236 | previous_is_cased = 1; |
| 6237 | else |
| 6238 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6239 | } |
| 6240 | return 1; |
| 6241 | } |
| 6242 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6243 | PyObject * |
| 6244 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6246 | const Py_UNICODE blank = ' '; |
| 6247 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6248 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6249 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6250 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6251 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6252 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6253 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6254 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6255 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6256 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6257 | fseq = PySequence_Fast(seq, ""); |
| 6258 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6259 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6260 | } |
| 6261 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6262 | /* NOTE: the following code can't call back into Python code, |
| 6263 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6264 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6265 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6266 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6267 | /* If empty sequence, return u"". */ |
| 6268 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6269 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6270 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6271 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6272 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6273 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6274 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6275 | item = items[0]; |
| 6276 | if (PyUnicode_CheckExact(item)) { |
| 6277 | Py_INCREF(item); |
| 6278 | res = (PyUnicodeObject *)item; |
| 6279 | goto Done; |
| 6280 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6281 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6282 | else { |
| 6283 | /* Set up sep and seplen */ |
| 6284 | if (separator == NULL) { |
| 6285 | sep = ␣ |
| 6286 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6287 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6288 | else { |
| 6289 | if (!PyUnicode_Check(separator)) { |
| 6290 | PyErr_Format(PyExc_TypeError, |
| 6291 | "separator: expected str instance," |
| 6292 | " %.80s found", |
| 6293 | Py_TYPE(separator)->tp_name); |
| 6294 | goto onError; |
| 6295 | } |
| 6296 | sep = PyUnicode_AS_UNICODE(separator); |
| 6297 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6298 | } |
| 6299 | } |
| 6300 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6301 | /* There are at least two things to join, or else we have a subclass |
| 6302 | * of str in the sequence. |
| 6303 | * Do a pre-pass to figure out the total amount of space we'll |
| 6304 | * need (sz), and see whether all argument are strings. |
| 6305 | */ |
| 6306 | sz = 0; |
| 6307 | for (i = 0; i < seqlen; i++) { |
| 6308 | const Py_ssize_t old_sz = sz; |
| 6309 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6310 | if (!PyUnicode_Check(item)) { |
| 6311 | PyErr_Format(PyExc_TypeError, |
| 6312 | "sequence item %zd: expected str instance," |
| 6313 | " %.80s found", |
| 6314 | i, Py_TYPE(item)->tp_name); |
| 6315 | goto onError; |
| 6316 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6317 | sz += PyUnicode_GET_SIZE(item); |
| 6318 | if (i != 0) |
| 6319 | sz += seplen; |
| 6320 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6321 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6322 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6323 | goto onError; |
| 6324 | } |
| 6325 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6326 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6327 | res = _PyUnicode_New(sz); |
| 6328 | if (res == NULL) |
| 6329 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6330 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6331 | /* Catenate everything. */ |
| 6332 | res_p = PyUnicode_AS_UNICODE(res); |
| 6333 | for (i = 0; i < seqlen; ++i) { |
| 6334 | Py_ssize_t itemlen; |
| 6335 | item = items[i]; |
| 6336 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6337 | /* Copy item, and maybe the separator. */ |
| 6338 | if (i) { |
| 6339 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6340 | res_p += seplen; |
| 6341 | } |
| 6342 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6343 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6344 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6345 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6346 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6347 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6348 | return (PyObject *)res; |
| 6349 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6350 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6351 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6352 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | return NULL; |
| 6354 | } |
| 6355 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6356 | static |
| 6357 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6358 | Py_ssize_t left, |
| 6359 | Py_ssize_t right, |
| 6360 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6361 | { |
| 6362 | PyUnicodeObject *u; |
| 6363 | |
| 6364 | if (left < 0) |
| 6365 | left = 0; |
| 6366 | if (right < 0) |
| 6367 | right = 0; |
| 6368 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6369 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6370 | Py_INCREF(self); |
| 6371 | return self; |
| 6372 | } |
| 6373 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6374 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6375 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6376 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6377 | return NULL; |
| 6378 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6379 | u = _PyUnicode_New(left + self->length + right); |
| 6380 | if (u) { |
| 6381 | if (left) |
| 6382 | Py_UNICODE_FILL(u->str, fill, left); |
| 6383 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6384 | if (right) |
| 6385 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6386 | } |
| 6387 | |
| 6388 | return u; |
| 6389 | } |
| 6390 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6391 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6392 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6393 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | |
| 6395 | string = PyUnicode_FromObject(string); |
| 6396 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6397 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6398 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6399 | list = stringlib_splitlines( |
| 6400 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6401 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6402 | |
| 6403 | Py_DECREF(string); |
| 6404 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6407 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6409 | PyUnicodeObject *substring, |
| 6410 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6411 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6413 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6415 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6416 | return stringlib_split_whitespace( |
| 6417 | (PyObject*) self, self->str, self->length, maxcount |
| 6418 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6419 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6420 | return stringlib_split( |
| 6421 | (PyObject*) self, self->str, self->length, |
| 6422 | substring->str, substring->length, |
| 6423 | maxcount |
| 6424 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | } |
| 6426 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6427 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6428 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6429 | PyUnicodeObject *substring, |
| 6430 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6431 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6432 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6433 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6434 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6435 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6436 | return stringlib_rsplit_whitespace( |
| 6437 | (PyObject*) self, self->str, self->length, maxcount |
| 6438 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6439 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6440 | return stringlib_rsplit( |
| 6441 | (PyObject*) self, self->str, self->length, |
| 6442 | substring->str, substring->length, |
| 6443 | maxcount |
| 6444 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6445 | } |
| 6446 | |
| 6447 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6449 | PyUnicodeObject *str1, |
| 6450 | PyUnicodeObject *str2, |
| 6451 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | { |
| 6453 | PyUnicodeObject *u; |
| 6454 | |
| 6455 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6456 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6457 | else if (maxcount == 0 || self->length == 0) |
| 6458 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6459 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6460 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6461 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6462 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6463 | if (str1->length == 0) |
| 6464 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6465 | if (str1->length == 1) { |
| 6466 | /* replace characters */ |
| 6467 | Py_UNICODE u1, u2; |
| 6468 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6469 | goto nothing; |
| 6470 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6471 | if (!u) |
| 6472 | return NULL; |
| 6473 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6474 | u1 = str1->str[0]; |
| 6475 | u2 = str2->str[0]; |
| 6476 | for (i = 0; i < u->length; i++) |
| 6477 | if (u->str[i] == u1) { |
| 6478 | if (--maxcount < 0) |
| 6479 | break; |
| 6480 | u->str[i] = u2; |
| 6481 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6483 | i = stringlib_find( |
| 6484 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6485 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6486 | if (i < 0) |
| 6487 | goto nothing; |
| 6488 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6489 | if (!u) |
| 6490 | return NULL; |
| 6491 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6492 | |
| 6493 | /* change everything in-place, starting with this one */ |
| 6494 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6495 | i += str1->length; |
| 6496 | |
| 6497 | while ( --maxcount > 0) { |
| 6498 | i = stringlib_find(self->str+i, self->length-i, |
| 6499 | str1->str, str1->length, |
| 6500 | i); |
| 6501 | if (i == -1) |
| 6502 | break; |
| 6503 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6504 | i += str1->length; |
| 6505 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6506 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6507 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6508 | |
| 6509 | Py_ssize_t n, i, j, e; |
| 6510 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6511 | Py_UNICODE *p; |
| 6512 | |
| 6513 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6514 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6515 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6516 | if (n == 0) |
| 6517 | goto nothing; |
| 6518 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6519 | delta = (str2->length - str1->length); |
| 6520 | if (delta == 0) { |
| 6521 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6522 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6523 | product = n * (str2->length - str1->length); |
| 6524 | if ((product / (str2->length - str1->length)) != n) { |
| 6525 | PyErr_SetString(PyExc_OverflowError, |
| 6526 | "replace string is too long"); |
| 6527 | return NULL; |
| 6528 | } |
| 6529 | new_size = self->length + product; |
| 6530 | if (new_size < 0) { |
| 6531 | PyErr_SetString(PyExc_OverflowError, |
| 6532 | "replace string is too long"); |
| 6533 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6534 | } |
| 6535 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6536 | u = _PyUnicode_New(new_size); |
| 6537 | if (!u) |
| 6538 | return NULL; |
| 6539 | i = 0; |
| 6540 | p = u->str; |
| 6541 | e = self->length - str1->length; |
| 6542 | if (str1->length > 0) { |
| 6543 | while (n-- > 0) { |
| 6544 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6545 | j = stringlib_find(self->str+i, self->length-i, |
| 6546 | str1->str, str1->length, |
| 6547 | i); |
| 6548 | if (j == -1) |
| 6549 | break; |
| 6550 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6551 | /* copy unchanged part [i:j] */ |
| 6552 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6553 | p += j - i; |
| 6554 | } |
| 6555 | /* copy substitution string */ |
| 6556 | if (str2->length > 0) { |
| 6557 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6558 | p += str2->length; |
| 6559 | } |
| 6560 | i = j + str1->length; |
| 6561 | } |
| 6562 | if (i < self->length) |
| 6563 | /* copy tail [i:] */ |
| 6564 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6565 | } else { |
| 6566 | /* interleave */ |
| 6567 | while (n > 0) { |
| 6568 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6569 | p += str2->length; |
| 6570 | if (--n <= 0) |
| 6571 | break; |
| 6572 | *p++ = self->str[i++]; |
| 6573 | } |
| 6574 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6575 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6576 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6578 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6579 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6580 | /* nothing to replace; return original string (when possible) */ |
| 6581 | if (PyUnicode_CheckExact(self)) { |
| 6582 | Py_INCREF(self); |
| 6583 | return (PyObject *) self; |
| 6584 | } |
| 6585 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | } |
| 6587 | |
| 6588 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6589 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6590 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6592 | \n\ |
| 6593 | 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] | 6594 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6595 | |
| 6596 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6597 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6599 | return fixup(self, fixtitle); |
| 6600 | } |
| 6601 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6602 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6603 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | \n\ |
| 6605 | 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] | 6606 | have upper case."); |
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_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6610 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6611 | return fixup(self, fixcapitalize); |
| 6612 | } |
| 6613 | |
| 6614 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6615 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6616 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6617 | \n\ |
| 6618 | 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] | 6619 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6620 | |
| 6621 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6622 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6623 | { |
| 6624 | PyObject *list; |
| 6625 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6626 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6628 | /* Split into words */ |
| 6629 | list = split(self, NULL, -1); |
| 6630 | if (!list) |
| 6631 | return NULL; |
| 6632 | |
| 6633 | /* Capitalize each word */ |
| 6634 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6635 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6636 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6637 | if (item == NULL) |
| 6638 | goto onError; |
| 6639 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6640 | PyList_SET_ITEM(list, i, item); |
| 6641 | } |
| 6642 | |
| 6643 | /* Join the words to form a new string */ |
| 6644 | item = PyUnicode_Join(NULL, list); |
| 6645 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6646 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6647 | Py_DECREF(list); |
| 6648 | return (PyObject *)item; |
| 6649 | } |
| 6650 | #endif |
| 6651 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6652 | /* Argument converter. Coerces to a single unicode character */ |
| 6653 | |
| 6654 | static int |
| 6655 | convert_uc(PyObject *obj, void *addr) |
| 6656 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6657 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6658 | PyObject *uniobj; |
| 6659 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6660 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6661 | uniobj = PyUnicode_FromObject(obj); |
| 6662 | if (uniobj == NULL) { |
| 6663 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6664 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6665 | return 0; |
| 6666 | } |
| 6667 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6668 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6669 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6670 | Py_DECREF(uniobj); |
| 6671 | return 0; |
| 6672 | } |
| 6673 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6674 | *fillcharloc = unistr[0]; |
| 6675 | Py_DECREF(uniobj); |
| 6676 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6677 | } |
| 6678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6679 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6680 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6682 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6683 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | |
| 6685 | static PyObject * |
| 6686 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6687 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6688 | Py_ssize_t marg, left; |
| 6689 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6690 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6692 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6693 | return NULL; |
| 6694 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6695 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | Py_INCREF(self); |
| 6697 | return (PyObject*) self; |
| 6698 | } |
| 6699 | |
| 6700 | marg = width - self->length; |
| 6701 | left = marg / 2 + (marg & width & 1); |
| 6702 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6703 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6704 | } |
| 6705 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6706 | #if 0 |
| 6707 | |
| 6708 | /* This code should go into some future Unicode collation support |
| 6709 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6710 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6711 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6712 | /* speedy UTF-16 code point order comparison */ |
| 6713 | /* gleaned from: */ |
| 6714 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6715 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6716 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6717 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6718 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6719 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6720 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6721 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6722 | }; |
| 6723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6724 | static int |
| 6725 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6726 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6727 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6728 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | Py_UNICODE *s1 = str1->str; |
| 6730 | Py_UNICODE *s2 = str2->str; |
| 6731 | |
| 6732 | len1 = str1->length; |
| 6733 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6736 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6737 | |
| 6738 | c1 = *s1++; |
| 6739 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6740 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6741 | if (c1 > (1<<11) * 26) |
| 6742 | c1 += utf16Fixup[c1>>11]; |
| 6743 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6744 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6745 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6746 | |
| 6747 | if (c1 != c2) |
| 6748 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6749 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6750 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
| 6753 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6754 | } |
| 6755 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6756 | #else |
| 6757 | |
| 6758 | static int |
| 6759 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6760 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6761 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6762 | |
| 6763 | Py_UNICODE *s1 = str1->str; |
| 6764 | Py_UNICODE *s2 = str2->str; |
| 6765 | |
| 6766 | len1 = str1->length; |
| 6767 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6768 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6769 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6770 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6771 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6772 | c1 = *s1++; |
| 6773 | c2 = *s2++; |
| 6774 | |
| 6775 | if (c1 != c2) |
| 6776 | return (c1 < c2) ? -1 : 1; |
| 6777 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6778 | len1--; len2--; |
| 6779 | } |
| 6780 | |
| 6781 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6782 | } |
| 6783 | |
| 6784 | #endif |
| 6785 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6786 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6787 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6789 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6790 | return unicode_compare((PyUnicodeObject *)left, |
| 6791 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6792 | PyErr_Format(PyExc_TypeError, |
| 6793 | "Can't compare %.100s and %.100s", |
| 6794 | left->ob_type->tp_name, |
| 6795 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6796 | return -1; |
| 6797 | } |
| 6798 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6799 | int |
| 6800 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6801 | { |
| 6802 | int i; |
| 6803 | Py_UNICODE *id; |
| 6804 | assert(PyUnicode_Check(uni)); |
| 6805 | id = PyUnicode_AS_UNICODE(uni); |
| 6806 | /* Compare Unicode string and source character set string */ |
| 6807 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6808 | if (id[i] != str[i]) |
| 6809 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 6810 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 6811 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 6812 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6813 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6814 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6815 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6816 | return 0; |
| 6817 | } |
| 6818 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6819 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6820 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6821 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6822 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6823 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6824 | PyObject *right, |
| 6825 | int op) |
| 6826 | { |
| 6827 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6828 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6829 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6830 | PyObject *v; |
| 6831 | if (((PyUnicodeObject *) left)->length != |
| 6832 | ((PyUnicodeObject *) right)->length) { |
| 6833 | if (op == Py_EQ) { |
| 6834 | Py_INCREF(Py_False); |
| 6835 | return Py_False; |
| 6836 | } |
| 6837 | if (op == Py_NE) { |
| 6838 | Py_INCREF(Py_True); |
| 6839 | return Py_True; |
| 6840 | } |
| 6841 | } |
| 6842 | if (left == right) |
| 6843 | result = 0; |
| 6844 | else |
| 6845 | result = unicode_compare((PyUnicodeObject *)left, |
| 6846 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6847 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6848 | /* Convert the return value to a Boolean */ |
| 6849 | switch (op) { |
| 6850 | case Py_EQ: |
| 6851 | v = TEST_COND(result == 0); |
| 6852 | break; |
| 6853 | case Py_NE: |
| 6854 | v = TEST_COND(result != 0); |
| 6855 | break; |
| 6856 | case Py_LE: |
| 6857 | v = TEST_COND(result <= 0); |
| 6858 | break; |
| 6859 | case Py_GE: |
| 6860 | v = TEST_COND(result >= 0); |
| 6861 | break; |
| 6862 | case Py_LT: |
| 6863 | v = TEST_COND(result == -1); |
| 6864 | break; |
| 6865 | case Py_GT: |
| 6866 | v = TEST_COND(result == 1); |
| 6867 | break; |
| 6868 | default: |
| 6869 | PyErr_BadArgument(); |
| 6870 | return NULL; |
| 6871 | } |
| 6872 | Py_INCREF(v); |
| 6873 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6874 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6875 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6876 | Py_INCREF(Py_NotImplemented); |
| 6877 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6878 | } |
| 6879 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6880 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6881 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6882 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6883 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6884 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6885 | |
| 6886 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6887 | sub = PyUnicode_FromObject(element); |
| 6888 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6889 | PyErr_Format(PyExc_TypeError, |
| 6890 | "'in <string>' requires string as left operand, not %s", |
| 6891 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6892 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6895 | str = PyUnicode_FromObject(container); |
| 6896 | if (!str) { |
| 6897 | Py_DECREF(sub); |
| 6898 | return -1; |
| 6899 | } |
| 6900 | |
| 6901 | result = stringlib_contains_obj(str, sub); |
| 6902 | |
| 6903 | Py_DECREF(str); |
| 6904 | Py_DECREF(sub); |
| 6905 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6906 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6909 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6910 | |
| 6911 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6912 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6913 | { |
| 6914 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6915 | |
| 6916 | /* Coerce the two arguments */ |
| 6917 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6918 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6919 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6920 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6921 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6922 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6923 | |
| 6924 | /* Shortcuts */ |
| 6925 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6926 | Py_DECREF(v); |
| 6927 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6928 | } |
| 6929 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6930 | Py_DECREF(u); |
| 6931 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6932 | } |
| 6933 | |
| 6934 | /* Concat the two Unicode strings */ |
| 6935 | w = _PyUnicode_New(u->length + v->length); |
| 6936 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6937 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6939 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6940 | |
| 6941 | Py_DECREF(u); |
| 6942 | Py_DECREF(v); |
| 6943 | return (PyObject *)w; |
| 6944 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6945 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | Py_XDECREF(u); |
| 6947 | Py_XDECREF(v); |
| 6948 | return NULL; |
| 6949 | } |
| 6950 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6951 | void |
| 6952 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6953 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6954 | PyObject *new; |
| 6955 | if (*pleft == NULL) |
| 6956 | return; |
| 6957 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6958 | Py_DECREF(*pleft); |
| 6959 | *pleft = NULL; |
| 6960 | return; |
| 6961 | } |
| 6962 | new = PyUnicode_Concat(*pleft, right); |
| 6963 | Py_DECREF(*pleft); |
| 6964 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6965 | } |
| 6966 | |
| 6967 | void |
| 6968 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6969 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6970 | PyUnicode_Append(pleft, right); |
| 6971 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6972 | } |
| 6973 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6974 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6975 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6976 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6977 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6978 | 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] | 6979 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6980 | |
| 6981 | static PyObject * |
| 6982 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6983 | { |
| 6984 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6985 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6986 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | PyObject *result; |
| 6988 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6989 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6990 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6991 | return NULL; |
| 6992 | |
| 6993 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6994 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6995 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6996 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6997 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6998 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6999 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7000 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7001 | substring->str, substring->length, |
| 7002 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7003 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | |
| 7005 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7006 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7007 | return result; |
| 7008 | } |
| 7009 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7010 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7011 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7012 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7013 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7014 | 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] | 7015 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7016 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7017 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7018 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7019 | |
| 7020 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7021 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7022 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7023 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7024 | char *encoding = NULL; |
| 7025 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7026 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7027 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7028 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7029 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7030 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7031 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7032 | if (v == NULL) |
| 7033 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7034 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7035 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7036 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7037 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7038 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7039 | Py_DECREF(v); |
| 7040 | return NULL; |
| 7041 | } |
| 7042 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7043 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7044 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7045 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7046 | } |
| 7047 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7048 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7049 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | \n\ |
| 7051 | 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] | 7052 | 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] | 7053 | |
| 7054 | static PyObject* |
| 7055 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7056 | { |
| 7057 | Py_UNICODE *e; |
| 7058 | Py_UNICODE *p; |
| 7059 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7060 | Py_UNICODE *qe; |
| 7061 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | PyUnicodeObject *u; |
| 7063 | int tabsize = 8; |
| 7064 | |
| 7065 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7066 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7067 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7068 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7069 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7070 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7071 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7072 | for (p = self->str; p < e; p++) |
| 7073 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7074 | if (tabsize > 0) { |
| 7075 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7076 | if (j > PY_SSIZE_T_MAX - incr) |
| 7077 | goto overflow1; |
| 7078 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7079 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7080 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7081 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7082 | if (j > PY_SSIZE_T_MAX - 1) |
| 7083 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7084 | j++; |
| 7085 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7086 | if (i > PY_SSIZE_T_MAX - j) |
| 7087 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7089 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7090 | } |
| 7091 | } |
| 7092 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7093 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7094 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7096 | /* Second pass: create output string and fill it */ |
| 7097 | u = _PyUnicode_New(i + j); |
| 7098 | if (!u) |
| 7099 | return NULL; |
| 7100 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7101 | j = 0; /* same as in first pass */ |
| 7102 | q = u->str; /* next output char */ |
| 7103 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7104 | |
| 7105 | for (p = self->str; p < e; p++) |
| 7106 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7107 | if (tabsize > 0) { |
| 7108 | i = tabsize - (j % tabsize); |
| 7109 | j += i; |
| 7110 | while (i--) { |
| 7111 | if (q >= qe) |
| 7112 | goto overflow2; |
| 7113 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7114 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7115 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7116 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7117 | else { |
| 7118 | if (q >= qe) |
| 7119 | goto overflow2; |
| 7120 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7121 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | if (*p == '\n' || *p == '\r') |
| 7123 | j = 0; |
| 7124 | } |
| 7125 | |
| 7126 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7127 | |
| 7128 | overflow2: |
| 7129 | Py_DECREF(u); |
| 7130 | overflow1: |
| 7131 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7132 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7135 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7136 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7137 | \n\ |
| 7138 | 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] | 7139 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7140 | arguments start and end are interpreted as in slice notation.\n\ |
| 7141 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7142 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | |
| 7144 | static PyObject * |
| 7145 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7146 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7147 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7148 | Py_ssize_t start; |
| 7149 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7150 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7151 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7152 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7155 | result = stringlib_find_slice( |
| 7156 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7157 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7158 | start, end |
| 7159 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7160 | |
| 7161 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7162 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7163 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7164 | } |
| 7165 | |
| 7166 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7167 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7168 | { |
| 7169 | if (index < 0 || index >= self->length) { |
| 7170 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7171 | return NULL; |
| 7172 | } |
| 7173 | |
| 7174 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7175 | } |
| 7176 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7177 | /* Believe it or not, this produces the same value for ASCII strings |
| 7178 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7179 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7180 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7181 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7182 | Py_ssize_t len; |
| 7183 | Py_UNICODE *p; |
| 7184 | long x; |
| 7185 | |
| 7186 | if (self->hash != -1) |
| 7187 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7188 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7189 | p = self->str; |
| 7190 | x = *p << 7; |
| 7191 | while (--len >= 0) |
| 7192 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7193 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7194 | if (x == -1) |
| 7195 | x = -2; |
| 7196 | self->hash = x; |
| 7197 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7198 | } |
| 7199 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7200 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7201 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7202 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7203 | 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] | 7204 | |
| 7205 | static PyObject * |
| 7206 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7207 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7208 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7209 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7210 | Py_ssize_t start; |
| 7211 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7212 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7213 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7214 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7215 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7216 | result = stringlib_find_slice( |
| 7217 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7218 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7219 | start, end |
| 7220 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7221 | |
| 7222 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7224 | if (result < 0) { |
| 7225 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7226 | return NULL; |
| 7227 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7228 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7229 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7230 | } |
| 7231 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7232 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7233 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7234 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7235 | 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] | 7236 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7237 | |
| 7238 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7239 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | { |
| 7241 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7242 | register const Py_UNICODE *e; |
| 7243 | int cased; |
| 7244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7245 | /* Shortcut for single character strings */ |
| 7246 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7247 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7248 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7249 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7250 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7251 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7253 | e = p + PyUnicode_GET_SIZE(self); |
| 7254 | cased = 0; |
| 7255 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7256 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7257 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7258 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7259 | return PyBool_FromLong(0); |
| 7260 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7261 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7262 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7263 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7264 | } |
| 7265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7266 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7267 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7268 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7269 | 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] | 7270 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7271 | |
| 7272 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7273 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7274 | { |
| 7275 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7276 | register const Py_UNICODE *e; |
| 7277 | int cased; |
| 7278 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7279 | /* Shortcut for single character strings */ |
| 7280 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7281 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7282 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7283 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7284 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7285 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7286 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7287 | e = p + PyUnicode_GET_SIZE(self); |
| 7288 | cased = 0; |
| 7289 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7290 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7291 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7292 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7293 | return PyBool_FromLong(0); |
| 7294 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7295 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7296 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7297 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | } |
| 7299 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7300 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7301 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7302 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7303 | Return True if S is a titlecased string and there is at least one\n\ |
| 7304 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7305 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7306 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7307 | |
| 7308 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7309 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7310 | { |
| 7311 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7312 | register const Py_UNICODE *e; |
| 7313 | int cased, previous_is_cased; |
| 7314 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7315 | /* Shortcut for single character strings */ |
| 7316 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7317 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7318 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7319 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7320 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7321 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7322 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7323 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7324 | e = p + PyUnicode_GET_SIZE(self); |
| 7325 | cased = 0; |
| 7326 | previous_is_cased = 0; |
| 7327 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7328 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7329 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7330 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7331 | if (previous_is_cased) |
| 7332 | return PyBool_FromLong(0); |
| 7333 | previous_is_cased = 1; |
| 7334 | cased = 1; |
| 7335 | } |
| 7336 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7337 | if (!previous_is_cased) |
| 7338 | return PyBool_FromLong(0); |
| 7339 | previous_is_cased = 1; |
| 7340 | cased = 1; |
| 7341 | } |
| 7342 | else |
| 7343 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7344 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7345 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7346 | } |
| 7347 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7348 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7349 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7350 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7351 | Return True if all characters in S are whitespace\n\ |
| 7352 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7353 | |
| 7354 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7355 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7356 | { |
| 7357 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7358 | register const Py_UNICODE *e; |
| 7359 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7360 | /* Shortcut for single character strings */ |
| 7361 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7362 | Py_UNICODE_ISSPACE(*p)) |
| 7363 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7364 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7365 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7366 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7367 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7368 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7369 | e = p + PyUnicode_GET_SIZE(self); |
| 7370 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7371 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7372 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7373 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7374 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7377 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7378 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7379 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7380 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7381 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7382 | |
| 7383 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7384 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7385 | { |
| 7386 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7387 | register const Py_UNICODE *e; |
| 7388 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7389 | /* Shortcut for single character strings */ |
| 7390 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7391 | Py_UNICODE_ISALPHA(*p)) |
| 7392 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7393 | |
| 7394 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7395 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7396 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7397 | |
| 7398 | e = p + PyUnicode_GET_SIZE(self); |
| 7399 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7400 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7401 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7402 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7403 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7404 | } |
| 7405 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7406 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7407 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7408 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7409 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7410 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7411 | |
| 7412 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7413 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7414 | { |
| 7415 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7416 | register const Py_UNICODE *e; |
| 7417 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7418 | /* Shortcut for single character strings */ |
| 7419 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7420 | Py_UNICODE_ISALNUM(*p)) |
| 7421 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7422 | |
| 7423 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7424 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7425 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7426 | |
| 7427 | e = p + PyUnicode_GET_SIZE(self); |
| 7428 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7429 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7430 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7431 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7432 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7433 | } |
| 7434 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7435 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7436 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7438 | Return True if there are only decimal characters in S,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7439 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | |
| 7441 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7442 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7443 | { |
| 7444 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7445 | register const Py_UNICODE *e; |
| 7446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7447 | /* Shortcut for single character strings */ |
| 7448 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7449 | Py_UNICODE_ISDECIMAL(*p)) |
| 7450 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7451 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7452 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7453 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7454 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7455 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 | e = p + PyUnicode_GET_SIZE(self); |
| 7457 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7458 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7459 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7460 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7461 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7462 | } |
| 7463 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7464 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7465 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7466 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7467 | Return True if all characters in S are digits\n\ |
| 7468 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7469 | |
| 7470 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7471 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7472 | { |
| 7473 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7474 | register const Py_UNICODE *e; |
| 7475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | /* Shortcut for single character strings */ |
| 7477 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7478 | Py_UNICODE_ISDIGIT(*p)) |
| 7479 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7480 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7481 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7482 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7483 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7485 | e = p + PyUnicode_GET_SIZE(self); |
| 7486 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7487 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7488 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7489 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7490 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7491 | } |
| 7492 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7493 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7494 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7495 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7496 | 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] | 7497 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7498 | |
| 7499 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7500 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7501 | { |
| 7502 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7503 | register const Py_UNICODE *e; |
| 7504 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | /* Shortcut for single character strings */ |
| 7506 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7507 | Py_UNICODE_ISNUMERIC(*p)) |
| 7508 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7510 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7511 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7512 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | e = p + PyUnicode_GET_SIZE(self); |
| 7515 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7516 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7517 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7518 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7519 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7520 | } |
| 7521 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7522 | int |
| 7523 | PyUnicode_IsIdentifier(PyObject *self) |
| 7524 | { |
| 7525 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7526 | register const Py_UNICODE *e; |
| 7527 | |
| 7528 | /* Special case for empty strings */ |
| 7529 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7531 | |
| 7532 | /* PEP 3131 says that the first character must be in |
| 7533 | XID_Start and subsequent characters in XID_Continue, |
| 7534 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7535 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7536 | letters, digits, underscore). However, given the current |
| 7537 | definition of XID_Start and XID_Continue, it is sufficient |
| 7538 | to check just for these, except that _ must be allowed |
| 7539 | as starting an identifier. */ |
| 7540 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7541 | return 0; |
| 7542 | |
| 7543 | e = p + PyUnicode_GET_SIZE(self); |
| 7544 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7545 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7546 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7547 | } |
| 7548 | return 1; |
| 7549 | } |
| 7550 | |
| 7551 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7552 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7553 | \n\ |
| 7554 | Return True if S is a valid identifier according\n\ |
| 7555 | to the language definition."); |
| 7556 | |
| 7557 | static PyObject* |
| 7558 | unicode_isidentifier(PyObject *self) |
| 7559 | { |
| 7560 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7561 | } |
| 7562 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7563 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7564 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7565 | \n\ |
| 7566 | Return True if all characters in S are considered\n\ |
| 7567 | printable in repr() or S is empty, False otherwise."); |
| 7568 | |
| 7569 | static PyObject* |
| 7570 | unicode_isprintable(PyObject *self) |
| 7571 | { |
| 7572 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7573 | register const Py_UNICODE *e; |
| 7574 | |
| 7575 | /* Shortcut for single character strings */ |
| 7576 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7577 | Py_RETURN_TRUE; |
| 7578 | } |
| 7579 | |
| 7580 | e = p + PyUnicode_GET_SIZE(self); |
| 7581 | for (; p < e; p++) { |
| 7582 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7583 | Py_RETURN_FALSE; |
| 7584 | } |
| 7585 | } |
| 7586 | Py_RETURN_TRUE; |
| 7587 | } |
| 7588 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7589 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7590 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | \n\ |
| 7592 | 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] | 7593 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7594 | |
| 7595 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7596 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7597 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7598 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | } |
| 7600 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7601 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7602 | unicode_length(PyUnicodeObject *self) |
| 7603 | { |
| 7604 | return self->length; |
| 7605 | } |
| 7606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7607 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7608 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7609 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7610 | 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] | 7611 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7612 | |
| 7613 | static PyObject * |
| 7614 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7615 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7616 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7617 | Py_UNICODE fillchar = ' '; |
| 7618 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7619 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7620 | return NULL; |
| 7621 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7622 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7623 | Py_INCREF(self); |
| 7624 | return (PyObject*) self; |
| 7625 | } |
| 7626 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7627 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7628 | } |
| 7629 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7630 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7631 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7632 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7633 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7634 | |
| 7635 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7636 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7637 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7638 | return fixup(self, fixlower); |
| 7639 | } |
| 7640 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7641 | #define LEFTSTRIP 0 |
| 7642 | #define RIGHTSTRIP 1 |
| 7643 | #define BOTHSTRIP 2 |
| 7644 | |
| 7645 | /* Arrays indexed by above */ |
| 7646 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7647 | |
| 7648 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7649 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7650 | /* externally visible for str.strip(unicode) */ |
| 7651 | PyObject * |
| 7652 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7653 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7654 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7655 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7656 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7657 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7658 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7659 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7660 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7661 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7662 | i = 0; |
| 7663 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7664 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7665 | i++; |
| 7666 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7667 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7668 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7669 | j = len; |
| 7670 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7671 | do { |
| 7672 | j--; |
| 7673 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7674 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7675 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7676 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7677 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7678 | Py_INCREF(self); |
| 7679 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7680 | } |
| 7681 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7682 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7683 | } |
| 7684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7685 | |
| 7686 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7687 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7688 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7689 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7690 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7691 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7692 | i = 0; |
| 7693 | if (striptype != RIGHTSTRIP) { |
| 7694 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7695 | i++; |
| 7696 | } |
| 7697 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7698 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7699 | j = len; |
| 7700 | if (striptype != LEFTSTRIP) { |
| 7701 | do { |
| 7702 | j--; |
| 7703 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7704 | j++; |
| 7705 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7706 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7707 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7708 | Py_INCREF(self); |
| 7709 | return (PyObject*)self; |
| 7710 | } |
| 7711 | else |
| 7712 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7713 | } |
| 7714 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7715 | |
| 7716 | static PyObject * |
| 7717 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7718 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7719 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7720 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7721 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7722 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7723 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7724 | if (sep != NULL && sep != Py_None) { |
| 7725 | if (PyUnicode_Check(sep)) |
| 7726 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7727 | else { |
| 7728 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7729 | "%s arg must be None or str", |
| 7730 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7731 | return NULL; |
| 7732 | } |
| 7733 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7734 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7735 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7736 | } |
| 7737 | |
| 7738 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7739 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7740 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7741 | \n\ |
| 7742 | Return a copy of the string S with leading and trailing\n\ |
| 7743 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7744 | 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] | 7745 | |
| 7746 | static PyObject * |
| 7747 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7748 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7749 | if (PyTuple_GET_SIZE(args) == 0) |
| 7750 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7751 | else |
| 7752 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7753 | } |
| 7754 | |
| 7755 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7756 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7757 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7758 | \n\ |
| 7759 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7760 | 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] | 7761 | |
| 7762 | static PyObject * |
| 7763 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7764 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7765 | if (PyTuple_GET_SIZE(args) == 0) |
| 7766 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7767 | else |
| 7768 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7769 | } |
| 7770 | |
| 7771 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7772 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7773 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7774 | \n\ |
| 7775 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7776 | 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] | 7777 | |
| 7778 | static PyObject * |
| 7779 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7780 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7781 | if (PyTuple_GET_SIZE(args) == 0) |
| 7782 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7783 | else |
| 7784 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7785 | } |
| 7786 | |
| 7787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7788 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7789 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7790 | { |
| 7791 | PyUnicodeObject *u; |
| 7792 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7793 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7794 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7795 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7796 | if (len < 1) { |
| 7797 | Py_INCREF(unicode_empty); |
| 7798 | return (PyObject *)unicode_empty; |
| 7799 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7800 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7801 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7802 | /* no repeat, return original string */ |
| 7803 | Py_INCREF(str); |
| 7804 | return (PyObject*) str; |
| 7805 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7806 | |
| 7807 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7808 | * needed doesn't overflow size_t |
| 7809 | */ |
| 7810 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7811 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7812 | PyErr_SetString(PyExc_OverflowError, |
| 7813 | "repeated string is too long"); |
| 7814 | return NULL; |
| 7815 | } |
| 7816 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7817 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7818 | PyErr_SetString(PyExc_OverflowError, |
| 7819 | "repeated string is too long"); |
| 7820 | return NULL; |
| 7821 | } |
| 7822 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7823 | if (!u) |
| 7824 | return NULL; |
| 7825 | |
| 7826 | p = u->str; |
| 7827 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7828 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7829 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7830 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7831 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7832 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7833 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7834 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7835 | Py_UNICODE_COPY(p+done, p, n); |
| 7836 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7837 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7838 | } |
| 7839 | |
| 7840 | return (PyObject*) u; |
| 7841 | } |
| 7842 | |
| 7843 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7844 | PyObject *subobj, |
| 7845 | PyObject *replobj, |
| 7846 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7847 | { |
| 7848 | PyObject *self; |
| 7849 | PyObject *str1; |
| 7850 | PyObject *str2; |
| 7851 | PyObject *result; |
| 7852 | |
| 7853 | self = PyUnicode_FromObject(obj); |
| 7854 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7855 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7856 | str1 = PyUnicode_FromObject(subobj); |
| 7857 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7858 | Py_DECREF(self); |
| 7859 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7860 | } |
| 7861 | str2 = PyUnicode_FromObject(replobj); |
| 7862 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7863 | Py_DECREF(self); |
| 7864 | Py_DECREF(str1); |
| 7865 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7866 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7867 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7868 | (PyUnicodeObject *)str1, |
| 7869 | (PyUnicodeObject *)str2, |
| 7870 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7871 | Py_DECREF(self); |
| 7872 | Py_DECREF(str1); |
| 7873 | Py_DECREF(str2); |
| 7874 | return result; |
| 7875 | } |
| 7876 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7877 | PyDoc_STRVAR(replace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7878 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7879 | \n\ |
| 7880 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7881 | old replaced by new. If the optional argument count is\n\ |
| 7882 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7883 | |
| 7884 | static PyObject* |
| 7885 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7886 | { |
| 7887 | PyUnicodeObject *str1; |
| 7888 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7889 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7890 | PyObject *result; |
| 7891 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7892 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7893 | return NULL; |
| 7894 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7895 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7896 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7897 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7898 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7899 | Py_DECREF(str1); |
| 7900 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7901 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | |
| 7903 | result = replace(self, str1, str2, maxcount); |
| 7904 | |
| 7905 | Py_DECREF(str1); |
| 7906 | Py_DECREF(str2); |
| 7907 | return result; |
| 7908 | } |
| 7909 | |
| 7910 | static |
| 7911 | PyObject *unicode_repr(PyObject *unicode) |
| 7912 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7913 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7914 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7915 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7916 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7917 | |
| 7918 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7919 | better to choose a different scheme. Perhaps scan the |
| 7920 | first N-chars of the string and allocate based on that size. |
| 7921 | */ |
| 7922 | /* Initial allocation is based on the longest-possible unichr |
| 7923 | escape. |
| 7924 | |
| 7925 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7926 | unichr, so in this case it's the longest unichr escape. In |
| 7927 | narrow (UTF-16) builds this is five chars per source unichr |
| 7928 | since there are two unichrs in the surrogate pair, so in narrow |
| 7929 | (UTF-16) builds it's not the longest unichr escape. |
| 7930 | |
| 7931 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7932 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7933 | escape. |
| 7934 | */ |
| 7935 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7936 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7937 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7938 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7939 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7940 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7941 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7942 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7943 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7944 | if (repr == NULL) |
| 7945 | return NULL; |
| 7946 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7947 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7948 | |
| 7949 | /* Add quote */ |
| 7950 | *p++ = (findchar(s, size, '\'') && |
| 7951 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7952 | while (size-- > 0) { |
| 7953 | Py_UNICODE ch = *s++; |
| 7954 | |
| 7955 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7956 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7957 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7958 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7959 | continue; |
| 7960 | } |
| 7961 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7962 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7963 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7964 | *p++ = '\\'; |
| 7965 | *p++ = 't'; |
| 7966 | } |
| 7967 | else if (ch == '\n') { |
| 7968 | *p++ = '\\'; |
| 7969 | *p++ = 'n'; |
| 7970 | } |
| 7971 | else if (ch == '\r') { |
| 7972 | *p++ = '\\'; |
| 7973 | *p++ = 'r'; |
| 7974 | } |
| 7975 | |
| 7976 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7977 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7978 | *p++ = '\\'; |
| 7979 | *p++ = 'x'; |
| 7980 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7981 | *p++ = hexdigits[ch & 0x000F]; |
| 7982 | } |
| 7983 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7984 | /* Copy ASCII characters as-is */ |
| 7985 | else if (ch < 0x7F) { |
| 7986 | *p++ = ch; |
| 7987 | } |
| 7988 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7989 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7990 | else { |
| 7991 | Py_UCS4 ucs = ch; |
| 7992 | |
| 7993 | #ifndef Py_UNICODE_WIDE |
| 7994 | Py_UNICODE ch2 = 0; |
| 7995 | /* Get code point from surrogate pair */ |
| 7996 | if (size > 0) { |
| 7997 | ch2 = *s; |
| 7998 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7999 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8000 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8001 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8002 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8003 | size--; |
| 8004 | } |
| 8005 | } |
| 8006 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8007 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8008 | (categories Z* and C* except ASCII space) |
| 8009 | */ |
| 8010 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8011 | /* Map 8-bit characters to '\xhh' */ |
| 8012 | if (ucs <= 0xff) { |
| 8013 | *p++ = '\\'; |
| 8014 | *p++ = 'x'; |
| 8015 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8016 | *p++ = hexdigits[ch & 0x000F]; |
| 8017 | } |
| 8018 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8019 | else if (ucs >= 0x10000) { |
| 8020 | *p++ = '\\'; |
| 8021 | *p++ = 'U'; |
| 8022 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8023 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8024 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8025 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8026 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8027 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8028 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8029 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8030 | } |
| 8031 | /* Map 16-bit characters to '\uxxxx' */ |
| 8032 | else { |
| 8033 | *p++ = '\\'; |
| 8034 | *p++ = 'u'; |
| 8035 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8036 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8037 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8038 | *p++ = hexdigits[ucs & 0x000F]; |
| 8039 | } |
| 8040 | } |
| 8041 | /* Copy characters as-is */ |
| 8042 | else { |
| 8043 | *p++ = ch; |
| 8044 | #ifndef Py_UNICODE_WIDE |
| 8045 | if (ucs >= 0x10000) |
| 8046 | *p++ = ch2; |
| 8047 | #endif |
| 8048 | } |
| 8049 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8050 | } |
| 8051 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8052 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8053 | |
| 8054 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8055 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8056 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8057 | } |
| 8058 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8059 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8060 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8061 | \n\ |
| 8062 | 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] | 8063 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8064 | arguments start and end are interpreted as in slice notation.\n\ |
| 8065 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8066 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8067 | |
| 8068 | static PyObject * |
| 8069 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8070 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8071 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8072 | Py_ssize_t start; |
| 8073 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8074 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8075 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8076 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8077 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8078 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8079 | result = stringlib_rfind_slice( |
| 8080 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8081 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8082 | start, end |
| 8083 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8084 | |
| 8085 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8086 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8087 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8088 | } |
| 8089 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8090 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8091 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8092 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8093 | 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] | 8094 | |
| 8095 | static PyObject * |
| 8096 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8097 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8098 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8099 | Py_ssize_t start; |
| 8100 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8101 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8102 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8103 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8104 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8105 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8106 | result = stringlib_rfind_slice( |
| 8107 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8108 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8109 | start, end |
| 8110 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8111 | |
| 8112 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8113 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8114 | if (result < 0) { |
| 8115 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8116 | return NULL; |
| 8117 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8118 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8119 | } |
| 8120 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8121 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8124 | 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] | 8125 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | |
| 8127 | static PyObject * |
| 8128 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8129 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8130 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8131 | Py_UNICODE fillchar = ' '; |
| 8132 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8133 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8134 | return NULL; |
| 8135 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8136 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | Py_INCREF(self); |
| 8138 | return (PyObject*) self; |
| 8139 | } |
| 8140 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8141 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8142 | } |
| 8143 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8144 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8145 | PyObject *sep, |
| 8146 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8147 | { |
| 8148 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8150 | s = PyUnicode_FromObject(s); |
| 8151 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8152 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8153 | if (sep != NULL) { |
| 8154 | sep = PyUnicode_FromObject(sep); |
| 8155 | if (sep == NULL) { |
| 8156 | Py_DECREF(s); |
| 8157 | return NULL; |
| 8158 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8159 | } |
| 8160 | |
| 8161 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8162 | |
| 8163 | Py_DECREF(s); |
| 8164 | Py_XDECREF(sep); |
| 8165 | return result; |
| 8166 | } |
| 8167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8168 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8169 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8170 | \n\ |
| 8171 | Return a list of the words in S, using sep as the\n\ |
| 8172 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8173 | 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] | 8174 | whitespace string is a separator and empty strings are\n\ |
| 8175 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8176 | |
| 8177 | static PyObject* |
| 8178 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8179 | { |
| 8180 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8181 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8182 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8183 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8184 | return NULL; |
| 8185 | |
| 8186 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8187 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8188 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8189 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8190 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8191 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8192 | } |
| 8193 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8194 | PyObject * |
| 8195 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8196 | { |
| 8197 | PyObject* str_obj; |
| 8198 | PyObject* sep_obj; |
| 8199 | PyObject* out; |
| 8200 | |
| 8201 | str_obj = PyUnicode_FromObject(str_in); |
| 8202 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8203 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8204 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8205 | if (!sep_obj) { |
| 8206 | Py_DECREF(str_obj); |
| 8207 | return NULL; |
| 8208 | } |
| 8209 | |
| 8210 | out = stringlib_partition( |
| 8211 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8212 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8213 | ); |
| 8214 | |
| 8215 | Py_DECREF(sep_obj); |
| 8216 | Py_DECREF(str_obj); |
| 8217 | |
| 8218 | return out; |
| 8219 | } |
| 8220 | |
| 8221 | |
| 8222 | PyObject * |
| 8223 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8224 | { |
| 8225 | PyObject* str_obj; |
| 8226 | PyObject* sep_obj; |
| 8227 | PyObject* out; |
| 8228 | |
| 8229 | str_obj = PyUnicode_FromObject(str_in); |
| 8230 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8231 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8232 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8233 | if (!sep_obj) { |
| 8234 | Py_DECREF(str_obj); |
| 8235 | return NULL; |
| 8236 | } |
| 8237 | |
| 8238 | out = stringlib_rpartition( |
| 8239 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8240 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8241 | ); |
| 8242 | |
| 8243 | Py_DECREF(sep_obj); |
| 8244 | Py_DECREF(str_obj); |
| 8245 | |
| 8246 | return out; |
| 8247 | } |
| 8248 | |
| 8249 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8250 | "S.partition(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, and return the part before it,\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8253 | 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] | 8254 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8255 | |
| 8256 | static PyObject* |
| 8257 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8258 | { |
| 8259 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8260 | } |
| 8261 | |
| 8262 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8263 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8264 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8265 | 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] | 8266 | 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] | 8267 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8268 | |
| 8269 | static PyObject* |
| 8270 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8271 | { |
| 8272 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8273 | } |
| 8274 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8275 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | PyObject *sep, |
| 8277 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8278 | { |
| 8279 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8280 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8281 | s = PyUnicode_FromObject(s); |
| 8282 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8283 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8284 | if (sep != NULL) { |
| 8285 | sep = PyUnicode_FromObject(sep); |
| 8286 | if (sep == NULL) { |
| 8287 | Py_DECREF(s); |
| 8288 | return NULL; |
| 8289 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8290 | } |
| 8291 | |
| 8292 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8293 | |
| 8294 | Py_DECREF(s); |
| 8295 | Py_XDECREF(sep); |
| 8296 | return result; |
| 8297 | } |
| 8298 | |
| 8299 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8300 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8301 | \n\ |
| 8302 | Return a list of the words in S, using sep as the\n\ |
| 8303 | delimiter string, starting at the end of the string and\n\ |
| 8304 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8305 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8306 | is a separator."); |
| 8307 | |
| 8308 | static PyObject* |
| 8309 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8310 | { |
| 8311 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8312 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8313 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8314 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8315 | return NULL; |
| 8316 | |
| 8317 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8318 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8319 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8320 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8321 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8322 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8323 | } |
| 8324 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8325 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8326 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | \n\ |
| 8328 | 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] | 8329 | 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] | 8330 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | |
| 8332 | static PyObject* |
| 8333 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8334 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8335 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8336 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8337 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8338 | return NULL; |
| 8339 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8340 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8341 | } |
| 8342 | |
| 8343 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8344 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8345 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8346 | if (PyUnicode_CheckExact(self)) { |
| 8347 | Py_INCREF(self); |
| 8348 | return self; |
| 8349 | } else |
| 8350 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8351 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8352 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | } |
| 8354 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8355 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8356 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8357 | \n\ |
| 8358 | 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] | 8359 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8360 | |
| 8361 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8362 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8363 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8364 | return fixup(self, fixswapcase); |
| 8365 | } |
| 8366 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8367 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8368 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8369 | \n\ |
| 8370 | Return a translation table usable for str.translate().\n\ |
| 8371 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8372 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8373 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8374 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8375 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8376 | character at the same position in y. If there is a third argument, it\n\ |
| 8377 | must be a string, whose characters will be mapped to None in the result."); |
| 8378 | |
| 8379 | static PyObject* |
| 8380 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8381 | { |
| 8382 | PyObject *x, *y = NULL, *z = NULL; |
| 8383 | PyObject *new = NULL, *key, *value; |
| 8384 | Py_ssize_t i = 0; |
| 8385 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8386 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8387 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8388 | return NULL; |
| 8389 | new = PyDict_New(); |
| 8390 | if (!new) |
| 8391 | return NULL; |
| 8392 | if (y != NULL) { |
| 8393 | /* x must be a string too, of equal length */ |
| 8394 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8395 | if (!PyUnicode_Check(x)) { |
| 8396 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8397 | "be a string if there is a second argument"); |
| 8398 | goto err; |
| 8399 | } |
| 8400 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8401 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8402 | "arguments must have equal length"); |
| 8403 | goto err; |
| 8404 | } |
| 8405 | /* create entries for translating chars in x to those in y */ |
| 8406 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8407 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8408 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8409 | if (!key || !value) |
| 8410 | goto err; |
| 8411 | res = PyDict_SetItem(new, key, value); |
| 8412 | Py_DECREF(key); |
| 8413 | Py_DECREF(value); |
| 8414 | if (res < 0) |
| 8415 | goto err; |
| 8416 | } |
| 8417 | /* create entries for deleting chars in z */ |
| 8418 | if (z != NULL) { |
| 8419 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8420 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8421 | if (!key) |
| 8422 | goto err; |
| 8423 | res = PyDict_SetItem(new, key, Py_None); |
| 8424 | Py_DECREF(key); |
| 8425 | if (res < 0) |
| 8426 | goto err; |
| 8427 | } |
| 8428 | } |
| 8429 | } else { |
| 8430 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8431 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8432 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8433 | "to maketrans it must be a dict"); |
| 8434 | goto err; |
| 8435 | } |
| 8436 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8437 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8438 | if (PyUnicode_Check(key)) { |
| 8439 | /* convert string keys to integer keys */ |
| 8440 | PyObject *newkey; |
| 8441 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8442 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8443 | "table must be of length 1"); |
| 8444 | goto err; |
| 8445 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8446 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8447 | if (!newkey) |
| 8448 | goto err; |
| 8449 | res = PyDict_SetItem(new, newkey, value); |
| 8450 | Py_DECREF(newkey); |
| 8451 | if (res < 0) |
| 8452 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8453 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8454 | /* just keep integer keys */ |
| 8455 | if (PyDict_SetItem(new, key, value) < 0) |
| 8456 | goto err; |
| 8457 | } else { |
| 8458 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8459 | "be strings or integers"); |
| 8460 | goto err; |
| 8461 | } |
| 8462 | } |
| 8463 | } |
| 8464 | return new; |
| 8465 | err: |
| 8466 | Py_DECREF(new); |
| 8467 | return NULL; |
| 8468 | } |
| 8469 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8470 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8471 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8472 | \n\ |
| 8473 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8474 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8475 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8476 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8477 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8478 | |
| 8479 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8480 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8481 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8482 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8483 | } |
| 8484 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8485 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8486 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8487 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8488 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8489 | |
| 8490 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8491 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8492 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8493 | return fixup(self, fixupper); |
| 8494 | } |
| 8495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8496 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8497 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8498 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8499 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8500 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8501 | |
| 8502 | static PyObject * |
| 8503 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8504 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8505 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8506 | PyUnicodeObject *u; |
| 8507 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8508 | Py_ssize_t width; |
| 8509 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8510 | return NULL; |
| 8511 | |
| 8512 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8513 | if (PyUnicode_CheckExact(self)) { |
| 8514 | Py_INCREF(self); |
| 8515 | return (PyObject*) self; |
| 8516 | } |
| 8517 | else |
| 8518 | return PyUnicode_FromUnicode( |
| 8519 | PyUnicode_AS_UNICODE(self), |
| 8520 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8521 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8522 | } |
| 8523 | |
| 8524 | fill = width - self->length; |
| 8525 | |
| 8526 | u = pad(self, fill, 0, '0'); |
| 8527 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8528 | if (u == NULL) |
| 8529 | return NULL; |
| 8530 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8531 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8532 | /* move sign to beginning of string */ |
| 8533 | u->str[0] = u->str[fill]; |
| 8534 | u->str[fill] = '0'; |
| 8535 | } |
| 8536 | |
| 8537 | return (PyObject*) u; |
| 8538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8539 | |
| 8540 | #if 0 |
| 8541 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8542 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8544 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8545 | } |
| 8546 | #endif |
| 8547 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8548 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8549 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8550 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8551 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8552 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8553 | With optional end, stop comparing S at that position.\n\ |
| 8554 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8555 | |
| 8556 | static PyObject * |
| 8557 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8558 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8559 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8560 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8561 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8562 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8563 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8564 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8565 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8566 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8567 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8568 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8569 | if (PyTuple_Check(subobj)) { |
| 8570 | Py_ssize_t i; |
| 8571 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8572 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8573 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8574 | if (substring == NULL) |
| 8575 | return NULL; |
| 8576 | result = tailmatch(self, substring, start, end, -1); |
| 8577 | Py_DECREF(substring); |
| 8578 | if (result) { |
| 8579 | Py_RETURN_TRUE; |
| 8580 | } |
| 8581 | } |
| 8582 | /* nothing matched */ |
| 8583 | Py_RETURN_FALSE; |
| 8584 | } |
| 8585 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8586 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8587 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8588 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8589 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8590 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8591 | } |
| 8592 | |
| 8593 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8594 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8595 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8596 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8597 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8598 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8599 | With optional end, stop comparing S at that position.\n\ |
| 8600 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8601 | |
| 8602 | static PyObject * |
| 8603 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8604 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8605 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8606 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8607 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8608 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8609 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8610 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8611 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8612 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8613 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8614 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8615 | if (PyTuple_Check(subobj)) { |
| 8616 | Py_ssize_t i; |
| 8617 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8618 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8619 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8620 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8621 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8622 | result = tailmatch(self, substring, start, end, +1); |
| 8623 | Py_DECREF(substring); |
| 8624 | if (result) { |
| 8625 | Py_RETURN_TRUE; |
| 8626 | } |
| 8627 | } |
| 8628 | Py_RETURN_FALSE; |
| 8629 | } |
| 8630 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8631 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8632 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8634 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8635 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8636 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8637 | } |
| 8638 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8639 | #include "stringlib/string_format.h" |
| 8640 | |
| 8641 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8642 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8643 | \n\ |
| 8644 | "); |
| 8645 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8646 | static PyObject * |
| 8647 | unicode__format__(PyObject* self, PyObject* args) |
| 8648 | { |
| 8649 | PyObject *format_spec; |
| 8650 | |
| 8651 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8652 | return NULL; |
| 8653 | |
| 8654 | return _PyUnicode_FormatAdvanced(self, |
| 8655 | PyUnicode_AS_UNICODE(format_spec), |
| 8656 | PyUnicode_GET_SIZE(format_spec)); |
| 8657 | } |
| 8658 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8659 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8660 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8661 | \n\ |
| 8662 | "); |
| 8663 | |
| 8664 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8665 | unicode__sizeof__(PyUnicodeObject *v) |
| 8666 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8667 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8668 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8669 | } |
| 8670 | |
| 8671 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8672 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8673 | |
| 8674 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8675 | unicode_getnewargs(PyUnicodeObject *v) |
| 8676 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8677 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8678 | } |
| 8679 | |
| 8680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8681 | static PyMethodDef unicode_methods[] = { |
| 8682 | |
| 8683 | /* Order is according to common usage: often used methods should |
| 8684 | appear first, since lookup is done sequentially. */ |
| 8685 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8686 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8687 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8688 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8689 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8690 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8691 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8692 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8693 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8694 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8695 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8696 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8697 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8698 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8699 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8700 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8701 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8702 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8703 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8704 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8705 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8706 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8707 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8708 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8709 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8710 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8711 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8712 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8713 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8714 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8715 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8716 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8717 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8718 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8719 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8720 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8721 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8722 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8723 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8724 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8725 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8726 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8727 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8728 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8729 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8730 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8731 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8732 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8733 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8734 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8735 | #endif |
| 8736 | |
| 8737 | #if 0 |
| 8738 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8739 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8740 | #endif |
| 8741 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8742 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8743 | {NULL, NULL} |
| 8744 | }; |
| 8745 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8746 | static PyObject * |
| 8747 | unicode_mod(PyObject *v, PyObject *w) |
| 8748 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8749 | if (!PyUnicode_Check(v)) { |
| 8750 | Py_INCREF(Py_NotImplemented); |
| 8751 | return Py_NotImplemented; |
| 8752 | } |
| 8753 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8754 | } |
| 8755 | |
| 8756 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8757 | 0, /*nb_add*/ |
| 8758 | 0, /*nb_subtract*/ |
| 8759 | 0, /*nb_multiply*/ |
| 8760 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8761 | }; |
| 8762 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8763 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8764 | (lenfunc) unicode_length, /* sq_length */ |
| 8765 | PyUnicode_Concat, /* sq_concat */ |
| 8766 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8767 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8768 | 0, /* sq_slice */ |
| 8769 | 0, /* sq_ass_item */ |
| 8770 | 0, /* sq_ass_slice */ |
| 8771 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8772 | }; |
| 8773 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8774 | static PyObject* |
| 8775 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8776 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8777 | if (PyIndex_Check(item)) { |
| 8778 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8779 | if (i == -1 && PyErr_Occurred()) |
| 8780 | return NULL; |
| 8781 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8782 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8783 | return unicode_getitem(self, i); |
| 8784 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8785 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8786 | Py_UNICODE* source_buf; |
| 8787 | Py_UNICODE* result_buf; |
| 8788 | PyObject* result; |
| 8789 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8790 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8791 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8792 | return NULL; |
| 8793 | } |
| 8794 | |
| 8795 | if (slicelength <= 0) { |
| 8796 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8797 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8798 | PyUnicode_CheckExact(self)) { |
| 8799 | Py_INCREF(self); |
| 8800 | return (PyObject *)self; |
| 8801 | } else if (step == 1) { |
| 8802 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8803 | } else { |
| 8804 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8805 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8806 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8807 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8808 | if (result_buf == NULL) |
| 8809 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8810 | |
| 8811 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8812 | result_buf[i] = source_buf[cur]; |
| 8813 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8814 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8815 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8816 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8817 | return result; |
| 8818 | } |
| 8819 | } else { |
| 8820 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8821 | return NULL; |
| 8822 | } |
| 8823 | } |
| 8824 | |
| 8825 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8826 | (lenfunc)unicode_length, /* mp_length */ |
| 8827 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8828 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8829 | }; |
| 8830 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8831 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8832 | /* Helpers for PyUnicode_Format() */ |
| 8833 | |
| 8834 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8835 | 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] | 8836 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8837 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8838 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8839 | (*p_argidx)++; |
| 8840 | if (arglen < 0) |
| 8841 | return args; |
| 8842 | else |
| 8843 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8844 | } |
| 8845 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8846 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8847 | return NULL; |
| 8848 | } |
| 8849 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8850 | /* 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] | 8851 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8852 | static PyObject * |
| 8853 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8854 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8855 | char *p; |
| 8856 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8857 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8858 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8859 | x = PyFloat_AsDouble(v); |
| 8860 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8861 | return NULL; |
| 8862 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8863 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8864 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8865 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8866 | p = PyOS_double_to_string(x, type, prec, |
| 8867 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8868 | if (p == NULL) |
| 8869 | return NULL; |
| 8870 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8871 | PyMem_Free(p); |
| 8872 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8873 | } |
| 8874 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8875 | static PyObject* |
| 8876 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8877 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8878 | char *buf; |
| 8879 | int len; |
| 8880 | PyObject *str; /* temporary string object. */ |
| 8881 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8882 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8883 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 8884 | if (!str) |
| 8885 | return NULL; |
| 8886 | result = PyUnicode_FromStringAndSize(buf, len); |
| 8887 | Py_DECREF(str); |
| 8888 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8889 | } |
| 8890 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8891 | static int |
| 8892 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8893 | size_t buflen, |
| 8894 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8895 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8896 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8897 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8898 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 8899 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 8900 | buf[1] = '\0'; |
| 8901 | return 1; |
| 8902 | } |
| 8903 | #ifndef Py_UNICODE_WIDE |
| 8904 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 8905 | /* Decode a valid surrogate pair */ |
| 8906 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 8907 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 8908 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 8909 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 8910 | buf[0] = c0; |
| 8911 | buf[1] = c1; |
| 8912 | buf[2] = '\0'; |
| 8913 | return 2; |
| 8914 | } |
| 8915 | } |
| 8916 | #endif |
| 8917 | goto onError; |
| 8918 | } |
| 8919 | else { |
| 8920 | /* Integer input truncated to a character */ |
| 8921 | long x; |
| 8922 | x = PyLong_AsLong(v); |
| 8923 | if (x == -1 && PyErr_Occurred()) |
| 8924 | goto onError; |
| 8925 | |
| 8926 | if (x < 0 || x > 0x10ffff) { |
| 8927 | PyErr_SetString(PyExc_OverflowError, |
| 8928 | "%c arg not in range(0x110000)"); |
| 8929 | return -1; |
| 8930 | } |
| 8931 | |
| 8932 | #ifndef Py_UNICODE_WIDE |
| 8933 | if (x > 0xffff) { |
| 8934 | x -= 0x10000; |
| 8935 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 8936 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 8937 | return 2; |
| 8938 | } |
| 8939 | #endif |
| 8940 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8941 | buf[1] = '\0'; |
| 8942 | return 1; |
| 8943 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8944 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8945 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8946 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8947 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8948 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8949 | } |
| 8950 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8951 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8952 | 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] | 8953 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8954 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8955 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8956 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8957 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8958 | { |
| 8959 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8960 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8961 | int args_owned = 0; |
| 8962 | PyUnicodeObject *result = NULL; |
| 8963 | PyObject *dict = NULL; |
| 8964 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8965 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8966 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8967 | PyErr_BadInternalCall(); |
| 8968 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8969 | } |
| 8970 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8971 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8972 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8973 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8974 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8975 | |
| 8976 | reslen = rescnt = fmtcnt + 100; |
| 8977 | result = _PyUnicode_New(reslen); |
| 8978 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8979 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8980 | res = PyUnicode_AS_UNICODE(result); |
| 8981 | |
| 8982 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8983 | arglen = PyTuple_Size(args); |
| 8984 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8985 | } |
| 8986 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8987 | arglen = -1; |
| 8988 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8989 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8990 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 8991 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8992 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | |
| 8994 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8995 | if (*fmt != '%') { |
| 8996 | if (--rescnt < 0) { |
| 8997 | rescnt = fmtcnt + 100; |
| 8998 | reslen += rescnt; |
| 8999 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9000 | goto onError; |
| 9001 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9002 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9003 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9004 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9005 | } |
| 9006 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9007 | /* Got a format specifier */ |
| 9008 | int flags = 0; |
| 9009 | Py_ssize_t width = -1; |
| 9010 | int prec = -1; |
| 9011 | Py_UNICODE c = '\0'; |
| 9012 | Py_UNICODE fill; |
| 9013 | int isnumok; |
| 9014 | PyObject *v = NULL; |
| 9015 | PyObject *temp = NULL; |
| 9016 | Py_UNICODE *pbuf; |
| 9017 | Py_UNICODE sign; |
| 9018 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9019 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9020 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9021 | fmt++; |
| 9022 | if (*fmt == '(') { |
| 9023 | Py_UNICODE *keystart; |
| 9024 | Py_ssize_t keylen; |
| 9025 | PyObject *key; |
| 9026 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9027 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9028 | if (dict == NULL) { |
| 9029 | PyErr_SetString(PyExc_TypeError, |
| 9030 | "format requires a mapping"); |
| 9031 | goto onError; |
| 9032 | } |
| 9033 | ++fmt; |
| 9034 | --fmtcnt; |
| 9035 | keystart = fmt; |
| 9036 | /* Skip over balanced parentheses */ |
| 9037 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9038 | if (*fmt == ')') |
| 9039 | --pcount; |
| 9040 | else if (*fmt == '(') |
| 9041 | ++pcount; |
| 9042 | fmt++; |
| 9043 | } |
| 9044 | keylen = fmt - keystart - 1; |
| 9045 | if (fmtcnt < 0 || pcount > 0) { |
| 9046 | PyErr_SetString(PyExc_ValueError, |
| 9047 | "incomplete format key"); |
| 9048 | goto onError; |
| 9049 | } |
| 9050 | #if 0 |
| 9051 | /* keys are converted to strings using UTF-8 and |
| 9052 | then looked up since Python uses strings to hold |
| 9053 | variables names etc. in its namespaces and we |
| 9054 | wouldn't want to break common idioms. */ |
| 9055 | key = PyUnicode_EncodeUTF8(keystart, |
| 9056 | keylen, |
| 9057 | NULL); |
| 9058 | #else |
| 9059 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9060 | #endif |
| 9061 | if (key == NULL) |
| 9062 | goto onError; |
| 9063 | if (args_owned) { |
| 9064 | Py_DECREF(args); |
| 9065 | args_owned = 0; |
| 9066 | } |
| 9067 | args = PyObject_GetItem(dict, key); |
| 9068 | Py_DECREF(key); |
| 9069 | if (args == NULL) { |
| 9070 | goto onError; |
| 9071 | } |
| 9072 | args_owned = 1; |
| 9073 | arglen = -1; |
| 9074 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9075 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9076 | while (--fmtcnt >= 0) { |
| 9077 | switch (c = *fmt++) { |
| 9078 | case '-': flags |= F_LJUST; continue; |
| 9079 | case '+': flags |= F_SIGN; continue; |
| 9080 | case ' ': flags |= F_BLANK; continue; |
| 9081 | case '#': flags |= F_ALT; continue; |
| 9082 | case '0': flags |= F_ZERO; continue; |
| 9083 | } |
| 9084 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9085 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9086 | if (c == '*') { |
| 9087 | v = getnextarg(args, arglen, &argidx); |
| 9088 | if (v == NULL) |
| 9089 | goto onError; |
| 9090 | if (!PyLong_Check(v)) { |
| 9091 | PyErr_SetString(PyExc_TypeError, |
| 9092 | "* wants int"); |
| 9093 | goto onError; |
| 9094 | } |
| 9095 | width = PyLong_AsLong(v); |
| 9096 | if (width == -1 && PyErr_Occurred()) |
| 9097 | goto onError; |
| 9098 | if (width < 0) { |
| 9099 | flags |= F_LJUST; |
| 9100 | width = -width; |
| 9101 | } |
| 9102 | if (--fmtcnt >= 0) |
| 9103 | c = *fmt++; |
| 9104 | } |
| 9105 | else if (c >= '0' && c <= '9') { |
| 9106 | width = c - '0'; |
| 9107 | while (--fmtcnt >= 0) { |
| 9108 | c = *fmt++; |
| 9109 | if (c < '0' || c > '9') |
| 9110 | break; |
| 9111 | if ((width*10) / 10 != width) { |
| 9112 | PyErr_SetString(PyExc_ValueError, |
| 9113 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9114 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9115 | } |
| 9116 | width = width*10 + (c - '0'); |
| 9117 | } |
| 9118 | } |
| 9119 | if (c == '.') { |
| 9120 | prec = 0; |
| 9121 | if (--fmtcnt >= 0) |
| 9122 | c = *fmt++; |
| 9123 | if (c == '*') { |
| 9124 | v = getnextarg(args, arglen, &argidx); |
| 9125 | if (v == NULL) |
| 9126 | goto onError; |
| 9127 | if (!PyLong_Check(v)) { |
| 9128 | PyErr_SetString(PyExc_TypeError, |
| 9129 | "* wants int"); |
| 9130 | goto onError; |
| 9131 | } |
| 9132 | prec = PyLong_AsLong(v); |
| 9133 | if (prec == -1 && PyErr_Occurred()) |
| 9134 | goto onError; |
| 9135 | if (prec < 0) |
| 9136 | prec = 0; |
| 9137 | if (--fmtcnt >= 0) |
| 9138 | c = *fmt++; |
| 9139 | } |
| 9140 | else if (c >= '0' && c <= '9') { |
| 9141 | prec = c - '0'; |
| 9142 | while (--fmtcnt >= 0) { |
| 9143 | c = Py_CHARMASK(*fmt++); |
| 9144 | if (c < '0' || c > '9') |
| 9145 | break; |
| 9146 | if ((prec*10) / 10 != prec) { |
| 9147 | PyErr_SetString(PyExc_ValueError, |
| 9148 | "prec too big"); |
| 9149 | goto onError; |
| 9150 | } |
| 9151 | prec = prec*10 + (c - '0'); |
| 9152 | } |
| 9153 | } |
| 9154 | } /* prec */ |
| 9155 | if (fmtcnt >= 0) { |
| 9156 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9157 | if (--fmtcnt >= 0) |
| 9158 | c = *fmt++; |
| 9159 | } |
| 9160 | } |
| 9161 | if (fmtcnt < 0) { |
| 9162 | PyErr_SetString(PyExc_ValueError, |
| 9163 | "incomplete format"); |
| 9164 | goto onError; |
| 9165 | } |
| 9166 | if (c != '%') { |
| 9167 | v = getnextarg(args, arglen, &argidx); |
| 9168 | if (v == NULL) |
| 9169 | goto onError; |
| 9170 | } |
| 9171 | sign = 0; |
| 9172 | fill = ' '; |
| 9173 | switch (c) { |
| 9174 | |
| 9175 | case '%': |
| 9176 | pbuf = formatbuf; |
| 9177 | /* presume that buffer length is at least 1 */ |
| 9178 | pbuf[0] = '%'; |
| 9179 | len = 1; |
| 9180 | break; |
| 9181 | |
| 9182 | case 's': |
| 9183 | case 'r': |
| 9184 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9185 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9186 | temp = v; |
| 9187 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9188 | } |
| 9189 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9190 | if (c == 's') |
| 9191 | temp = PyObject_Str(v); |
| 9192 | else if (c == 'r') |
| 9193 | temp = PyObject_Repr(v); |
| 9194 | else |
| 9195 | temp = PyObject_ASCII(v); |
| 9196 | if (temp == NULL) |
| 9197 | goto onError; |
| 9198 | if (PyUnicode_Check(temp)) |
| 9199 | /* nothing to do */; |
| 9200 | else { |
| 9201 | Py_DECREF(temp); |
| 9202 | PyErr_SetString(PyExc_TypeError, |
| 9203 | "%s argument has non-string str()"); |
| 9204 | goto onError; |
| 9205 | } |
| 9206 | } |
| 9207 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9208 | len = PyUnicode_GET_SIZE(temp); |
| 9209 | if (prec >= 0 && len > prec) |
| 9210 | len = prec; |
| 9211 | break; |
| 9212 | |
| 9213 | case 'i': |
| 9214 | case 'd': |
| 9215 | case 'u': |
| 9216 | case 'o': |
| 9217 | case 'x': |
| 9218 | case 'X': |
| 9219 | if (c == 'i') |
| 9220 | c = 'd'; |
| 9221 | isnumok = 0; |
| 9222 | if (PyNumber_Check(v)) { |
| 9223 | PyObject *iobj=NULL; |
| 9224 | |
| 9225 | if (PyLong_Check(v)) { |
| 9226 | iobj = v; |
| 9227 | Py_INCREF(iobj); |
| 9228 | } |
| 9229 | else { |
| 9230 | iobj = PyNumber_Long(v); |
| 9231 | } |
| 9232 | if (iobj!=NULL) { |
| 9233 | if (PyLong_Check(iobj)) { |
| 9234 | isnumok = 1; |
| 9235 | temp = formatlong(iobj, flags, prec, c); |
| 9236 | Py_DECREF(iobj); |
| 9237 | if (!temp) |
| 9238 | goto onError; |
| 9239 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9240 | len = PyUnicode_GET_SIZE(temp); |
| 9241 | sign = 1; |
| 9242 | } |
| 9243 | else { |
| 9244 | Py_DECREF(iobj); |
| 9245 | } |
| 9246 | } |
| 9247 | } |
| 9248 | if (!isnumok) { |
| 9249 | PyErr_Format(PyExc_TypeError, |
| 9250 | "%%%c format: a number is required, " |
| 9251 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9252 | goto onError; |
| 9253 | } |
| 9254 | if (flags & F_ZERO) |
| 9255 | fill = '0'; |
| 9256 | break; |
| 9257 | |
| 9258 | case 'e': |
| 9259 | case 'E': |
| 9260 | case 'f': |
| 9261 | case 'F': |
| 9262 | case 'g': |
| 9263 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9264 | temp = formatfloat(v, flags, prec, c); |
| 9265 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9266 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9267 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9268 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9269 | sign = 1; |
| 9270 | if (flags & F_ZERO) |
| 9271 | fill = '0'; |
| 9272 | break; |
| 9273 | |
| 9274 | case 'c': |
| 9275 | pbuf = formatbuf; |
| 9276 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9277 | if (len < 0) |
| 9278 | goto onError; |
| 9279 | break; |
| 9280 | |
| 9281 | default: |
| 9282 | PyErr_Format(PyExc_ValueError, |
| 9283 | "unsupported format character '%c' (0x%x) " |
| 9284 | "at index %zd", |
| 9285 | (31<=c && c<=126) ? (char)c : '?', |
| 9286 | (int)c, |
| 9287 | (Py_ssize_t)(fmt - 1 - |
| 9288 | PyUnicode_AS_UNICODE(uformat))); |
| 9289 | goto onError; |
| 9290 | } |
| 9291 | if (sign) { |
| 9292 | if (*pbuf == '-' || *pbuf == '+') { |
| 9293 | sign = *pbuf++; |
| 9294 | len--; |
| 9295 | } |
| 9296 | else if (flags & F_SIGN) |
| 9297 | sign = '+'; |
| 9298 | else if (flags & F_BLANK) |
| 9299 | sign = ' '; |
| 9300 | else |
| 9301 | sign = 0; |
| 9302 | } |
| 9303 | if (width < len) |
| 9304 | width = len; |
| 9305 | if (rescnt - (sign != 0) < width) { |
| 9306 | reslen -= rescnt; |
| 9307 | rescnt = width + fmtcnt + 100; |
| 9308 | reslen += rescnt; |
| 9309 | if (reslen < 0) { |
| 9310 | Py_XDECREF(temp); |
| 9311 | PyErr_NoMemory(); |
| 9312 | goto onError; |
| 9313 | } |
| 9314 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9315 | Py_XDECREF(temp); |
| 9316 | goto onError; |
| 9317 | } |
| 9318 | res = PyUnicode_AS_UNICODE(result) |
| 9319 | + reslen - rescnt; |
| 9320 | } |
| 9321 | if (sign) { |
| 9322 | if (fill != ' ') |
| 9323 | *res++ = sign; |
| 9324 | rescnt--; |
| 9325 | if (width > len) |
| 9326 | width--; |
| 9327 | } |
| 9328 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9329 | assert(pbuf[0] == '0'); |
| 9330 | assert(pbuf[1] == c); |
| 9331 | if (fill != ' ') { |
| 9332 | *res++ = *pbuf++; |
| 9333 | *res++ = *pbuf++; |
| 9334 | } |
| 9335 | rescnt -= 2; |
| 9336 | width -= 2; |
| 9337 | if (width < 0) |
| 9338 | width = 0; |
| 9339 | len -= 2; |
| 9340 | } |
| 9341 | if (width > len && !(flags & F_LJUST)) { |
| 9342 | do { |
| 9343 | --rescnt; |
| 9344 | *res++ = fill; |
| 9345 | } while (--width > len); |
| 9346 | } |
| 9347 | if (fill == ' ') { |
| 9348 | if (sign) |
| 9349 | *res++ = sign; |
| 9350 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9351 | assert(pbuf[0] == '0'); |
| 9352 | assert(pbuf[1] == c); |
| 9353 | *res++ = *pbuf++; |
| 9354 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9355 | } |
| 9356 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9357 | Py_UNICODE_COPY(res, pbuf, len); |
| 9358 | res += len; |
| 9359 | rescnt -= len; |
| 9360 | while (--width >= len) { |
| 9361 | --rescnt; |
| 9362 | *res++ = ' '; |
| 9363 | } |
| 9364 | if (dict && (argidx < arglen) && c != '%') { |
| 9365 | PyErr_SetString(PyExc_TypeError, |
| 9366 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9367 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9368 | goto onError; |
| 9369 | } |
| 9370 | Py_XDECREF(temp); |
| 9371 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9372 | } /* until end */ |
| 9373 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9374 | PyErr_SetString(PyExc_TypeError, |
| 9375 | "not all arguments converted during string formatting"); |
| 9376 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9377 | } |
| 9378 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9379 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9380 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9381 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9382 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9383 | } |
| 9384 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9385 | return (PyObject *)result; |
| 9386 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9387 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9388 | Py_XDECREF(result); |
| 9389 | Py_DECREF(uformat); |
| 9390 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9391 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9392 | } |
| 9393 | return NULL; |
| 9394 | } |
| 9395 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9396 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9397 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9398 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9399 | static PyObject * |
| 9400 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9401 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9402 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9403 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9404 | char *encoding = NULL; |
| 9405 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9406 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9407 | if (type != &PyUnicode_Type) |
| 9408 | return unicode_subtype_new(type, args, kwds); |
| 9409 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9410 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9411 | return NULL; |
| 9412 | if (x == NULL) |
| 9413 | return (PyObject *)_PyUnicode_New(0); |
| 9414 | if (encoding == NULL && errors == NULL) |
| 9415 | return PyObject_Str(x); |
| 9416 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9417 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9418 | } |
| 9419 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9420 | static PyObject * |
| 9421 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9422 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9423 | PyUnicodeObject *tmp, *pnew; |
| 9424 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9425 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9426 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9427 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9428 | if (tmp == NULL) |
| 9429 | return NULL; |
| 9430 | assert(PyUnicode_Check(tmp)); |
| 9431 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9432 | if (pnew == NULL) { |
| 9433 | Py_DECREF(tmp); |
| 9434 | return NULL; |
| 9435 | } |
| 9436 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9437 | if (pnew->str == NULL) { |
| 9438 | _Py_ForgetReference((PyObject *)pnew); |
| 9439 | PyObject_Del(pnew); |
| 9440 | Py_DECREF(tmp); |
| 9441 | return PyErr_NoMemory(); |
| 9442 | } |
| 9443 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9444 | pnew->length = n; |
| 9445 | pnew->hash = tmp->hash; |
| 9446 | Py_DECREF(tmp); |
| 9447 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9448 | } |
| 9449 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9450 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9451 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9452 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9453 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9454 | encoding defaults to the current default string encoding.\n\ |
| 9455 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9456 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9457 | static PyObject *unicode_iter(PyObject *seq); |
| 9458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9459 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9460 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9461 | "str", /* tp_name */ |
| 9462 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9463 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9464 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9465 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9466 | 0, /* tp_print */ |
| 9467 | 0, /* tp_getattr */ |
| 9468 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9469 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9470 | unicode_repr, /* tp_repr */ |
| 9471 | &unicode_as_number, /* tp_as_number */ |
| 9472 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9473 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9474 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9475 | 0, /* tp_call*/ |
| 9476 | (reprfunc) unicode_str, /* tp_str */ |
| 9477 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9478 | 0, /* tp_setattro */ |
| 9479 | 0, /* tp_as_buffer */ |
| 9480 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9481 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9482 | unicode_doc, /* tp_doc */ |
| 9483 | 0, /* tp_traverse */ |
| 9484 | 0, /* tp_clear */ |
| 9485 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9486 | 0, /* tp_weaklistoffset */ |
| 9487 | unicode_iter, /* tp_iter */ |
| 9488 | 0, /* tp_iternext */ |
| 9489 | unicode_methods, /* tp_methods */ |
| 9490 | 0, /* tp_members */ |
| 9491 | 0, /* tp_getset */ |
| 9492 | &PyBaseObject_Type, /* tp_base */ |
| 9493 | 0, /* tp_dict */ |
| 9494 | 0, /* tp_descr_get */ |
| 9495 | 0, /* tp_descr_set */ |
| 9496 | 0, /* tp_dictoffset */ |
| 9497 | 0, /* tp_init */ |
| 9498 | 0, /* tp_alloc */ |
| 9499 | unicode_new, /* tp_new */ |
| 9500 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9501 | }; |
| 9502 | |
| 9503 | /* Initialize the Unicode implementation */ |
| 9504 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9505 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9506 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9507 | int i; |
| 9508 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9509 | /* XXX - move this array to unicodectype.c ? */ |
| 9510 | Py_UNICODE linebreak[] = { |
| 9511 | 0x000A, /* LINE FEED */ |
| 9512 | 0x000D, /* CARRIAGE RETURN */ |
| 9513 | 0x001C, /* FILE SEPARATOR */ |
| 9514 | 0x001D, /* GROUP SEPARATOR */ |
| 9515 | 0x001E, /* RECORD SEPARATOR */ |
| 9516 | 0x0085, /* NEXT LINE */ |
| 9517 | 0x2028, /* LINE SEPARATOR */ |
| 9518 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9519 | }; |
| 9520 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9521 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9522 | free_list = NULL; |
| 9523 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9524 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9525 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9526 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9527 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9528 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9529 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9530 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9531 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9532 | |
| 9533 | /* initialize the linebreak bloom filter */ |
| 9534 | bloom_linebreak = make_bloom_mask( |
| 9535 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9536 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9537 | |
| 9538 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9539 | } |
| 9540 | |
| 9541 | /* Finalize the Unicode implementation */ |
| 9542 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9543 | int |
| 9544 | PyUnicode_ClearFreeList(void) |
| 9545 | { |
| 9546 | int freelist_size = numfree; |
| 9547 | PyUnicodeObject *u; |
| 9548 | |
| 9549 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9550 | PyUnicodeObject *v = u; |
| 9551 | u = *(PyUnicodeObject **)u; |
| 9552 | if (v->str) |
| 9553 | PyObject_DEL(v->str); |
| 9554 | Py_XDECREF(v->defenc); |
| 9555 | PyObject_Del(v); |
| 9556 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9557 | } |
| 9558 | free_list = NULL; |
| 9559 | assert(numfree == 0); |
| 9560 | return freelist_size; |
| 9561 | } |
| 9562 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9563 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9564 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9565 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9566 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9567 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9568 | Py_XDECREF(unicode_empty); |
| 9569 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9570 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9571 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9572 | if (unicode_latin1[i]) { |
| 9573 | Py_DECREF(unicode_latin1[i]); |
| 9574 | unicode_latin1[i] = NULL; |
| 9575 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9576 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9577 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9578 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9579 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9580 | void |
| 9581 | PyUnicode_InternInPlace(PyObject **p) |
| 9582 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9583 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9584 | PyObject *t; |
| 9585 | if (s == NULL || !PyUnicode_Check(s)) |
| 9586 | Py_FatalError( |
| 9587 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9588 | /* If it's a subclass, we don't really know what putting |
| 9589 | it in the interned dict might do. */ |
| 9590 | if (!PyUnicode_CheckExact(s)) |
| 9591 | return; |
| 9592 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9593 | return; |
| 9594 | if (interned == NULL) { |
| 9595 | interned = PyDict_New(); |
| 9596 | if (interned == NULL) { |
| 9597 | PyErr_Clear(); /* Don't leave an exception */ |
| 9598 | return; |
| 9599 | } |
| 9600 | } |
| 9601 | /* It might be that the GetItem call fails even |
| 9602 | though the key is present in the dictionary, |
| 9603 | namely when this happens during a stack overflow. */ |
| 9604 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9605 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9606 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9607 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9608 | if (t) { |
| 9609 | Py_INCREF(t); |
| 9610 | Py_DECREF(*p); |
| 9611 | *p = t; |
| 9612 | return; |
| 9613 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9614 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9615 | PyThreadState_GET()->recursion_critical = 1; |
| 9616 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9617 | PyErr_Clear(); |
| 9618 | PyThreadState_GET()->recursion_critical = 0; |
| 9619 | return; |
| 9620 | } |
| 9621 | PyThreadState_GET()->recursion_critical = 0; |
| 9622 | /* The two references in interned are not counted by refcnt. |
| 9623 | The deallocator will take care of this */ |
| 9624 | Py_REFCNT(s) -= 2; |
| 9625 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9626 | } |
| 9627 | |
| 9628 | void |
| 9629 | PyUnicode_InternImmortal(PyObject **p) |
| 9630 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9631 | PyUnicode_InternInPlace(p); |
| 9632 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9633 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9634 | Py_INCREF(*p); |
| 9635 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9636 | } |
| 9637 | |
| 9638 | PyObject * |
| 9639 | PyUnicode_InternFromString(const char *cp) |
| 9640 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9641 | PyObject *s = PyUnicode_FromString(cp); |
| 9642 | if (s == NULL) |
| 9643 | return NULL; |
| 9644 | PyUnicode_InternInPlace(&s); |
| 9645 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9646 | } |
| 9647 | |
| 9648 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9649 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9650 | PyObject *keys; |
| 9651 | PyUnicodeObject *s; |
| 9652 | Py_ssize_t i, n; |
| 9653 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
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 | if (interned == NULL || !PyDict_Check(interned)) |
| 9656 | return; |
| 9657 | keys = PyDict_Keys(interned); |
| 9658 | if (keys == NULL || !PyList_Check(keys)) { |
| 9659 | PyErr_Clear(); |
| 9660 | return; |
| 9661 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9662 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9663 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9664 | detector, interned unicode strings are not forcibly deallocated; |
| 9665 | rather, we give them their stolen references back, and then clear |
| 9666 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9667 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9668 | n = PyList_GET_SIZE(keys); |
| 9669 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9670 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9671 | for (i = 0; i < n; i++) { |
| 9672 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9673 | switch (s->state) { |
| 9674 | case SSTATE_NOT_INTERNED: |
| 9675 | /* XXX Shouldn't happen */ |
| 9676 | break; |
| 9677 | case SSTATE_INTERNED_IMMORTAL: |
| 9678 | Py_REFCNT(s) += 1; |
| 9679 | immortal_size += s->length; |
| 9680 | break; |
| 9681 | case SSTATE_INTERNED_MORTAL: |
| 9682 | Py_REFCNT(s) += 2; |
| 9683 | mortal_size += s->length; |
| 9684 | break; |
| 9685 | default: |
| 9686 | Py_FatalError("Inconsistent interned string state."); |
| 9687 | } |
| 9688 | s->state = SSTATE_NOT_INTERNED; |
| 9689 | } |
| 9690 | fprintf(stderr, "total size of all interned strings: " |
| 9691 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9692 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9693 | Py_DECREF(keys); |
| 9694 | PyDict_Clear(interned); |
| 9695 | Py_DECREF(interned); |
| 9696 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9697 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9698 | |
| 9699 | |
| 9700 | /********************* Unicode Iterator **************************/ |
| 9701 | |
| 9702 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9703 | PyObject_HEAD |
| 9704 | Py_ssize_t it_index; |
| 9705 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9706 | } unicodeiterobject; |
| 9707 | |
| 9708 | static void |
| 9709 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9710 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9711 | _PyObject_GC_UNTRACK(it); |
| 9712 | Py_XDECREF(it->it_seq); |
| 9713 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9714 | } |
| 9715 | |
| 9716 | static int |
| 9717 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9718 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9719 | Py_VISIT(it->it_seq); |
| 9720 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9721 | } |
| 9722 | |
| 9723 | static PyObject * |
| 9724 | unicodeiter_next(unicodeiterobject *it) |
| 9725 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9726 | PyUnicodeObject *seq; |
| 9727 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9728 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9729 | assert(it != NULL); |
| 9730 | seq = it->it_seq; |
| 9731 | if (seq == NULL) |
| 9732 | return NULL; |
| 9733 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9734 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9735 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9736 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9737 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9738 | if (item != NULL) |
| 9739 | ++it->it_index; |
| 9740 | return item; |
| 9741 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9742 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9743 | Py_DECREF(seq); |
| 9744 | it->it_seq = NULL; |
| 9745 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9746 | } |
| 9747 | |
| 9748 | static PyObject * |
| 9749 | unicodeiter_len(unicodeiterobject *it) |
| 9750 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9751 | Py_ssize_t len = 0; |
| 9752 | if (it->it_seq) |
| 9753 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9754 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9755 | } |
| 9756 | |
| 9757 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9758 | |
| 9759 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9760 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9761 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9762 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9763 | }; |
| 9764 | |
| 9765 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9766 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9767 | "str_iterator", /* tp_name */ |
| 9768 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9769 | 0, /* tp_itemsize */ |
| 9770 | /* methods */ |
| 9771 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9772 | 0, /* tp_print */ |
| 9773 | 0, /* tp_getattr */ |
| 9774 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9775 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9776 | 0, /* tp_repr */ |
| 9777 | 0, /* tp_as_number */ |
| 9778 | 0, /* tp_as_sequence */ |
| 9779 | 0, /* tp_as_mapping */ |
| 9780 | 0, /* tp_hash */ |
| 9781 | 0, /* tp_call */ |
| 9782 | 0, /* tp_str */ |
| 9783 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9784 | 0, /* tp_setattro */ |
| 9785 | 0, /* tp_as_buffer */ |
| 9786 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9787 | 0, /* tp_doc */ |
| 9788 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9789 | 0, /* tp_clear */ |
| 9790 | 0, /* tp_richcompare */ |
| 9791 | 0, /* tp_weaklistoffset */ |
| 9792 | PyObject_SelfIter, /* tp_iter */ |
| 9793 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9794 | unicodeiter_methods, /* tp_methods */ |
| 9795 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9796 | }; |
| 9797 | |
| 9798 | static PyObject * |
| 9799 | unicode_iter(PyObject *seq) |
| 9800 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9801 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9802 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9803 | if (!PyUnicode_Check(seq)) { |
| 9804 | PyErr_BadInternalCall(); |
| 9805 | return NULL; |
| 9806 | } |
| 9807 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9808 | if (it == NULL) |
| 9809 | return NULL; |
| 9810 | it->it_index = 0; |
| 9811 | Py_INCREF(seq); |
| 9812 | it->it_seq = (PyUnicodeObject *)seq; |
| 9813 | _PyObject_GC_TRACK(it); |
| 9814 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9815 | } |
| 9816 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9817 | size_t |
| 9818 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9819 | { |
| 9820 | int res = 0; |
| 9821 | while(*u++) |
| 9822 | res++; |
| 9823 | return res; |
| 9824 | } |
| 9825 | |
| 9826 | Py_UNICODE* |
| 9827 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9828 | { |
| 9829 | Py_UNICODE *u = s1; |
| 9830 | while ((*u++ = *s2++)); |
| 9831 | return s1; |
| 9832 | } |
| 9833 | |
| 9834 | Py_UNICODE* |
| 9835 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9836 | { |
| 9837 | Py_UNICODE *u = s1; |
| 9838 | while ((*u++ = *s2++)) |
| 9839 | if (n-- == 0) |
| 9840 | break; |
| 9841 | return s1; |
| 9842 | } |
| 9843 | |
| 9844 | int |
| 9845 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9846 | { |
| 9847 | while (*s1 && *s2 && *s1 == *s2) |
| 9848 | s1++, s2++; |
| 9849 | if (*s1 && *s2) |
| 9850 | return (*s1 < *s2) ? -1 : +1; |
| 9851 | if (*s1) |
| 9852 | return 1; |
| 9853 | if (*s2) |
| 9854 | return -1; |
| 9855 | return 0; |
| 9856 | } |
| 9857 | |
| 9858 | Py_UNICODE* |
| 9859 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9860 | { |
| 9861 | const Py_UNICODE *p; |
| 9862 | for (p = s; *p; p++) |
| 9863 | if (*p == c) |
| 9864 | return (Py_UNICODE*)p; |
| 9865 | return NULL; |
| 9866 | } |
| 9867 | |
| 9868 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9869 | #ifdef __cplusplus |
| 9870 | } |
| 9871 | #endif |