Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 46 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 47 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 49 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 50 | #include <windows.h> |
| 51 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Limit for the Unicode object free list */ |
| 54 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
| 57 | /* Limit for the Unicode object free list stay alive optimization. |
| 58 | |
| 59 | The implementation will keep allocated Unicode memory intact for |
| 60 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | malloc()-overhead) bytes of unused garbage. |
| 66 | |
| 67 | Setting the limit to 0 effectively turns the feature off. |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | Note: This is an experimental feature ! If you get core dumps when |
| 70 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | */ |
| 73 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Endianness switches; defaults to little endian */ |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | # define BYTEORDER_IS_BIG_ENDIAN |
| 80 | #else |
| 81 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 82 | #endif |
| 83 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ |
| 85 | |
| 86 | The globals are initialized by the _PyUnicode_Init() API and should |
| 87 | not be used before calling that API. |
| 88 | |
| 89 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 96 | /* This dictionary holds all interned unicode strings. Note that references |
| 97 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 98 | When the interned string reaches a refcnt of 0 the string deallocation |
| 99 | function will delete the reference from this dictionary. |
| 100 | |
| 101 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 102 | count of a string is: s->ob_refcnt + (s->state ? 2 : 0) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 103 | */ |
| 104 | static PyObject *interned; |
| 105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 106 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 107 | static PyUnicodeObject *free_list; |
| 108 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 109 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 110 | /* The empty Unicode object is shared to improve performance. */ |
| 111 | static PyUnicodeObject *unicode_empty; |
| 112 | |
| 113 | /* Single character Unicode strings in the Latin-1 range are being |
| 114 | shared as well. */ |
| 115 | static PyUnicodeObject *unicode_latin1[256]; |
| 116 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 117 | /* Default encoding to use and assume when NULL is passed as encoding |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 118 | parameter; it is fixed to "utf-8". Always use the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 119 | PyUnicode_GetDefaultEncoding() API to access this global. |
| 120 | |
Alexandre Vassalotti | 3d2fd7f | 2007-10-16 00:26:33 +0000 | [diff] [blame] | 121 | Don't forget to alter Py_FileSystemDefaultEncoding if you change the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 122 | hard coded default! |
| 123 | */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 124 | static const char unicode_default_encoding[] = "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 125 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 126 | /* Fast detection of the most frequent whitespace characters */ |
| 127 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 128 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 129 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 130 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 131 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 132 | /* case 0x000C: * FORM FEED */ |
| 133 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 134 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 136 | /* case 0x001C: * FILE SEPARATOR */ |
| 137 | /* case 0x001D: * GROUP SEPARATOR */ |
| 138 | /* case 0x001E: * RECORD SEPARATOR */ |
| 139 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 140 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 141 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 142 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 146 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 147 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 149 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 150 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 151 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 153 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 154 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 157 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 158 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 159 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 160 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 161 | |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 162 | static void raise_encode_exception(PyObject **exceptionObject, |
| 163 | const char *encoding, |
| 164 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 165 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 166 | const char *reason); |
| 167 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 168 | /* Same for linebreaks */ |
| 169 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 171 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 172 | /* 0x000B, * LINE TABULATION */ |
| 173 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 174 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 175 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 177 | /* 0x001C, * FILE SEPARATOR */ |
| 178 | /* 0x001D, * GROUP SEPARATOR */ |
| 179 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 180 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 184 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 185 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 186 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 187 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 188 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 189 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 190 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 191 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 192 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 193 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 197 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 198 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 199 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 200 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 201 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 202 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 203 | /* This is actually an illegal character, so it should |
| 204 | not be passed to unichr. */ |
| 205 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 206 | #endif |
| 207 | } |
| 208 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 209 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 210 | |
| 211 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 212 | to keep things simple, we use a single bitmask, using the least 5 |
| 213 | bits from each unicode characters as the bit index. */ |
| 214 | |
| 215 | /* the linebreak mask is set up by Unicode_Init below */ |
| 216 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 217 | #if LONG_BIT >= 128 |
| 218 | #define BLOOM_WIDTH 128 |
| 219 | #elif LONG_BIT >= 64 |
| 220 | #define BLOOM_WIDTH 64 |
| 221 | #elif LONG_BIT >= 32 |
| 222 | #define BLOOM_WIDTH 32 |
| 223 | #else |
| 224 | #error "LONG_BIT is smaller than 32" |
| 225 | #endif |
| 226 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 227 | #define BLOOM_MASK unsigned long |
| 228 | |
| 229 | static BLOOM_MASK bloom_linebreak; |
| 230 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 231 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 232 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 233 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 234 | #define BLOOM_LINEBREAK(ch) \ |
| 235 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 236 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 237 | |
| 238 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 239 | { |
| 240 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 241 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 242 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 243 | Py_ssize_t i; |
| 244 | |
| 245 | mask = 0; |
| 246 | for (i = 0; i < len; i++) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 247 | BLOOM_ADD(mask, ptr[i]); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 248 | |
| 249 | return mask; |
| 250 | } |
| 251 | |
| 252 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 253 | { |
| 254 | Py_ssize_t i; |
| 255 | |
| 256 | for (i = 0; i < setlen; i++) |
| 257 | if (set[i] == chr) |
| 258 | return 1; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 263 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 264 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | /* --- Unicode Object ----------------------------------------------------- */ |
| 267 | |
| 268 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 269 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 270 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 271 | { |
| 272 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 274 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 275 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 276 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 278 | /* Resizing shared object (unicode_empty or single character |
| 279 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 280 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 281 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 282 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 283 | (unicode->length == 1 && |
| 284 | unicode->str[0] < 256U && |
| 285 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 286 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 287 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | return -1; |
| 289 | } |
| 290 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 291 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 292 | The overallocation is also used by fastsearch, which assumes that it's |
| 293 | safe to look at str[length] (without making any assumptions about what |
| 294 | it contains). */ |
| 295 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 296 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 297 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 298 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 300 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 301 | PyErr_NoMemory(); |
| 302 | return -1; |
| 303 | } |
| 304 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 305 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 307 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 308 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 309 | if (unicode->defenc) { |
| 310 | Py_DECREF(unicode->defenc); |
| 311 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 312 | } |
| 313 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 314 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | /* We allocate one more byte to make sure the string is |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 319 | Ux0000 terminated; some code (e.g. new_identifier) |
| 320 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 321 | |
| 322 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 323 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 324 | |
| 325 | */ |
| 326 | |
| 327 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 328 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 329 | { |
| 330 | register PyUnicodeObject *unicode; |
| 331 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 332 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 333 | if (length == 0 && unicode_empty != NULL) { |
| 334 | Py_INCREF(unicode_empty); |
| 335 | return unicode_empty; |
| 336 | } |
| 337 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 338 | /* Ensure we won't overflow the size. */ |
| 339 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 340 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 341 | } |
| 342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 343 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 344 | if (free_list) { |
| 345 | unicode = free_list; |
| 346 | free_list = *(PyUnicodeObject **)unicode; |
| 347 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 348 | if (unicode->str) { |
| 349 | /* Keep-Alive optimization: we only upsize the buffer, |
| 350 | never downsize it. */ |
| 351 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 352 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 353 | PyObject_DEL(unicode->str); |
| 354 | unicode->str = NULL; |
| 355 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 356 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 357 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 358 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 359 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 360 | } |
| 361 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 362 | } |
| 363 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 364 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 365 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 366 | if (unicode == NULL) |
| 367 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 368 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 369 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 372 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 373 | PyErr_NoMemory(); |
| 374 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 375 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 376 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 377 | * the caller fails before initializing str -- unicode_resize() |
| 378 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 379 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 380 | * We don't want unicode_resize to read uninitialized memory in |
| 381 | * that case. |
| 382 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 383 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 384 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 385 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 386 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 387 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 388 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 389 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 390 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 391 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 392 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 393 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 394 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 395 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 396 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 400 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 401 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 402 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 403 | case SSTATE_NOT_INTERNED: |
| 404 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 405 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 406 | case SSTATE_INTERNED_MORTAL: |
| 407 | /* revive dead object temporarily for DelItem */ |
| 408 | Py_REFCNT(unicode) = 3; |
| 409 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 410 | Py_FatalError( |
| 411 | "deletion of interned string failed"); |
| 412 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 413 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 414 | case SSTATE_INTERNED_IMMORTAL: |
| 415 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 416 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 417 | default: |
| 418 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 421 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 422 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 423 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 424 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 425 | PyObject_DEL(unicode->str); |
| 426 | unicode->str = NULL; |
| 427 | unicode->length = 0; |
| 428 | } |
| 429 | if (unicode->defenc) { |
| 430 | Py_DECREF(unicode->defenc); |
| 431 | unicode->defenc = NULL; |
| 432 | } |
| 433 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 434 | *(PyUnicodeObject **)unicode = free_list; |
| 435 | free_list = unicode; |
| 436 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 437 | } |
| 438 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 439 | PyObject_DEL(unicode->str); |
| 440 | Py_XDECREF(unicode->defenc); |
| 441 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 445 | static |
| 446 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 447 | { |
| 448 | register PyUnicodeObject *v; |
| 449 | |
| 450 | /* Argument checks */ |
| 451 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 452 | PyErr_BadInternalCall(); |
| 453 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 454 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 455 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 456 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 457 | PyErr_BadInternalCall(); |
| 458 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* Resizing unicode_empty and single character objects is not |
| 462 | possible since these are being shared. We simply return a fresh |
| 463 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 464 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 465 | (v == unicode_empty || v->length == 1)) { |
| 466 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 467 | if (w == NULL) |
| 468 | return -1; |
| 469 | Py_UNICODE_COPY(w->str, v->str, |
| 470 | length < v->length ? length : v->length); |
| 471 | Py_DECREF(*unicode); |
| 472 | *unicode = w; |
| 473 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 477 | objects, since we can modify them in-place. */ |
| 478 | return unicode_resize(v, length); |
| 479 | } |
| 480 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 481 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 482 | { |
| 483 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 484 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 486 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 487 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 488 | { |
| 489 | PyUnicodeObject *unicode; |
| 490 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 491 | /* If the Unicode data is known at construction time, we can apply |
| 492 | some optimizations which share commonly used objects. */ |
| 493 | if (u != NULL) { |
| 494 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 495 | /* Optimization for empty strings */ |
| 496 | if (size == 0 && unicode_empty != NULL) { |
| 497 | Py_INCREF(unicode_empty); |
| 498 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 499 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 500 | |
| 501 | /* Single character Unicode objects in the Latin-1 range are |
| 502 | shared when using this constructor */ |
| 503 | if (size == 1 && *u < 256) { |
| 504 | unicode = unicode_latin1[*u]; |
| 505 | if (!unicode) { |
| 506 | unicode = _PyUnicode_New(1); |
| 507 | if (!unicode) |
| 508 | return NULL; |
| 509 | unicode->str[0] = *u; |
| 510 | unicode_latin1[*u] = unicode; |
| 511 | } |
| 512 | Py_INCREF(unicode); |
| 513 | return (PyObject *)unicode; |
| 514 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 515 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 517 | unicode = _PyUnicode_New(size); |
| 518 | if (!unicode) |
| 519 | return NULL; |
| 520 | |
| 521 | /* Copy the Unicode data into the new object */ |
| 522 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 523 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 524 | |
| 525 | return (PyObject *)unicode; |
| 526 | } |
| 527 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 528 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 529 | { |
| 530 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 531 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 532 | if (size < 0) { |
| 533 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 534 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 535 | return NULL; |
| 536 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 537 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 538 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 539 | some optimizations which share commonly used objects. |
| 540 | Also, this means the input must be UTF-8, so fall back to the |
| 541 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 542 | if (u != NULL) { |
| 543 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 544 | /* Optimization for empty strings */ |
| 545 | if (size == 0 && unicode_empty != NULL) { |
| 546 | Py_INCREF(unicode_empty); |
| 547 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 548 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 549 | |
| 550 | /* Single characters are shared when using this constructor. |
| 551 | Restrict to ASCII, since the input must be UTF-8. */ |
| 552 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 553 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 554 | if (!unicode) { |
| 555 | unicode = _PyUnicode_New(1); |
| 556 | if (!unicode) |
| 557 | return NULL; |
| 558 | unicode->str[0] = Py_CHARMASK(*u); |
| 559 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 560 | } |
| 561 | Py_INCREF(unicode); |
| 562 | return (PyObject *)unicode; |
| 563 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 564 | |
| 565 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 568 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 569 | if (!unicode) |
| 570 | return NULL; |
| 571 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 572 | return (PyObject *)unicode; |
| 573 | } |
| 574 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 575 | PyObject *PyUnicode_FromString(const char *u) |
| 576 | { |
| 577 | size_t size = strlen(u); |
| 578 | if (size > PY_SSIZE_T_MAX) { |
| 579 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | return PyUnicode_FromStringAndSize(u, size); |
| 584 | } |
| 585 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 586 | #ifdef HAVE_WCHAR_H |
| 587 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 588 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 589 | # define CONVERT_WCHAR_TO_SURROGATES |
| 590 | #endif |
| 591 | |
| 592 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 593 | |
| 594 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 595 | to convert from UTF32 to UTF16. */ |
| 596 | |
| 597 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 598 | Py_ssize_t size) |
| 599 | { |
| 600 | PyUnicodeObject *unicode; |
| 601 | register Py_ssize_t i; |
| 602 | Py_ssize_t alloc; |
| 603 | const wchar_t *orig_w; |
| 604 | |
| 605 | if (w == NULL) { |
| 606 | if (size == 0) |
| 607 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 608 | PyErr_BadInternalCall(); |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | if (size == -1) { |
| 613 | size = wcslen(w); |
| 614 | } |
| 615 | |
| 616 | alloc = size; |
| 617 | orig_w = w; |
| 618 | for (i = size; i > 0; i--) { |
| 619 | if (*w > 0xFFFF) |
| 620 | alloc++; |
| 621 | w++; |
| 622 | } |
| 623 | w = orig_w; |
| 624 | unicode = _PyUnicode_New(alloc); |
| 625 | if (!unicode) |
| 626 | return NULL; |
| 627 | |
| 628 | /* Copy the wchar_t data into the new object */ |
| 629 | { |
| 630 | register Py_UNICODE *u; |
| 631 | u = PyUnicode_AS_UNICODE(unicode); |
| 632 | for (i = size; i > 0; i--) { |
| 633 | if (*w > 0xFFFF) { |
| 634 | wchar_t ordinal = *w++; |
| 635 | ordinal -= 0x10000; |
| 636 | *u++ = 0xD800 | (ordinal >> 10); |
| 637 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 638 | } |
| 639 | else |
| 640 | *u++ = *w++; |
| 641 | } |
| 642 | } |
| 643 | return (PyObject *)unicode; |
| 644 | } |
| 645 | |
| 646 | #else |
| 647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 648 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 649 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 650 | { |
| 651 | PyUnicodeObject *unicode; |
| 652 | |
| 653 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 654 | if (size == 0) |
| 655 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 656 | PyErr_BadInternalCall(); |
| 657 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 660 | if (size == -1) { |
| 661 | size = wcslen(w); |
| 662 | } |
| 663 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 664 | unicode = _PyUnicode_New(size); |
| 665 | if (!unicode) |
| 666 | return NULL; |
| 667 | |
| 668 | /* Copy the wchar_t data into the new object */ |
| 669 | #ifdef HAVE_USABLE_WCHAR_T |
| 670 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 671 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 672 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 673 | register Py_UNICODE *u; |
| 674 | register Py_ssize_t i; |
| 675 | u = PyUnicode_AS_UNICODE(unicode); |
| 676 | for (i = size; i > 0; i--) |
| 677 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 678 | } |
| 679 | #endif |
| 680 | |
| 681 | return (PyObject *)unicode; |
| 682 | } |
| 683 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 684 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 685 | |
| 686 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 687 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 688 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 689 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 690 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 691 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 692 | *fmt++ = '%'; |
| 693 | if (width) { |
| 694 | if (zeropad) |
| 695 | *fmt++ = '0'; |
| 696 | fmt += sprintf(fmt, "%d", width); |
| 697 | } |
| 698 | if (precision) |
| 699 | fmt += sprintf(fmt, ".%d", precision); |
| 700 | if (longflag) |
| 701 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 702 | else if (longlongflag) { |
| 703 | /* longlongflag should only ever be nonzero on machines with |
| 704 | HAVE_LONG_LONG defined */ |
| 705 | #ifdef HAVE_LONG_LONG |
| 706 | char *f = PY_FORMAT_LONG_LONG; |
| 707 | while (*f) |
| 708 | *fmt++ = *f++; |
| 709 | #else |
| 710 | /* we shouldn't ever get here */ |
| 711 | assert(0); |
| 712 | *fmt++ = 'l'; |
| 713 | #endif |
| 714 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 715 | else if (size_tflag) { |
| 716 | char *f = PY_FORMAT_SIZE_T; |
| 717 | while (*f) |
| 718 | *fmt++ = *f++; |
| 719 | } |
| 720 | *fmt++ = c; |
| 721 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 724 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 725 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 726 | /* size of fixed-size buffer for formatting single arguments */ |
| 727 | #define ITEM_BUFFER_LEN 21 |
| 728 | /* maximum number of characters required for output of %ld. 21 characters |
| 729 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 730 | #define MAX_LONG_CHARS 21 |
| 731 | /* maximum number of characters required for output of %lld. |
| 732 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 733 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 734 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 735 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 736 | PyObject * |
| 737 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 738 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 739 | va_list count; |
| 740 | Py_ssize_t callcount = 0; |
| 741 | PyObject **callresults = NULL; |
| 742 | PyObject **callresult = NULL; |
| 743 | Py_ssize_t n = 0; |
| 744 | int width = 0; |
| 745 | int precision = 0; |
| 746 | int zeropad; |
| 747 | const char* f; |
| 748 | Py_UNICODE *s; |
| 749 | PyObject *string; |
| 750 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 751 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 752 | /* use abuffer instead of buffer, if we need more space |
| 753 | * (which can happen if there's a format specifier with width). */ |
| 754 | char *abuffer = NULL; |
| 755 | char *realbuffer; |
| 756 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 757 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 758 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 759 | |
| 760 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 761 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 762 | #else |
| 763 | #ifdef __va_copy |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 764 | __va_copy(count, vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 765 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 766 | count = vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 767 | #endif |
| 768 | #endif |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 769 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 770 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 771 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 772 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 773 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 774 | if (*f == '%') { |
| 775 | if (*(f+1)=='%') |
| 776 | continue; |
| 777 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 778 | ++callcount; |
| 779 | while (ISDIGIT((unsigned)*f)) |
| 780 | width = (width*10) + *f++ - '0'; |
| 781 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 782 | ; |
| 783 | if (*f == 's') |
| 784 | ++callcount; |
| 785 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 786 | } |
| 787 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 788 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 789 | if (callcount) { |
| 790 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 791 | if (!callresults) { |
| 792 | PyErr_NoMemory(); |
| 793 | return NULL; |
| 794 | } |
| 795 | callresult = callresults; |
| 796 | } |
| 797 | /* step 3: figure out how large a buffer we need */ |
| 798 | for (f = format; *f; f++) { |
| 799 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 800 | #ifdef HAVE_LONG_LONG |
| 801 | int longlongflag = 0; |
| 802 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 803 | const char* p = f; |
| 804 | width = 0; |
| 805 | while (ISDIGIT((unsigned)*f)) |
| 806 | width = (width*10) + *f++ - '0'; |
| 807 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 808 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 809 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 810 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 811 | * they don't affect the amount of space we reserve. |
| 812 | */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 813 | if (*f == 'l') { |
| 814 | if (f[1] == 'd' || f[1] == 'u') { |
| 815 | ++f; |
| 816 | } |
| 817 | #ifdef HAVE_LONG_LONG |
| 818 | else if (f[1] == 'l' && |
| 819 | (f[2] == 'd' || f[2] == 'u')) { |
| 820 | longlongflag = 1; |
| 821 | f += 2; |
| 822 | } |
| 823 | #endif |
| 824 | } |
| 825 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 826 | ++f; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 827 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 828 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 829 | switch (*f) { |
| 830 | case 'c': |
| 831 | (void)va_arg(count, int); |
| 832 | /* fall through... */ |
| 833 | case '%': |
| 834 | n++; |
| 835 | break; |
| 836 | case 'd': case 'u': case 'i': case 'x': |
| 837 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 838 | #ifdef HAVE_LONG_LONG |
| 839 | if (longlongflag) { |
| 840 | if (width < MAX_LONG_LONG_CHARS) |
| 841 | width = MAX_LONG_LONG_CHARS; |
| 842 | } |
| 843 | else |
| 844 | #endif |
| 845 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 846 | including sign. Decimal takes the most space. This |
| 847 | isn't enough for octal. If a width is specified we |
| 848 | need more (which we allocate later). */ |
| 849 | if (width < MAX_LONG_CHARS) |
| 850 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 851 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 852 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 853 | if (abuffersize < width) |
| 854 | abuffersize = width; |
| 855 | break; |
| 856 | case 's': |
| 857 | { |
| 858 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 859 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 860 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 861 | if (!str) |
| 862 | goto fail; |
| 863 | n += PyUnicode_GET_SIZE(str); |
| 864 | /* Remember the str and switch to the next slot */ |
| 865 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 866 | break; |
| 867 | } |
| 868 | case 'U': |
| 869 | { |
| 870 | PyObject *obj = va_arg(count, PyObject *); |
| 871 | assert(obj && PyUnicode_Check(obj)); |
| 872 | n += PyUnicode_GET_SIZE(obj); |
| 873 | break; |
| 874 | } |
| 875 | case 'V': |
| 876 | { |
| 877 | PyObject *obj = va_arg(count, PyObject *); |
| 878 | const char *str = va_arg(count, const char *); |
| 879 | assert(obj || str); |
| 880 | assert(!obj || PyUnicode_Check(obj)); |
| 881 | if (obj) |
| 882 | n += PyUnicode_GET_SIZE(obj); |
| 883 | else |
| 884 | n += strlen(str); |
| 885 | break; |
| 886 | } |
| 887 | case 'S': |
| 888 | { |
| 889 | PyObject *obj = va_arg(count, PyObject *); |
| 890 | PyObject *str; |
| 891 | assert(obj); |
| 892 | str = PyObject_Str(obj); |
| 893 | if (!str) |
| 894 | goto fail; |
| 895 | n += PyUnicode_GET_SIZE(str); |
| 896 | /* Remember the str and switch to the next slot */ |
| 897 | *callresult++ = str; |
| 898 | break; |
| 899 | } |
| 900 | case 'R': |
| 901 | { |
| 902 | PyObject *obj = va_arg(count, PyObject *); |
| 903 | PyObject *repr; |
| 904 | assert(obj); |
| 905 | repr = PyObject_Repr(obj); |
| 906 | if (!repr) |
| 907 | goto fail; |
| 908 | n += PyUnicode_GET_SIZE(repr); |
| 909 | /* Remember the repr and switch to the next slot */ |
| 910 | *callresult++ = repr; |
| 911 | break; |
| 912 | } |
| 913 | case 'A': |
| 914 | { |
| 915 | PyObject *obj = va_arg(count, PyObject *); |
| 916 | PyObject *ascii; |
| 917 | assert(obj); |
| 918 | ascii = PyObject_ASCII(obj); |
| 919 | if (!ascii) |
| 920 | goto fail; |
| 921 | n += PyUnicode_GET_SIZE(ascii); |
| 922 | /* Remember the repr and switch to the next slot */ |
| 923 | *callresult++ = ascii; |
| 924 | break; |
| 925 | } |
| 926 | case 'p': |
| 927 | (void) va_arg(count, int); |
| 928 | /* maximum 64-bit pointer representation: |
| 929 | * 0xffffffffffffffff |
| 930 | * so 19 characters is enough. |
| 931 | * XXX I count 18 -- what's the extra for? |
| 932 | */ |
| 933 | n += 19; |
| 934 | break; |
| 935 | default: |
| 936 | /* if we stumble upon an unknown |
| 937 | formatting code, copy the rest of |
| 938 | the format string to the output |
| 939 | string. (we cannot just skip the |
| 940 | code, since there's no way to know |
| 941 | what's in the argument list) */ |
| 942 | n += strlen(p); |
| 943 | goto expand; |
| 944 | } |
| 945 | } else |
| 946 | n++; |
| 947 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 948 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 949 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 950 | /* add 1 for sprintf's trailing null byte */ |
| 951 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 952 | if (!abuffer) { |
| 953 | PyErr_NoMemory(); |
| 954 | goto fail; |
| 955 | } |
| 956 | realbuffer = abuffer; |
| 957 | } |
| 958 | else |
| 959 | realbuffer = buffer; |
| 960 | /* step 4: fill the buffer */ |
| 961 | /* Since we've analyzed how much space we need for the worst case, |
| 962 | we don't have to resize the string. |
| 963 | There can be no errors beyond this point. */ |
| 964 | string = PyUnicode_FromUnicode(NULL, n); |
| 965 | if (!string) |
| 966 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 967 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 968 | s = PyUnicode_AS_UNICODE(string); |
| 969 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 970 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 971 | for (f = format; *f; f++) { |
| 972 | if (*f == '%') { |
| 973 | const char* p = f++; |
| 974 | int longflag = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 975 | int longlongflag = 0; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 976 | int size_tflag = 0; |
| 977 | zeropad = (*f == '0'); |
| 978 | /* parse the width.precision part */ |
| 979 | width = 0; |
| 980 | while (ISDIGIT((unsigned)*f)) |
| 981 | width = (width*10) + *f++ - '0'; |
| 982 | precision = 0; |
| 983 | if (*f == '.') { |
| 984 | f++; |
| 985 | while (ISDIGIT((unsigned)*f)) |
| 986 | precision = (precision*10) + *f++ - '0'; |
| 987 | } |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 988 | /* Handle %ld, %lu, %lld and %llu. */ |
| 989 | if (*f == 'l') { |
| 990 | if (f[1] == 'd' || f[1] == 'u') { |
| 991 | longflag = 1; |
| 992 | ++f; |
| 993 | } |
| 994 | #ifdef HAVE_LONG_LONG |
| 995 | else if (f[1] == 'l' && |
| 996 | (f[2] == 'd' || f[2] == 'u')) { |
| 997 | longlongflag = 1; |
| 998 | f += 2; |
| 999 | } |
| 1000 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1001 | } |
| 1002 | /* handle the size_t flag. */ |
| 1003 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 1004 | size_tflag = 1; |
| 1005 | ++f; |
| 1006 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1007 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1008 | switch (*f) { |
| 1009 | case 'c': |
| 1010 | *s++ = va_arg(vargs, int); |
| 1011 | break; |
| 1012 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1013 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1014 | width, precision, 'd'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1015 | if (longflag) |
| 1016 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1017 | #ifdef HAVE_LONG_LONG |
| 1018 | else if (longlongflag) |
| 1019 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1020 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1021 | else if (size_tflag) |
| 1022 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1023 | else |
| 1024 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1025 | appendstring(realbuffer); |
| 1026 | break; |
| 1027 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1028 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1029 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1030 | if (longflag) |
| 1031 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1032 | #ifdef HAVE_LONG_LONG |
| 1033 | else if (longlongflag) |
| 1034 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1035 | unsigned PY_LONG_LONG)); |
| 1036 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1037 | else if (size_tflag) |
| 1038 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1039 | else |
| 1040 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1041 | appendstring(realbuffer); |
| 1042 | break; |
| 1043 | case 'i': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1044 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1045 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1046 | appendstring(realbuffer); |
| 1047 | break; |
| 1048 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1049 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1050 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1051 | appendstring(realbuffer); |
| 1052 | break; |
| 1053 | case 's': |
| 1054 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1055 | /* unused, since we already have the result */ |
| 1056 | (void) va_arg(vargs, char *); |
| 1057 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1058 | PyUnicode_GET_SIZE(*callresult)); |
| 1059 | s += PyUnicode_GET_SIZE(*callresult); |
| 1060 | /* We're done with the unicode()/repr() => forget it */ |
| 1061 | Py_DECREF(*callresult); |
| 1062 | /* switch to next unicode()/repr() result */ |
| 1063 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | } |
| 1066 | case 'U': |
| 1067 | { |
| 1068 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1069 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1070 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1071 | s += size; |
| 1072 | break; |
| 1073 | } |
| 1074 | case 'V': |
| 1075 | { |
| 1076 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1077 | const char *str = va_arg(vargs, const char *); |
| 1078 | if (obj) { |
| 1079 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1080 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1081 | s += size; |
| 1082 | } else { |
| 1083 | appendstring(str); |
| 1084 | } |
| 1085 | break; |
| 1086 | } |
| 1087 | case 'S': |
| 1088 | case 'R': |
| 1089 | { |
| 1090 | Py_UNICODE *ucopy; |
| 1091 | Py_ssize_t usize; |
| 1092 | Py_ssize_t upos; |
| 1093 | /* unused, since we already have the result */ |
| 1094 | (void) va_arg(vargs, PyObject *); |
| 1095 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1096 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1097 | for (upos = 0; upos<usize;) |
| 1098 | *s++ = ucopy[upos++]; |
| 1099 | /* We're done with the unicode()/repr() => forget it */ |
| 1100 | Py_DECREF(*callresult); |
| 1101 | /* switch to next unicode()/repr() result */ |
| 1102 | ++callresult; |
| 1103 | break; |
| 1104 | } |
| 1105 | case 'p': |
| 1106 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1107 | /* %p is ill-defined: ensure leading 0x. */ |
| 1108 | if (buffer[1] == 'X') |
| 1109 | buffer[1] = 'x'; |
| 1110 | else if (buffer[1] != 'x') { |
| 1111 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1112 | buffer[0] = '0'; |
| 1113 | buffer[1] = 'x'; |
| 1114 | } |
| 1115 | appendstring(buffer); |
| 1116 | break; |
| 1117 | case '%': |
| 1118 | *s++ = '%'; |
| 1119 | break; |
| 1120 | default: |
| 1121 | appendstring(p); |
| 1122 | goto end; |
| 1123 | } |
| 1124 | } else |
| 1125 | *s++ = *f; |
| 1126 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1127 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1128 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1129 | if (callresults) |
| 1130 | PyObject_Free(callresults); |
| 1131 | if (abuffer) |
| 1132 | PyObject_Free(abuffer); |
| 1133 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1134 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1135 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1136 | if (callresults) { |
| 1137 | PyObject **callresult2 = callresults; |
| 1138 | while (callresult2 < callresult) { |
| 1139 | Py_DECREF(*callresult2); |
| 1140 | ++callresult2; |
| 1141 | } |
| 1142 | PyObject_Free(callresults); |
| 1143 | } |
| 1144 | if (abuffer) |
| 1145 | PyObject_Free(abuffer); |
| 1146 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | #undef appendstring |
| 1150 | |
| 1151 | PyObject * |
| 1152 | PyUnicode_FromFormat(const char *format, ...) |
| 1153 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1154 | PyObject* ret; |
| 1155 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1156 | |
| 1157 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1158 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1159 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1160 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1161 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1162 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1163 | va_end(vargs); |
| 1164 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1167 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1168 | wchar_t *w, |
| 1169 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1170 | { |
| 1171 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1172 | PyErr_BadInternalCall(); |
| 1173 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1174 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1175 | |
| 1176 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1177 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1178 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1180 | #ifdef HAVE_USABLE_WCHAR_T |
| 1181 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1182 | #else |
| 1183 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1184 | register Py_UNICODE *u; |
| 1185 | register Py_ssize_t i; |
| 1186 | u = PyUnicode_AS_UNICODE(unicode); |
| 1187 | for (i = size; i > 0; i--) |
| 1188 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1189 | } |
| 1190 | #endif |
| 1191 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1192 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1193 | return PyUnicode_GET_SIZE(unicode); |
| 1194 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1195 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | #endif |
| 1199 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1200 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1201 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1202 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1203 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1204 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1205 | PyErr_SetString(PyExc_ValueError, |
| 1206 | "chr() arg not in range(0x110000)"); |
| 1207 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1208 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1209 | |
| 1210 | #ifndef Py_UNICODE_WIDE |
| 1211 | if (ordinal > 0xffff) { |
| 1212 | ordinal -= 0x10000; |
| 1213 | s[0] = 0xD800 | (ordinal >> 10); |
| 1214 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1215 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1216 | } |
| 1217 | #endif |
| 1218 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1219 | s[0] = (Py_UNICODE)ordinal; |
| 1220 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1223 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1224 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1225 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1226 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1227 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1228 | Py_INCREF(obj); |
| 1229 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1230 | } |
| 1231 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1232 | /* For a Unicode subtype that's not a Unicode object, |
| 1233 | return a true Unicode object with the same data. */ |
| 1234 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1235 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1236 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1237 | PyErr_Format(PyExc_TypeError, |
| 1238 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1239 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1240 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1244 | const char *encoding, |
| 1245 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1246 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1247 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1248 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1249 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1251 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1252 | PyErr_BadInternalCall(); |
| 1253 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1254 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1255 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1256 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1257 | PyErr_SetString(PyExc_TypeError, |
| 1258 | "decoding str is not supported"); |
| 1259 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1260 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1263 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1264 | s = PyBytes_AS_STRING(obj); |
| 1265 | len = PyBytes_GET_SIZE(obj); |
| 1266 | } |
| 1267 | else if (PyByteArray_Check(obj)) { |
| 1268 | s = PyByteArray_AS_STRING(obj); |
| 1269 | len = PyByteArray_GET_SIZE(obj); |
| 1270 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1271 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1272 | /* Overwrite the error message with something more useful in |
| 1273 | case of a TypeError. */ |
| 1274 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1275 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1276 | "coercing to str: need string or buffer, " |
| 1277 | "%.80s found", |
| 1278 | Py_TYPE(obj)->tp_name); |
| 1279 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1280 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1281 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1282 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1283 | if (len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1284 | Py_INCREF(unicode_empty); |
| 1285 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1286 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1287 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1288 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1289 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1290 | return v; |
| 1291 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1292 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1293 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1297 | Py_ssize_t size, |
| 1298 | const char *encoding, |
| 1299 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1300 | { |
| 1301 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1302 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1303 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1304 | char *l; |
| 1305 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1306 | |
| 1307 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1308 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1309 | |
| 1310 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1311 | catch e.g. UTF_8 */ |
| 1312 | e = encoding; |
| 1313 | l = lower; |
| 1314 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1315 | if (ISUPPER(*e)) { |
| 1316 | *l++ = TOLOWER(*e++); |
| 1317 | } |
| 1318 | else if (*e == '_') { |
| 1319 | *l++ = '-'; |
| 1320 | e++; |
| 1321 | } |
| 1322 | else { |
| 1323 | *l++ = *e++; |
| 1324 | } |
| 1325 | } |
| 1326 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1329 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1330 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1331 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1332 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1333 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1334 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1335 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1336 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1337 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1338 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1339 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1340 | else if (strcmp(lower, "utf-16") == 0) |
| 1341 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1342 | else if (strcmp(lower, "utf-32") == 0) |
| 1343 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1344 | |
| 1345 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1346 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1347 | if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_FULL_RO) < 0) |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1348 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1349 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1350 | if (buffer == NULL) |
| 1351 | goto onError; |
| 1352 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1353 | if (unicode == NULL) |
| 1354 | goto onError; |
| 1355 | if (!PyUnicode_Check(unicode)) { |
| 1356 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1357 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1358 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1359 | Py_DECREF(unicode); |
| 1360 | goto onError; |
| 1361 | } |
| 1362 | Py_DECREF(buffer); |
| 1363 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1364 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1365 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1366 | Py_XDECREF(buffer); |
| 1367 | return NULL; |
| 1368 | } |
| 1369 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1370 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1371 | const char *encoding, |
| 1372 | const char *errors) |
| 1373 | { |
| 1374 | PyObject *v; |
| 1375 | |
| 1376 | if (!PyUnicode_Check(unicode)) { |
| 1377 | PyErr_BadArgument(); |
| 1378 | goto onError; |
| 1379 | } |
| 1380 | |
| 1381 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1382 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1383 | |
| 1384 | /* Decode via the codec registry */ |
| 1385 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1386 | if (v == NULL) |
| 1387 | goto onError; |
| 1388 | return v; |
| 1389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1390 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1391 | return NULL; |
| 1392 | } |
| 1393 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1394 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1395 | const char *encoding, |
| 1396 | const char *errors) |
| 1397 | { |
| 1398 | PyObject *v; |
| 1399 | |
| 1400 | if (!PyUnicode_Check(unicode)) { |
| 1401 | PyErr_BadArgument(); |
| 1402 | goto onError; |
| 1403 | } |
| 1404 | |
| 1405 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1406 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1407 | |
| 1408 | /* Decode via the codec registry */ |
| 1409 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1410 | if (v == NULL) |
| 1411 | goto onError; |
| 1412 | if (!PyUnicode_Check(v)) { |
| 1413 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1414 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1415 | Py_TYPE(v)->tp_name); |
| 1416 | Py_DECREF(v); |
| 1417 | goto onError; |
| 1418 | } |
| 1419 | return v; |
| 1420 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1421 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1422 | return NULL; |
| 1423 | } |
| 1424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1425 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1426 | Py_ssize_t size, |
| 1427 | const char *encoding, |
| 1428 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1429 | { |
| 1430 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1432 | unicode = PyUnicode_FromUnicode(s, size); |
| 1433 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1434 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1435 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1436 | Py_DECREF(unicode); |
| 1437 | return v; |
| 1438 | } |
| 1439 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1440 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1441 | const char *encoding, |
| 1442 | const char *errors) |
| 1443 | { |
| 1444 | PyObject *v; |
| 1445 | |
| 1446 | if (!PyUnicode_Check(unicode)) { |
| 1447 | PyErr_BadArgument(); |
| 1448 | goto onError; |
| 1449 | } |
| 1450 | |
| 1451 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1452 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1453 | |
| 1454 | /* Encode via the codec registry */ |
| 1455 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1456 | if (v == NULL) |
| 1457 | goto onError; |
| 1458 | return v; |
| 1459 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1460 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1461 | return NULL; |
| 1462 | } |
| 1463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1464 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1465 | const char *encoding, |
| 1466 | const char *errors) |
| 1467 | { |
| 1468 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1469 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1470 | if (!PyUnicode_Check(unicode)) { |
| 1471 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1472 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1473 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1474 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1475 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1476 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1477 | |
| 1478 | /* Shortcuts for common default encodings */ |
| 1479 | if (errors == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1480 | if (strcmp(encoding, "utf-8") == 0) |
| 1481 | return PyUnicode_AsUTF8String(unicode); |
| 1482 | else if (strcmp(encoding, "latin-1") == 0) |
| 1483 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1484 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1485 | else if (strcmp(encoding, "mbcs") == 0) |
| 1486 | return PyUnicode_AsMBCSString(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1487 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1488 | else if (strcmp(encoding, "ascii") == 0) |
| 1489 | return PyUnicode_AsASCIIString(unicode); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1490 | /* During bootstrap, we may need to find the encodings |
| 1491 | package, to load the file system encoding, and require the |
| 1492 | file system encoding in order to load the encodings |
| 1493 | package. |
| 1494 | |
| 1495 | Break out of this dependency by assuming that the path to |
| 1496 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1497 | instead, if the file system encoding is the locale's |
| 1498 | encoding. */ |
| 1499 | else if (Py_FileSystemDefaultEncoding && |
| 1500 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1501 | !PyThreadState_GET()->interp->codecs_initialized) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1502 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1503 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1504 | |
| 1505 | /* Encode via the codec registry */ |
| 1506 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1507 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1508 | return NULL; |
| 1509 | |
| 1510 | /* The normal path */ |
| 1511 | if (PyBytes_Check(v)) |
| 1512 | return v; |
| 1513 | |
| 1514 | /* 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] | 1515 | if (PyByteArray_Check(v)) { |
| 1516 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1517 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1518 | PyOS_snprintf(msg, sizeof(msg), |
| 1519 | "encoder %s returned buffer instead of bytes", |
| 1520 | encoding); |
| 1521 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1522 | Py_DECREF(v); |
| 1523 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1524 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1525 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1526 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1527 | Py_DECREF(v); |
| 1528 | return b; |
| 1529 | } |
| 1530 | |
| 1531 | PyErr_Format(PyExc_TypeError, |
| 1532 | "encoder did not return a bytes object (type=%.400s)", |
| 1533 | Py_TYPE(v)->tp_name); |
| 1534 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1535 | return NULL; |
| 1536 | } |
| 1537 | |
| 1538 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1539 | const char *encoding, |
| 1540 | const char *errors) |
| 1541 | { |
| 1542 | PyObject *v; |
| 1543 | |
| 1544 | if (!PyUnicode_Check(unicode)) { |
| 1545 | PyErr_BadArgument(); |
| 1546 | goto onError; |
| 1547 | } |
| 1548 | |
| 1549 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1550 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* Encode via the codec registry */ |
| 1553 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1554 | if (v == NULL) |
| 1555 | goto onError; |
| 1556 | if (!PyUnicode_Check(v)) { |
| 1557 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1558 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1559 | Py_TYPE(v)->tp_name); |
| 1560 | Py_DECREF(v); |
| 1561 | goto onError; |
| 1562 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1563 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1564 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1565 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1566 | return NULL; |
| 1567 | } |
| 1568 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1569 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1570 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1571 | { |
| 1572 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1573 | if (v) |
| 1574 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1575 | if (errors != NULL) |
| 1576 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1577 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1578 | PyUnicode_GET_SIZE(unicode), |
| 1579 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1580 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1581 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1582 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1583 | return v; |
| 1584 | } |
| 1585 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1586 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1587 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1588 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1589 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1590 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1591 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1592 | PyObject* |
| 1593 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1594 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1595 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1596 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1597 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1598 | bootstrapping process where the codecs aren't ready yet. |
| 1599 | */ |
| 1600 | if (Py_FileSystemDefaultEncoding) { |
| 1601 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1602 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1603 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1604 | } |
| 1605 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1606 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1607 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1608 | } |
| 1609 | #endif |
| 1610 | return PyUnicode_Decode(s, size, |
| 1611 | Py_FileSystemDefaultEncoding, |
| 1612 | "replace"); |
| 1613 | } |
| 1614 | else { |
| 1615 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1616 | } |
| 1617 | } |
| 1618 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1619 | /* Convert the argument to a bytes object, according to the file |
Gregory P. Smith | cc47d8c | 2010-02-27 08:33:11 +0000 | [diff] [blame] | 1620 | system encoding. The addr param must be a PyObject**. |
| 1621 | 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] | 1622 | |
| 1623 | int |
| 1624 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1625 | { |
| 1626 | PyObject *output = NULL; |
| 1627 | Py_ssize_t size; |
| 1628 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1629 | if (arg == NULL) { |
| 1630 | Py_DECREF(*(PyObject**)addr); |
| 1631 | return 1; |
| 1632 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1633 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1634 | output = arg; |
| 1635 | Py_INCREF(output); |
| 1636 | } |
| 1637 | else { |
| 1638 | arg = PyUnicode_FromObject(arg); |
| 1639 | if (!arg) |
| 1640 | return 0; |
| 1641 | output = PyUnicode_AsEncodedObject(arg, |
| 1642 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1643 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1644 | Py_DECREF(arg); |
| 1645 | if (!output) |
| 1646 | return 0; |
| 1647 | if (!PyBytes_Check(output)) { |
| 1648 | Py_DECREF(output); |
| 1649 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1650 | return 0; |
| 1651 | } |
| 1652 | } |
| 1653 | if (PyBytes_Check(output)) { |
| 1654 | size = PyBytes_GET_SIZE(output); |
| 1655 | data = PyBytes_AS_STRING(output); |
| 1656 | } |
| 1657 | else { |
| 1658 | size = PyByteArray_GET_SIZE(output); |
| 1659 | data = PyByteArray_AS_STRING(output); |
| 1660 | } |
| 1661 | if (size != strlen(data)) { |
| 1662 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1663 | Py_DECREF(output); |
| 1664 | return 0; |
| 1665 | } |
| 1666 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1667 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1671 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1672 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1673 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1674 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1675 | if (!PyUnicode_Check(unicode)) { |
| 1676 | PyErr_BadArgument(); |
| 1677 | return NULL; |
| 1678 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1679 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1680 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1681 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1682 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1683 | *psize = PyBytes_GET_SIZE(bytes); |
| 1684 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1688 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1689 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1690 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1693 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1694 | { |
| 1695 | if (!PyUnicode_Check(unicode)) { |
| 1696 | PyErr_BadArgument(); |
| 1697 | goto onError; |
| 1698 | } |
| 1699 | return PyUnicode_AS_UNICODE(unicode); |
| 1700 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1701 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1702 | return NULL; |
| 1703 | } |
| 1704 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1705 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1706 | { |
| 1707 | if (!PyUnicode_Check(unicode)) { |
| 1708 | PyErr_BadArgument(); |
| 1709 | goto onError; |
| 1710 | } |
| 1711 | return PyUnicode_GET_SIZE(unicode); |
| 1712 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1713 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1714 | return -1; |
| 1715 | } |
| 1716 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1717 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1718 | { |
| 1719 | return unicode_default_encoding; |
| 1720 | } |
| 1721 | |
| 1722 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1723 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1724 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1725 | PyErr_Format(PyExc_ValueError, |
| 1726 | "Can only set default encoding to %s", |
| 1727 | unicode_default_encoding); |
| 1728 | return -1; |
| 1729 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1730 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1733 | /* error handling callback helper: |
| 1734 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1735 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1736 | and adjust various state variables. |
| 1737 | return 0 on success, -1 on error |
| 1738 | */ |
| 1739 | |
| 1740 | static |
| 1741 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1742 | const char *encoding, const char *reason, |
| 1743 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1744 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1745 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1746 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1747 | 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] | 1748 | |
| 1749 | PyObject *restuple = NULL; |
| 1750 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1751 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1752 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1753 | Py_ssize_t requiredsize; |
| 1754 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1755 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1756 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1757 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1758 | int res = -1; |
| 1759 | |
| 1760 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1761 | *errorHandler = PyCodec_LookupError(errors); |
| 1762 | if (*errorHandler == NULL) |
| 1763 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1767 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1768 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
| 1769 | if (*exceptionObject == NULL) |
| 1770 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1771 | } |
| 1772 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1773 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1774 | goto onError; |
| 1775 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1776 | goto onError; |
| 1777 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1778 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1782 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1783 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1784 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1785 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1786 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1787 | } |
| 1788 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1789 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1790 | |
| 1791 | /* Copy back the bytes variables, which might have been modified by the |
| 1792 | callback */ |
| 1793 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1794 | if (!inputobj) |
| 1795 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1796 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1797 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1798 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1799 | *input = PyBytes_AS_STRING(inputobj); |
| 1800 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1801 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1802 | /* we can DECREF safely, as the exception has another reference, |
| 1803 | so the object won't go away. */ |
| 1804 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1805 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1806 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1807 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1808 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1809 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1810 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1811 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1812 | |
| 1813 | /* need more space? (at least enough for what we |
| 1814 | have+the replacement+the rest of the string (starting |
| 1815 | at the new input position), so we won't have to check space |
| 1816 | when there are no errors in the rest of the string) */ |
| 1817 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1818 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1819 | requiredsize = *outpos + repsize + insize-newpos; |
| 1820 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1821 | if (requiredsize<2*outsize) |
| 1822 | requiredsize = 2*outsize; |
| 1823 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1824 | goto onError; |
| 1825 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1826 | } |
| 1827 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1828 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1829 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1830 | *outptr += repsize; |
| 1831 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1832 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1833 | /* we made it! */ |
| 1834 | res = 0; |
| 1835 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1836 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1837 | Py_XDECREF(restuple); |
| 1838 | return res; |
| 1839 | } |
| 1840 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1841 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1842 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1843 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1844 | |
| 1845 | /* Three simple macros defining base-64. */ |
| 1846 | |
| 1847 | /* Is c a base-64 character? */ |
| 1848 | |
| 1849 | #define IS_BASE64(c) \ |
| 1850 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1851 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1852 | ((c) >= '0' && (c) <= '9') || \ |
| 1853 | (c) == '+' || (c) == '/') |
| 1854 | |
| 1855 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1856 | |
| 1857 | #define FROM_BASE64(c) \ |
| 1858 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1859 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1860 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1861 | (c) == '+' ? 62 : 63) |
| 1862 | |
| 1863 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1864 | |
| 1865 | #define TO_BASE64(n) \ |
| 1866 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1867 | |
| 1868 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1869 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1870 | * byte not decoding to itself is the + which begins a base64 |
| 1871 | * string. */ |
| 1872 | |
| 1873 | #define DECODE_DIRECT(c) \ |
| 1874 | ((c) <= 127 && (c) != '+') |
| 1875 | |
| 1876 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1877 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1878 | * the above). See RFC2152. This array identifies these different |
| 1879 | * sets: |
| 1880 | * 0 : "Set D" |
| 1881 | * alphanumeric and '(),-./:? |
| 1882 | * 1 : "Set O" |
| 1883 | * !"#$%&*;<=>@[]^_`{|} |
| 1884 | * 2 : "whitespace" |
| 1885 | * ht nl cr sp |
| 1886 | * 3 : special (must be base64 encoded) |
| 1887 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1888 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1889 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1890 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1891 | char utf7_category[128] = { |
| 1892 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 1893 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 1894 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 1895 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1896 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 1897 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 1898 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 1899 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 1900 | /* @ A B C D E F G H I J K L M N O */ |
| 1901 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1902 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 1903 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 1904 | /* ` a b c d e f g h i j k l m n o */ |
| 1905 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1906 | /* p q r s t u v w x y z { | } ~ del */ |
| 1907 | 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] | 1908 | }; |
| 1909 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1910 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 1911 | * answer depends on whether we are encoding set O as itself, and also |
| 1912 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 1913 | * clear that the answers to these questions vary between |
| 1914 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1915 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1916 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 1917 | ((c) < 128 && (c) > 0 && \ |
| 1918 | ((utf7_category[(c)] == 0) || \ |
| 1919 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 1920 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1921 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1922 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1923 | Py_ssize_t size, |
| 1924 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1925 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1926 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1927 | } |
| 1928 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1929 | /* The decoder. The only state we preserve is our read position, |
| 1930 | * i.e. how many characters we have consumed. So if we end in the |
| 1931 | * middle of a shift sequence we have to back off the read position |
| 1932 | * and the output to the beginning of the sequence, otherwise we lose |
| 1933 | * all the shift state (seen bits, number of bits seen, high |
| 1934 | * surrogate). */ |
| 1935 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1936 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1937 | Py_ssize_t size, |
| 1938 | const char *errors, |
| 1939 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1940 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1941 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1942 | Py_ssize_t startinpos; |
| 1943 | Py_ssize_t endinpos; |
| 1944 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1945 | const char *e; |
| 1946 | PyUnicodeObject *unicode; |
| 1947 | Py_UNICODE *p; |
| 1948 | const char *errmsg = ""; |
| 1949 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1950 | Py_UNICODE *shiftOutStart; |
| 1951 | unsigned int base64bits = 0; |
| 1952 | unsigned long base64buffer = 0; |
| 1953 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1954 | PyObject *errorHandler = NULL; |
| 1955 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1956 | |
| 1957 | unicode = _PyUnicode_New(size); |
| 1958 | if (!unicode) |
| 1959 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1960 | if (size == 0) { |
| 1961 | if (consumed) |
| 1962 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1963 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1964 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1965 | |
| 1966 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1967 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1968 | e = s + size; |
| 1969 | |
| 1970 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1971 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1972 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1973 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1974 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1975 | if (inShift) { /* in a base-64 section */ |
| 1976 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 1977 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 1978 | base64bits += 6; |
| 1979 | s++; |
| 1980 | if (base64bits >= 16) { |
| 1981 | /* we have enough bits for a UTF-16 value */ |
| 1982 | Py_UNICODE outCh = (Py_UNICODE) |
| 1983 | (base64buffer >> (base64bits-16)); |
| 1984 | base64bits -= 16; |
| 1985 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 1986 | if (surrogate) { |
| 1987 | /* expecting a second surrogate */ |
| 1988 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 1989 | #ifdef Py_UNICODE_WIDE |
| 1990 | *p++ = (((surrogate & 0x3FF)<<10) |
| 1991 | | (outCh & 0x3FF)) + 0x10000; |
| 1992 | #else |
| 1993 | *p++ = surrogate; |
| 1994 | *p++ = outCh; |
| 1995 | #endif |
| 1996 | surrogate = 0; |
| 1997 | } |
| 1998 | else { |
| 1999 | surrogate = 0; |
| 2000 | errmsg = "second surrogate missing"; |
| 2001 | goto utf7Error; |
| 2002 | } |
| 2003 | } |
| 2004 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2005 | /* first surrogate */ |
| 2006 | surrogate = outCh; |
| 2007 | } |
| 2008 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2009 | errmsg = "unexpected second surrogate"; |
| 2010 | goto utf7Error; |
| 2011 | } |
| 2012 | else { |
| 2013 | *p++ = outCh; |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2018 | inShift = 0; |
| 2019 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2020 | if (surrogate) { |
| 2021 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2022 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2023 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2024 | if (base64bits > 0) { /* left-over bits */ |
| 2025 | if (base64bits >= 6) { |
| 2026 | /* We've seen at least one base-64 character */ |
| 2027 | errmsg = "partial character in shift sequence"; |
| 2028 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2029 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2030 | else { |
| 2031 | /* Some bits remain; they should be zero */ |
| 2032 | if (base64buffer != 0) { |
| 2033 | errmsg = "non-zero padding bits in shift sequence"; |
| 2034 | goto utf7Error; |
| 2035 | } |
| 2036 | } |
| 2037 | } |
| 2038 | if (ch != '-') { |
| 2039 | /* '-' is absorbed; other terminating |
| 2040 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2041 | *p++ = ch; |
| 2042 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2046 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2047 | s++; /* consume '+' */ |
| 2048 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2049 | s++; |
| 2050 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2051 | } |
| 2052 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2053 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2054 | shiftOutStart = p; |
| 2055 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2056 | } |
| 2057 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2058 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2059 | *p++ = ch; |
| 2060 | s++; |
| 2061 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2062 | else { |
| 2063 | startinpos = s-starts; |
| 2064 | s++; |
| 2065 | errmsg = "unexpected special character"; |
| 2066 | goto utf7Error; |
| 2067 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2068 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2069 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2070 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2071 | endinpos = s-starts; |
| 2072 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2073 | errors, &errorHandler, |
| 2074 | "utf7", errmsg, |
| 2075 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2076 | &unicode, &outpos, &p)) |
| 2077 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2080 | /* end of string */ |
| 2081 | |
| 2082 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2083 | /* if we're in an inconsistent state, that's an error */ |
| 2084 | if (surrogate || |
| 2085 | (base64bits >= 6) || |
| 2086 | (base64bits > 0 && base64buffer != 0)) { |
| 2087 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2088 | endinpos = size; |
| 2089 | if (unicode_decode_call_errorhandler( |
| 2090 | errors, &errorHandler, |
| 2091 | "utf7", "unterminated shift sequence", |
| 2092 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2093 | &unicode, &outpos, &p)) |
| 2094 | goto onError; |
| 2095 | if (s < e) |
| 2096 | goto restart; |
| 2097 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2098 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2099 | |
| 2100 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2101 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2102 | if (inShift) { |
| 2103 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2104 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2105 | } |
| 2106 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2107 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2108 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2109 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2110 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2111 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2112 | goto onError; |
| 2113 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2114 | Py_XDECREF(errorHandler); |
| 2115 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2116 | return (PyObject *)unicode; |
| 2117 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2118 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2119 | Py_XDECREF(errorHandler); |
| 2120 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2121 | Py_DECREF(unicode); |
| 2122 | return NULL; |
| 2123 | } |
| 2124 | |
| 2125 | |
| 2126 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2127 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2128 | int base64SetO, |
| 2129 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2130 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2131 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2132 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2133 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2134 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2135 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2136 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2137 | unsigned int base64bits = 0; |
| 2138 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2139 | char * out; |
| 2140 | char * start; |
| 2141 | |
| 2142 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2143 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2144 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2145 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2146 | return PyErr_NoMemory(); |
| 2147 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2148 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2149 | if (v == NULL) |
| 2150 | return NULL; |
| 2151 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2152 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2153 | for (;i < size; ++i) { |
| 2154 | Py_UNICODE ch = s[i]; |
| 2155 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2156 | if (inShift) { |
| 2157 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2158 | /* shifting out */ |
| 2159 | if (base64bits) { /* output remaining bits */ |
| 2160 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2161 | base64buffer = 0; |
| 2162 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2163 | } |
| 2164 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2165 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2166 | so no '-' is required, except if the character is itself a '-' */ |
| 2167 | if (IS_BASE64(ch) || ch == '-') { |
| 2168 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2169 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2170 | *out++ = (char) ch; |
| 2171 | } |
| 2172 | else { |
| 2173 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2174 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2175 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2176 | else { /* not in a shift sequence */ |
| 2177 | if (ch == '+') { |
| 2178 | *out++ = '+'; |
| 2179 | *out++ = '-'; |
| 2180 | } |
| 2181 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2182 | *out++ = (char) ch; |
| 2183 | } |
| 2184 | else { |
| 2185 | *out++ = '+'; |
| 2186 | inShift = 1; |
| 2187 | goto encode_char; |
| 2188 | } |
| 2189 | } |
| 2190 | continue; |
| 2191 | encode_char: |
| 2192 | #ifdef Py_UNICODE_WIDE |
| 2193 | if (ch >= 0x10000) { |
| 2194 | /* code first surrogate */ |
| 2195 | base64bits += 16; |
| 2196 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2197 | while (base64bits >= 6) { |
| 2198 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2199 | base64bits -= 6; |
| 2200 | } |
| 2201 | /* prepare second surrogate */ |
| 2202 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2203 | } |
| 2204 | #endif |
| 2205 | base64bits += 16; |
| 2206 | base64buffer = (base64buffer << 16) | ch; |
| 2207 | while (base64bits >= 6) { |
| 2208 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2209 | base64bits -= 6; |
| 2210 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2211 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2212 | if (base64bits) |
| 2213 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2214 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2215 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2216 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2217 | return NULL; |
| 2218 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2221 | #undef IS_BASE64 |
| 2222 | #undef FROM_BASE64 |
| 2223 | #undef TO_BASE64 |
| 2224 | #undef DECODE_DIRECT |
| 2225 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2226 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2227 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2228 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2229 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2230 | char utf8_code_length[256] = { |
| 2231 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 2232 | illegal prefix. see RFC 2279 for details */ |
| 2233 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2234 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2235 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2236 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2237 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2238 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2239 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2240 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2241 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2242 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2243 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2244 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2245 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2246 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2247 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2248 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 2249 | }; |
| 2250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2251 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2252 | Py_ssize_t size, |
| 2253 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2254 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2255 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2256 | } |
| 2257 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2258 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2259 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2260 | |
| 2261 | /* Mask to quickly check whether a C 'long' contains a |
| 2262 | non-ASCII, UTF8-encoded char. */ |
| 2263 | #if (SIZEOF_LONG == 8) |
| 2264 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2265 | #elif (SIZEOF_LONG == 4) |
| 2266 | # define ASCII_CHAR_MASK 0x80808080L |
| 2267 | #else |
| 2268 | # error C 'long' size should be either 4 or 8! |
| 2269 | #endif |
| 2270 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2271 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2272 | Py_ssize_t size, |
| 2273 | const char *errors, |
| 2274 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2275 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2276 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2277 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2278 | Py_ssize_t startinpos; |
| 2279 | Py_ssize_t endinpos; |
| 2280 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2281 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2282 | PyUnicodeObject *unicode; |
| 2283 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2284 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2285 | PyObject *errorHandler = NULL; |
| 2286 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2287 | |
| 2288 | /* Note: size will always be longer than the resulting Unicode |
| 2289 | character count */ |
| 2290 | unicode = _PyUnicode_New(size); |
| 2291 | if (!unicode) |
| 2292 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2293 | if (size == 0) { |
| 2294 | if (consumed) |
| 2295 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2296 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2297 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2298 | |
| 2299 | /* Unpack UTF-8 encoded data */ |
| 2300 | p = unicode->str; |
| 2301 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2302 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2303 | |
| 2304 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2305 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2306 | |
| 2307 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2308 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2309 | input will consist of an overwhelming majority of ASCII |
| 2310 | characters, we try to optimize for this case by checking |
| 2311 | as many characters as a C 'long' can contain. |
| 2312 | First, check if we can do an aligned read, as most CPUs have |
| 2313 | a penalty for unaligned reads. |
| 2314 | */ |
| 2315 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2316 | /* Help register allocation */ |
| 2317 | register const char *_s = s; |
| 2318 | register Py_UNICODE *_p = p; |
| 2319 | while (_s < aligned_end) { |
| 2320 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2321 | and do a fast unrolled copy if it only contains ASCII |
| 2322 | characters. */ |
| 2323 | unsigned long data = *(unsigned long *) _s; |
| 2324 | if (data & ASCII_CHAR_MASK) |
| 2325 | break; |
| 2326 | _p[0] = (unsigned char) _s[0]; |
| 2327 | _p[1] = (unsigned char) _s[1]; |
| 2328 | _p[2] = (unsigned char) _s[2]; |
| 2329 | _p[3] = (unsigned char) _s[3]; |
| 2330 | #if (SIZEOF_LONG == 8) |
| 2331 | _p[4] = (unsigned char) _s[4]; |
| 2332 | _p[5] = (unsigned char) _s[5]; |
| 2333 | _p[6] = (unsigned char) _s[6]; |
| 2334 | _p[7] = (unsigned char) _s[7]; |
| 2335 | #endif |
| 2336 | _s += SIZEOF_LONG; |
| 2337 | _p += SIZEOF_LONG; |
| 2338 | } |
| 2339 | s = _s; |
| 2340 | p = _p; |
| 2341 | if (s == e) |
| 2342 | break; |
| 2343 | ch = (unsigned char)*s; |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2348 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2349 | s++; |
| 2350 | continue; |
| 2351 | } |
| 2352 | |
| 2353 | n = utf8_code_length[ch]; |
| 2354 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2355 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2356 | if (consumed) |
| 2357 | break; |
| 2358 | else { |
| 2359 | errmsg = "unexpected end of data"; |
| 2360 | startinpos = s-starts; |
| 2361 | endinpos = size; |
| 2362 | goto utf8Error; |
| 2363 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2364 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2365 | |
| 2366 | switch (n) { |
| 2367 | |
| 2368 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2369 | errmsg = "unexpected code byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2370 | startinpos = s-starts; |
| 2371 | endinpos = startinpos+1; |
| 2372 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2373 | |
| 2374 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2375 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2376 | startinpos = s-starts; |
| 2377 | endinpos = startinpos+1; |
| 2378 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2379 | |
| 2380 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2381 | if ((s[1] & 0xc0) != 0x80) { |
| 2382 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2383 | startinpos = s-starts; |
| 2384 | endinpos = startinpos+2; |
| 2385 | goto utf8Error; |
| 2386 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2387 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2388 | if (ch < 0x80) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2389 | startinpos = s-starts; |
| 2390 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2391 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2392 | goto utf8Error; |
| 2393 | } |
| 2394 | else |
| 2395 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2396 | break; |
| 2397 | |
| 2398 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2399 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2400 | (s[2] & 0xc0) != 0x80) { |
| 2401 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2402 | startinpos = s-starts; |
| 2403 | endinpos = startinpos+3; |
| 2404 | goto utf8Error; |
| 2405 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2406 | 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] | 2407 | if (ch < 0x0800 || (ch >= 0xd800 && ch <= 0xDFFF)) { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2408 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2409 | startinpos = s-starts; |
| 2410 | endinpos = startinpos+3; |
| 2411 | goto utf8Error; |
| 2412 | } |
| 2413 | else |
| 2414 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2415 | break; |
| 2416 | |
| 2417 | case 4: |
| 2418 | if ((s[1] & 0xc0) != 0x80 || |
| 2419 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2420 | (s[3] & 0xc0) != 0x80) { |
| 2421 | errmsg = "invalid data"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2422 | startinpos = s-starts; |
| 2423 | endinpos = startinpos+4; |
| 2424 | goto utf8Error; |
| 2425 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2426 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2427 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2428 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2429 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2430 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2431 | || (ch > 0x10ffff)) /* maximum value allowed for |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2432 | UTF-16 */ |
| 2433 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2434 | errmsg = "illegal encoding"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2435 | startinpos = s-starts; |
| 2436 | endinpos = startinpos+4; |
| 2437 | goto utf8Error; |
| 2438 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2439 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2440 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2441 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2442 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2443 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2444 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2445 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2446 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2447 | /* high surrogate = top 10 bits added to D800 */ |
| 2448 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2449 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2450 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2451 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2452 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2453 | break; |
| 2454 | |
| 2455 | default: |
| 2456 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2457 | errmsg = "unsupported Unicode code range"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2458 | startinpos = s-starts; |
| 2459 | endinpos = startinpos+n; |
| 2460 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2461 | } |
| 2462 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2463 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2464 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2465 | utf8Error: |
| 2466 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2467 | if (unicode_decode_call_errorhandler( |
| 2468 | errors, &errorHandler, |
| 2469 | "utf8", errmsg, |
| 2470 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2471 | &unicode, &outpos, &p)) |
| 2472 | goto onError; |
| 2473 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2474 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2475 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2476 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2477 | |
| 2478 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2479 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2480 | goto onError; |
| 2481 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2482 | Py_XDECREF(errorHandler); |
| 2483 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2484 | return (PyObject *)unicode; |
| 2485 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2486 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2487 | Py_XDECREF(errorHandler); |
| 2488 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2489 | Py_DECREF(unicode); |
| 2490 | return NULL; |
| 2491 | } |
| 2492 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2493 | #undef ASCII_CHAR_MASK |
| 2494 | |
| 2495 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2496 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2497 | and allocate exactly as much space needed at the end. Else allocate the |
| 2498 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2499 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2500 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2501 | PyObject * |
| 2502 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2503 | Py_ssize_t size, |
| 2504 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2505 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2506 | #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] | 2507 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2508 | Py_ssize_t i; /* index into s of next input byte */ |
| 2509 | PyObject *result; /* result string object */ |
| 2510 | char *p; /* next free byte in output buffer */ |
| 2511 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2512 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2513 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2514 | PyObject *errorHandler = NULL; |
| 2515 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2516 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2517 | assert(s != NULL); |
| 2518 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2519 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2520 | if (size <= MAX_SHORT_UNICHARS) { |
| 2521 | /* Write into the stack buffer; nallocated can't overflow. |
| 2522 | * At the end, we'll allocate exactly as much heap space as it |
| 2523 | * turns out we need. |
| 2524 | */ |
| 2525 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2526 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2527 | p = stackbuf; |
| 2528 | } |
| 2529 | else { |
| 2530 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2531 | nallocated = size * 4; |
| 2532 | if (nallocated / 4 != size) /* overflow! */ |
| 2533 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2534 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2535 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2536 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2537 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2538 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2539 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2540 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2541 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2542 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2543 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2544 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2545 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2546 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2547 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2548 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2549 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2550 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2551 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2552 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2553 | /* Special case: check for high and low surrogate */ |
| 2554 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2555 | Py_UCS4 ch2 = s[i]; |
| 2556 | /* Combine the two surrogates to form a UCS4 value */ |
| 2557 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2558 | i++; |
| 2559 | |
| 2560 | /* Encode UCS4 Unicode ordinals */ |
| 2561 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2562 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2563 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2564 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2565 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame^] | 2566 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2567 | Py_ssize_t newpos; |
| 2568 | PyObject *rep; |
| 2569 | Py_ssize_t repsize, k; |
| 2570 | rep = unicode_encode_call_errorhandler |
| 2571 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2572 | s, size, &exc, i-1, i, &newpos); |
| 2573 | if (!rep) |
| 2574 | goto error; |
| 2575 | |
| 2576 | if (PyBytes_Check(rep)) |
| 2577 | repsize = PyBytes_GET_SIZE(rep); |
| 2578 | else |
| 2579 | repsize = PyUnicode_GET_SIZE(rep); |
| 2580 | |
| 2581 | if (repsize > 4) { |
| 2582 | Py_ssize_t offset; |
| 2583 | |
| 2584 | if (result == NULL) |
| 2585 | offset = p - stackbuf; |
| 2586 | else |
| 2587 | offset = p - PyBytes_AS_STRING(result); |
| 2588 | |
| 2589 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2590 | /* integer overflow */ |
| 2591 | PyErr_NoMemory(); |
| 2592 | goto error; |
| 2593 | } |
| 2594 | nallocated += repsize - 4; |
| 2595 | if (result != NULL) { |
| 2596 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2597 | goto error; |
| 2598 | } else { |
| 2599 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2600 | if (result == NULL) |
| 2601 | goto error; |
| 2602 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2603 | } |
| 2604 | p = PyBytes_AS_STRING(result) + offset; |
| 2605 | } |
| 2606 | |
| 2607 | if (PyBytes_Check(rep)) { |
| 2608 | char *prep = PyBytes_AS_STRING(rep); |
| 2609 | for(k = repsize; k > 0; k--) |
| 2610 | *p++ = *prep++; |
| 2611 | } else /* rep is unicode */ { |
| 2612 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2613 | Py_UNICODE c; |
| 2614 | |
| 2615 | for(k=0; k<repsize; k++) { |
| 2616 | c = prep[k]; |
| 2617 | if (0x80 <= c) { |
| 2618 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2619 | i-1, i, "surrogates not allowed"); |
| 2620 | goto error; |
| 2621 | } |
| 2622 | *p++ = (char)prep[k]; |
| 2623 | } |
| 2624 | } |
| 2625 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame^] | 2626 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2627 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame^] | 2628 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2629 | } else if (ch < 0x10000) { |
| 2630 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2631 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2632 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2633 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2634 | /* Encode UCS4 Unicode ordinals */ |
| 2635 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2636 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2637 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2638 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2639 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2640 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2641 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2642 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2643 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2644 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2645 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2646 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2647 | } |
| 2648 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2649 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2650 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2651 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2652 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2653 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2654 | Py_XDECREF(errorHandler); |
| 2655 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2656 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2657 | error: |
| 2658 | Py_XDECREF(errorHandler); |
| 2659 | Py_XDECREF(exc); |
| 2660 | Py_XDECREF(result); |
| 2661 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2662 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2663 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2666 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2667 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2668 | if (!PyUnicode_Check(unicode)) { |
| 2669 | PyErr_BadArgument(); |
| 2670 | return NULL; |
| 2671 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2672 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2673 | PyUnicode_GET_SIZE(unicode), |
| 2674 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2677 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2678 | |
| 2679 | PyObject * |
| 2680 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2681 | Py_ssize_t size, |
| 2682 | const char *errors, |
| 2683 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2684 | { |
| 2685 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2686 | } |
| 2687 | |
| 2688 | PyObject * |
| 2689 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2690 | Py_ssize_t size, |
| 2691 | const char *errors, |
| 2692 | int *byteorder, |
| 2693 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2694 | { |
| 2695 | const char *starts = s; |
| 2696 | Py_ssize_t startinpos; |
| 2697 | Py_ssize_t endinpos; |
| 2698 | Py_ssize_t outpos; |
| 2699 | PyUnicodeObject *unicode; |
| 2700 | Py_UNICODE *p; |
| 2701 | #ifndef Py_UNICODE_WIDE |
| 2702 | int i, pairs; |
| 2703 | #else |
| 2704 | const int pairs = 0; |
| 2705 | #endif |
| 2706 | const unsigned char *q, *e; |
| 2707 | int bo = 0; /* assume native ordering by default */ |
| 2708 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2709 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2710 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2711 | int iorder[] = {0, 1, 2, 3}; |
| 2712 | #else |
| 2713 | int iorder[] = {3, 2, 1, 0}; |
| 2714 | #endif |
| 2715 | PyObject *errorHandler = NULL; |
| 2716 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2717 | /* On narrow builds we split characters outside the BMP into two |
| 2718 | codepoints => count how much extra space we need. */ |
| 2719 | #ifndef Py_UNICODE_WIDE |
| 2720 | for (i = pairs = 0; i < size/4; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2721 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2722 | pairs++; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2723 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2724 | |
| 2725 | /* This might be one to much, because of a BOM */ |
| 2726 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2727 | if (!unicode) |
| 2728 | return NULL; |
| 2729 | if (size == 0) |
| 2730 | return (PyObject *)unicode; |
| 2731 | |
| 2732 | /* Unpack UTF-32 encoded data */ |
| 2733 | p = unicode->str; |
| 2734 | q = (unsigned char *)s; |
| 2735 | e = q + size; |
| 2736 | |
| 2737 | if (byteorder) |
| 2738 | bo = *byteorder; |
| 2739 | |
| 2740 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2741 | byte order setting accordingly. In native mode, the leading BOM |
| 2742 | mark is skipped, in all other modes, it is copied to the output |
| 2743 | stream as-is (giving a ZWNBSP character). */ |
| 2744 | if (bo == 0) { |
| 2745 | if (size >= 4) { |
| 2746 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2747 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2748 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2749 | if (bom == 0x0000FEFF) { |
| 2750 | q += 4; |
| 2751 | bo = -1; |
| 2752 | } |
| 2753 | else if (bom == 0xFFFE0000) { |
| 2754 | q += 4; |
| 2755 | bo = 1; |
| 2756 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2757 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2758 | if (bom == 0x0000FEFF) { |
| 2759 | q += 4; |
| 2760 | bo = 1; |
| 2761 | } |
| 2762 | else if (bom == 0xFFFE0000) { |
| 2763 | q += 4; |
| 2764 | bo = -1; |
| 2765 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2766 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2767 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | if (bo == -1) { |
| 2771 | /* force LE */ |
| 2772 | iorder[0] = 0; |
| 2773 | iorder[1] = 1; |
| 2774 | iorder[2] = 2; |
| 2775 | iorder[3] = 3; |
| 2776 | } |
| 2777 | else if (bo == 1) { |
| 2778 | /* force BE */ |
| 2779 | iorder[0] = 3; |
| 2780 | iorder[1] = 2; |
| 2781 | iorder[2] = 1; |
| 2782 | iorder[3] = 0; |
| 2783 | } |
| 2784 | |
| 2785 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2786 | Py_UCS4 ch; |
| 2787 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2788 | if (e-q<4) { |
| 2789 | if (consumed) |
| 2790 | break; |
| 2791 | errmsg = "truncated data"; |
| 2792 | startinpos = ((const char *)q)-starts; |
| 2793 | endinpos = ((const char *)e)-starts; |
| 2794 | goto utf32Error; |
| 2795 | /* The remaining input chars are ignored if the callback |
| 2796 | chooses to skip the input */ |
| 2797 | } |
| 2798 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2799 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2800 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2801 | if (ch >= 0x110000) |
| 2802 | { |
| 2803 | errmsg = "codepoint not in range(0x110000)"; |
| 2804 | startinpos = ((const char *)q)-starts; |
| 2805 | endinpos = startinpos+4; |
| 2806 | goto utf32Error; |
| 2807 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2808 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2809 | if (ch >= 0x10000) |
| 2810 | { |
| 2811 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2812 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2813 | } |
| 2814 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2815 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2816 | *p++ = ch; |
| 2817 | q += 4; |
| 2818 | continue; |
| 2819 | utf32Error: |
| 2820 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2821 | if (unicode_decode_call_errorhandler( |
| 2822 | errors, &errorHandler, |
| 2823 | "utf32", errmsg, |
| 2824 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2825 | &unicode, &outpos, &p)) |
| 2826 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | if (byteorder) |
| 2830 | *byteorder = bo; |
| 2831 | |
| 2832 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2833 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2834 | |
| 2835 | /* Adjust length */ |
| 2836 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2837 | goto onError; |
| 2838 | |
| 2839 | Py_XDECREF(errorHandler); |
| 2840 | Py_XDECREF(exc); |
| 2841 | return (PyObject *)unicode; |
| 2842 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2843 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2844 | Py_DECREF(unicode); |
| 2845 | Py_XDECREF(errorHandler); |
| 2846 | Py_XDECREF(exc); |
| 2847 | return NULL; |
| 2848 | } |
| 2849 | |
| 2850 | PyObject * |
| 2851 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2852 | Py_ssize_t size, |
| 2853 | const char *errors, |
| 2854 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2855 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2856 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2857 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2858 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2859 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2860 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2861 | #else |
| 2862 | const int pairs = 0; |
| 2863 | #endif |
| 2864 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2865 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2866 | int iorder[] = {0, 1, 2, 3}; |
| 2867 | #else |
| 2868 | int iorder[] = {3, 2, 1, 0}; |
| 2869 | #endif |
| 2870 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2871 | #define STORECHAR(CH) \ |
| 2872 | do { \ |
| 2873 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2874 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2875 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2876 | p[iorder[0]] = (CH) & 0xff; \ |
| 2877 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2878 | } while(0) |
| 2879 | |
| 2880 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2881 | so we need less space. */ |
| 2882 | #ifndef Py_UNICODE_WIDE |
| 2883 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2884 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2885 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2886 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2887 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2888 | nsize = (size - pairs + (byteorder == 0)); |
| 2889 | bytesize = nsize * 4; |
| 2890 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2891 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2892 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2893 | if (v == NULL) |
| 2894 | return NULL; |
| 2895 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2896 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2897 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2898 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2899 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2900 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2901 | |
| 2902 | if (byteorder == -1) { |
| 2903 | /* force LE */ |
| 2904 | iorder[0] = 0; |
| 2905 | iorder[1] = 1; |
| 2906 | iorder[2] = 2; |
| 2907 | iorder[3] = 3; |
| 2908 | } |
| 2909 | else if (byteorder == 1) { |
| 2910 | /* force BE */ |
| 2911 | iorder[0] = 3; |
| 2912 | iorder[1] = 2; |
| 2913 | iorder[2] = 1; |
| 2914 | iorder[3] = 0; |
| 2915 | } |
| 2916 | |
| 2917 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2918 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2919 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2920 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2921 | Py_UCS4 ch2 = *s; |
| 2922 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2923 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2924 | s++; |
| 2925 | size--; |
| 2926 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2927 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2928 | #endif |
| 2929 | STORECHAR(ch); |
| 2930 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2931 | |
| 2932 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2933 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2934 | #undef STORECHAR |
| 2935 | } |
| 2936 | |
| 2937 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2938 | { |
| 2939 | if (!PyUnicode_Check(unicode)) { |
| 2940 | PyErr_BadArgument(); |
| 2941 | return NULL; |
| 2942 | } |
| 2943 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2944 | PyUnicode_GET_SIZE(unicode), |
| 2945 | NULL, |
| 2946 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2949 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2950 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2951 | PyObject * |
| 2952 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2953 | Py_ssize_t size, |
| 2954 | const char *errors, |
| 2955 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2956 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2957 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2958 | } |
| 2959 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2960 | /* Two masks for fast checking of whether a C 'long' may contain |
| 2961 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 2962 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 2963 | rare in most input. |
| 2964 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 2965 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2966 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2967 | #if (SIZEOF_LONG == 8) |
| 2968 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 2969 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 2970 | #elif (SIZEOF_LONG == 4) |
| 2971 | # define FAST_CHAR_MASK 0x80008000L |
| 2972 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 2973 | #else |
| 2974 | # error C 'long' size should be either 4 or 8! |
| 2975 | #endif |
| 2976 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2977 | PyObject * |
| 2978 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2979 | Py_ssize_t size, |
| 2980 | const char *errors, |
| 2981 | int *byteorder, |
| 2982 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2983 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2984 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2985 | Py_ssize_t startinpos; |
| 2986 | Py_ssize_t endinpos; |
| 2987 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2988 | PyUnicodeObject *unicode; |
| 2989 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2990 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2991 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2992 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2993 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2994 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2995 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2996 | int ihi = 1, ilo = 0; |
| 2997 | #else |
| 2998 | int ihi = 0, ilo = 1; |
| 2999 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3000 | PyObject *errorHandler = NULL; |
| 3001 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3002 | |
| 3003 | /* Note: size will always be longer than the resulting Unicode |
| 3004 | character count */ |
| 3005 | unicode = _PyUnicode_New(size); |
| 3006 | if (!unicode) |
| 3007 | return NULL; |
| 3008 | if (size == 0) |
| 3009 | return (PyObject *)unicode; |
| 3010 | |
| 3011 | /* Unpack UTF-16 encoded data */ |
| 3012 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3013 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3014 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3015 | |
| 3016 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3017 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3018 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3019 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3020 | byte order setting accordingly. In native mode, the leading BOM |
| 3021 | mark is skipped, in all other modes, it is copied to the output |
| 3022 | stream as-is (giving a ZWNBSP character). */ |
| 3023 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3024 | if (size >= 2) { |
| 3025 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3026 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3027 | if (bom == 0xFEFF) { |
| 3028 | q += 2; |
| 3029 | bo = -1; |
| 3030 | } |
| 3031 | else if (bom == 0xFFFE) { |
| 3032 | q += 2; |
| 3033 | bo = 1; |
| 3034 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3035 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3036 | if (bom == 0xFEFF) { |
| 3037 | q += 2; |
| 3038 | bo = 1; |
| 3039 | } |
| 3040 | else if (bom == 0xFFFE) { |
| 3041 | q += 2; |
| 3042 | bo = -1; |
| 3043 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3044 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3045 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3046 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3047 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3048 | if (bo == -1) { |
| 3049 | /* force LE */ |
| 3050 | ihi = 1; |
| 3051 | ilo = 0; |
| 3052 | } |
| 3053 | else if (bo == 1) { |
| 3054 | /* force BE */ |
| 3055 | ihi = 0; |
| 3056 | ilo = 1; |
| 3057 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3058 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3059 | native_ordering = ilo < ihi; |
| 3060 | #else |
| 3061 | native_ordering = ilo > ihi; |
| 3062 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3063 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3064 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3065 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3066 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3067 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3068 | reads are more expensive, better to defer to another iteration. */ |
| 3069 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3070 | /* Fast path for runs of non-surrogate chars. */ |
| 3071 | register const unsigned char *_q = q; |
| 3072 | Py_UNICODE *_p = p; |
| 3073 | if (native_ordering) { |
| 3074 | /* Native ordering is simple: as long as the input cannot |
| 3075 | possibly contain a surrogate char, do an unrolled copy |
| 3076 | of several 16-bit code points to the target object. |
| 3077 | The non-surrogate check is done on several input bytes |
| 3078 | at a time (as many as a C 'long' can contain). */ |
| 3079 | while (_q < aligned_end) { |
| 3080 | unsigned long data = * (unsigned long *) _q; |
| 3081 | if (data & FAST_CHAR_MASK) |
| 3082 | break; |
| 3083 | _p[0] = ((unsigned short *) _q)[0]; |
| 3084 | _p[1] = ((unsigned short *) _q)[1]; |
| 3085 | #if (SIZEOF_LONG == 8) |
| 3086 | _p[2] = ((unsigned short *) _q)[2]; |
| 3087 | _p[3] = ((unsigned short *) _q)[3]; |
| 3088 | #endif |
| 3089 | _q += SIZEOF_LONG; |
| 3090 | _p += SIZEOF_LONG / 2; |
| 3091 | } |
| 3092 | } |
| 3093 | else { |
| 3094 | /* Byteswapped ordering is similar, but we must decompose |
| 3095 | the copy bytewise, and take care of zero'ing out the |
| 3096 | upper bytes if the target object is in 32-bit units |
| 3097 | (that is, in UCS-4 builds). */ |
| 3098 | while (_q < aligned_end) { |
| 3099 | unsigned long data = * (unsigned long *) _q; |
| 3100 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3101 | break; |
| 3102 | /* Zero upper bytes in UCS-4 builds */ |
| 3103 | #if (Py_UNICODE_SIZE > 2) |
| 3104 | _p[0] = 0; |
| 3105 | _p[1] = 0; |
| 3106 | #if (SIZEOF_LONG == 8) |
| 3107 | _p[2] = 0; |
| 3108 | _p[3] = 0; |
| 3109 | #endif |
| 3110 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3111 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3112 | fill the two last bytes of each 4-byte unit. */ |
| 3113 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3114 | # define OFF 2 |
| 3115 | #else |
| 3116 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3117 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3118 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3119 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3120 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3121 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3122 | #if (SIZEOF_LONG == 8) |
| 3123 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3124 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3125 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3126 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3127 | #endif |
| 3128 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3129 | _q += SIZEOF_LONG; |
| 3130 | _p += SIZEOF_LONG / 2; |
| 3131 | } |
| 3132 | } |
| 3133 | p = _p; |
| 3134 | q = _q; |
| 3135 | if (q >= e) |
| 3136 | break; |
| 3137 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3138 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3139 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3140 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3141 | |
| 3142 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3143 | *p++ = ch; |
| 3144 | continue; |
| 3145 | } |
| 3146 | |
| 3147 | /* UTF-16 code pair: */ |
| 3148 | if (q > e) { |
| 3149 | errmsg = "unexpected end of data"; |
| 3150 | startinpos = (((const char *)q) - 2) - starts; |
| 3151 | endinpos = ((const char *)e) + 1 - starts; |
| 3152 | goto utf16Error; |
| 3153 | } |
| 3154 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3155 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3156 | q += 2; |
| 3157 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3158 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3159 | *p++ = ch; |
| 3160 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3161 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3162 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3163 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3164 | continue; |
| 3165 | } |
| 3166 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3167 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3168 | startinpos = (((const char *)q)-4)-starts; |
| 3169 | endinpos = startinpos+2; |
| 3170 | goto utf16Error; |
| 3171 | } |
| 3172 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3173 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3174 | errmsg = "illegal encoding"; |
| 3175 | startinpos = (((const char *)q)-2)-starts; |
| 3176 | endinpos = startinpos+2; |
| 3177 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3178 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3179 | utf16Error: |
| 3180 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3181 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3182 | errors, |
| 3183 | &errorHandler, |
| 3184 | "utf16", errmsg, |
| 3185 | &starts, |
| 3186 | (const char **)&e, |
| 3187 | &startinpos, |
| 3188 | &endinpos, |
| 3189 | &exc, |
| 3190 | (const char **)&q, |
| 3191 | &unicode, |
| 3192 | &outpos, |
| 3193 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3194 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3195 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3196 | /* remaining byte at the end? (size should be even) */ |
| 3197 | if (e == q) { |
| 3198 | if (!consumed) { |
| 3199 | errmsg = "truncated data"; |
| 3200 | startinpos = ((const char *)q) - starts; |
| 3201 | endinpos = ((const char *)e) + 1 - starts; |
| 3202 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3203 | if (unicode_decode_call_errorhandler( |
| 3204 | errors, |
| 3205 | &errorHandler, |
| 3206 | "utf16", errmsg, |
| 3207 | &starts, |
| 3208 | (const char **)&e, |
| 3209 | &startinpos, |
| 3210 | &endinpos, |
| 3211 | &exc, |
| 3212 | (const char **)&q, |
| 3213 | &unicode, |
| 3214 | &outpos, |
| 3215 | &p)) |
| 3216 | goto onError; |
| 3217 | /* The remaining input chars are ignored if the callback |
| 3218 | chooses to skip the input */ |
| 3219 | } |
| 3220 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3221 | |
| 3222 | if (byteorder) |
| 3223 | *byteorder = bo; |
| 3224 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3225 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3226 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3227 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3228 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3229 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3230 | goto onError; |
| 3231 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3232 | Py_XDECREF(errorHandler); |
| 3233 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3234 | return (PyObject *)unicode; |
| 3235 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3236 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3237 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3238 | Py_XDECREF(errorHandler); |
| 3239 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3240 | return NULL; |
| 3241 | } |
| 3242 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3243 | #undef FAST_CHAR_MASK |
| 3244 | #undef SWAPPED_FAST_CHAR_MASK |
| 3245 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3246 | PyObject * |
| 3247 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3248 | Py_ssize_t size, |
| 3249 | const char *errors, |
| 3250 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3251 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3252 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3253 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3254 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3255 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3256 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3257 | #else |
| 3258 | const int pairs = 0; |
| 3259 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3260 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3261 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3262 | int ihi = 1, ilo = 0; |
| 3263 | #else |
| 3264 | int ihi = 0, ilo = 1; |
| 3265 | #endif |
| 3266 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3267 | #define STORECHAR(CH) \ |
| 3268 | do { \ |
| 3269 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3270 | p[ilo] = (CH) & 0xff; \ |
| 3271 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3272 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3273 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3274 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3275 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3276 | if (s[i] >= 0x10000) |
| 3277 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3278 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3279 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3280 | if (size > PY_SSIZE_T_MAX || |
| 3281 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3282 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3283 | nsize = size + pairs + (byteorder == 0); |
| 3284 | bytesize = nsize * 2; |
| 3285 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3286 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3287 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3288 | if (v == NULL) |
| 3289 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3290 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3291 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3292 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3293 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3294 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3295 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3296 | |
| 3297 | if (byteorder == -1) { |
| 3298 | /* force LE */ |
| 3299 | ihi = 1; |
| 3300 | ilo = 0; |
| 3301 | } |
| 3302 | else if (byteorder == 1) { |
| 3303 | /* force BE */ |
| 3304 | ihi = 0; |
| 3305 | ilo = 1; |
| 3306 | } |
| 3307 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3308 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3309 | Py_UNICODE ch = *s++; |
| 3310 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3311 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3312 | if (ch >= 0x10000) { |
| 3313 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3314 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3315 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3316 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3317 | STORECHAR(ch); |
| 3318 | if (ch2) |
| 3319 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3320 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3321 | |
| 3322 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3323 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3324 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3328 | { |
| 3329 | if (!PyUnicode_Check(unicode)) { |
| 3330 | PyErr_BadArgument(); |
| 3331 | return NULL; |
| 3332 | } |
| 3333 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3334 | PyUnicode_GET_SIZE(unicode), |
| 3335 | NULL, |
| 3336 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3340 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3341 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3343 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3344 | Py_ssize_t size, |
| 3345 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3346 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3347 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3348 | Py_ssize_t startinpos; |
| 3349 | Py_ssize_t endinpos; |
| 3350 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3351 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3352 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3353 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3354 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3355 | char* message; |
| 3356 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3357 | PyObject *errorHandler = NULL; |
| 3358 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3359 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3360 | /* Escaped strings will always be longer than the resulting |
| 3361 | 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] | 3362 | length after conversion to the true value. |
| 3363 | (but if the error callback returns a long replacement string |
| 3364 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3365 | v = _PyUnicode_New(size); |
| 3366 | if (v == NULL) |
| 3367 | goto onError; |
| 3368 | if (size == 0) |
| 3369 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3370 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3371 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3372 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3373 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3374 | while (s < end) { |
| 3375 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3376 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3377 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 | |
| 3379 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3380 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3381 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3382 | continue; |
| 3383 | } |
| 3384 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3385 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3386 | /* \ - Escapes */ |
| 3387 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3388 | c = *s++; |
| 3389 | if (s > end) |
| 3390 | c = '\0'; /* Invalid after \ */ |
| 3391 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3392 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3393 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3394 | case '\n': break; |
| 3395 | case '\\': *p++ = '\\'; break; |
| 3396 | case '\'': *p++ = '\''; break; |
| 3397 | case '\"': *p++ = '\"'; break; |
| 3398 | case 'b': *p++ = '\b'; break; |
| 3399 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3400 | case 't': *p++ = '\t'; break; |
| 3401 | case 'n': *p++ = '\n'; break; |
| 3402 | case 'r': *p++ = '\r'; break; |
| 3403 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3404 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3405 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3406 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3407 | case '0': case '1': case '2': case '3': |
| 3408 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3409 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3410 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3411 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3412 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3413 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3414 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3415 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3416 | break; |
| 3417 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3418 | /* hex escapes */ |
| 3419 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3420 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3421 | digits = 2; |
| 3422 | message = "truncated \\xXX escape"; |
| 3423 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3424 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3425 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3426 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3427 | digits = 4; |
| 3428 | message = "truncated \\uXXXX escape"; |
| 3429 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3430 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3431 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3432 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3433 | digits = 8; |
| 3434 | message = "truncated \\UXXXXXXXX escape"; |
| 3435 | hexescape: |
| 3436 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3437 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3438 | if (s+digits>end) { |
| 3439 | endinpos = size; |
| 3440 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3441 | errors, &errorHandler, |
| 3442 | "unicodeescape", "end of string in escape sequence", |
| 3443 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3444 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3445 | goto onError; |
| 3446 | goto nextByte; |
| 3447 | } |
| 3448 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3449 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3450 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3451 | endinpos = (s+i+1)-starts; |
| 3452 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3453 | errors, &errorHandler, |
| 3454 | "unicodeescape", message, |
| 3455 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3456 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3457 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3458 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3459 | } |
| 3460 | chr = (chr<<4) & ~0xF; |
| 3461 | if (c >= '0' && c <= '9') |
| 3462 | chr += c - '0'; |
| 3463 | else if (c >= 'a' && c <= 'f') |
| 3464 | chr += 10 + c - 'a'; |
| 3465 | else |
| 3466 | chr += 10 + c - 'A'; |
| 3467 | } |
| 3468 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3469 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3470 | /* _decoding_error will have already written into the |
| 3471 | target buffer. */ |
| 3472 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3473 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3474 | /* when we get here, chr is a 32-bit unicode character */ |
| 3475 | if (chr <= 0xffff) |
| 3476 | /* UCS-2 character */ |
| 3477 | *p++ = (Py_UNICODE) chr; |
| 3478 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3479 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3480 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3481 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3482 | *p++ = chr; |
| 3483 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3484 | chr -= 0x10000L; |
| 3485 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3486 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3487 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3488 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3489 | endinpos = s-starts; |
| 3490 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3491 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3492 | errors, &errorHandler, |
| 3493 | "unicodeescape", "illegal Unicode character", |
| 3494 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3495 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3496 | goto onError; |
| 3497 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3498 | break; |
| 3499 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3500 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3501 | case 'N': |
| 3502 | message = "malformed \\N character escape"; |
| 3503 | if (ucnhash_CAPI == NULL) { |
| 3504 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3505 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3506 | if (ucnhash_CAPI == NULL) |
| 3507 | goto ucnhashError; |
| 3508 | } |
| 3509 | if (*s == '{') { |
| 3510 | const char *start = s+1; |
| 3511 | /* look for the closing brace */ |
| 3512 | while (*s != '}' && s < end) |
| 3513 | s++; |
| 3514 | if (s > start && s < end && *s == '}') { |
| 3515 | /* found a name. look it up in the unicode database */ |
| 3516 | message = "unknown Unicode character name"; |
| 3517 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3518 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3519 | goto store; |
| 3520 | } |
| 3521 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3522 | endinpos = s-starts; |
| 3523 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3524 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3525 | errors, &errorHandler, |
| 3526 | "unicodeescape", message, |
| 3527 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3528 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3529 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3530 | break; |
| 3531 | |
| 3532 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3533 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3534 | message = "\\ at end of string"; |
| 3535 | s--; |
| 3536 | endinpos = s-starts; |
| 3537 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3538 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3539 | errors, &errorHandler, |
| 3540 | "unicodeescape", message, |
| 3541 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3542 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3543 | goto onError; |
| 3544 | } |
| 3545 | else { |
| 3546 | *p++ = '\\'; |
| 3547 | *p++ = (unsigned char)s[-1]; |
| 3548 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3549 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3550 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3551 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3552 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3553 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3554 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3555 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3556 | Py_XDECREF(errorHandler); |
| 3557 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3558 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3559 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3560 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3561 | PyErr_SetString( |
| 3562 | PyExc_UnicodeError, |
| 3563 | "\\N escapes not supported (can't load unicodedata module)" |
| 3564 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3565 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3566 | Py_XDECREF(errorHandler); |
| 3567 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3568 | return NULL; |
| 3569 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3570 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3571 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3572 | Py_XDECREF(errorHandler); |
| 3573 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3574 | return NULL; |
| 3575 | } |
| 3576 | |
| 3577 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3578 | |
| 3579 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3580 | appropriate. |
| 3581 | |
| 3582 | */ |
| 3583 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3584 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3585 | Py_ssize_t size, |
| 3586 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3587 | { |
| 3588 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3589 | |
| 3590 | while (size-- > 0) { |
| 3591 | if (*s == ch) |
| 3592 | return s; |
| 3593 | s++; |
| 3594 | } |
| 3595 | |
| 3596 | return NULL; |
| 3597 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3598 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3599 | static const char *hexdigits = "0123456789abcdef"; |
| 3600 | |
| 3601 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3602 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3603 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3604 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3605 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3606 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3607 | #ifdef Py_UNICODE_WIDE |
| 3608 | const Py_ssize_t expandsize = 10; |
| 3609 | #else |
| 3610 | const Py_ssize_t expandsize = 6; |
| 3611 | #endif |
| 3612 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3613 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3614 | better to choose a different scheme. Perhaps scan the |
| 3615 | first N-chars of the string and allocate based on that size. |
| 3616 | */ |
| 3617 | /* Initial allocation is based on the longest-possible unichr |
| 3618 | escape. |
| 3619 | |
| 3620 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3621 | unichr, so in this case it's the longest unichr escape. In |
| 3622 | narrow (UTF-16) builds this is five chars per source unichr |
| 3623 | since there are two unichrs in the surrogate pair, so in narrow |
| 3624 | (UTF-16) builds it's not the longest unichr escape. |
| 3625 | |
| 3626 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3627 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3628 | escape. |
| 3629 | */ |
| 3630 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3631 | if (size == 0) |
| 3632 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3633 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3634 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3635 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3636 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3637 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3638 | 2 |
| 3639 | + expandsize*size |
| 3640 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3641 | if (repr == NULL) |
| 3642 | return NULL; |
| 3643 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3644 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3646 | while (size-- > 0) { |
| 3647 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3648 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3649 | /* Escape backslashes */ |
| 3650 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3651 | *p++ = '\\'; |
| 3652 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3653 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3654 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3655 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3656 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3657 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3658 | else if (ch >= 0x10000) { |
| 3659 | *p++ = '\\'; |
| 3660 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3661 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3662 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3663 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3664 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3665 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3666 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3667 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3668 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3669 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3670 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3671 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3672 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3673 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3674 | Py_UNICODE ch2; |
| 3675 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3676 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3677 | ch2 = *s++; |
| 3678 | size--; |
| 3679 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3680 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3681 | *p++ = '\\'; |
| 3682 | *p++ = 'U'; |
| 3683 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3684 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3685 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3686 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3687 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3688 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3689 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3690 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3691 | continue; |
| 3692 | } |
| 3693 | /* Fall through: isolated surrogates are copied as-is */ |
| 3694 | s--; |
| 3695 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3696 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3697 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3699 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3700 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3701 | *p++ = '\\'; |
| 3702 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3703 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3704 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3705 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3706 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3707 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3708 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3709 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3710 | else if (ch == '\t') { |
| 3711 | *p++ = '\\'; |
| 3712 | *p++ = 't'; |
| 3713 | } |
| 3714 | else if (ch == '\n') { |
| 3715 | *p++ = '\\'; |
| 3716 | *p++ = 'n'; |
| 3717 | } |
| 3718 | else if (ch == '\r') { |
| 3719 | *p++ = '\\'; |
| 3720 | *p++ = 'r'; |
| 3721 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3722 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3723 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3724 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3725 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3726 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3727 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3728 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3729 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3731 | /* Copy everything else as-is */ |
| 3732 | else |
| 3733 | *p++ = (char) ch; |
| 3734 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3735 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3736 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3737 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3738 | return NULL; |
| 3739 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3742 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3743 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3744 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3745 | if (!PyUnicode_Check(unicode)) { |
| 3746 | PyErr_BadArgument(); |
| 3747 | return NULL; |
| 3748 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3749 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3750 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3751 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3755 | |
| 3756 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3757 | Py_ssize_t size, |
| 3758 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3759 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3760 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3761 | Py_ssize_t startinpos; |
| 3762 | Py_ssize_t endinpos; |
| 3763 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3764 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3765 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3766 | const char *end; |
| 3767 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3768 | PyObject *errorHandler = NULL; |
| 3769 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3771 | /* Escaped strings will always be longer than the resulting |
| 3772 | 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] | 3773 | length after conversion to the true value. (But decoding error |
| 3774 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3775 | v = _PyUnicode_New(size); |
| 3776 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3777 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3778 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3779 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3780 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3781 | end = s + size; |
| 3782 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3783 | unsigned char c; |
| 3784 | Py_UCS4 x; |
| 3785 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3786 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3787 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3788 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3789 | if (*s != '\\') { |
| 3790 | *p++ = (unsigned char)*s++; |
| 3791 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3792 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3793 | startinpos = s-starts; |
| 3794 | |
| 3795 | /* \u-escapes are only interpreted iff the number of leading |
| 3796 | backslashes if odd */ |
| 3797 | bs = s; |
| 3798 | for (;s < end;) { |
| 3799 | if (*s != '\\') |
| 3800 | break; |
| 3801 | *p++ = (unsigned char)*s++; |
| 3802 | } |
| 3803 | if (((s - bs) & 1) == 0 || |
| 3804 | s >= end || |
| 3805 | (*s != 'u' && *s != 'U')) { |
| 3806 | continue; |
| 3807 | } |
| 3808 | p--; |
| 3809 | count = *s=='u' ? 4 : 8; |
| 3810 | s++; |
| 3811 | |
| 3812 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3813 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3814 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3815 | c = (unsigned char)*s; |
| 3816 | if (!ISXDIGIT(c)) { |
| 3817 | endinpos = s-starts; |
| 3818 | if (unicode_decode_call_errorhandler( |
| 3819 | errors, &errorHandler, |
| 3820 | "rawunicodeescape", "truncated \\uXXXX", |
| 3821 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3822 | &v, &outpos, &p)) |
| 3823 | goto onError; |
| 3824 | goto nextByte; |
| 3825 | } |
| 3826 | x = (x<<4) & ~0xF; |
| 3827 | if (c >= '0' && c <= '9') |
| 3828 | x += c - '0'; |
| 3829 | else if (c >= 'a' && c <= 'f') |
| 3830 | x += 10 + c - 'a'; |
| 3831 | else |
| 3832 | x += 10 + c - 'A'; |
| 3833 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3834 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3835 | /* UCS-2 character */ |
| 3836 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3837 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3838 | /* UCS-4 character. Either store directly, or as |
| 3839 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3840 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3841 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3842 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3843 | x -= 0x10000L; |
| 3844 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3845 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3846 | #endif |
| 3847 | } else { |
| 3848 | endinpos = s-starts; |
| 3849 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3850 | if (unicode_decode_call_errorhandler( |
| 3851 | errors, &errorHandler, |
| 3852 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3853 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3854 | &v, &outpos, &p)) |
| 3855 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3856 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3857 | nextByte: |
| 3858 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3859 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3860 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3861 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3862 | Py_XDECREF(errorHandler); |
| 3863 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3864 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3865 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3866 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3867 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3868 | Py_XDECREF(errorHandler); |
| 3869 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3870 | return NULL; |
| 3871 | } |
| 3872 | |
| 3873 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3874 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3875 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3876 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3877 | char *p; |
| 3878 | char *q; |
| 3879 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3880 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3881 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3882 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3883 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3884 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3885 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3886 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3887 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3888 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3889 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3890 | if (repr == NULL) |
| 3891 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3892 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3893 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3894 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3895 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3896 | while (size-- > 0) { |
| 3897 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3898 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3899 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3900 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3901 | *p++ = '\\'; |
| 3902 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3903 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3904 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3905 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3906 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3907 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3908 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3909 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3910 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3911 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3912 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3913 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3914 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3915 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3916 | Py_UNICODE ch2; |
| 3917 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3918 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3919 | ch2 = *s++; |
| 3920 | size--; |
| 3921 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3922 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3923 | *p++ = '\\'; |
| 3924 | *p++ = 'U'; |
| 3925 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3926 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3927 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3928 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3929 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3930 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3931 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3932 | *p++ = hexdigits[ucs & 0xf]; |
| 3933 | continue; |
| 3934 | } |
| 3935 | /* Fall through: isolated surrogates are copied as-is */ |
| 3936 | s--; |
| 3937 | size++; |
| 3938 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3939 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3940 | /* Map 16-bit characters to '\uxxxx' */ |
| 3941 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3942 | *p++ = '\\'; |
| 3943 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3944 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3945 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3946 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3947 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3948 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3949 | /* Copy everything else as-is */ |
| 3950 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3951 | *p++ = (char) ch; |
| 3952 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3953 | size = p - q; |
| 3954 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3955 | assert(size > 0); |
| 3956 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3957 | return NULL; |
| 3958 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3962 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3963 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3964 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3965 | PyErr_BadArgument(); |
| 3966 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3967 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3968 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3969 | PyUnicode_GET_SIZE(unicode)); |
| 3970 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3971 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3974 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3975 | |
| 3976 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3977 | Py_ssize_t size, |
| 3978 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3979 | { |
| 3980 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3981 | Py_ssize_t startinpos; |
| 3982 | Py_ssize_t endinpos; |
| 3983 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3984 | PyUnicodeObject *v; |
| 3985 | Py_UNICODE *p; |
| 3986 | const char *end; |
| 3987 | const char *reason; |
| 3988 | PyObject *errorHandler = NULL; |
| 3989 | PyObject *exc = NULL; |
| 3990 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3991 | #ifdef Py_UNICODE_WIDE |
| 3992 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3993 | #endif |
| 3994 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3995 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3996 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3997 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3998 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3999 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4000 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4001 | p = PyUnicode_AS_UNICODE(v); |
| 4002 | end = s + size; |
| 4003 | |
| 4004 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4005 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4006 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4007 | some malformed UCS-4 data. */ |
| 4008 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4009 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4010 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4011 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4012 | end-s < Py_UNICODE_SIZE |
| 4013 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4014 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4015 | startinpos = s - starts; |
| 4016 | if (end-s < Py_UNICODE_SIZE) { |
| 4017 | endinpos = end-starts; |
| 4018 | reason = "truncated input"; |
| 4019 | } |
| 4020 | else { |
| 4021 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4022 | reason = "illegal code point (> 0x10FFFF)"; |
| 4023 | } |
| 4024 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4025 | if (unicode_decode_call_errorhandler( |
| 4026 | errors, &errorHandler, |
| 4027 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4028 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4029 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4030 | goto onError; |
| 4031 | } |
| 4032 | } |
| 4033 | else { |
| 4034 | p++; |
| 4035 | s += Py_UNICODE_SIZE; |
| 4036 | } |
| 4037 | } |
| 4038 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4039 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4040 | goto onError; |
| 4041 | Py_XDECREF(errorHandler); |
| 4042 | Py_XDECREF(exc); |
| 4043 | return (PyObject *)v; |
| 4044 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4045 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4046 | Py_XDECREF(v); |
| 4047 | Py_XDECREF(errorHandler); |
| 4048 | Py_XDECREF(exc); |
| 4049 | return NULL; |
| 4050 | } |
| 4051 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4052 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4053 | |
| 4054 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4055 | Py_ssize_t size, |
| 4056 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4057 | { |
| 4058 | PyUnicodeObject *v; |
| 4059 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4060 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4061 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4062 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4063 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4064 | Py_UNICODE r = *(unsigned char*)s; |
| 4065 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4068 | v = _PyUnicode_New(size); |
| 4069 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4070 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4071 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4072 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4073 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4074 | e = s + size; |
| 4075 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4076 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4077 | unrolled_end = e - 4; |
| 4078 | while (s < unrolled_end) { |
| 4079 | p[0] = (unsigned char) s[0]; |
| 4080 | p[1] = (unsigned char) s[1]; |
| 4081 | p[2] = (unsigned char) s[2]; |
| 4082 | p[3] = (unsigned char) s[3]; |
| 4083 | s += 4; |
| 4084 | p += 4; |
| 4085 | } |
| 4086 | while (s < e) |
| 4087 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4088 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4089 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4090 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4091 | Py_XDECREF(v); |
| 4092 | return NULL; |
| 4093 | } |
| 4094 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4095 | /* create or adjust a UnicodeEncodeError */ |
| 4096 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4097 | const char *encoding, |
| 4098 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4099 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4100 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4101 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4102 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4103 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4104 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4105 | } |
| 4106 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4107 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4108 | goto onError; |
| 4109 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4110 | goto onError; |
| 4111 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4112 | goto onError; |
| 4113 | return; |
| 4114 | onError: |
| 4115 | Py_DECREF(*exceptionObject); |
| 4116 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4117 | } |
| 4118 | } |
| 4119 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4120 | /* raises a UnicodeEncodeError */ |
| 4121 | static void raise_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) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4126 | { |
| 4127 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4128 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4129 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4130 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | /* error handling callback helper: |
| 4134 | build arguments, call the callback and check the arguments, |
| 4135 | put the result into newpos and return the replacement string, which |
| 4136 | has to be freed by the caller */ |
| 4137 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4138 | PyObject **errorHandler, |
| 4139 | const char *encoding, const char *reason, |
| 4140 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4141 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4142 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4143 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4144 | 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] | 4145 | |
| 4146 | PyObject *restuple; |
| 4147 | PyObject *resunicode; |
| 4148 | |
| 4149 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4150 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4151 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4152 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4156 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4157 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4158 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4159 | |
| 4160 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4161 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4162 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4163 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4164 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4165 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4166 | Py_DECREF(restuple); |
| 4167 | return NULL; |
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 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4170 | &resunicode, newpos)) { |
| 4171 | Py_DECREF(restuple); |
| 4172 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4173 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4174 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4175 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4176 | Py_DECREF(restuple); |
| 4177 | return NULL; |
| 4178 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4179 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4180 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4181 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4182 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4183 | Py_DECREF(restuple); |
| 4184 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4185 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4186 | Py_INCREF(resunicode); |
| 4187 | Py_DECREF(restuple); |
| 4188 | return resunicode; |
| 4189 | } |
| 4190 | |
| 4191 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4192 | Py_ssize_t size, |
| 4193 | const char *errors, |
| 4194 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4195 | { |
| 4196 | /* output object */ |
| 4197 | PyObject *res; |
| 4198 | /* pointers to the beginning and end+1 of input */ |
| 4199 | const Py_UNICODE *startp = p; |
| 4200 | const Py_UNICODE *endp = p + size; |
| 4201 | /* pointer to the beginning of the unencodable characters */ |
| 4202 | /* const Py_UNICODE *badp = NULL; */ |
| 4203 | /* pointer into the output */ |
| 4204 | char *str; |
| 4205 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4206 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4207 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4208 | 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] | 4209 | PyObject *errorHandler = NULL; |
| 4210 | PyObject *exc = NULL; |
| 4211 | /* the following variable is used for caching string comparisons |
| 4212 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4213 | int known_errorHandler = -1; |
| 4214 | |
| 4215 | /* allocate enough for a simple encoding without |
| 4216 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4217 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4218 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4219 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4220 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4221 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4222 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4223 | ressize = size; |
| 4224 | |
| 4225 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4226 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4227 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4228 | /* can we encode this? */ |
| 4229 | if (c<limit) { |
| 4230 | /* no overflow check, because we know that the space is enough */ |
| 4231 | *str++ = (char)c; |
| 4232 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4233 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4234 | else { |
| 4235 | Py_ssize_t unicodepos = p-startp; |
| 4236 | Py_ssize_t requiredsize; |
| 4237 | PyObject *repunicode; |
| 4238 | Py_ssize_t repsize; |
| 4239 | Py_ssize_t newpos; |
| 4240 | Py_ssize_t respos; |
| 4241 | Py_UNICODE *uni2; |
| 4242 | /* startpos for collecting unencodable chars */ |
| 4243 | const Py_UNICODE *collstart = p; |
| 4244 | const Py_UNICODE *collend = p; |
| 4245 | /* find all unecodable characters */ |
| 4246 | while ((collend < endp) && ((*collend)>=limit)) |
| 4247 | ++collend; |
| 4248 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4249 | if (known_errorHandler==-1) { |
| 4250 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4251 | known_errorHandler = 1; |
| 4252 | else if (!strcmp(errors, "replace")) |
| 4253 | known_errorHandler = 2; |
| 4254 | else if (!strcmp(errors, "ignore")) |
| 4255 | known_errorHandler = 3; |
| 4256 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4257 | known_errorHandler = 4; |
| 4258 | else |
| 4259 | known_errorHandler = 0; |
| 4260 | } |
| 4261 | switch (known_errorHandler) { |
| 4262 | case 1: /* strict */ |
| 4263 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4264 | goto onError; |
| 4265 | case 2: /* replace */ |
| 4266 | while (collstart++<collend) |
| 4267 | *str++ = '?'; /* fall through */ |
| 4268 | case 3: /* ignore */ |
| 4269 | p = collend; |
| 4270 | break; |
| 4271 | case 4: /* xmlcharrefreplace */ |
| 4272 | respos = str - PyBytes_AS_STRING(res); |
| 4273 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4274 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4275 | if (*p<10) |
| 4276 | repsize += 2+1+1; |
| 4277 | else if (*p<100) |
| 4278 | repsize += 2+2+1; |
| 4279 | else if (*p<1000) |
| 4280 | repsize += 2+3+1; |
| 4281 | else if (*p<10000) |
| 4282 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4283 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4284 | else |
| 4285 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4286 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4287 | else if (*p<100000) |
| 4288 | repsize += 2+5+1; |
| 4289 | else if (*p<1000000) |
| 4290 | repsize += 2+6+1; |
| 4291 | else |
| 4292 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4293 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4294 | } |
| 4295 | requiredsize = respos+repsize+(endp-collend); |
| 4296 | if (requiredsize > ressize) { |
| 4297 | if (requiredsize<2*ressize) |
| 4298 | requiredsize = 2*ressize; |
| 4299 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4300 | goto onError; |
| 4301 | str = PyBytes_AS_STRING(res) + respos; |
| 4302 | ressize = requiredsize; |
| 4303 | } |
| 4304 | /* generate replacement (temporarily (mis)uses p) */ |
| 4305 | for (p = collstart; p < collend; ++p) { |
| 4306 | str += sprintf(str, "&#%d;", (int)*p); |
| 4307 | } |
| 4308 | p = collend; |
| 4309 | break; |
| 4310 | default: |
| 4311 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4312 | encoding, reason, startp, size, &exc, |
| 4313 | collstart-startp, collend-startp, &newpos); |
| 4314 | if (repunicode == NULL) |
| 4315 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4316 | if (PyBytes_Check(repunicode)) { |
| 4317 | /* Directly copy bytes result to output. */ |
| 4318 | repsize = PyBytes_Size(repunicode); |
| 4319 | if (repsize > 1) { |
| 4320 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4321 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4322 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4323 | Py_DECREF(repunicode); |
| 4324 | goto onError; |
| 4325 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4326 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4327 | ressize += repsize-1; |
| 4328 | } |
| 4329 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4330 | str += repsize; |
| 4331 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4332 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4333 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4334 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4335 | /* need more space? (at least enough for what we |
| 4336 | have+the replacement+the rest of the string, so |
| 4337 | we won't have to check space for encodable characters) */ |
| 4338 | respos = str - PyBytes_AS_STRING(res); |
| 4339 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4340 | requiredsize = respos+repsize+(endp-collend); |
| 4341 | if (requiredsize > ressize) { |
| 4342 | if (requiredsize<2*ressize) |
| 4343 | requiredsize = 2*ressize; |
| 4344 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4345 | Py_DECREF(repunicode); |
| 4346 | goto onError; |
| 4347 | } |
| 4348 | str = PyBytes_AS_STRING(res) + respos; |
| 4349 | ressize = requiredsize; |
| 4350 | } |
| 4351 | /* check if there is anything unencodable in the replacement |
| 4352 | and copy it to the output */ |
| 4353 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4354 | c = *uni2; |
| 4355 | if (c >= limit) { |
| 4356 | raise_encode_exception(&exc, encoding, startp, size, |
| 4357 | unicodepos, unicodepos+1, reason); |
| 4358 | Py_DECREF(repunicode); |
| 4359 | goto onError; |
| 4360 | } |
| 4361 | *str = (char)c; |
| 4362 | } |
| 4363 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4364 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4365 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4366 | } |
| 4367 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4368 | /* Resize if we allocated to much */ |
| 4369 | size = str - PyBytes_AS_STRING(res); |
| 4370 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4371 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4372 | if (_PyBytes_Resize(&res, size) < 0) |
| 4373 | goto onError; |
| 4374 | } |
| 4375 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4376 | Py_XDECREF(errorHandler); |
| 4377 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4378 | return res; |
| 4379 | |
| 4380 | onError: |
| 4381 | Py_XDECREF(res); |
| 4382 | Py_XDECREF(errorHandler); |
| 4383 | Py_XDECREF(exc); |
| 4384 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4387 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4388 | Py_ssize_t size, |
| 4389 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4390 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4391 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4395 | { |
| 4396 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4397 | PyErr_BadArgument(); |
| 4398 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4399 | } |
| 4400 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4401 | PyUnicode_GET_SIZE(unicode), |
| 4402 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4406 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4407 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4408 | Py_ssize_t size, |
| 4409 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4410 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4411 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4412 | PyUnicodeObject *v; |
| 4413 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4414 | Py_ssize_t startinpos; |
| 4415 | Py_ssize_t endinpos; |
| 4416 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4417 | const char *e; |
| 4418 | PyObject *errorHandler = NULL; |
| 4419 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4420 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4422 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4423 | Py_UNICODE r = *(unsigned char*)s; |
| 4424 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4425 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4426 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4427 | v = _PyUnicode_New(size); |
| 4428 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4429 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4430 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4431 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4432 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4433 | e = s + size; |
| 4434 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4435 | register unsigned char c = (unsigned char)*s; |
| 4436 | if (c < 128) { |
| 4437 | *p++ = c; |
| 4438 | ++s; |
| 4439 | } |
| 4440 | else { |
| 4441 | startinpos = s-starts; |
| 4442 | endinpos = startinpos + 1; |
| 4443 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4444 | if (unicode_decode_call_errorhandler( |
| 4445 | errors, &errorHandler, |
| 4446 | "ascii", "ordinal not in range(128)", |
| 4447 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4448 | &v, &outpos, &p)) |
| 4449 | goto onError; |
| 4450 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4451 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4452 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4453 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4454 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4455 | Py_XDECREF(errorHandler); |
| 4456 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4457 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4458 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4459 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4460 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4461 | Py_XDECREF(errorHandler); |
| 4462 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4463 | return NULL; |
| 4464 | } |
| 4465 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4466 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4467 | Py_ssize_t size, |
| 4468 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4469 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4470 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4474 | { |
| 4475 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4476 | PyErr_BadArgument(); |
| 4477 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4478 | } |
| 4479 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4480 | PyUnicode_GET_SIZE(unicode), |
| 4481 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4484 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4485 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4486 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4487 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4488 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4489 | #define NEED_RETRY |
| 4490 | #endif |
| 4491 | |
| 4492 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4493 | a) it assumes an incomplete character consists of a single byte, and |
| 4494 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4495 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4496 | |
| 4497 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4498 | { |
| 4499 | const char *curr = s + offset; |
| 4500 | |
| 4501 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4502 | const char *prev = CharPrev(s, curr); |
| 4503 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4504 | } |
| 4505 | return 0; |
| 4506 | } |
| 4507 | |
| 4508 | /* |
| 4509 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4510 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4511 | */ |
| 4512 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4513 | const char *s, /* MBCS string */ |
| 4514 | int size, /* sizeof MBCS string */ |
| 4515 | int final) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4516 | { |
| 4517 | Py_UNICODE *p; |
| 4518 | Py_ssize_t n = 0; |
| 4519 | int usize = 0; |
| 4520 | |
| 4521 | assert(size >= 0); |
| 4522 | |
| 4523 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4524 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4525 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4526 | |
| 4527 | /* First get the size of the result */ |
| 4528 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4529 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4530 | if (usize == 0) { |
| 4531 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4532 | return -1; |
| 4533 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4537 | /* Create unicode object */ |
| 4538 | *v = _PyUnicode_New(usize); |
| 4539 | if (*v == NULL) |
| 4540 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4541 | } |
| 4542 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4543 | /* Extend unicode object */ |
| 4544 | n = PyUnicode_GET_SIZE(*v); |
| 4545 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4546 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | /* Do the conversion */ |
| 4550 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4551 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4552 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4553 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4554 | return -1; |
| 4555 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | return size; |
| 4559 | } |
| 4560 | |
| 4561 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4562 | Py_ssize_t size, |
| 4563 | const char *errors, |
| 4564 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4565 | { |
| 4566 | PyUnicodeObject *v = NULL; |
| 4567 | int done; |
| 4568 | |
| 4569 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4570 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4571 | |
| 4572 | #ifdef NEED_RETRY |
| 4573 | retry: |
| 4574 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4575 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4576 | else |
| 4577 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4578 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4579 | |
| 4580 | if (done < 0) { |
| 4581 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4582 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4586 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4587 | |
| 4588 | #ifdef NEED_RETRY |
| 4589 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4590 | s += done; |
| 4591 | size -= done; |
| 4592 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4593 | } |
| 4594 | #endif |
| 4595 | |
| 4596 | return (PyObject *)v; |
| 4597 | } |
| 4598 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4599 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4600 | Py_ssize_t size, |
| 4601 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4602 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4603 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4604 | } |
| 4605 | |
| 4606 | /* |
| 4607 | * Convert unicode into string object (MBCS). |
| 4608 | * Returns 0 if succeed, -1 otherwise. |
| 4609 | */ |
| 4610 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4611 | const Py_UNICODE *p, /* unicode */ |
| 4612 | int size) /* size of unicode */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4613 | { |
| 4614 | int mbcssize = 0; |
| 4615 | Py_ssize_t n = 0; |
| 4616 | |
| 4617 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4618 | |
| 4619 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4620 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4621 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4622 | if (mbcssize == 0) { |
| 4623 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4624 | return -1; |
| 4625 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4628 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4629 | /* Create string object */ |
| 4630 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4631 | if (*repr == NULL) |
| 4632 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4633 | } |
| 4634 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4635 | /* Extend string object */ |
| 4636 | n = PyBytes_Size(*repr); |
| 4637 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4638 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | /* Do the conversion */ |
| 4642 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4643 | char *s = PyBytes_AS_STRING(*repr) + n; |
| 4644 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4645 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4646 | return -1; |
| 4647 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4654 | Py_ssize_t size, |
| 4655 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4656 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4657 | PyObject *repr = NULL; |
| 4658 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4659 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4660 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4661 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4662 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4663 | ret = encode_mbcs(&repr, p, INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4664 | else |
| 4665 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4666 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4667 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4668 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4669 | Py_XDECREF(repr); |
| 4670 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4671 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4672 | |
| 4673 | #ifdef NEED_RETRY |
| 4674 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4675 | p += INT_MAX; |
| 4676 | size -= INT_MAX; |
| 4677 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4678 | } |
| 4679 | #endif |
| 4680 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4681 | return repr; |
| 4682 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4683 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4684 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4685 | { |
| 4686 | if (!PyUnicode_Check(unicode)) { |
| 4687 | PyErr_BadArgument(); |
| 4688 | return NULL; |
| 4689 | } |
| 4690 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4691 | PyUnicode_GET_SIZE(unicode), |
| 4692 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4695 | #undef NEED_RETRY |
| 4696 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4697 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4699 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4700 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4701 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4702 | Py_ssize_t size, |
| 4703 | PyObject *mapping, |
| 4704 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4705 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4706 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4707 | Py_ssize_t startinpos; |
| 4708 | Py_ssize_t endinpos; |
| 4709 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4710 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4711 | PyUnicodeObject *v; |
| 4712 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4713 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4714 | PyObject *errorHandler = NULL; |
| 4715 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4716 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4717 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4719 | /* Default to Latin-1 */ |
| 4720 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4721 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4722 | |
| 4723 | v = _PyUnicode_New(size); |
| 4724 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4725 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4726 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4727 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4728 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4729 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4730 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4731 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4732 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4733 | while (s < e) { |
| 4734 | unsigned char ch = *s; |
| 4735 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4736 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4737 | if (ch < maplen) |
| 4738 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4739 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4740 | if (x == 0xfffe) { |
| 4741 | /* undefined mapping */ |
| 4742 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4743 | startinpos = s-starts; |
| 4744 | endinpos = startinpos+1; |
| 4745 | if (unicode_decode_call_errorhandler( |
| 4746 | errors, &errorHandler, |
| 4747 | "charmap", "character maps to <undefined>", |
| 4748 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4749 | &v, &outpos, &p)) { |
| 4750 | goto onError; |
| 4751 | } |
| 4752 | continue; |
| 4753 | } |
| 4754 | *p++ = x; |
| 4755 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4756 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4757 | } |
| 4758 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4759 | while (s < e) { |
| 4760 | unsigned char ch = *s; |
| 4761 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4762 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4763 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4764 | w = PyLong_FromLong((long)ch); |
| 4765 | if (w == NULL) |
| 4766 | goto onError; |
| 4767 | x = PyObject_GetItem(mapping, w); |
| 4768 | Py_DECREF(w); |
| 4769 | if (x == NULL) { |
| 4770 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4771 | /* No mapping found means: mapping is undefined. */ |
| 4772 | PyErr_Clear(); |
| 4773 | x = Py_None; |
| 4774 | Py_INCREF(x); |
| 4775 | } else |
| 4776 | goto onError; |
| 4777 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4778 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4779 | /* Apply mapping */ |
| 4780 | if (PyLong_Check(x)) { |
| 4781 | long value = PyLong_AS_LONG(x); |
| 4782 | if (value < 0 || value > 65535) { |
| 4783 | PyErr_SetString(PyExc_TypeError, |
| 4784 | "character mapping must be in range(65536)"); |
| 4785 | Py_DECREF(x); |
| 4786 | goto onError; |
| 4787 | } |
| 4788 | *p++ = (Py_UNICODE)value; |
| 4789 | } |
| 4790 | else if (x == Py_None) { |
| 4791 | /* undefined mapping */ |
| 4792 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4793 | startinpos = s-starts; |
| 4794 | endinpos = startinpos+1; |
| 4795 | if (unicode_decode_call_errorhandler( |
| 4796 | errors, &errorHandler, |
| 4797 | "charmap", "character maps to <undefined>", |
| 4798 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4799 | &v, &outpos, &p)) { |
| 4800 | Py_DECREF(x); |
| 4801 | goto onError; |
| 4802 | } |
| 4803 | Py_DECREF(x); |
| 4804 | continue; |
| 4805 | } |
| 4806 | else if (PyUnicode_Check(x)) { |
| 4807 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4808 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4809 | if (targetsize == 1) |
| 4810 | /* 1-1 mapping */ |
| 4811 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4812 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4813 | else if (targetsize > 1) { |
| 4814 | /* 1-n mapping */ |
| 4815 | if (targetsize > extrachars) { |
| 4816 | /* resize first */ |
| 4817 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4818 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4819 | (targetsize << 2); |
| 4820 | extrachars += needed; |
| 4821 | /* XXX overflow detection missing */ |
| 4822 | if (_PyUnicode_Resize(&v, |
| 4823 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4824 | Py_DECREF(x); |
| 4825 | goto onError; |
| 4826 | } |
| 4827 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4828 | } |
| 4829 | Py_UNICODE_COPY(p, |
| 4830 | PyUnicode_AS_UNICODE(x), |
| 4831 | targetsize); |
| 4832 | p += targetsize; |
| 4833 | extrachars -= targetsize; |
| 4834 | } |
| 4835 | /* 1-0 mapping: skip the character */ |
| 4836 | } |
| 4837 | else { |
| 4838 | /* wrong return value */ |
| 4839 | PyErr_SetString(PyExc_TypeError, |
| 4840 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4841 | Py_DECREF(x); |
| 4842 | goto onError; |
| 4843 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4844 | Py_DECREF(x); |
| 4845 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4846 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4847 | } |
| 4848 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4849 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4850 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4851 | Py_XDECREF(errorHandler); |
| 4852 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4853 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4854 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4855 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4856 | Py_XDECREF(errorHandler); |
| 4857 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4858 | Py_XDECREF(v); |
| 4859 | return NULL; |
| 4860 | } |
| 4861 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4862 | /* Charmap encoding: the lookup table */ |
| 4863 | |
| 4864 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4865 | PyObject_HEAD |
| 4866 | unsigned char level1[32]; |
| 4867 | int count2, count3; |
| 4868 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4869 | }; |
| 4870 | |
| 4871 | static PyObject* |
| 4872 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4873 | { |
| 4874 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4875 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4876 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4880 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4881 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4882 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4883 | }; |
| 4884 | |
| 4885 | static void |
| 4886 | encoding_map_dealloc(PyObject* o) |
| 4887 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4888 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4892 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4893 | "EncodingMap", /*tp_name*/ |
| 4894 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4895 | 0, /*tp_itemsize*/ |
| 4896 | /* methods */ |
| 4897 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4898 | 0, /*tp_print*/ |
| 4899 | 0, /*tp_getattr*/ |
| 4900 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4901 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4902 | 0, /*tp_repr*/ |
| 4903 | 0, /*tp_as_number*/ |
| 4904 | 0, /*tp_as_sequence*/ |
| 4905 | 0, /*tp_as_mapping*/ |
| 4906 | 0, /*tp_hash*/ |
| 4907 | 0, /*tp_call*/ |
| 4908 | 0, /*tp_str*/ |
| 4909 | 0, /*tp_getattro*/ |
| 4910 | 0, /*tp_setattro*/ |
| 4911 | 0, /*tp_as_buffer*/ |
| 4912 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4913 | 0, /*tp_doc*/ |
| 4914 | 0, /*tp_traverse*/ |
| 4915 | 0, /*tp_clear*/ |
| 4916 | 0, /*tp_richcompare*/ |
| 4917 | 0, /*tp_weaklistoffset*/ |
| 4918 | 0, /*tp_iter*/ |
| 4919 | 0, /*tp_iternext*/ |
| 4920 | encoding_map_methods, /*tp_methods*/ |
| 4921 | 0, /*tp_members*/ |
| 4922 | 0, /*tp_getset*/ |
| 4923 | 0, /*tp_base*/ |
| 4924 | 0, /*tp_dict*/ |
| 4925 | 0, /*tp_descr_get*/ |
| 4926 | 0, /*tp_descr_set*/ |
| 4927 | 0, /*tp_dictoffset*/ |
| 4928 | 0, /*tp_init*/ |
| 4929 | 0, /*tp_alloc*/ |
| 4930 | 0, /*tp_new*/ |
| 4931 | 0, /*tp_free*/ |
| 4932 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4933 | }; |
| 4934 | |
| 4935 | PyObject* |
| 4936 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4937 | { |
| 4938 | Py_UNICODE *decode; |
| 4939 | PyObject *result; |
| 4940 | struct encoding_map *mresult; |
| 4941 | int i; |
| 4942 | int need_dict = 0; |
| 4943 | unsigned char level1[32]; |
| 4944 | unsigned char level2[512]; |
| 4945 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4946 | int count2 = 0, count3 = 0; |
| 4947 | |
| 4948 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4949 | PyErr_BadArgument(); |
| 4950 | return NULL; |
| 4951 | } |
| 4952 | decode = PyUnicode_AS_UNICODE(string); |
| 4953 | memset(level1, 0xFF, sizeof level1); |
| 4954 | memset(level2, 0xFF, sizeof level2); |
| 4955 | |
| 4956 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4957 | or if there are non-BMP characters, we need to use |
| 4958 | a mapping dictionary. */ |
| 4959 | if (decode[0] != 0) |
| 4960 | need_dict = 1; |
| 4961 | for (i = 1; i < 256; i++) { |
| 4962 | int l1, l2; |
| 4963 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4964 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4965 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4966 | #endif |
| 4967 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4968 | need_dict = 1; |
| 4969 | break; |
| 4970 | } |
| 4971 | if (decode[i] == 0xFFFE) |
| 4972 | /* unmapped character */ |
| 4973 | continue; |
| 4974 | l1 = decode[i] >> 11; |
| 4975 | l2 = decode[i] >> 7; |
| 4976 | if (level1[l1] == 0xFF) |
| 4977 | level1[l1] = count2++; |
| 4978 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4979 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4983 | need_dict = 1; |
| 4984 | |
| 4985 | if (need_dict) { |
| 4986 | PyObject *result = PyDict_New(); |
| 4987 | PyObject *key, *value; |
| 4988 | if (!result) |
| 4989 | return NULL; |
| 4990 | for (i = 0; i < 256; i++) { |
| 4991 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4992 | key = PyLong_FromLong(decode[i]); |
| 4993 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4994 | if (!key || !value) |
| 4995 | goto failed1; |
| 4996 | if (PyDict_SetItem(result, key, value) == -1) |
| 4997 | goto failed1; |
| 4998 | Py_DECREF(key); |
| 4999 | Py_DECREF(value); |
| 5000 | } |
| 5001 | return result; |
| 5002 | failed1: |
| 5003 | Py_XDECREF(key); |
| 5004 | Py_XDECREF(value); |
| 5005 | Py_DECREF(result); |
| 5006 | return NULL; |
| 5007 | } |
| 5008 | |
| 5009 | /* Create a three-level trie */ |
| 5010 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5011 | 16*count2 + 128*count3 - 1); |
| 5012 | if (!result) |
| 5013 | return PyErr_NoMemory(); |
| 5014 | PyObject_Init(result, &EncodingMapType); |
| 5015 | mresult = (struct encoding_map*)result; |
| 5016 | mresult->count2 = count2; |
| 5017 | mresult->count3 = count3; |
| 5018 | mlevel1 = mresult->level1; |
| 5019 | mlevel2 = mresult->level23; |
| 5020 | mlevel3 = mresult->level23 + 16*count2; |
| 5021 | memcpy(mlevel1, level1, 32); |
| 5022 | memset(mlevel2, 0xFF, 16*count2); |
| 5023 | memset(mlevel3, 0, 128*count3); |
| 5024 | count3 = 0; |
| 5025 | for (i = 1; i < 256; i++) { |
| 5026 | int o1, o2, o3, i2, i3; |
| 5027 | if (decode[i] == 0xFFFE) |
| 5028 | /* unmapped character */ |
| 5029 | continue; |
| 5030 | o1 = decode[i]>>11; |
| 5031 | o2 = (decode[i]>>7) & 0xF; |
| 5032 | i2 = 16*mlevel1[o1] + o2; |
| 5033 | if (mlevel2[i2] == 0xFF) |
| 5034 | mlevel2[i2] = count3++; |
| 5035 | o3 = decode[i] & 0x7F; |
| 5036 | i3 = 128*mlevel2[i2] + o3; |
| 5037 | mlevel3[i3] = i; |
| 5038 | } |
| 5039 | return result; |
| 5040 | } |
| 5041 | |
| 5042 | static int |
| 5043 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5044 | { |
| 5045 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5046 | int l1 = c>>11; |
| 5047 | int l2 = (c>>7) & 0xF; |
| 5048 | int l3 = c & 0x7F; |
| 5049 | int i; |
| 5050 | |
| 5051 | #ifdef Py_UNICODE_WIDE |
| 5052 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5054 | } |
| 5055 | #endif |
| 5056 | if (c == 0) |
| 5057 | return 0; |
| 5058 | /* level 1*/ |
| 5059 | i = map->level1[l1]; |
| 5060 | if (i == 0xFF) { |
| 5061 | return -1; |
| 5062 | } |
| 5063 | /* level 2*/ |
| 5064 | i = map->level23[16*i+l2]; |
| 5065 | if (i == 0xFF) { |
| 5066 | return -1; |
| 5067 | } |
| 5068 | /* level 3 */ |
| 5069 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5070 | if (i == 0) { |
| 5071 | return -1; |
| 5072 | } |
| 5073 | return i; |
| 5074 | } |
| 5075 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5076 | /* Lookup the character ch in the mapping. If the character |
| 5077 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5078 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5079 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5080 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5081 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5082 | PyObject *x; |
| 5083 | |
| 5084 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5085 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5086 | x = PyObject_GetItem(mapping, w); |
| 5087 | Py_DECREF(w); |
| 5088 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5089 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5090 | /* No mapping found means: mapping is undefined. */ |
| 5091 | PyErr_Clear(); |
| 5092 | x = Py_None; |
| 5093 | Py_INCREF(x); |
| 5094 | return x; |
| 5095 | } else |
| 5096 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5097 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5098 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5099 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5100 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5101 | long value = PyLong_AS_LONG(x); |
| 5102 | if (value < 0 || value > 255) { |
| 5103 | PyErr_SetString(PyExc_TypeError, |
| 5104 | "character mapping must be in range(256)"); |
| 5105 | Py_DECREF(x); |
| 5106 | return NULL; |
| 5107 | } |
| 5108 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5109 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5110 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5111 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5112 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5113 | /* wrong return value */ |
| 5114 | PyErr_Format(PyExc_TypeError, |
| 5115 | "character mapping must return integer, bytes or None, not %.400s", |
| 5116 | x->ob_type->tp_name); |
| 5117 | Py_DECREF(x); |
| 5118 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | } |
| 5120 | } |
| 5121 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5122 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5123 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5124 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5125 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5126 | /* exponentially overallocate to minimize reallocations */ |
| 5127 | if (requiredsize < 2*outsize) |
| 5128 | requiredsize = 2*outsize; |
| 5129 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5130 | return -1; |
| 5131 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5132 | } |
| 5133 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5134 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5135 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5136 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5137 | /* 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] | 5138 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5139 | space is available. Return a new reference to the object that |
| 5140 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5141 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5142 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5143 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5144 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5145 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5146 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5147 | PyObject *rep; |
| 5148 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5149 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5150 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5151 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5152 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5153 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5154 | if (res == -1) |
| 5155 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5156 | if (outsize<requiredsize) |
| 5157 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5158 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5159 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5160 | outstart[(*outpos)++] = (char)res; |
| 5161 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5165 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5166 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5167 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5168 | Py_DECREF(rep); |
| 5169 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5170 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5171 | if (PyLong_Check(rep)) { |
| 5172 | Py_ssize_t requiredsize = *outpos+1; |
| 5173 | if (outsize<requiredsize) |
| 5174 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5175 | Py_DECREF(rep); |
| 5176 | return enc_EXCEPTION; |
| 5177 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5178 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5179 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5180 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5181 | else { |
| 5182 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5183 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5184 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5185 | if (outsize<requiredsize) |
| 5186 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5187 | Py_DECREF(rep); |
| 5188 | return enc_EXCEPTION; |
| 5189 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5190 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5191 | memcpy(outstart + *outpos, repchars, repsize); |
| 5192 | *outpos += repsize; |
| 5193 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5194 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5195 | Py_DECREF(rep); |
| 5196 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | /* handle an error in PyUnicode_EncodeCharmap |
| 5200 | Return 0 on success, -1 on error */ |
| 5201 | static |
| 5202 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5203 | 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] | 5204 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5205 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5206 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5207 | { |
| 5208 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5209 | Py_ssize_t repsize; |
| 5210 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5211 | Py_UNICODE *uni2; |
| 5212 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5213 | Py_ssize_t collstartpos = *inpos; |
| 5214 | Py_ssize_t collendpos = *inpos+1; |
| 5215 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5216 | char *encoding = "charmap"; |
| 5217 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5218 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5219 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5220 | /* find all unencodable characters */ |
| 5221 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5222 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5223 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5224 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5225 | if (res != -1) |
| 5226 | break; |
| 5227 | ++collendpos; |
| 5228 | continue; |
| 5229 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5230 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5231 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5232 | if (rep==NULL) |
| 5233 | return -1; |
| 5234 | else if (rep!=Py_None) { |
| 5235 | Py_DECREF(rep); |
| 5236 | break; |
| 5237 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5238 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5239 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5240 | } |
| 5241 | /* cache callback name lookup |
| 5242 | * (if not done yet, i.e. it's the first error) */ |
| 5243 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5244 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5245 | *known_errorHandler = 1; |
| 5246 | else if (!strcmp(errors, "replace")) |
| 5247 | *known_errorHandler = 2; |
| 5248 | else if (!strcmp(errors, "ignore")) |
| 5249 | *known_errorHandler = 3; |
| 5250 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5251 | *known_errorHandler = 4; |
| 5252 | else |
| 5253 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5254 | } |
| 5255 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5256 | case 1: /* strict */ |
| 5257 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5258 | return -1; |
| 5259 | case 2: /* replace */ |
| 5260 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5261 | x = charmapencode_output('?', mapping, res, respos); |
| 5262 | if (x==enc_EXCEPTION) { |
| 5263 | return -1; |
| 5264 | } |
| 5265 | else if (x==enc_FAILED) { |
| 5266 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5267 | return -1; |
| 5268 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5269 | } |
| 5270 | /* fall through */ |
| 5271 | case 3: /* ignore */ |
| 5272 | *inpos = collendpos; |
| 5273 | break; |
| 5274 | case 4: /* xmlcharrefreplace */ |
| 5275 | /* generate replacement (temporarily (mis)uses p) */ |
| 5276 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5277 | char buffer[2+29+1+1]; |
| 5278 | char *cp; |
| 5279 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5280 | for (cp = buffer; *cp; ++cp) { |
| 5281 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5282 | if (x==enc_EXCEPTION) |
| 5283 | return -1; |
| 5284 | else if (x==enc_FAILED) { |
| 5285 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5286 | return -1; |
| 5287 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5288 | } |
| 5289 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5290 | *inpos = collendpos; |
| 5291 | break; |
| 5292 | default: |
| 5293 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5294 | encoding, reason, p, size, exceptionObject, |
| 5295 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5296 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5297 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5298 | if (PyBytes_Check(repunicode)) { |
| 5299 | /* Directly copy bytes result to output. */ |
| 5300 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5301 | Py_ssize_t requiredsize; |
| 5302 | repsize = PyBytes_Size(repunicode); |
| 5303 | requiredsize = *respos + repsize; |
| 5304 | if (requiredsize > outsize) |
| 5305 | /* Make room for all additional bytes. */ |
| 5306 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5307 | Py_DECREF(repunicode); |
| 5308 | return -1; |
| 5309 | } |
| 5310 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5311 | PyBytes_AsString(repunicode), repsize); |
| 5312 | *respos += repsize; |
| 5313 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5314 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5315 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5316 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5317 | /* generate replacement */ |
| 5318 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5319 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5320 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5321 | if (x==enc_EXCEPTION) { |
| 5322 | return -1; |
| 5323 | } |
| 5324 | else if (x==enc_FAILED) { |
| 5325 | Py_DECREF(repunicode); |
| 5326 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5327 | return -1; |
| 5328 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5329 | } |
| 5330 | *inpos = newpos; |
| 5331 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5332 | } |
| 5333 | return 0; |
| 5334 | } |
| 5335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5337 | Py_ssize_t size, |
| 5338 | PyObject *mapping, |
| 5339 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5340 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5341 | /* output object */ |
| 5342 | PyObject *res = NULL; |
| 5343 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5344 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5345 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5346 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5347 | PyObject *errorHandler = NULL; |
| 5348 | PyObject *exc = NULL; |
| 5349 | /* the following variable is used for caching string comparisons |
| 5350 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5351 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5352 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5353 | |
| 5354 | /* Default to Latin-1 */ |
| 5355 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5356 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5357 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5358 | /* allocate enough for a simple encoding without |
| 5359 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5360 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5361 | if (res == NULL) |
| 5362 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5363 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5364 | return res; |
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 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5367 | /* try to encode it */ |
| 5368 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5369 | if (x==enc_EXCEPTION) /* error */ |
| 5370 | goto onError; |
| 5371 | if (x==enc_FAILED) { /* unencodable character */ |
| 5372 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5373 | &exc, |
| 5374 | &known_errorHandler, &errorHandler, errors, |
| 5375 | &res, &respos)) { |
| 5376 | goto onError; |
| 5377 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5378 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | else |
| 5380 | /* done with this character => adjust input position */ |
| 5381 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5384 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5385 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5386 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5387 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5388 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5389 | Py_XDECREF(exc); |
| 5390 | Py_XDECREF(errorHandler); |
| 5391 | return res; |
| 5392 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5393 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5394 | Py_XDECREF(res); |
| 5395 | Py_XDECREF(exc); |
| 5396 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | return NULL; |
| 5398 | } |
| 5399 | |
| 5400 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5401 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 | { |
| 5403 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5404 | PyErr_BadArgument(); |
| 5405 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5406 | } |
| 5407 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5408 | PyUnicode_GET_SIZE(unicode), |
| 5409 | mapping, |
| 5410 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5413 | /* create or adjust a UnicodeTranslateError */ |
| 5414 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5415 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5416 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5417 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5419 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5420 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5421 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | } |
| 5423 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5424 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5425 | goto onError; |
| 5426 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5427 | goto onError; |
| 5428 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5429 | goto onError; |
| 5430 | return; |
| 5431 | onError: |
| 5432 | Py_DECREF(*exceptionObject); |
| 5433 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5434 | } |
| 5435 | } |
| 5436 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5437 | /* raises a UnicodeTranslateError */ |
| 5438 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5439 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5440 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5441 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5442 | { |
| 5443 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5444 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5445 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5446 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | /* error handling callback helper: |
| 5450 | build arguments, call the callback and check the arguments, |
| 5451 | put the result into newpos and return the replacement string, which |
| 5452 | has to be freed by the caller */ |
| 5453 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5454 | PyObject **errorHandler, |
| 5455 | const char *reason, |
| 5456 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5457 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5458 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5459 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5460 | 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] | 5461 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5462 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5463 | PyObject *restuple; |
| 5464 | PyObject *resunicode; |
| 5465 | |
| 5466 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5467 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5468 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5469 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
| 5472 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5473 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5474 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5475 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5476 | |
| 5477 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5478 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5479 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5480 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5481 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5482 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5483 | Py_DECREF(restuple); |
| 5484 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5485 | } |
| 5486 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5487 | &resunicode, &i_newpos)) { |
| 5488 | Py_DECREF(restuple); |
| 5489 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5490 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5491 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5492 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5493 | else |
| 5494 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5495 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5496 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5497 | Py_DECREF(restuple); |
| 5498 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5499 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5500 | Py_INCREF(resunicode); |
| 5501 | Py_DECREF(restuple); |
| 5502 | return resunicode; |
| 5503 | } |
| 5504 | |
| 5505 | /* Lookup the character ch in the mapping and put the result in result, |
| 5506 | which must be decrefed by the caller. |
| 5507 | Return 0 on success, -1 on error */ |
| 5508 | static |
| 5509 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5510 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5511 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5512 | PyObject *x; |
| 5513 | |
| 5514 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5515 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5516 | x = PyObject_GetItem(mapping, w); |
| 5517 | Py_DECREF(w); |
| 5518 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5519 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5520 | /* No mapping found means: use 1:1 mapping. */ |
| 5521 | PyErr_Clear(); |
| 5522 | *result = NULL; |
| 5523 | return 0; |
| 5524 | } else |
| 5525 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5526 | } |
| 5527 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5528 | *result = x; |
| 5529 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5530 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5531 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5532 | long value = PyLong_AS_LONG(x); |
| 5533 | long max = PyUnicode_GetMax(); |
| 5534 | if (value < 0 || value > max) { |
| 5535 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5536 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5537 | Py_DECREF(x); |
| 5538 | return -1; |
| 5539 | } |
| 5540 | *result = x; |
| 5541 | return 0; |
| 5542 | } |
| 5543 | else if (PyUnicode_Check(x)) { |
| 5544 | *result = x; |
| 5545 | return 0; |
| 5546 | } |
| 5547 | else { |
| 5548 | /* wrong return value */ |
| 5549 | PyErr_SetString(PyExc_TypeError, |
| 5550 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5551 | Py_DECREF(x); |
| 5552 | return -1; |
| 5553 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5554 | } |
| 5555 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5556 | if not reallocate and adjust various state variables. |
| 5557 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5558 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5559 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5560 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5561 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5562 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5563 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5564 | /* remember old output position */ |
| 5565 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5566 | /* exponentially overallocate to minimize reallocations */ |
| 5567 | if (requiredsize < 2 * oldsize) |
| 5568 | requiredsize = 2 * oldsize; |
| 5569 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5570 | return -1; |
| 5571 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5572 | } |
| 5573 | return 0; |
| 5574 | } |
| 5575 | /* lookup the character, put the result in the output string and adjust |
| 5576 | various state variables. Return a new reference to the object that |
| 5577 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5578 | undefined (in which case no character was written). |
| 5579 | The called must decref result. |
| 5580 | Return 0 on success, -1 on error. */ |
| 5581 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5582 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5583 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5584 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5585 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5586 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5587 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5588 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5589 | /* not found => default to 1:1 mapping */ |
| 5590 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5591 | } |
| 5592 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5593 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5594 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5595 | /* no overflow check, because we know that the space is enough */ |
| 5596 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5597 | } |
| 5598 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5599 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5600 | if (repsize==1) { |
| 5601 | /* no overflow check, because we know that the space is enough */ |
| 5602 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5603 | } |
| 5604 | else if (repsize!=0) { |
| 5605 | /* more than one character */ |
| 5606 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5607 | (insize - (curinp-startinp)) + |
| 5608 | repsize - 1; |
| 5609 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5610 | return -1; |
| 5611 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5612 | *outp += repsize; |
| 5613 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5614 | } |
| 5615 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5616 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5617 | return 0; |
| 5618 | } |
| 5619 | |
| 5620 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5621 | Py_ssize_t size, |
| 5622 | PyObject *mapping, |
| 5623 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5624 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5625 | /* output object */ |
| 5626 | PyObject *res = NULL; |
| 5627 | /* pointers to the beginning and end+1 of input */ |
| 5628 | const Py_UNICODE *startp = p; |
| 5629 | const Py_UNICODE *endp = p + size; |
| 5630 | /* pointer into the output */ |
| 5631 | Py_UNICODE *str; |
| 5632 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5633 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5634 | char *reason = "character maps to <undefined>"; |
| 5635 | PyObject *errorHandler = NULL; |
| 5636 | PyObject *exc = NULL; |
| 5637 | /* the following variable is used for caching string comparisons |
| 5638 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5639 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5640 | int known_errorHandler = -1; |
| 5641 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5643 | PyErr_BadArgument(); |
| 5644 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5645 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5646 | |
| 5647 | /* allocate enough for a simple 1:1 translation without |
| 5648 | replacements, if we need more, we'll resize */ |
| 5649 | res = PyUnicode_FromUnicode(NULL, size); |
| 5650 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5651 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5652 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5653 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5654 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5655 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5656 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5657 | /* try to encode it */ |
| 5658 | PyObject *x = NULL; |
| 5659 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5660 | Py_XDECREF(x); |
| 5661 | goto onError; |
| 5662 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5663 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5664 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5665 | ++p; |
| 5666 | else { /* untranslatable character */ |
| 5667 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5668 | Py_ssize_t repsize; |
| 5669 | Py_ssize_t newpos; |
| 5670 | Py_UNICODE *uni2; |
| 5671 | /* startpos for collecting untranslatable chars */ |
| 5672 | const Py_UNICODE *collstart = p; |
| 5673 | const Py_UNICODE *collend = p+1; |
| 5674 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5676 | /* find all untranslatable characters */ |
| 5677 | while (collend < endp) { |
| 5678 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5679 | goto onError; |
| 5680 | Py_XDECREF(x); |
| 5681 | if (x!=Py_None) |
| 5682 | break; |
| 5683 | ++collend; |
| 5684 | } |
| 5685 | /* cache callback name lookup |
| 5686 | * (if not done yet, i.e. it's the first error) */ |
| 5687 | if (known_errorHandler==-1) { |
| 5688 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5689 | known_errorHandler = 1; |
| 5690 | else if (!strcmp(errors, "replace")) |
| 5691 | known_errorHandler = 2; |
| 5692 | else if (!strcmp(errors, "ignore")) |
| 5693 | known_errorHandler = 3; |
| 5694 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5695 | known_errorHandler = 4; |
| 5696 | else |
| 5697 | known_errorHandler = 0; |
| 5698 | } |
| 5699 | switch (known_errorHandler) { |
| 5700 | case 1: /* strict */ |
| 5701 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5702 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5703 | case 2: /* replace */ |
| 5704 | /* No need to check for space, this is a 1:1 replacement */ |
| 5705 | for (coll = collstart; coll<collend; ++coll) |
| 5706 | *str++ = '?'; |
| 5707 | /* fall through */ |
| 5708 | case 3: /* ignore */ |
| 5709 | p = collend; |
| 5710 | break; |
| 5711 | case 4: /* xmlcharrefreplace */ |
| 5712 | /* generate replacement (temporarily (mis)uses p) */ |
| 5713 | for (p = collstart; p < collend; ++p) { |
| 5714 | char buffer[2+29+1+1]; |
| 5715 | char *cp; |
| 5716 | sprintf(buffer, "&#%d;", (int)*p); |
| 5717 | if (charmaptranslate_makespace(&res, &str, |
| 5718 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5719 | goto onError; |
| 5720 | for (cp = buffer; *cp; ++cp) |
| 5721 | *str++ = *cp; |
| 5722 | } |
| 5723 | p = collend; |
| 5724 | break; |
| 5725 | default: |
| 5726 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5727 | reason, startp, size, &exc, |
| 5728 | collstart-startp, collend-startp, &newpos); |
| 5729 | if (repunicode == NULL) |
| 5730 | goto onError; |
| 5731 | /* generate replacement */ |
| 5732 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5733 | if (charmaptranslate_makespace(&res, &str, |
| 5734 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5735 | Py_DECREF(repunicode); |
| 5736 | goto onError; |
| 5737 | } |
| 5738 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5739 | *str++ = *uni2; |
| 5740 | p = startp + newpos; |
| 5741 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5742 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5743 | } |
| 5744 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5745 | /* Resize if we allocated to much */ |
| 5746 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5747 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5748 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5749 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5750 | } |
| 5751 | Py_XDECREF(exc); |
| 5752 | Py_XDECREF(errorHandler); |
| 5753 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5756 | Py_XDECREF(res); |
| 5757 | Py_XDECREF(exc); |
| 5758 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | return NULL; |
| 5760 | } |
| 5761 | |
| 5762 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5763 | PyObject *mapping, |
| 5764 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5765 | { |
| 5766 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5767 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5768 | str = PyUnicode_FromObject(str); |
| 5769 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5770 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5772 | PyUnicode_GET_SIZE(str), |
| 5773 | mapping, |
| 5774 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5775 | Py_DECREF(str); |
| 5776 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5777 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5778 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5779 | Py_XDECREF(str); |
| 5780 | return NULL; |
| 5781 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5782 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5783 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5784 | |
| 5785 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5786 | Py_ssize_t length, |
| 5787 | char *output, |
| 5788 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5789 | { |
| 5790 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5791 | PyObject *errorHandler = NULL; |
| 5792 | PyObject *exc = NULL; |
| 5793 | const char *encoding = "decimal"; |
| 5794 | const char *reason = "invalid decimal Unicode string"; |
| 5795 | /* the following variable is used for caching string comparisons |
| 5796 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5797 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5798 | |
| 5799 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5800 | PyErr_BadArgument(); |
| 5801 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
| 5804 | p = s; |
| 5805 | end = s + length; |
| 5806 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5807 | register Py_UNICODE ch = *p; |
| 5808 | int decimal; |
| 5809 | PyObject *repunicode; |
| 5810 | Py_ssize_t repsize; |
| 5811 | Py_ssize_t newpos; |
| 5812 | Py_UNICODE *uni2; |
| 5813 | Py_UNICODE *collstart; |
| 5814 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5815 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5816 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5817 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5818 | ++p; |
| 5819 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5820 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5821 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5822 | if (decimal >= 0) { |
| 5823 | *output++ = '0' + decimal; |
| 5824 | ++p; |
| 5825 | continue; |
| 5826 | } |
| 5827 | if (0 < ch && ch < 256) { |
| 5828 | *output++ = (char)ch; |
| 5829 | ++p; |
| 5830 | continue; |
| 5831 | } |
| 5832 | /* All other characters are considered unencodable */ |
| 5833 | collstart = p; |
| 5834 | collend = p+1; |
| 5835 | while (collend < end) { |
| 5836 | if ((0 < *collend && *collend < 256) || |
| 5837 | !Py_UNICODE_ISSPACE(*collend) || |
| 5838 | Py_UNICODE_TODECIMAL(*collend)) |
| 5839 | break; |
| 5840 | } |
| 5841 | /* cache callback name lookup |
| 5842 | * (if not done yet, i.e. it's the first error) */ |
| 5843 | if (known_errorHandler==-1) { |
| 5844 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5845 | known_errorHandler = 1; |
| 5846 | else if (!strcmp(errors, "replace")) |
| 5847 | known_errorHandler = 2; |
| 5848 | else if (!strcmp(errors, "ignore")) |
| 5849 | known_errorHandler = 3; |
| 5850 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5851 | known_errorHandler = 4; |
| 5852 | else |
| 5853 | known_errorHandler = 0; |
| 5854 | } |
| 5855 | switch (known_errorHandler) { |
| 5856 | case 1: /* strict */ |
| 5857 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5858 | goto onError; |
| 5859 | case 2: /* replace */ |
| 5860 | for (p = collstart; p < collend; ++p) |
| 5861 | *output++ = '?'; |
| 5862 | /* fall through */ |
| 5863 | case 3: /* ignore */ |
| 5864 | p = collend; |
| 5865 | break; |
| 5866 | case 4: /* xmlcharrefreplace */ |
| 5867 | /* generate replacement (temporarily (mis)uses p) */ |
| 5868 | for (p = collstart; p < collend; ++p) |
| 5869 | output += sprintf(output, "&#%d;", (int)*p); |
| 5870 | p = collend; |
| 5871 | break; |
| 5872 | default: |
| 5873 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5874 | encoding, reason, s, length, &exc, |
| 5875 | collstart-s, collend-s, &newpos); |
| 5876 | if (repunicode == NULL) |
| 5877 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5878 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5879 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5880 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 5881 | Py_DECREF(repunicode); |
| 5882 | goto onError; |
| 5883 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5884 | /* generate replacement */ |
| 5885 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5886 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5887 | Py_UNICODE ch = *uni2; |
| 5888 | if (Py_UNICODE_ISSPACE(ch)) |
| 5889 | *output++ = ' '; |
| 5890 | else { |
| 5891 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5892 | if (decimal >= 0) |
| 5893 | *output++ = '0' + decimal; |
| 5894 | else if (0 < ch && ch < 256) |
| 5895 | *output++ = (char)ch; |
| 5896 | else { |
| 5897 | Py_DECREF(repunicode); |
| 5898 | raise_encode_exception(&exc, encoding, |
| 5899 | s, length, collstart-s, collend-s, reason); |
| 5900 | goto onError; |
| 5901 | } |
| 5902 | } |
| 5903 | } |
| 5904 | p = s + newpos; |
| 5905 | Py_DECREF(repunicode); |
| 5906 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5907 | } |
| 5908 | /* 0-terminate the output string */ |
| 5909 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5910 | Py_XDECREF(exc); |
| 5911 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5912 | return 0; |
| 5913 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5914 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5915 | Py_XDECREF(exc); |
| 5916 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5917 | return -1; |
| 5918 | } |
| 5919 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | /* --- Helpers ------------------------------------------------------------ */ |
| 5921 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5922 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5923 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5924 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5925 | #include "stringlib/count.h" |
| 5926 | #include "stringlib/find.h" |
| 5927 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5928 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5929 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5930 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 5931 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5932 | #include "stringlib/localeutil.h" |
| 5933 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5934 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5935 | #define ADJUST_INDICES(start, end, len) \ |
| 5936 | if (end > len) \ |
| 5937 | end = len; \ |
| 5938 | else if (end < 0) { \ |
| 5939 | end += len; \ |
| 5940 | if (end < 0) \ |
| 5941 | end = 0; \ |
| 5942 | } \ |
| 5943 | if (start < 0) { \ |
| 5944 | start += len; \ |
| 5945 | if (start < 0) \ |
| 5946 | start = 0; \ |
| 5947 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5948 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5949 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5950 | PyObject *substr, |
| 5951 | Py_ssize_t start, |
| 5952 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5954 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5955 | PyUnicodeObject* str_obj; |
| 5956 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5957 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5958 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5959 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5960 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5961 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5962 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5963 | Py_DECREF(str_obj); |
| 5964 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5965 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5966 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5967 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5968 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 5969 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 5970 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5971 | ); |
| 5972 | |
| 5973 | Py_DECREF(sub_obj); |
| 5974 | Py_DECREF(str_obj); |
| 5975 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | return result; |
| 5977 | } |
| 5978 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5979 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5980 | PyObject *sub, |
| 5981 | Py_ssize_t start, |
| 5982 | Py_ssize_t end, |
| 5983 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5985 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5988 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5989 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5990 | sub = PyUnicode_FromObject(sub); |
| 5991 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5992 | Py_DECREF(str); |
| 5993 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5994 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5995 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5996 | if (direction > 0) |
| 5997 | result = stringlib_find_slice( |
| 5998 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5999 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6000 | start, end |
| 6001 | ); |
| 6002 | else |
| 6003 | result = stringlib_rfind_slice( |
| 6004 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6005 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6006 | start, end |
| 6007 | ); |
| 6008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6010 | Py_DECREF(sub); |
| 6011 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | return result; |
| 6013 | } |
| 6014 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6015 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6017 | PyUnicodeObject *substring, |
| 6018 | Py_ssize_t start, |
| 6019 | Py_ssize_t end, |
| 6020 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | if (substring->length == 0) |
| 6023 | return 1; |
| 6024 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6025 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6026 | end -= substring->length; |
| 6027 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6028 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | |
| 6030 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6031 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6032 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | } else { |
| 6034 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6035 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | } |
| 6037 | |
| 6038 | return 0; |
| 6039 | } |
| 6040 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6041 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | PyObject *substr, |
| 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 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6047 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | str = PyUnicode_FromObject(str); |
| 6050 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6051 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6052 | substr = PyUnicode_FromObject(substr); |
| 6053 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | Py_DECREF(str); |
| 6055 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6059 | (PyUnicodeObject *)substr, |
| 6060 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | Py_DECREF(str); |
| 6062 | Py_DECREF(substr); |
| 6063 | return result; |
| 6064 | } |
| 6065 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | /* Apply fixfct filter to the Unicode object self and return a |
| 6067 | reference to the modified object */ |
| 6068 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6069 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6071 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | { |
| 6073 | |
| 6074 | PyUnicodeObject *u; |
| 6075 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6076 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6077 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6078 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6079 | |
| 6080 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6081 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6082 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6083 | /* fixfct should return TRUE if it modified the buffer. If |
| 6084 | FALSE, return a reference to the original buffer instead |
| 6085 | (to save space, not time) */ |
| 6086 | Py_INCREF(self); |
| 6087 | Py_DECREF(u); |
| 6088 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | } |
| 6090 | return (PyObject*) u; |
| 6091 | } |
| 6092 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6093 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6094 | int fixupper(PyUnicodeObject *self) |
| 6095 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6096 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | Py_UNICODE *s = self->str; |
| 6098 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6099 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6101 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6102 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6103 | ch = Py_UNICODE_TOUPPER(*s); |
| 6104 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6105 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6106 | *s = ch; |
| 6107 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6108 | s++; |
| 6109 | } |
| 6110 | |
| 6111 | return status; |
| 6112 | } |
| 6113 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6114 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6115 | int fixlower(PyUnicodeObject *self) |
| 6116 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6117 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6118 | Py_UNICODE *s = self->str; |
| 6119 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6120 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6122 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6123 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6124 | ch = Py_UNICODE_TOLOWER(*s); |
| 6125 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6127 | *s = ch; |
| 6128 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | s++; |
| 6130 | } |
| 6131 | |
| 6132 | return status; |
| 6133 | } |
| 6134 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6135 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | int fixswapcase(PyUnicodeObject *self) |
| 6137 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6138 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6139 | Py_UNICODE *s = self->str; |
| 6140 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | while (len-- > 0) { |
| 6143 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6144 | *s = Py_UNICODE_TOLOWER(*s); |
| 6145 | status = 1; |
| 6146 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6147 | *s = Py_UNICODE_TOUPPER(*s); |
| 6148 | status = 1; |
| 6149 | } |
| 6150 | s++; |
| 6151 | } |
| 6152 | |
| 6153 | return status; |
| 6154 | } |
| 6155 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6156 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6157 | int fixcapitalize(PyUnicodeObject *self) |
| 6158 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6159 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6160 | Py_UNICODE *s = self->str; |
| 6161 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6162 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6163 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6164 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6165 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6166 | *s = Py_UNICODE_TOUPPER(*s); |
| 6167 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6169 | s++; |
| 6170 | while (--len > 0) { |
| 6171 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6172 | *s = Py_UNICODE_TOLOWER(*s); |
| 6173 | status = 1; |
| 6174 | } |
| 6175 | s++; |
| 6176 | } |
| 6177 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
| 6180 | static |
| 6181 | int fixtitle(PyUnicodeObject *self) |
| 6182 | { |
| 6183 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6184 | register Py_UNICODE *e; |
| 6185 | int previous_is_cased; |
| 6186 | |
| 6187 | /* Shortcut for single character strings */ |
| 6188 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6189 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6190 | if (*p != ch) { |
| 6191 | *p = ch; |
| 6192 | return 1; |
| 6193 | } |
| 6194 | else |
| 6195 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6196 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6198 | e = p + PyUnicode_GET_SIZE(self); |
| 6199 | previous_is_cased = 0; |
| 6200 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6202 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6203 | if (previous_is_cased) |
| 6204 | *p = Py_UNICODE_TOLOWER(ch); |
| 6205 | else |
| 6206 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6207 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6208 | if (Py_UNICODE_ISLOWER(ch) || |
| 6209 | Py_UNICODE_ISUPPER(ch) || |
| 6210 | Py_UNICODE_ISTITLE(ch)) |
| 6211 | previous_is_cased = 1; |
| 6212 | else |
| 6213 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6214 | } |
| 6215 | return 1; |
| 6216 | } |
| 6217 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6218 | PyObject * |
| 6219 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6220 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6221 | const Py_UNICODE blank = ' '; |
| 6222 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6223 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6224 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6225 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6226 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6227 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6228 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6229 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6230 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6232 | fseq = PySequence_Fast(seq, ""); |
| 6233 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6234 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6237 | /* NOTE: the following code can't call back into Python code, |
| 6238 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6239 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6240 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6241 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6242 | /* If empty sequence, return u"". */ |
| 6243 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6244 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6245 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6246 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6247 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6248 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6249 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6250 | item = items[0]; |
| 6251 | if (PyUnicode_CheckExact(item)) { |
| 6252 | Py_INCREF(item); |
| 6253 | res = (PyUnicodeObject *)item; |
| 6254 | goto Done; |
| 6255 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6256 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6257 | else { |
| 6258 | /* Set up sep and seplen */ |
| 6259 | if (separator == NULL) { |
| 6260 | sep = ␣ |
| 6261 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6262 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6263 | else { |
| 6264 | if (!PyUnicode_Check(separator)) { |
| 6265 | PyErr_Format(PyExc_TypeError, |
| 6266 | "separator: expected str instance," |
| 6267 | " %.80s found", |
| 6268 | Py_TYPE(separator)->tp_name); |
| 6269 | goto onError; |
| 6270 | } |
| 6271 | sep = PyUnicode_AS_UNICODE(separator); |
| 6272 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6273 | } |
| 6274 | } |
| 6275 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6276 | /* There are at least two things to join, or else we have a subclass |
| 6277 | * of str in the sequence. |
| 6278 | * Do a pre-pass to figure out the total amount of space we'll |
| 6279 | * need (sz), and see whether all argument are strings. |
| 6280 | */ |
| 6281 | sz = 0; |
| 6282 | for (i = 0; i < seqlen; i++) { |
| 6283 | const Py_ssize_t old_sz = sz; |
| 6284 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 | if (!PyUnicode_Check(item)) { |
| 6286 | PyErr_Format(PyExc_TypeError, |
| 6287 | "sequence item %zd: expected str instance," |
| 6288 | " %.80s found", |
| 6289 | i, Py_TYPE(item)->tp_name); |
| 6290 | goto onError; |
| 6291 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6292 | sz += PyUnicode_GET_SIZE(item); |
| 6293 | if (i != 0) |
| 6294 | sz += seplen; |
| 6295 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6296 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6298 | goto onError; |
| 6299 | } |
| 6300 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6301 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6302 | res = _PyUnicode_New(sz); |
| 6303 | if (res == NULL) |
| 6304 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6305 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6306 | /* Catenate everything. */ |
| 6307 | res_p = PyUnicode_AS_UNICODE(res); |
| 6308 | for (i = 0; i < seqlen; ++i) { |
| 6309 | Py_ssize_t itemlen; |
| 6310 | item = items[i]; |
| 6311 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6312 | /* Copy item, and maybe the separator. */ |
| 6313 | if (i) { |
| 6314 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6315 | res_p += seplen; |
| 6316 | } |
| 6317 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6318 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6319 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6320 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6321 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6322 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6323 | return (PyObject *)res; |
| 6324 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6325 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6326 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6327 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6328 | return NULL; |
| 6329 | } |
| 6330 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6331 | static |
| 6332 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6333 | Py_ssize_t left, |
| 6334 | Py_ssize_t right, |
| 6335 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6336 | { |
| 6337 | PyUnicodeObject *u; |
| 6338 | |
| 6339 | if (left < 0) |
| 6340 | left = 0; |
| 6341 | if (right < 0) |
| 6342 | right = 0; |
| 6343 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6344 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | Py_INCREF(self); |
| 6346 | return self; |
| 6347 | } |
| 6348 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6349 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6350 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6351 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6352 | return NULL; |
| 6353 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | u = _PyUnicode_New(left + self->length + right); |
| 6355 | if (u) { |
| 6356 | if (left) |
| 6357 | Py_UNICODE_FILL(u->str, fill, left); |
| 6358 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6359 | if (right) |
| 6360 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6361 | } |
| 6362 | |
| 6363 | return u; |
| 6364 | } |
| 6365 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6366 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6367 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6368 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6369 | |
| 6370 | string = PyUnicode_FromObject(string); |
| 6371 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6372 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6374 | list = stringlib_splitlines( |
| 6375 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6376 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | |
| 6378 | Py_DECREF(string); |
| 6379 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6380 | } |
| 6381 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6382 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6383 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6384 | PyUnicodeObject *substring, |
| 6385 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6387 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6388 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6389 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6390 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6391 | return stringlib_split_whitespace( |
| 6392 | (PyObject*) self, self->str, self->length, maxcount |
| 6393 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6395 | return stringlib_split( |
| 6396 | (PyObject*) self, self->str, self->length, |
| 6397 | substring->str, substring->length, |
| 6398 | maxcount |
| 6399 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6402 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6403 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6404 | PyUnicodeObject *substring, |
| 6405 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6406 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6407 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6408 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6409 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6410 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6411 | return stringlib_rsplit_whitespace( |
| 6412 | (PyObject*) self, self->str, self->length, maxcount |
| 6413 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6414 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6415 | return stringlib_rsplit( |
| 6416 | (PyObject*) self, self->str, self->length, |
| 6417 | substring->str, substring->length, |
| 6418 | maxcount |
| 6419 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6420 | } |
| 6421 | |
| 6422 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6423 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6424 | PyUnicodeObject *str1, |
| 6425 | PyUnicodeObject *str2, |
| 6426 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6427 | { |
| 6428 | PyUnicodeObject *u; |
| 6429 | |
| 6430 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6431 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6432 | else if (maxcount == 0 || self->length == 0) |
| 6433 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6435 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6436 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6437 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6438 | if (str1->length == 0) |
| 6439 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6440 | if (str1->length == 1) { |
| 6441 | /* replace characters */ |
| 6442 | Py_UNICODE u1, u2; |
| 6443 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6444 | goto nothing; |
| 6445 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6446 | if (!u) |
| 6447 | return NULL; |
| 6448 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6449 | u1 = str1->str[0]; |
| 6450 | u2 = str2->str[0]; |
| 6451 | for (i = 0; i < u->length; i++) |
| 6452 | if (u->str[i] == u1) { |
| 6453 | if (--maxcount < 0) |
| 6454 | break; |
| 6455 | u->str[i] = u2; |
| 6456 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6457 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6458 | i = stringlib_find( |
| 6459 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6461 | if (i < 0) |
| 6462 | goto nothing; |
| 6463 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6464 | if (!u) |
| 6465 | return NULL; |
| 6466 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6467 | |
| 6468 | /* change everything in-place, starting with this one */ |
| 6469 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6470 | i += str1->length; |
| 6471 | |
| 6472 | while ( --maxcount > 0) { |
| 6473 | i = stringlib_find(self->str+i, self->length-i, |
| 6474 | str1->str, str1->length, |
| 6475 | i); |
| 6476 | if (i == -1) |
| 6477 | break; |
| 6478 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6479 | i += str1->length; |
| 6480 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6481 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6483 | |
| 6484 | Py_ssize_t n, i, j, e; |
| 6485 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | Py_UNICODE *p; |
| 6487 | |
| 6488 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6489 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6490 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6491 | if (n == 0) |
| 6492 | goto nothing; |
| 6493 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6494 | delta = (str2->length - str1->length); |
| 6495 | if (delta == 0) { |
| 6496 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6497 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6498 | product = n * (str2->length - str1->length); |
| 6499 | if ((product / (str2->length - str1->length)) != n) { |
| 6500 | PyErr_SetString(PyExc_OverflowError, |
| 6501 | "replace string is too long"); |
| 6502 | return NULL; |
| 6503 | } |
| 6504 | new_size = self->length + product; |
| 6505 | if (new_size < 0) { |
| 6506 | PyErr_SetString(PyExc_OverflowError, |
| 6507 | "replace string is too long"); |
| 6508 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | } |
| 6510 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6511 | u = _PyUnicode_New(new_size); |
| 6512 | if (!u) |
| 6513 | return NULL; |
| 6514 | i = 0; |
| 6515 | p = u->str; |
| 6516 | e = self->length - str1->length; |
| 6517 | if (str1->length > 0) { |
| 6518 | while (n-- > 0) { |
| 6519 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6520 | j = stringlib_find(self->str+i, self->length-i, |
| 6521 | str1->str, str1->length, |
| 6522 | i); |
| 6523 | if (j == -1) |
| 6524 | break; |
| 6525 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6526 | /* copy unchanged part [i:j] */ |
| 6527 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6528 | p += j - i; |
| 6529 | } |
| 6530 | /* copy substitution string */ |
| 6531 | if (str2->length > 0) { |
| 6532 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6533 | p += str2->length; |
| 6534 | } |
| 6535 | i = j + str1->length; |
| 6536 | } |
| 6537 | if (i < self->length) |
| 6538 | /* copy tail [i:] */ |
| 6539 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6540 | } else { |
| 6541 | /* interleave */ |
| 6542 | while (n > 0) { |
| 6543 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6544 | p += str2->length; |
| 6545 | if (--n <= 0) |
| 6546 | break; |
| 6547 | *p++ = self->str[i++]; |
| 6548 | } |
| 6549 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6550 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6552 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6553 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6554 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6555 | /* nothing to replace; return original string (when possible) */ |
| 6556 | if (PyUnicode_CheckExact(self)) { |
| 6557 | Py_INCREF(self); |
| 6558 | return (PyObject *) self; |
| 6559 | } |
| 6560 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6561 | } |
| 6562 | |
| 6563 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6564 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6565 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | \n\ |
| 6568 | 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] | 6569 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6570 | |
| 6571 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6572 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6573 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6574 | return fixup(self, fixtitle); |
| 6575 | } |
| 6576 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6577 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6578 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | \n\ |
| 6580 | 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] | 6581 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6582 | |
| 6583 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6584 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6585 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | return fixup(self, fixcapitalize); |
| 6587 | } |
| 6588 | |
| 6589 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6590 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6592 | \n\ |
| 6593 | 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] | 6594 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
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_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | { |
| 6599 | PyObject *list; |
| 6600 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6601 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6602 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6603 | /* Split into words */ |
| 6604 | list = split(self, NULL, -1); |
| 6605 | if (!list) |
| 6606 | return NULL; |
| 6607 | |
| 6608 | /* Capitalize each word */ |
| 6609 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6610 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6611 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6612 | if (item == NULL) |
| 6613 | goto onError; |
| 6614 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6615 | PyList_SET_ITEM(list, i, item); |
| 6616 | } |
| 6617 | |
| 6618 | /* Join the words to form a new string */ |
| 6619 | item = PyUnicode_Join(NULL, list); |
| 6620 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6621 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | Py_DECREF(list); |
| 6623 | return (PyObject *)item; |
| 6624 | } |
| 6625 | #endif |
| 6626 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6627 | /* Argument converter. Coerces to a single unicode character */ |
| 6628 | |
| 6629 | static int |
| 6630 | convert_uc(PyObject *obj, void *addr) |
| 6631 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6632 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6633 | PyObject *uniobj; |
| 6634 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6635 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6636 | uniobj = PyUnicode_FromObject(obj); |
| 6637 | if (uniobj == NULL) { |
| 6638 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6639 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6640 | return 0; |
| 6641 | } |
| 6642 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6643 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6644 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6645 | Py_DECREF(uniobj); |
| 6646 | return 0; |
| 6647 | } |
| 6648 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6649 | *fillcharloc = unistr[0]; |
| 6650 | Py_DECREF(uniobj); |
| 6651 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6652 | } |
| 6653 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6654 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6655 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6657 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6658 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | |
| 6660 | static PyObject * |
| 6661 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6662 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6663 | Py_ssize_t marg, left; |
| 6664 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6665 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6666 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6667 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | return NULL; |
| 6669 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6670 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | Py_INCREF(self); |
| 6672 | return (PyObject*) self; |
| 6673 | } |
| 6674 | |
| 6675 | marg = width - self->length; |
| 6676 | left = marg / 2 + (marg & width & 1); |
| 6677 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6678 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6679 | } |
| 6680 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6681 | #if 0 |
| 6682 | |
| 6683 | /* This code should go into some future Unicode collation support |
| 6684 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6685 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6686 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6687 | /* speedy UTF-16 code point order comparison */ |
| 6688 | /* gleaned from: */ |
| 6689 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6690 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6691 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6692 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6693 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6694 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6695 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6696 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6697 | }; |
| 6698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 | static int |
| 6700 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6701 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6702 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6704 | Py_UNICODE *s1 = str1->str; |
| 6705 | Py_UNICODE *s2 = str2->str; |
| 6706 | |
| 6707 | len1 = str1->length; |
| 6708 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6709 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6710 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6711 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6712 | |
| 6713 | c1 = *s1++; |
| 6714 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6715 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6716 | if (c1 > (1<<11) * 26) |
| 6717 | c1 += utf16Fixup[c1>>11]; |
| 6718 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6719 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6720 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6721 | |
| 6722 | if (c1 != c2) |
| 6723 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6724 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6725 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6726 | } |
| 6727 | |
| 6728 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6729 | } |
| 6730 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6731 | #else |
| 6732 | |
| 6733 | static int |
| 6734 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6735 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6736 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6737 | |
| 6738 | Py_UNICODE *s1 = str1->str; |
| 6739 | Py_UNICODE *s2 = str2->str; |
| 6740 | |
| 6741 | len1 = str1->length; |
| 6742 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6743 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6744 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6745 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6746 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6747 | c1 = *s1++; |
| 6748 | c2 = *s2++; |
| 6749 | |
| 6750 | if (c1 != c2) |
| 6751 | return (c1 < c2) ? -1 : 1; |
| 6752 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6753 | len1--; len2--; |
| 6754 | } |
| 6755 | |
| 6756 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6757 | } |
| 6758 | |
| 6759 | #endif |
| 6760 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6761 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6762 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6763 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6764 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6765 | return unicode_compare((PyUnicodeObject *)left, |
| 6766 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6767 | PyErr_Format(PyExc_TypeError, |
| 6768 | "Can't compare %.100s and %.100s", |
| 6769 | left->ob_type->tp_name, |
| 6770 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | return -1; |
| 6772 | } |
| 6773 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6774 | int |
| 6775 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6776 | { |
| 6777 | int i; |
| 6778 | Py_UNICODE *id; |
| 6779 | assert(PyUnicode_Check(uni)); |
| 6780 | id = PyUnicode_AS_UNICODE(uni); |
| 6781 | /* Compare Unicode string and source character set string */ |
| 6782 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6783 | if (id[i] != str[i]) |
| 6784 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 6785 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 6786 | to C strings identical up to that point. */ |
| 6787 | if (PyUnicode_GET_SIZE(uni) != i) |
| 6788 | /* We'll say the Python string is longer. */ |
| 6789 | return 1; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6790 | if (id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6791 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6792 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6793 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6794 | return 0; |
| 6795 | } |
| 6796 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6797 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6798 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6799 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6800 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6801 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6802 | PyObject *right, |
| 6803 | int op) |
| 6804 | { |
| 6805 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6806 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6807 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6808 | PyObject *v; |
| 6809 | if (((PyUnicodeObject *) left)->length != |
| 6810 | ((PyUnicodeObject *) right)->length) { |
| 6811 | if (op == Py_EQ) { |
| 6812 | Py_INCREF(Py_False); |
| 6813 | return Py_False; |
| 6814 | } |
| 6815 | if (op == Py_NE) { |
| 6816 | Py_INCREF(Py_True); |
| 6817 | return Py_True; |
| 6818 | } |
| 6819 | } |
| 6820 | if (left == right) |
| 6821 | result = 0; |
| 6822 | else |
| 6823 | result = unicode_compare((PyUnicodeObject *)left, |
| 6824 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6825 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6826 | /* Convert the return value to a Boolean */ |
| 6827 | switch (op) { |
| 6828 | case Py_EQ: |
| 6829 | v = TEST_COND(result == 0); |
| 6830 | break; |
| 6831 | case Py_NE: |
| 6832 | v = TEST_COND(result != 0); |
| 6833 | break; |
| 6834 | case Py_LE: |
| 6835 | v = TEST_COND(result <= 0); |
| 6836 | break; |
| 6837 | case Py_GE: |
| 6838 | v = TEST_COND(result >= 0); |
| 6839 | break; |
| 6840 | case Py_LT: |
| 6841 | v = TEST_COND(result == -1); |
| 6842 | break; |
| 6843 | case Py_GT: |
| 6844 | v = TEST_COND(result == 1); |
| 6845 | break; |
| 6846 | default: |
| 6847 | PyErr_BadArgument(); |
| 6848 | return NULL; |
| 6849 | } |
| 6850 | Py_INCREF(v); |
| 6851 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6852 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6853 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6854 | Py_INCREF(Py_NotImplemented); |
| 6855 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6858 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6859 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6860 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6861 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6862 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6863 | |
| 6864 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6865 | sub = PyUnicode_FromObject(element); |
| 6866 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6867 | PyErr_Format(PyExc_TypeError, |
| 6868 | "'in <string>' requires string as left operand, not %s", |
| 6869 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6870 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6871 | } |
| 6872 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6873 | str = PyUnicode_FromObject(container); |
| 6874 | if (!str) { |
| 6875 | Py_DECREF(sub); |
| 6876 | return -1; |
| 6877 | } |
| 6878 | |
| 6879 | result = stringlib_contains_obj(str, sub); |
| 6880 | |
| 6881 | Py_DECREF(str); |
| 6882 | Py_DECREF(sub); |
| 6883 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6884 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6885 | } |
| 6886 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6888 | |
| 6889 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6890 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6891 | { |
| 6892 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6893 | |
| 6894 | /* Coerce the two arguments */ |
| 6895 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6896 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6897 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6898 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6899 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6900 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6901 | |
| 6902 | /* Shortcuts */ |
| 6903 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6904 | Py_DECREF(v); |
| 6905 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6906 | } |
| 6907 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6908 | Py_DECREF(u); |
| 6909 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6910 | } |
| 6911 | |
| 6912 | /* Concat the two Unicode strings */ |
| 6913 | w = _PyUnicode_New(u->length + v->length); |
| 6914 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6915 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6916 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6917 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6918 | |
| 6919 | Py_DECREF(u); |
| 6920 | Py_DECREF(v); |
| 6921 | return (PyObject *)w; |
| 6922 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6923 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6924 | Py_XDECREF(u); |
| 6925 | Py_XDECREF(v); |
| 6926 | return NULL; |
| 6927 | } |
| 6928 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6929 | void |
| 6930 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6931 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6932 | PyObject *new; |
| 6933 | if (*pleft == NULL) |
| 6934 | return; |
| 6935 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6936 | Py_DECREF(*pleft); |
| 6937 | *pleft = NULL; |
| 6938 | return; |
| 6939 | } |
| 6940 | new = PyUnicode_Concat(*pleft, right); |
| 6941 | Py_DECREF(*pleft); |
| 6942 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6943 | } |
| 6944 | |
| 6945 | void |
| 6946 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6947 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6948 | PyUnicode_Append(pleft, right); |
| 6949 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6950 | } |
| 6951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6952 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6953 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6954 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6955 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6956 | 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] | 6957 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6958 | |
| 6959 | static PyObject * |
| 6960 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6961 | { |
| 6962 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6963 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6964 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6965 | PyObject *result; |
| 6966 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6967 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6968 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6969 | return NULL; |
| 6970 | |
| 6971 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6972 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6973 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6974 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6975 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6976 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6977 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6978 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6979 | substring->str, substring->length, |
| 6980 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6981 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 | |
| 6983 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6985 | return result; |
| 6986 | } |
| 6987 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6988 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6989 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6990 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6991 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6992 | 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] | 6993 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6994 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6995 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6996 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6997 | |
| 6998 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 6999 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7000 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7001 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7002 | char *encoding = NULL; |
| 7003 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7004 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7005 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7006 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7007 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7008 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7009 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7010 | if (v == NULL) |
| 7011 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7012 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7013 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7014 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7015 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7016 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7017 | Py_DECREF(v); |
| 7018 | return NULL; |
| 7019 | } |
| 7020 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7021 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7022 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7023 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7024 | } |
| 7025 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7026 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7027 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | \n\ |
| 7029 | 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] | 7030 | 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] | 7031 | |
| 7032 | static PyObject* |
| 7033 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7034 | { |
| 7035 | Py_UNICODE *e; |
| 7036 | Py_UNICODE *p; |
| 7037 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7038 | Py_UNICODE *qe; |
| 7039 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7040 | PyUnicodeObject *u; |
| 7041 | int tabsize = 8; |
| 7042 | |
| 7043 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7044 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7045 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7046 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7047 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7048 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7049 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | for (p = self->str; p < e; p++) |
| 7051 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7052 | if (tabsize > 0) { |
| 7053 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7054 | if (j > PY_SSIZE_T_MAX - incr) |
| 7055 | goto overflow1; |
| 7056 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7057 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7058 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7059 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7060 | if (j > PY_SSIZE_T_MAX - 1) |
| 7061 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | j++; |
| 7063 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7064 | if (i > PY_SSIZE_T_MAX - j) |
| 7065 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7066 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7067 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7068 | } |
| 7069 | } |
| 7070 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7071 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7072 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | /* Second pass: create output string and fill it */ |
| 7075 | u = _PyUnicode_New(i + j); |
| 7076 | if (!u) |
| 7077 | return NULL; |
| 7078 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7079 | j = 0; /* same as in first pass */ |
| 7080 | q = u->str; /* next output char */ |
| 7081 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7082 | |
| 7083 | for (p = self->str; p < e; p++) |
| 7084 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7085 | if (tabsize > 0) { |
| 7086 | i = tabsize - (j % tabsize); |
| 7087 | j += i; |
| 7088 | while (i--) { |
| 7089 | if (q >= qe) |
| 7090 | goto overflow2; |
| 7091 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7092 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7093 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7094 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7095 | else { |
| 7096 | if (q >= qe) |
| 7097 | goto overflow2; |
| 7098 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7099 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7100 | if (*p == '\n' || *p == '\r') |
| 7101 | j = 0; |
| 7102 | } |
| 7103 | |
| 7104 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7105 | |
| 7106 | overflow2: |
| 7107 | Py_DECREF(u); |
| 7108 | overflow1: |
| 7109 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7110 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7113 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7114 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7115 | \n\ |
| 7116 | 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] | 7117 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7118 | arguments start and end are interpreted as in slice notation.\n\ |
| 7119 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7120 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7121 | |
| 7122 | static PyObject * |
| 7123 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7124 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7125 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7126 | Py_ssize_t start; |
| 7127 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7128 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7129 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7130 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7131 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7133 | result = stringlib_find_slice( |
| 7134 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7135 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7136 | start, end |
| 7137 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7138 | |
| 7139 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7140 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7141 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7142 | } |
| 7143 | |
| 7144 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7145 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7146 | { |
| 7147 | if (index < 0 || index >= self->length) { |
| 7148 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7149 | return NULL; |
| 7150 | } |
| 7151 | |
| 7152 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7153 | } |
| 7154 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7155 | /* Believe it or not, this produces the same value for ASCII strings |
| 7156 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7157 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7158 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7159 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7160 | Py_ssize_t len; |
| 7161 | Py_UNICODE *p; |
| 7162 | long x; |
| 7163 | |
| 7164 | if (self->hash != -1) |
| 7165 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7166 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7167 | p = self->str; |
| 7168 | x = *p << 7; |
| 7169 | while (--len >= 0) |
| 7170 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7171 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7172 | if (x == -1) |
| 7173 | x = -2; |
| 7174 | self->hash = x; |
| 7175 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7178 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7179 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7180 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7181 | 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] | 7182 | |
| 7183 | static PyObject * |
| 7184 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7185 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7186 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7187 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7188 | Py_ssize_t start; |
| 7189 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7190 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7191 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7192 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7193 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7194 | result = stringlib_find_slice( |
| 7195 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7196 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7197 | start, end |
| 7198 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7199 | |
| 7200 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7202 | if (result < 0) { |
| 7203 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7204 | return NULL; |
| 7205 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7206 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7207 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7208 | } |
| 7209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7210 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7211 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7212 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7213 | 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] | 7214 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7215 | |
| 7216 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7217 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7218 | { |
| 7219 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7220 | register const Py_UNICODE *e; |
| 7221 | int cased; |
| 7222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7223 | /* Shortcut for single character strings */ |
| 7224 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7225 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7226 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7227 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7228 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7229 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7231 | e = p + PyUnicode_GET_SIZE(self); |
| 7232 | cased = 0; |
| 7233 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7234 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7235 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7236 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7237 | return PyBool_FromLong(0); |
| 7238 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7239 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7241 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7242 | } |
| 7243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7244 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7245 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7246 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7247 | 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] | 7248 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7249 | |
| 7250 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7251 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7252 | { |
| 7253 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7254 | register const Py_UNICODE *e; |
| 7255 | int cased; |
| 7256 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7257 | /* Shortcut for single character strings */ |
| 7258 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7259 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7260 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7261 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7262 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7263 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7264 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7265 | e = p + PyUnicode_GET_SIZE(self); |
| 7266 | cased = 0; |
| 7267 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7268 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7269 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7270 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7271 | return PyBool_FromLong(0); |
| 7272 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7273 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7274 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7275 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7276 | } |
| 7277 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7278 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7279 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7280 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7281 | Return True if S is a titlecased string and there is at least one\n\ |
| 7282 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7283 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7284 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7285 | |
| 7286 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7287 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7288 | { |
| 7289 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7290 | register const Py_UNICODE *e; |
| 7291 | int cased, previous_is_cased; |
| 7292 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7293 | /* Shortcut for single character strings */ |
| 7294 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7295 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7296 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7297 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7298 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7299 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7300 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7301 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7302 | e = p + PyUnicode_GET_SIZE(self); |
| 7303 | cased = 0; |
| 7304 | previous_is_cased = 0; |
| 7305 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7306 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7307 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7308 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7309 | if (previous_is_cased) |
| 7310 | return PyBool_FromLong(0); |
| 7311 | previous_is_cased = 1; |
| 7312 | cased = 1; |
| 7313 | } |
| 7314 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7315 | if (!previous_is_cased) |
| 7316 | return PyBool_FromLong(0); |
| 7317 | previous_is_cased = 1; |
| 7318 | cased = 1; |
| 7319 | } |
| 7320 | else |
| 7321 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7322 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7323 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7324 | } |
| 7325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7326 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7327 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7328 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7329 | Return True if all characters in S are whitespace\n\ |
| 7330 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7331 | |
| 7332 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7333 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7334 | { |
| 7335 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7336 | register const Py_UNICODE *e; |
| 7337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7338 | /* Shortcut for single character strings */ |
| 7339 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7340 | Py_UNICODE_ISSPACE(*p)) |
| 7341 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7342 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7343 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7344 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7345 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7347 | e = p + PyUnicode_GET_SIZE(self); |
| 7348 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7349 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7350 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7351 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7352 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7353 | } |
| 7354 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7355 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7356 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7357 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7358 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7359 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7360 | |
| 7361 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7362 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7363 | { |
| 7364 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7365 | register const Py_UNICODE *e; |
| 7366 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7367 | /* Shortcut for single character strings */ |
| 7368 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7369 | Py_UNICODE_ISALPHA(*p)) |
| 7370 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7371 | |
| 7372 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7373 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7374 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7375 | |
| 7376 | e = p + PyUnicode_GET_SIZE(self); |
| 7377 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7378 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7379 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7380 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7381 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7382 | } |
| 7383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7384 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7385 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7386 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7387 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7388 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7389 | |
| 7390 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7391 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7392 | { |
| 7393 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7394 | register const Py_UNICODE *e; |
| 7395 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7396 | /* Shortcut for single character strings */ |
| 7397 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7398 | Py_UNICODE_ISALNUM(*p)) |
| 7399 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7400 | |
| 7401 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7402 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7403 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7404 | |
| 7405 | e = p + PyUnicode_GET_SIZE(self); |
| 7406 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7407 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7408 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7409 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7410 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7411 | } |
| 7412 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7413 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7414 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7415 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7416 | 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] | 7417 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7418 | |
| 7419 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7420 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7421 | { |
| 7422 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7423 | register const Py_UNICODE *e; |
| 7424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | /* Shortcut for single character strings */ |
| 7426 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7427 | Py_UNICODE_ISDECIMAL(*p)) |
| 7428 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7429 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7430 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7431 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7432 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7433 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7434 | e = p + PyUnicode_GET_SIZE(self); |
| 7435 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7436 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7437 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7438 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7439 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | } |
| 7441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7442 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7443 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7444 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7445 | Return True if all characters in S are digits\n\ |
| 7446 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7447 | |
| 7448 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7449 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7450 | { |
| 7451 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7452 | register const Py_UNICODE *e; |
| 7453 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7454 | /* Shortcut for single character strings */ |
| 7455 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7456 | Py_UNICODE_ISDIGIT(*p)) |
| 7457 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7458 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7459 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7460 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7461 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7463 | e = p + PyUnicode_GET_SIZE(self); |
| 7464 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7465 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7466 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7468 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7469 | } |
| 7470 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7471 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7472 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7473 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7474 | 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] | 7475 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | |
| 7477 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7478 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | { |
| 7480 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7481 | register const Py_UNICODE *e; |
| 7482 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | /* Shortcut for single character strings */ |
| 7484 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7485 | Py_UNICODE_ISNUMERIC(*p)) |
| 7486 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7487 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7488 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7489 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7490 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7492 | e = p + PyUnicode_GET_SIZE(self); |
| 7493 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7494 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7495 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7497 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7498 | } |
| 7499 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7500 | int |
| 7501 | PyUnicode_IsIdentifier(PyObject *self) |
| 7502 | { |
| 7503 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7504 | register const Py_UNICODE *e; |
| 7505 | |
| 7506 | /* Special case for empty strings */ |
| 7507 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7508 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7509 | |
| 7510 | /* PEP 3131 says that the first character must be in |
| 7511 | XID_Start and subsequent characters in XID_Continue, |
| 7512 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7513 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7514 | letters, digits, underscore). However, given the current |
| 7515 | definition of XID_Start and XID_Continue, it is sufficient |
| 7516 | to check just for these, except that _ must be allowed |
| 7517 | as starting an identifier. */ |
| 7518 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7519 | return 0; |
| 7520 | |
| 7521 | e = p + PyUnicode_GET_SIZE(self); |
| 7522 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7523 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7524 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7525 | } |
| 7526 | return 1; |
| 7527 | } |
| 7528 | |
| 7529 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7531 | \n\ |
| 7532 | Return True if S is a valid identifier according\n\ |
| 7533 | to the language definition."); |
| 7534 | |
| 7535 | static PyObject* |
| 7536 | unicode_isidentifier(PyObject *self) |
| 7537 | { |
| 7538 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7539 | } |
| 7540 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7541 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7542 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7543 | \n\ |
| 7544 | Return True if all characters in S are considered\n\ |
| 7545 | printable in repr() or S is empty, False otherwise."); |
| 7546 | |
| 7547 | static PyObject* |
| 7548 | unicode_isprintable(PyObject *self) |
| 7549 | { |
| 7550 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7551 | register const Py_UNICODE *e; |
| 7552 | |
| 7553 | /* Shortcut for single character strings */ |
| 7554 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7555 | Py_RETURN_TRUE; |
| 7556 | } |
| 7557 | |
| 7558 | e = p + PyUnicode_GET_SIZE(self); |
| 7559 | for (; p < e; p++) { |
| 7560 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7561 | Py_RETURN_FALSE; |
| 7562 | } |
| 7563 | } |
| 7564 | Py_RETURN_TRUE; |
| 7565 | } |
| 7566 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7567 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7568 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7569 | \n\ |
| 7570 | 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] | 7571 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7572 | |
| 7573 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7574 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7575 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7576 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7577 | } |
| 7578 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7579 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7580 | unicode_length(PyUnicodeObject *self) |
| 7581 | { |
| 7582 | return self->length; |
| 7583 | } |
| 7584 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7585 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7586 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7587 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7588 | 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] | 7589 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7590 | |
| 7591 | static PyObject * |
| 7592 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7593 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7594 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7595 | Py_UNICODE fillchar = ' '; |
| 7596 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7597 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7598 | return NULL; |
| 7599 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7600 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7601 | Py_INCREF(self); |
| 7602 | return (PyObject*) self; |
| 7603 | } |
| 7604 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7605 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7606 | } |
| 7607 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7608 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7609 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7610 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7611 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7612 | |
| 7613 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7614 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7615 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7616 | return fixup(self, fixlower); |
| 7617 | } |
| 7618 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7619 | #define LEFTSTRIP 0 |
| 7620 | #define RIGHTSTRIP 1 |
| 7621 | #define BOTHSTRIP 2 |
| 7622 | |
| 7623 | /* Arrays indexed by above */ |
| 7624 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7625 | |
| 7626 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7627 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7628 | /* externally visible for str.strip(unicode) */ |
| 7629 | PyObject * |
| 7630 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7631 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7632 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7633 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7634 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7635 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7636 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7637 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7638 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7639 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7640 | i = 0; |
| 7641 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7642 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7643 | i++; |
| 7644 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7645 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7646 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7647 | j = len; |
| 7648 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7649 | do { |
| 7650 | j--; |
| 7651 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7652 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7653 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7654 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7655 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7656 | Py_INCREF(self); |
| 7657 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7658 | } |
| 7659 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7660 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7661 | } |
| 7662 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7663 | |
| 7664 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7665 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7666 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7667 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7668 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7669 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7670 | i = 0; |
| 7671 | if (striptype != RIGHTSTRIP) { |
| 7672 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7673 | i++; |
| 7674 | } |
| 7675 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7676 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7677 | j = len; |
| 7678 | if (striptype != LEFTSTRIP) { |
| 7679 | do { |
| 7680 | j--; |
| 7681 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7682 | j++; |
| 7683 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7684 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7685 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7686 | Py_INCREF(self); |
| 7687 | return (PyObject*)self; |
| 7688 | } |
| 7689 | else |
| 7690 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7691 | } |
| 7692 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7693 | |
| 7694 | static PyObject * |
| 7695 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7696 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7697 | PyObject *sep = NULL; |
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 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7700 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7701 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7702 | if (sep != NULL && sep != Py_None) { |
| 7703 | if (PyUnicode_Check(sep)) |
| 7704 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7705 | else { |
| 7706 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7707 | "%s arg must be None or str", |
| 7708 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7709 | return NULL; |
| 7710 | } |
| 7711 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7712 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7713 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7714 | } |
| 7715 | |
| 7716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7717 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7719 | \n\ |
| 7720 | Return a copy of the string S with leading and trailing\n\ |
| 7721 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7722 | 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] | 7723 | |
| 7724 | static PyObject * |
| 7725 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7726 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7727 | if (PyTuple_GET_SIZE(args) == 0) |
| 7728 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7729 | else |
| 7730 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7731 | } |
| 7732 | |
| 7733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7734 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7735 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7736 | \n\ |
| 7737 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7738 | 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] | 7739 | |
| 7740 | static PyObject * |
| 7741 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7742 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7743 | if (PyTuple_GET_SIZE(args) == 0) |
| 7744 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7745 | else |
| 7746 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7747 | } |
| 7748 | |
| 7749 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7750 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7751 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7752 | \n\ |
| 7753 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7754 | 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] | 7755 | |
| 7756 | static PyObject * |
| 7757 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7758 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7759 | if (PyTuple_GET_SIZE(args) == 0) |
| 7760 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7761 | else |
| 7762 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7763 | } |
| 7764 | |
| 7765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7766 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7767 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7768 | { |
| 7769 | PyUnicodeObject *u; |
| 7770 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7771 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7772 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7774 | if (len < 1) { |
| 7775 | Py_INCREF(unicode_empty); |
| 7776 | return (PyObject *)unicode_empty; |
| 7777 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7779 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 | /* no repeat, return original string */ |
| 7781 | Py_INCREF(str); |
| 7782 | return (PyObject*) str; |
| 7783 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7784 | |
| 7785 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7786 | * needed doesn't overflow size_t |
| 7787 | */ |
| 7788 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7789 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7790 | PyErr_SetString(PyExc_OverflowError, |
| 7791 | "repeated string is too long"); |
| 7792 | return NULL; |
| 7793 | } |
| 7794 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7795 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7796 | PyErr_SetString(PyExc_OverflowError, |
| 7797 | "repeated string is too long"); |
| 7798 | return NULL; |
| 7799 | } |
| 7800 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7801 | if (!u) |
| 7802 | return NULL; |
| 7803 | |
| 7804 | p = u->str; |
| 7805 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7806 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7807 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7808 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7809 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7810 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7811 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7812 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7813 | Py_UNICODE_COPY(p+done, p, n); |
| 7814 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7815 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7816 | } |
| 7817 | |
| 7818 | return (PyObject*) u; |
| 7819 | } |
| 7820 | |
| 7821 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7822 | PyObject *subobj, |
| 7823 | PyObject *replobj, |
| 7824 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7825 | { |
| 7826 | PyObject *self; |
| 7827 | PyObject *str1; |
| 7828 | PyObject *str2; |
| 7829 | PyObject *result; |
| 7830 | |
| 7831 | self = PyUnicode_FromObject(obj); |
| 7832 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7833 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 | str1 = PyUnicode_FromObject(subobj); |
| 7835 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7836 | Py_DECREF(self); |
| 7837 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7838 | } |
| 7839 | str2 = PyUnicode_FromObject(replobj); |
| 7840 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | Py_DECREF(self); |
| 7842 | Py_DECREF(str1); |
| 7843 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7845 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7846 | (PyUnicodeObject *)str1, |
| 7847 | (PyUnicodeObject *)str2, |
| 7848 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | Py_DECREF(self); |
| 7850 | Py_DECREF(str1); |
| 7851 | Py_DECREF(str2); |
| 7852 | return result; |
| 7853 | } |
| 7854 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7855 | PyDoc_STRVAR(replace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7856 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7857 | \n\ |
| 7858 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7859 | old replaced by new. If the optional argument count is\n\ |
| 7860 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | |
| 7862 | static PyObject* |
| 7863 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7864 | { |
| 7865 | PyUnicodeObject *str1; |
| 7866 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7867 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7868 | PyObject *result; |
| 7869 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7870 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7871 | return NULL; |
| 7872 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7873 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7874 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7875 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7876 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7877 | Py_DECREF(str1); |
| 7878 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7879 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7880 | |
| 7881 | result = replace(self, str1, str2, maxcount); |
| 7882 | |
| 7883 | Py_DECREF(str1); |
| 7884 | Py_DECREF(str2); |
| 7885 | return result; |
| 7886 | } |
| 7887 | |
| 7888 | static |
| 7889 | PyObject *unicode_repr(PyObject *unicode) |
| 7890 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7891 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7892 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7893 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7894 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7895 | |
| 7896 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7897 | better to choose a different scheme. Perhaps scan the |
| 7898 | first N-chars of the string and allocate based on that size. |
| 7899 | */ |
| 7900 | /* Initial allocation is based on the longest-possible unichr |
| 7901 | escape. |
| 7902 | |
| 7903 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7904 | unichr, so in this case it's the longest unichr escape. In |
| 7905 | narrow (UTF-16) builds this is five chars per source unichr |
| 7906 | since there are two unichrs in the surrogate pair, so in narrow |
| 7907 | (UTF-16) builds it's not the longest unichr escape. |
| 7908 | |
| 7909 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7910 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7911 | escape. |
| 7912 | */ |
| 7913 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7914 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7915 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7916 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7917 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7918 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7919 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7920 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7921 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7922 | if (repr == NULL) |
| 7923 | return NULL; |
| 7924 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7925 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7926 | |
| 7927 | /* Add quote */ |
| 7928 | *p++ = (findchar(s, size, '\'') && |
| 7929 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7930 | while (size-- > 0) { |
| 7931 | Py_UNICODE ch = *s++; |
| 7932 | |
| 7933 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7934 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7935 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7936 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7937 | continue; |
| 7938 | } |
| 7939 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7940 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7941 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7942 | *p++ = '\\'; |
| 7943 | *p++ = 't'; |
| 7944 | } |
| 7945 | else if (ch == '\n') { |
| 7946 | *p++ = '\\'; |
| 7947 | *p++ = 'n'; |
| 7948 | } |
| 7949 | else if (ch == '\r') { |
| 7950 | *p++ = '\\'; |
| 7951 | *p++ = 'r'; |
| 7952 | } |
| 7953 | |
| 7954 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7955 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7956 | *p++ = '\\'; |
| 7957 | *p++ = 'x'; |
| 7958 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7959 | *p++ = hexdigits[ch & 0x000F]; |
| 7960 | } |
| 7961 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7962 | /* Copy ASCII characters as-is */ |
| 7963 | else if (ch < 0x7F) { |
| 7964 | *p++ = ch; |
| 7965 | } |
| 7966 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7967 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7968 | else { |
| 7969 | Py_UCS4 ucs = ch; |
| 7970 | |
| 7971 | #ifndef Py_UNICODE_WIDE |
| 7972 | Py_UNICODE ch2 = 0; |
| 7973 | /* Get code point from surrogate pair */ |
| 7974 | if (size > 0) { |
| 7975 | ch2 = *s; |
| 7976 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7977 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7978 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7979 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7980 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7981 | size--; |
| 7982 | } |
| 7983 | } |
| 7984 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7985 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7986 | (categories Z* and C* except ASCII space) |
| 7987 | */ |
| 7988 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 7989 | /* Map 8-bit characters to '\xhh' */ |
| 7990 | if (ucs <= 0xff) { |
| 7991 | *p++ = '\\'; |
| 7992 | *p++ = 'x'; |
| 7993 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7994 | *p++ = hexdigits[ch & 0x000F]; |
| 7995 | } |
| 7996 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7997 | else if (ucs >= 0x10000) { |
| 7998 | *p++ = '\\'; |
| 7999 | *p++ = 'U'; |
| 8000 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8001 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8002 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8003 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8004 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8005 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8006 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8007 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8008 | } |
| 8009 | /* Map 16-bit characters to '\uxxxx' */ |
| 8010 | else { |
| 8011 | *p++ = '\\'; |
| 8012 | *p++ = 'u'; |
| 8013 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8014 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8015 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8016 | *p++ = hexdigits[ucs & 0x000F]; |
| 8017 | } |
| 8018 | } |
| 8019 | /* Copy characters as-is */ |
| 8020 | else { |
| 8021 | *p++ = ch; |
| 8022 | #ifndef Py_UNICODE_WIDE |
| 8023 | if (ucs >= 0x10000) |
| 8024 | *p++ = ch2; |
| 8025 | #endif |
| 8026 | } |
| 8027 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8028 | } |
| 8029 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8030 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8031 | |
| 8032 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8033 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8034 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | } |
| 8036 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8037 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8038 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8039 | \n\ |
| 8040 | 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] | 8041 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8042 | arguments start and end are interpreted as in slice notation.\n\ |
| 8043 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8044 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8045 | |
| 8046 | static PyObject * |
| 8047 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8048 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8049 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8050 | Py_ssize_t start; |
| 8051 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8052 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8054 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8055 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8056 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8057 | result = stringlib_rfind_slice( |
| 8058 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8059 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8060 | start, end |
| 8061 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8062 | |
| 8063 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8064 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8065 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8066 | } |
| 8067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8068 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8069 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8070 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8071 | 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] | 8072 | |
| 8073 | static PyObject * |
| 8074 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8075 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8076 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8077 | Py_ssize_t start; |
| 8078 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8079 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8080 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8081 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8082 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8083 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8084 | result = stringlib_rfind_slice( |
| 8085 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8086 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8087 | start, end |
| 8088 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | |
| 8090 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8091 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8092 | if (result < 0) { |
| 8093 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8094 | return NULL; |
| 8095 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8096 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8097 | } |
| 8098 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8099 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8100 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8101 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8102 | 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] | 8103 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8104 | |
| 8105 | static PyObject * |
| 8106 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8107 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8108 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8109 | Py_UNICODE fillchar = ' '; |
| 8110 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8111 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8112 | return NULL; |
| 8113 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8114 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8115 | Py_INCREF(self); |
| 8116 | return (PyObject*) self; |
| 8117 | } |
| 8118 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8119 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8120 | } |
| 8121 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8122 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8123 | PyObject *sep, |
| 8124 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8125 | { |
| 8126 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8128 | s = PyUnicode_FromObject(s); |
| 8129 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8130 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8131 | if (sep != NULL) { |
| 8132 | sep = PyUnicode_FromObject(sep); |
| 8133 | if (sep == NULL) { |
| 8134 | Py_DECREF(s); |
| 8135 | return NULL; |
| 8136 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
| 8139 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8140 | |
| 8141 | Py_DECREF(s); |
| 8142 | Py_XDECREF(sep); |
| 8143 | return result; |
| 8144 | } |
| 8145 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8146 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8147 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8148 | \n\ |
| 8149 | Return a list of the words in S, using sep as the\n\ |
| 8150 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8151 | 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] | 8152 | whitespace string is a separator and empty strings are\n\ |
| 8153 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8154 | |
| 8155 | static PyObject* |
| 8156 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8157 | { |
| 8158 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8159 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8160 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8161 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8162 | return NULL; |
| 8163 | |
| 8164 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8165 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8166 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8167 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8168 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8169 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8170 | } |
| 8171 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8172 | PyObject * |
| 8173 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8174 | { |
| 8175 | PyObject* str_obj; |
| 8176 | PyObject* sep_obj; |
| 8177 | PyObject* out; |
| 8178 | |
| 8179 | str_obj = PyUnicode_FromObject(str_in); |
| 8180 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8181 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8182 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8183 | if (!sep_obj) { |
| 8184 | Py_DECREF(str_obj); |
| 8185 | return NULL; |
| 8186 | } |
| 8187 | |
| 8188 | out = stringlib_partition( |
| 8189 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8190 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8191 | ); |
| 8192 | |
| 8193 | Py_DECREF(sep_obj); |
| 8194 | Py_DECREF(str_obj); |
| 8195 | |
| 8196 | return out; |
| 8197 | } |
| 8198 | |
| 8199 | |
| 8200 | PyObject * |
| 8201 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8202 | { |
| 8203 | PyObject* str_obj; |
| 8204 | PyObject* sep_obj; |
| 8205 | PyObject* out; |
| 8206 | |
| 8207 | str_obj = PyUnicode_FromObject(str_in); |
| 8208 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8209 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8210 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8211 | if (!sep_obj) { |
| 8212 | Py_DECREF(str_obj); |
| 8213 | return NULL; |
| 8214 | } |
| 8215 | |
| 8216 | out = stringlib_rpartition( |
| 8217 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8218 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8219 | ); |
| 8220 | |
| 8221 | Py_DECREF(sep_obj); |
| 8222 | Py_DECREF(str_obj); |
| 8223 | |
| 8224 | return out; |
| 8225 | } |
| 8226 | |
| 8227 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8228 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8229 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8230 | 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] | 8231 | 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] | 8232 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8233 | |
| 8234 | static PyObject* |
| 8235 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8236 | { |
| 8237 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8238 | } |
| 8239 | |
| 8240 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8241 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8242 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8243 | 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] | 8244 | 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] | 8245 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8246 | |
| 8247 | static PyObject* |
| 8248 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8249 | { |
| 8250 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8251 | } |
| 8252 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8253 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8254 | PyObject *sep, |
| 8255 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8256 | { |
| 8257 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8258 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8259 | s = PyUnicode_FromObject(s); |
| 8260 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8261 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8262 | if (sep != NULL) { |
| 8263 | sep = PyUnicode_FromObject(sep); |
| 8264 | if (sep == NULL) { |
| 8265 | Py_DECREF(s); |
| 8266 | return NULL; |
| 8267 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8268 | } |
| 8269 | |
| 8270 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8271 | |
| 8272 | Py_DECREF(s); |
| 8273 | Py_XDECREF(sep); |
| 8274 | return result; |
| 8275 | } |
| 8276 | |
| 8277 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8279 | \n\ |
| 8280 | Return a list of the words in S, using sep as the\n\ |
| 8281 | delimiter string, starting at the end of the string and\n\ |
| 8282 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8283 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8284 | is a separator."); |
| 8285 | |
| 8286 | static PyObject* |
| 8287 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8288 | { |
| 8289 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8290 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8291 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8292 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8293 | return NULL; |
| 8294 | |
| 8295 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8296 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8297 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8298 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8299 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8300 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8301 | } |
| 8302 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8303 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8304 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8305 | \n\ |
| 8306 | 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] | 8307 | 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] | 8308 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8309 | |
| 8310 | static PyObject* |
| 8311 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8312 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8313 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8314 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8315 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8316 | return NULL; |
| 8317 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8318 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8319 | } |
| 8320 | |
| 8321 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8322 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8323 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8324 | if (PyUnicode_CheckExact(self)) { |
| 8325 | Py_INCREF(self); |
| 8326 | return self; |
| 8327 | } else |
| 8328 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8329 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8330 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | } |
| 8332 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8333 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8334 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8335 | \n\ |
| 8336 | 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] | 8337 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8338 | |
| 8339 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8340 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8341 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8342 | return fixup(self, fixswapcase); |
| 8343 | } |
| 8344 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8345 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8346 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8347 | \n\ |
| 8348 | Return a translation table usable for str.translate().\n\ |
| 8349 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8350 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8351 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8352 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8353 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8354 | character at the same position in y. If there is a third argument, it\n\ |
| 8355 | must be a string, whose characters will be mapped to None in the result."); |
| 8356 | |
| 8357 | static PyObject* |
| 8358 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8359 | { |
| 8360 | PyObject *x, *y = NULL, *z = NULL; |
| 8361 | PyObject *new = NULL, *key, *value; |
| 8362 | Py_ssize_t i = 0; |
| 8363 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8364 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8365 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8366 | return NULL; |
| 8367 | new = PyDict_New(); |
| 8368 | if (!new) |
| 8369 | return NULL; |
| 8370 | if (y != NULL) { |
| 8371 | /* x must be a string too, of equal length */ |
| 8372 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8373 | if (!PyUnicode_Check(x)) { |
| 8374 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8375 | "be a string if there is a second argument"); |
| 8376 | goto err; |
| 8377 | } |
| 8378 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8379 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8380 | "arguments must have equal length"); |
| 8381 | goto err; |
| 8382 | } |
| 8383 | /* create entries for translating chars in x to those in y */ |
| 8384 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8385 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8386 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8387 | if (!key || !value) |
| 8388 | goto err; |
| 8389 | res = PyDict_SetItem(new, key, value); |
| 8390 | Py_DECREF(key); |
| 8391 | Py_DECREF(value); |
| 8392 | if (res < 0) |
| 8393 | goto err; |
| 8394 | } |
| 8395 | /* create entries for deleting chars in z */ |
| 8396 | if (z != NULL) { |
| 8397 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8398 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8399 | if (!key) |
| 8400 | goto err; |
| 8401 | res = PyDict_SetItem(new, key, Py_None); |
| 8402 | Py_DECREF(key); |
| 8403 | if (res < 0) |
| 8404 | goto err; |
| 8405 | } |
| 8406 | } |
| 8407 | } else { |
| 8408 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8409 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8410 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8411 | "to maketrans it must be a dict"); |
| 8412 | goto err; |
| 8413 | } |
| 8414 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8415 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8416 | if (PyUnicode_Check(key)) { |
| 8417 | /* convert string keys to integer keys */ |
| 8418 | PyObject *newkey; |
| 8419 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8420 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8421 | "table must be of length 1"); |
| 8422 | goto err; |
| 8423 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8424 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8425 | if (!newkey) |
| 8426 | goto err; |
| 8427 | res = PyDict_SetItem(new, newkey, value); |
| 8428 | Py_DECREF(newkey); |
| 8429 | if (res < 0) |
| 8430 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8431 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8432 | /* just keep integer keys */ |
| 8433 | if (PyDict_SetItem(new, key, value) < 0) |
| 8434 | goto err; |
| 8435 | } else { |
| 8436 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8437 | "be strings or integers"); |
| 8438 | goto err; |
| 8439 | } |
| 8440 | } |
| 8441 | } |
| 8442 | return new; |
| 8443 | err: |
| 8444 | Py_DECREF(new); |
| 8445 | return NULL; |
| 8446 | } |
| 8447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8448 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8450 | \n\ |
| 8451 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8452 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8453 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8454 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8455 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8456 | |
| 8457 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8458 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8460 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8461 | } |
| 8462 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8463 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8464 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8465 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8466 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | |
| 8468 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8469 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8470 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8471 | return fixup(self, fixupper); |
| 8472 | } |
| 8473 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8474 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8475 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8476 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8477 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8478 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8479 | |
| 8480 | static PyObject * |
| 8481 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8482 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8483 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8484 | PyUnicodeObject *u; |
| 8485 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8486 | Py_ssize_t width; |
| 8487 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8488 | return NULL; |
| 8489 | |
| 8490 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8491 | if (PyUnicode_CheckExact(self)) { |
| 8492 | Py_INCREF(self); |
| 8493 | return (PyObject*) self; |
| 8494 | } |
| 8495 | else |
| 8496 | return PyUnicode_FromUnicode( |
| 8497 | PyUnicode_AS_UNICODE(self), |
| 8498 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8499 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8500 | } |
| 8501 | |
| 8502 | fill = width - self->length; |
| 8503 | |
| 8504 | u = pad(self, fill, 0, '0'); |
| 8505 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8506 | if (u == NULL) |
| 8507 | return NULL; |
| 8508 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8509 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8510 | /* move sign to beginning of string */ |
| 8511 | u->str[0] = u->str[fill]; |
| 8512 | u->str[fill] = '0'; |
| 8513 | } |
| 8514 | |
| 8515 | return (PyObject*) u; |
| 8516 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8517 | |
| 8518 | #if 0 |
| 8519 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8520 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8521 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8522 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8523 | } |
| 8524 | #endif |
| 8525 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8526 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8527 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8528 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8529 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8530 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8531 | With optional end, stop comparing S at that position.\n\ |
| 8532 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8533 | |
| 8534 | static PyObject * |
| 8535 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8536 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8537 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8538 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8539 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8540 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8541 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8542 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8544 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8545 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8546 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8547 | if (PyTuple_Check(subobj)) { |
| 8548 | Py_ssize_t i; |
| 8549 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8550 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8551 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8552 | if (substring == NULL) |
| 8553 | return NULL; |
| 8554 | result = tailmatch(self, substring, start, end, -1); |
| 8555 | Py_DECREF(substring); |
| 8556 | if (result) { |
| 8557 | Py_RETURN_TRUE; |
| 8558 | } |
| 8559 | } |
| 8560 | /* nothing matched */ |
| 8561 | Py_RETURN_FALSE; |
| 8562 | } |
| 8563 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8564 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8565 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8566 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8567 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8568 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8569 | } |
| 8570 | |
| 8571 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8572 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8573 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8574 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8575 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8576 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8577 | With optional end, stop comparing S at that position.\n\ |
| 8578 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8579 | |
| 8580 | static PyObject * |
| 8581 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8582 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8583 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8584 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8585 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8586 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8587 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8588 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8589 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8590 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8591 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8592 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8593 | if (PyTuple_Check(subobj)) { |
| 8594 | Py_ssize_t i; |
| 8595 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8596 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8597 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8598 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8599 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8600 | result = tailmatch(self, substring, start, end, +1); |
| 8601 | Py_DECREF(substring); |
| 8602 | if (result) { |
| 8603 | Py_RETURN_TRUE; |
| 8604 | } |
| 8605 | } |
| 8606 | Py_RETURN_FALSE; |
| 8607 | } |
| 8608 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8609 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | return NULL; |
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 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8613 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8614 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8615 | } |
| 8616 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8617 | #include "stringlib/string_format.h" |
| 8618 | |
| 8619 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8620 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8621 | \n\ |
| 8622 | "); |
| 8623 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8624 | static PyObject * |
| 8625 | unicode__format__(PyObject* self, PyObject* args) |
| 8626 | { |
| 8627 | PyObject *format_spec; |
| 8628 | |
| 8629 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8630 | return NULL; |
| 8631 | |
| 8632 | return _PyUnicode_FormatAdvanced(self, |
| 8633 | PyUnicode_AS_UNICODE(format_spec), |
| 8634 | PyUnicode_GET_SIZE(format_spec)); |
| 8635 | } |
| 8636 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8637 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8638 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8639 | \n\ |
| 8640 | "); |
| 8641 | |
| 8642 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8643 | unicode__sizeof__(PyUnicodeObject *v) |
| 8644 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8645 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8646 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8647 | } |
| 8648 | |
| 8649 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8650 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8651 | |
| 8652 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8653 | unicode_getnewargs(PyUnicodeObject *v) |
| 8654 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8655 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8656 | } |
| 8657 | |
| 8658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8659 | static PyMethodDef unicode_methods[] = { |
| 8660 | |
| 8661 | /* Order is according to common usage: often used methods should |
| 8662 | appear first, since lookup is done sequentially. */ |
| 8663 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8664 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8665 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8666 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8667 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8668 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8669 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8670 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8671 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8672 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8673 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8674 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8675 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8676 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8677 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8678 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8679 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8680 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8681 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8682 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8683 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8684 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8685 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8686 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8687 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8688 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8689 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8690 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8691 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8692 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8693 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8694 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8695 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8696 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8697 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8698 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8699 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8700 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8701 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8702 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8703 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8704 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8705 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8706 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8707 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8708 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8709 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8710 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8711 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8712 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8713 | #endif |
| 8714 | |
| 8715 | #if 0 |
| 8716 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8717 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8718 | #endif |
| 8719 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8720 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8721 | {NULL, NULL} |
| 8722 | }; |
| 8723 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8724 | static PyObject * |
| 8725 | unicode_mod(PyObject *v, PyObject *w) |
| 8726 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8727 | if (!PyUnicode_Check(v)) { |
| 8728 | Py_INCREF(Py_NotImplemented); |
| 8729 | return Py_NotImplemented; |
| 8730 | } |
| 8731 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8732 | } |
| 8733 | |
| 8734 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8735 | 0, /*nb_add*/ |
| 8736 | 0, /*nb_subtract*/ |
| 8737 | 0, /*nb_multiply*/ |
| 8738 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8739 | }; |
| 8740 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8741 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8742 | (lenfunc) unicode_length, /* sq_length */ |
| 8743 | PyUnicode_Concat, /* sq_concat */ |
| 8744 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8745 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8746 | 0, /* sq_slice */ |
| 8747 | 0, /* sq_ass_item */ |
| 8748 | 0, /* sq_ass_slice */ |
| 8749 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8750 | }; |
| 8751 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8752 | static PyObject* |
| 8753 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8754 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8755 | if (PyIndex_Check(item)) { |
| 8756 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8757 | if (i == -1 && PyErr_Occurred()) |
| 8758 | return NULL; |
| 8759 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8760 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8761 | return unicode_getitem(self, i); |
| 8762 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8763 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8764 | Py_UNICODE* source_buf; |
| 8765 | Py_UNICODE* result_buf; |
| 8766 | PyObject* result; |
| 8767 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8768 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8769 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8770 | return NULL; |
| 8771 | } |
| 8772 | |
| 8773 | if (slicelength <= 0) { |
| 8774 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8775 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8776 | PyUnicode_CheckExact(self)) { |
| 8777 | Py_INCREF(self); |
| 8778 | return (PyObject *)self; |
| 8779 | } else if (step == 1) { |
| 8780 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8781 | } else { |
| 8782 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8783 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8784 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8785 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8786 | if (result_buf == NULL) |
| 8787 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8788 | |
| 8789 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8790 | result_buf[i] = source_buf[cur]; |
| 8791 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8792 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8793 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8794 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8795 | return result; |
| 8796 | } |
| 8797 | } else { |
| 8798 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8799 | return NULL; |
| 8800 | } |
| 8801 | } |
| 8802 | |
| 8803 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8804 | (lenfunc)unicode_length, /* mp_length */ |
| 8805 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8806 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8807 | }; |
| 8808 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8809 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8810 | /* Helpers for PyUnicode_Format() */ |
| 8811 | |
| 8812 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8813 | 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] | 8814 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8815 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8816 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8817 | (*p_argidx)++; |
| 8818 | if (arglen < 0) |
| 8819 | return args; |
| 8820 | else |
| 8821 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8822 | } |
| 8823 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8824 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8825 | return NULL; |
| 8826 | } |
| 8827 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8828 | /* 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] | 8829 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8830 | static PyObject * |
| 8831 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8832 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8833 | char *p; |
| 8834 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8835 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8836 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8837 | x = PyFloat_AsDouble(v); |
| 8838 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8839 | return NULL; |
| 8840 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8841 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8842 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8843 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8844 | p = PyOS_double_to_string(x, type, prec, |
| 8845 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8846 | if (p == NULL) |
| 8847 | return NULL; |
| 8848 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8849 | PyMem_Free(p); |
| 8850 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8851 | } |
| 8852 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8853 | static PyObject* |
| 8854 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8855 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8856 | char *buf; |
| 8857 | int len; |
| 8858 | PyObject *str; /* temporary string object. */ |
| 8859 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8860 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8861 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 8862 | if (!str) |
| 8863 | return NULL; |
| 8864 | result = PyUnicode_FromStringAndSize(buf, len); |
| 8865 | Py_DECREF(str); |
| 8866 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8867 | } |
| 8868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8869 | static int |
| 8870 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8871 | size_t buflen, |
| 8872 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8873 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8874 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8875 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8876 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 8877 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 8878 | buf[1] = '\0'; |
| 8879 | return 1; |
| 8880 | } |
| 8881 | #ifndef Py_UNICODE_WIDE |
| 8882 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 8883 | /* Decode a valid surrogate pair */ |
| 8884 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 8885 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 8886 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 8887 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 8888 | buf[0] = c0; |
| 8889 | buf[1] = c1; |
| 8890 | buf[2] = '\0'; |
| 8891 | return 2; |
| 8892 | } |
| 8893 | } |
| 8894 | #endif |
| 8895 | goto onError; |
| 8896 | } |
| 8897 | else { |
| 8898 | /* Integer input truncated to a character */ |
| 8899 | long x; |
| 8900 | x = PyLong_AsLong(v); |
| 8901 | if (x == -1 && PyErr_Occurred()) |
| 8902 | goto onError; |
| 8903 | |
| 8904 | if (x < 0 || x > 0x10ffff) { |
| 8905 | PyErr_SetString(PyExc_OverflowError, |
| 8906 | "%c arg not in range(0x110000)"); |
| 8907 | return -1; |
| 8908 | } |
| 8909 | |
| 8910 | #ifndef Py_UNICODE_WIDE |
| 8911 | if (x > 0xffff) { |
| 8912 | x -= 0x10000; |
| 8913 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 8914 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 8915 | return 2; |
| 8916 | } |
| 8917 | #endif |
| 8918 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8919 | buf[1] = '\0'; |
| 8920 | return 1; |
| 8921 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8922 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8923 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8924 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8925 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8926 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8927 | } |
| 8928 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8929 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8930 | 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] | 8931 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8932 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8933 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8934 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8935 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8936 | { |
| 8937 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8938 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8939 | int args_owned = 0; |
| 8940 | PyUnicodeObject *result = NULL; |
| 8941 | PyObject *dict = NULL; |
| 8942 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8943 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8944 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8945 | PyErr_BadInternalCall(); |
| 8946 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8947 | } |
| 8948 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8949 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8950 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8951 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8952 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8953 | |
| 8954 | reslen = rescnt = fmtcnt + 100; |
| 8955 | result = _PyUnicode_New(reslen); |
| 8956 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8957 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8958 | res = PyUnicode_AS_UNICODE(result); |
| 8959 | |
| 8960 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8961 | arglen = PyTuple_Size(args); |
| 8962 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8963 | } |
| 8964 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8965 | arglen = -1; |
| 8966 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8967 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8968 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 8969 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8970 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8971 | |
| 8972 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8973 | if (*fmt != '%') { |
| 8974 | if (--rescnt < 0) { |
| 8975 | rescnt = fmtcnt + 100; |
| 8976 | reslen += rescnt; |
| 8977 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 8978 | goto onError; |
| 8979 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8980 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8981 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8982 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8983 | } |
| 8984 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8985 | /* Got a format specifier */ |
| 8986 | int flags = 0; |
| 8987 | Py_ssize_t width = -1; |
| 8988 | int prec = -1; |
| 8989 | Py_UNICODE c = '\0'; |
| 8990 | Py_UNICODE fill; |
| 8991 | int isnumok; |
| 8992 | PyObject *v = NULL; |
| 8993 | PyObject *temp = NULL; |
| 8994 | Py_UNICODE *pbuf; |
| 8995 | Py_UNICODE sign; |
| 8996 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8997 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8998 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8999 | fmt++; |
| 9000 | if (*fmt == '(') { |
| 9001 | Py_UNICODE *keystart; |
| 9002 | Py_ssize_t keylen; |
| 9003 | PyObject *key; |
| 9004 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9005 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9006 | if (dict == NULL) { |
| 9007 | PyErr_SetString(PyExc_TypeError, |
| 9008 | "format requires a mapping"); |
| 9009 | goto onError; |
| 9010 | } |
| 9011 | ++fmt; |
| 9012 | --fmtcnt; |
| 9013 | keystart = fmt; |
| 9014 | /* Skip over balanced parentheses */ |
| 9015 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9016 | if (*fmt == ')') |
| 9017 | --pcount; |
| 9018 | else if (*fmt == '(') |
| 9019 | ++pcount; |
| 9020 | fmt++; |
| 9021 | } |
| 9022 | keylen = fmt - keystart - 1; |
| 9023 | if (fmtcnt < 0 || pcount > 0) { |
| 9024 | PyErr_SetString(PyExc_ValueError, |
| 9025 | "incomplete format key"); |
| 9026 | goto onError; |
| 9027 | } |
| 9028 | #if 0 |
| 9029 | /* keys are converted to strings using UTF-8 and |
| 9030 | then looked up since Python uses strings to hold |
| 9031 | variables names etc. in its namespaces and we |
| 9032 | wouldn't want to break common idioms. */ |
| 9033 | key = PyUnicode_EncodeUTF8(keystart, |
| 9034 | keylen, |
| 9035 | NULL); |
| 9036 | #else |
| 9037 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9038 | #endif |
| 9039 | if (key == NULL) |
| 9040 | goto onError; |
| 9041 | if (args_owned) { |
| 9042 | Py_DECREF(args); |
| 9043 | args_owned = 0; |
| 9044 | } |
| 9045 | args = PyObject_GetItem(dict, key); |
| 9046 | Py_DECREF(key); |
| 9047 | if (args == NULL) { |
| 9048 | goto onError; |
| 9049 | } |
| 9050 | args_owned = 1; |
| 9051 | arglen = -1; |
| 9052 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9053 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9054 | while (--fmtcnt >= 0) { |
| 9055 | switch (c = *fmt++) { |
| 9056 | case '-': flags |= F_LJUST; continue; |
| 9057 | case '+': flags |= F_SIGN; continue; |
| 9058 | case ' ': flags |= F_BLANK; continue; |
| 9059 | case '#': flags |= F_ALT; continue; |
| 9060 | case '0': flags |= F_ZERO; continue; |
| 9061 | } |
| 9062 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9063 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9064 | if (c == '*') { |
| 9065 | v = getnextarg(args, arglen, &argidx); |
| 9066 | if (v == NULL) |
| 9067 | goto onError; |
| 9068 | if (!PyLong_Check(v)) { |
| 9069 | PyErr_SetString(PyExc_TypeError, |
| 9070 | "* wants int"); |
| 9071 | goto onError; |
| 9072 | } |
| 9073 | width = PyLong_AsLong(v); |
| 9074 | if (width == -1 && PyErr_Occurred()) |
| 9075 | goto onError; |
| 9076 | if (width < 0) { |
| 9077 | flags |= F_LJUST; |
| 9078 | width = -width; |
| 9079 | } |
| 9080 | if (--fmtcnt >= 0) |
| 9081 | c = *fmt++; |
| 9082 | } |
| 9083 | else if (c >= '0' && c <= '9') { |
| 9084 | width = c - '0'; |
| 9085 | while (--fmtcnt >= 0) { |
| 9086 | c = *fmt++; |
| 9087 | if (c < '0' || c > '9') |
| 9088 | break; |
| 9089 | if ((width*10) / 10 != width) { |
| 9090 | PyErr_SetString(PyExc_ValueError, |
| 9091 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9092 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9093 | } |
| 9094 | width = width*10 + (c - '0'); |
| 9095 | } |
| 9096 | } |
| 9097 | if (c == '.') { |
| 9098 | prec = 0; |
| 9099 | if (--fmtcnt >= 0) |
| 9100 | c = *fmt++; |
| 9101 | if (c == '*') { |
| 9102 | v = getnextarg(args, arglen, &argidx); |
| 9103 | if (v == NULL) |
| 9104 | goto onError; |
| 9105 | if (!PyLong_Check(v)) { |
| 9106 | PyErr_SetString(PyExc_TypeError, |
| 9107 | "* wants int"); |
| 9108 | goto onError; |
| 9109 | } |
| 9110 | prec = PyLong_AsLong(v); |
| 9111 | if (prec == -1 && PyErr_Occurred()) |
| 9112 | goto onError; |
| 9113 | if (prec < 0) |
| 9114 | prec = 0; |
| 9115 | if (--fmtcnt >= 0) |
| 9116 | c = *fmt++; |
| 9117 | } |
| 9118 | else if (c >= '0' && c <= '9') { |
| 9119 | prec = c - '0'; |
| 9120 | while (--fmtcnt >= 0) { |
| 9121 | c = Py_CHARMASK(*fmt++); |
| 9122 | if (c < '0' || c > '9') |
| 9123 | break; |
| 9124 | if ((prec*10) / 10 != prec) { |
| 9125 | PyErr_SetString(PyExc_ValueError, |
| 9126 | "prec too big"); |
| 9127 | goto onError; |
| 9128 | } |
| 9129 | prec = prec*10 + (c - '0'); |
| 9130 | } |
| 9131 | } |
| 9132 | } /* prec */ |
| 9133 | if (fmtcnt >= 0) { |
| 9134 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9135 | if (--fmtcnt >= 0) |
| 9136 | c = *fmt++; |
| 9137 | } |
| 9138 | } |
| 9139 | if (fmtcnt < 0) { |
| 9140 | PyErr_SetString(PyExc_ValueError, |
| 9141 | "incomplete format"); |
| 9142 | goto onError; |
| 9143 | } |
| 9144 | if (c != '%') { |
| 9145 | v = getnextarg(args, arglen, &argidx); |
| 9146 | if (v == NULL) |
| 9147 | goto onError; |
| 9148 | } |
| 9149 | sign = 0; |
| 9150 | fill = ' '; |
| 9151 | switch (c) { |
| 9152 | |
| 9153 | case '%': |
| 9154 | pbuf = formatbuf; |
| 9155 | /* presume that buffer length is at least 1 */ |
| 9156 | pbuf[0] = '%'; |
| 9157 | len = 1; |
| 9158 | break; |
| 9159 | |
| 9160 | case 's': |
| 9161 | case 'r': |
| 9162 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9163 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9164 | temp = v; |
| 9165 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9166 | } |
| 9167 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9168 | if (c == 's') |
| 9169 | temp = PyObject_Str(v); |
| 9170 | else if (c == 'r') |
| 9171 | temp = PyObject_Repr(v); |
| 9172 | else |
| 9173 | temp = PyObject_ASCII(v); |
| 9174 | if (temp == NULL) |
| 9175 | goto onError; |
| 9176 | if (PyUnicode_Check(temp)) |
| 9177 | /* nothing to do */; |
| 9178 | else { |
| 9179 | Py_DECREF(temp); |
| 9180 | PyErr_SetString(PyExc_TypeError, |
| 9181 | "%s argument has non-string str()"); |
| 9182 | goto onError; |
| 9183 | } |
| 9184 | } |
| 9185 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9186 | len = PyUnicode_GET_SIZE(temp); |
| 9187 | if (prec >= 0 && len > prec) |
| 9188 | len = prec; |
| 9189 | break; |
| 9190 | |
| 9191 | case 'i': |
| 9192 | case 'd': |
| 9193 | case 'u': |
| 9194 | case 'o': |
| 9195 | case 'x': |
| 9196 | case 'X': |
| 9197 | if (c == 'i') |
| 9198 | c = 'd'; |
| 9199 | isnumok = 0; |
| 9200 | if (PyNumber_Check(v)) { |
| 9201 | PyObject *iobj=NULL; |
| 9202 | |
| 9203 | if (PyLong_Check(v)) { |
| 9204 | iobj = v; |
| 9205 | Py_INCREF(iobj); |
| 9206 | } |
| 9207 | else { |
| 9208 | iobj = PyNumber_Long(v); |
| 9209 | } |
| 9210 | if (iobj!=NULL) { |
| 9211 | if (PyLong_Check(iobj)) { |
| 9212 | isnumok = 1; |
| 9213 | temp = formatlong(iobj, flags, prec, c); |
| 9214 | Py_DECREF(iobj); |
| 9215 | if (!temp) |
| 9216 | goto onError; |
| 9217 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9218 | len = PyUnicode_GET_SIZE(temp); |
| 9219 | sign = 1; |
| 9220 | } |
| 9221 | else { |
| 9222 | Py_DECREF(iobj); |
| 9223 | } |
| 9224 | } |
| 9225 | } |
| 9226 | if (!isnumok) { |
| 9227 | PyErr_Format(PyExc_TypeError, |
| 9228 | "%%%c format: a number is required, " |
| 9229 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9230 | goto onError; |
| 9231 | } |
| 9232 | if (flags & F_ZERO) |
| 9233 | fill = '0'; |
| 9234 | break; |
| 9235 | |
| 9236 | case 'e': |
| 9237 | case 'E': |
| 9238 | case 'f': |
| 9239 | case 'F': |
| 9240 | case 'g': |
| 9241 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9242 | temp = formatfloat(v, flags, prec, c); |
| 9243 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9244 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9245 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9246 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9247 | sign = 1; |
| 9248 | if (flags & F_ZERO) |
| 9249 | fill = '0'; |
| 9250 | break; |
| 9251 | |
| 9252 | case 'c': |
| 9253 | pbuf = formatbuf; |
| 9254 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9255 | if (len < 0) |
| 9256 | goto onError; |
| 9257 | break; |
| 9258 | |
| 9259 | default: |
| 9260 | PyErr_Format(PyExc_ValueError, |
| 9261 | "unsupported format character '%c' (0x%x) " |
| 9262 | "at index %zd", |
| 9263 | (31<=c && c<=126) ? (char)c : '?', |
| 9264 | (int)c, |
| 9265 | (Py_ssize_t)(fmt - 1 - |
| 9266 | PyUnicode_AS_UNICODE(uformat))); |
| 9267 | goto onError; |
| 9268 | } |
| 9269 | if (sign) { |
| 9270 | if (*pbuf == '-' || *pbuf == '+') { |
| 9271 | sign = *pbuf++; |
| 9272 | len--; |
| 9273 | } |
| 9274 | else if (flags & F_SIGN) |
| 9275 | sign = '+'; |
| 9276 | else if (flags & F_BLANK) |
| 9277 | sign = ' '; |
| 9278 | else |
| 9279 | sign = 0; |
| 9280 | } |
| 9281 | if (width < len) |
| 9282 | width = len; |
| 9283 | if (rescnt - (sign != 0) < width) { |
| 9284 | reslen -= rescnt; |
| 9285 | rescnt = width + fmtcnt + 100; |
| 9286 | reslen += rescnt; |
| 9287 | if (reslen < 0) { |
| 9288 | Py_XDECREF(temp); |
| 9289 | PyErr_NoMemory(); |
| 9290 | goto onError; |
| 9291 | } |
| 9292 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9293 | Py_XDECREF(temp); |
| 9294 | goto onError; |
| 9295 | } |
| 9296 | res = PyUnicode_AS_UNICODE(result) |
| 9297 | + reslen - rescnt; |
| 9298 | } |
| 9299 | if (sign) { |
| 9300 | if (fill != ' ') |
| 9301 | *res++ = sign; |
| 9302 | rescnt--; |
| 9303 | if (width > len) |
| 9304 | width--; |
| 9305 | } |
| 9306 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9307 | assert(pbuf[0] == '0'); |
| 9308 | assert(pbuf[1] == c); |
| 9309 | if (fill != ' ') { |
| 9310 | *res++ = *pbuf++; |
| 9311 | *res++ = *pbuf++; |
| 9312 | } |
| 9313 | rescnt -= 2; |
| 9314 | width -= 2; |
| 9315 | if (width < 0) |
| 9316 | width = 0; |
| 9317 | len -= 2; |
| 9318 | } |
| 9319 | if (width > len && !(flags & F_LJUST)) { |
| 9320 | do { |
| 9321 | --rescnt; |
| 9322 | *res++ = fill; |
| 9323 | } while (--width > len); |
| 9324 | } |
| 9325 | if (fill == ' ') { |
| 9326 | if (sign) |
| 9327 | *res++ = sign; |
| 9328 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9329 | assert(pbuf[0] == '0'); |
| 9330 | assert(pbuf[1] == c); |
| 9331 | *res++ = *pbuf++; |
| 9332 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9333 | } |
| 9334 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9335 | Py_UNICODE_COPY(res, pbuf, len); |
| 9336 | res += len; |
| 9337 | rescnt -= len; |
| 9338 | while (--width >= len) { |
| 9339 | --rescnt; |
| 9340 | *res++ = ' '; |
| 9341 | } |
| 9342 | if (dict && (argidx < arglen) && c != '%') { |
| 9343 | PyErr_SetString(PyExc_TypeError, |
| 9344 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9345 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9346 | goto onError; |
| 9347 | } |
| 9348 | Py_XDECREF(temp); |
| 9349 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9350 | } /* until end */ |
| 9351 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9352 | PyErr_SetString(PyExc_TypeError, |
| 9353 | "not all arguments converted during string formatting"); |
| 9354 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9355 | } |
| 9356 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9357 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9358 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9359 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9360 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9361 | } |
| 9362 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9363 | return (PyObject *)result; |
| 9364 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9365 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9366 | Py_XDECREF(result); |
| 9367 | Py_DECREF(uformat); |
| 9368 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9369 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9370 | } |
| 9371 | return NULL; |
| 9372 | } |
| 9373 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9374 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9375 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9376 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9377 | static PyObject * |
| 9378 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9379 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9380 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9381 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9382 | char *encoding = NULL; |
| 9383 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9384 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9385 | if (type != &PyUnicode_Type) |
| 9386 | return unicode_subtype_new(type, args, kwds); |
| 9387 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9388 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9389 | return NULL; |
| 9390 | if (x == NULL) |
| 9391 | return (PyObject *)_PyUnicode_New(0); |
| 9392 | if (encoding == NULL && errors == NULL) |
| 9393 | return PyObject_Str(x); |
| 9394 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9395 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9396 | } |
| 9397 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9398 | static PyObject * |
| 9399 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9400 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9401 | PyUnicodeObject *tmp, *pnew; |
| 9402 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9403 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9404 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9405 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9406 | if (tmp == NULL) |
| 9407 | return NULL; |
| 9408 | assert(PyUnicode_Check(tmp)); |
| 9409 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9410 | if (pnew == NULL) { |
| 9411 | Py_DECREF(tmp); |
| 9412 | return NULL; |
| 9413 | } |
| 9414 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9415 | if (pnew->str == NULL) { |
| 9416 | _Py_ForgetReference((PyObject *)pnew); |
| 9417 | PyObject_Del(pnew); |
| 9418 | Py_DECREF(tmp); |
| 9419 | return PyErr_NoMemory(); |
| 9420 | } |
| 9421 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9422 | pnew->length = n; |
| 9423 | pnew->hash = tmp->hash; |
| 9424 | Py_DECREF(tmp); |
| 9425 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9426 | } |
| 9427 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9428 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9429 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9430 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9431 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9432 | encoding defaults to the current default string encoding.\n\ |
| 9433 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9434 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9435 | static PyObject *unicode_iter(PyObject *seq); |
| 9436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9437 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9438 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9439 | "str", /* tp_name */ |
| 9440 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9441 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9442 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9443 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9444 | 0, /* tp_print */ |
| 9445 | 0, /* tp_getattr */ |
| 9446 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9447 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9448 | unicode_repr, /* tp_repr */ |
| 9449 | &unicode_as_number, /* tp_as_number */ |
| 9450 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9451 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9452 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9453 | 0, /* tp_call*/ |
| 9454 | (reprfunc) unicode_str, /* tp_str */ |
| 9455 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9456 | 0, /* tp_setattro */ |
| 9457 | 0, /* tp_as_buffer */ |
| 9458 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9459 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9460 | unicode_doc, /* tp_doc */ |
| 9461 | 0, /* tp_traverse */ |
| 9462 | 0, /* tp_clear */ |
| 9463 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9464 | 0, /* tp_weaklistoffset */ |
| 9465 | unicode_iter, /* tp_iter */ |
| 9466 | 0, /* tp_iternext */ |
| 9467 | unicode_methods, /* tp_methods */ |
| 9468 | 0, /* tp_members */ |
| 9469 | 0, /* tp_getset */ |
| 9470 | &PyBaseObject_Type, /* tp_base */ |
| 9471 | 0, /* tp_dict */ |
| 9472 | 0, /* tp_descr_get */ |
| 9473 | 0, /* tp_descr_set */ |
| 9474 | 0, /* tp_dictoffset */ |
| 9475 | 0, /* tp_init */ |
| 9476 | 0, /* tp_alloc */ |
| 9477 | unicode_new, /* tp_new */ |
| 9478 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9479 | }; |
| 9480 | |
| 9481 | /* Initialize the Unicode implementation */ |
| 9482 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9483 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9484 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9485 | int i; |
| 9486 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9487 | /* XXX - move this array to unicodectype.c ? */ |
| 9488 | Py_UNICODE linebreak[] = { |
| 9489 | 0x000A, /* LINE FEED */ |
| 9490 | 0x000D, /* CARRIAGE RETURN */ |
| 9491 | 0x001C, /* FILE SEPARATOR */ |
| 9492 | 0x001D, /* GROUP SEPARATOR */ |
| 9493 | 0x001E, /* RECORD SEPARATOR */ |
| 9494 | 0x0085, /* NEXT LINE */ |
| 9495 | 0x2028, /* LINE SEPARATOR */ |
| 9496 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9497 | }; |
| 9498 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9499 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9500 | free_list = NULL; |
| 9501 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9502 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9503 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9504 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9505 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9506 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9507 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9508 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9509 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9510 | |
| 9511 | /* initialize the linebreak bloom filter */ |
| 9512 | bloom_linebreak = make_bloom_mask( |
| 9513 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9514 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9515 | |
| 9516 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9517 | } |
| 9518 | |
| 9519 | /* Finalize the Unicode implementation */ |
| 9520 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9521 | int |
| 9522 | PyUnicode_ClearFreeList(void) |
| 9523 | { |
| 9524 | int freelist_size = numfree; |
| 9525 | PyUnicodeObject *u; |
| 9526 | |
| 9527 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9528 | PyUnicodeObject *v = u; |
| 9529 | u = *(PyUnicodeObject **)u; |
| 9530 | if (v->str) |
| 9531 | PyObject_DEL(v->str); |
| 9532 | Py_XDECREF(v->defenc); |
| 9533 | PyObject_Del(v); |
| 9534 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9535 | } |
| 9536 | free_list = NULL; |
| 9537 | assert(numfree == 0); |
| 9538 | return freelist_size; |
| 9539 | } |
| 9540 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9541 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9542 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9543 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9544 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9545 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9546 | Py_XDECREF(unicode_empty); |
| 9547 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9548 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9549 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9550 | if (unicode_latin1[i]) { |
| 9551 | Py_DECREF(unicode_latin1[i]); |
| 9552 | unicode_latin1[i] = NULL; |
| 9553 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9554 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9555 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9556 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9557 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9558 | void |
| 9559 | PyUnicode_InternInPlace(PyObject **p) |
| 9560 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9561 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9562 | PyObject *t; |
| 9563 | if (s == NULL || !PyUnicode_Check(s)) |
| 9564 | Py_FatalError( |
| 9565 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9566 | /* If it's a subclass, we don't really know what putting |
| 9567 | it in the interned dict might do. */ |
| 9568 | if (!PyUnicode_CheckExact(s)) |
| 9569 | return; |
| 9570 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9571 | return; |
| 9572 | if (interned == NULL) { |
| 9573 | interned = PyDict_New(); |
| 9574 | if (interned == NULL) { |
| 9575 | PyErr_Clear(); /* Don't leave an exception */ |
| 9576 | return; |
| 9577 | } |
| 9578 | } |
| 9579 | /* It might be that the GetItem call fails even |
| 9580 | though the key is present in the dictionary, |
| 9581 | namely when this happens during a stack overflow. */ |
| 9582 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9583 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9584 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9585 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9586 | if (t) { |
| 9587 | Py_INCREF(t); |
| 9588 | Py_DECREF(*p); |
| 9589 | *p = t; |
| 9590 | return; |
| 9591 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9592 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9593 | PyThreadState_GET()->recursion_critical = 1; |
| 9594 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9595 | PyErr_Clear(); |
| 9596 | PyThreadState_GET()->recursion_critical = 0; |
| 9597 | return; |
| 9598 | } |
| 9599 | PyThreadState_GET()->recursion_critical = 0; |
| 9600 | /* The two references in interned are not counted by refcnt. |
| 9601 | The deallocator will take care of this */ |
| 9602 | Py_REFCNT(s) -= 2; |
| 9603 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9604 | } |
| 9605 | |
| 9606 | void |
| 9607 | PyUnicode_InternImmortal(PyObject **p) |
| 9608 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9609 | PyUnicode_InternInPlace(p); |
| 9610 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9611 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9612 | Py_INCREF(*p); |
| 9613 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9614 | } |
| 9615 | |
| 9616 | PyObject * |
| 9617 | PyUnicode_InternFromString(const char *cp) |
| 9618 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9619 | PyObject *s = PyUnicode_FromString(cp); |
| 9620 | if (s == NULL) |
| 9621 | return NULL; |
| 9622 | PyUnicode_InternInPlace(&s); |
| 9623 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9624 | } |
| 9625 | |
| 9626 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9627 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9628 | PyObject *keys; |
| 9629 | PyUnicodeObject *s; |
| 9630 | Py_ssize_t i, n; |
| 9631 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9632 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9633 | if (interned == NULL || !PyDict_Check(interned)) |
| 9634 | return; |
| 9635 | keys = PyDict_Keys(interned); |
| 9636 | if (keys == NULL || !PyList_Check(keys)) { |
| 9637 | PyErr_Clear(); |
| 9638 | return; |
| 9639 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9640 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9641 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9642 | detector, interned unicode strings are not forcibly deallocated; |
| 9643 | rather, we give them their stolen references back, and then clear |
| 9644 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9645 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9646 | n = PyList_GET_SIZE(keys); |
| 9647 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9648 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9649 | for (i = 0; i < n; i++) { |
| 9650 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9651 | switch (s->state) { |
| 9652 | case SSTATE_NOT_INTERNED: |
| 9653 | /* XXX Shouldn't happen */ |
| 9654 | break; |
| 9655 | case SSTATE_INTERNED_IMMORTAL: |
| 9656 | Py_REFCNT(s) += 1; |
| 9657 | immortal_size += s->length; |
| 9658 | break; |
| 9659 | case SSTATE_INTERNED_MORTAL: |
| 9660 | Py_REFCNT(s) += 2; |
| 9661 | mortal_size += s->length; |
| 9662 | break; |
| 9663 | default: |
| 9664 | Py_FatalError("Inconsistent interned string state."); |
| 9665 | } |
| 9666 | s->state = SSTATE_NOT_INTERNED; |
| 9667 | } |
| 9668 | fprintf(stderr, "total size of all interned strings: " |
| 9669 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9670 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9671 | Py_DECREF(keys); |
| 9672 | PyDict_Clear(interned); |
| 9673 | Py_DECREF(interned); |
| 9674 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9675 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9676 | |
| 9677 | |
| 9678 | /********************* Unicode Iterator **************************/ |
| 9679 | |
| 9680 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9681 | PyObject_HEAD |
| 9682 | Py_ssize_t it_index; |
| 9683 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9684 | } unicodeiterobject; |
| 9685 | |
| 9686 | static void |
| 9687 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9688 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9689 | _PyObject_GC_UNTRACK(it); |
| 9690 | Py_XDECREF(it->it_seq); |
| 9691 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9692 | } |
| 9693 | |
| 9694 | static int |
| 9695 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9696 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9697 | Py_VISIT(it->it_seq); |
| 9698 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9699 | } |
| 9700 | |
| 9701 | static PyObject * |
| 9702 | unicodeiter_next(unicodeiterobject *it) |
| 9703 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9704 | PyUnicodeObject *seq; |
| 9705 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9706 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9707 | assert(it != NULL); |
| 9708 | seq = it->it_seq; |
| 9709 | if (seq == NULL) |
| 9710 | return NULL; |
| 9711 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9712 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9713 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9714 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9715 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9716 | if (item != NULL) |
| 9717 | ++it->it_index; |
| 9718 | return item; |
| 9719 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9720 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9721 | Py_DECREF(seq); |
| 9722 | it->it_seq = NULL; |
| 9723 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9724 | } |
| 9725 | |
| 9726 | static PyObject * |
| 9727 | unicodeiter_len(unicodeiterobject *it) |
| 9728 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9729 | Py_ssize_t len = 0; |
| 9730 | if (it->it_seq) |
| 9731 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9732 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9733 | } |
| 9734 | |
| 9735 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9736 | |
| 9737 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9738 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9739 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9740 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9741 | }; |
| 9742 | |
| 9743 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9744 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9745 | "str_iterator", /* tp_name */ |
| 9746 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9747 | 0, /* tp_itemsize */ |
| 9748 | /* methods */ |
| 9749 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9750 | 0, /* tp_print */ |
| 9751 | 0, /* tp_getattr */ |
| 9752 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9753 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9754 | 0, /* tp_repr */ |
| 9755 | 0, /* tp_as_number */ |
| 9756 | 0, /* tp_as_sequence */ |
| 9757 | 0, /* tp_as_mapping */ |
| 9758 | 0, /* tp_hash */ |
| 9759 | 0, /* tp_call */ |
| 9760 | 0, /* tp_str */ |
| 9761 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9762 | 0, /* tp_setattro */ |
| 9763 | 0, /* tp_as_buffer */ |
| 9764 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9765 | 0, /* tp_doc */ |
| 9766 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9767 | 0, /* tp_clear */ |
| 9768 | 0, /* tp_richcompare */ |
| 9769 | 0, /* tp_weaklistoffset */ |
| 9770 | PyObject_SelfIter, /* tp_iter */ |
| 9771 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9772 | unicodeiter_methods, /* tp_methods */ |
| 9773 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9774 | }; |
| 9775 | |
| 9776 | static PyObject * |
| 9777 | unicode_iter(PyObject *seq) |
| 9778 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9779 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9780 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9781 | if (!PyUnicode_Check(seq)) { |
| 9782 | PyErr_BadInternalCall(); |
| 9783 | return NULL; |
| 9784 | } |
| 9785 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9786 | if (it == NULL) |
| 9787 | return NULL; |
| 9788 | it->it_index = 0; |
| 9789 | Py_INCREF(seq); |
| 9790 | it->it_seq = (PyUnicodeObject *)seq; |
| 9791 | _PyObject_GC_TRACK(it); |
| 9792 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9793 | } |
| 9794 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9795 | size_t |
| 9796 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9797 | { |
| 9798 | int res = 0; |
| 9799 | while(*u++) |
| 9800 | res++; |
| 9801 | return res; |
| 9802 | } |
| 9803 | |
| 9804 | Py_UNICODE* |
| 9805 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9806 | { |
| 9807 | Py_UNICODE *u = s1; |
| 9808 | while ((*u++ = *s2++)); |
| 9809 | return s1; |
| 9810 | } |
| 9811 | |
| 9812 | Py_UNICODE* |
| 9813 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9814 | { |
| 9815 | Py_UNICODE *u = s1; |
| 9816 | while ((*u++ = *s2++)) |
| 9817 | if (n-- == 0) |
| 9818 | break; |
| 9819 | return s1; |
| 9820 | } |
| 9821 | |
| 9822 | int |
| 9823 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9824 | { |
| 9825 | while (*s1 && *s2 && *s1 == *s2) |
| 9826 | s1++, s2++; |
| 9827 | if (*s1 && *s2) |
| 9828 | return (*s1 < *s2) ? -1 : +1; |
| 9829 | if (*s1) |
| 9830 | return 1; |
| 9831 | if (*s2) |
| 9832 | return -1; |
| 9833 | return 0; |
| 9834 | } |
| 9835 | |
| 9836 | Py_UNICODE* |
| 9837 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9838 | { |
| 9839 | const Py_UNICODE *p; |
| 9840 | for (p = s; *p; p++) |
| 9841 | if (*p == c) |
| 9842 | return (Py_UNICODE*)p; |
| 9843 | return NULL; |
| 9844 | } |
| 9845 | |
| 9846 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9847 | #ifdef __cplusplus |
| 9848 | } |
| 9849 | #endif |