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) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 310 | Py_CLEAR(unicode->defenc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 311 | } |
| 312 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* 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] | 318 | Ux0000 terminated; some code (e.g. new_identifier) |
| 319 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 320 | |
| 321 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 322 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 323 | |
| 324 | */ |
| 325 | |
| 326 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 327 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 328 | { |
| 329 | register PyUnicodeObject *unicode; |
| 330 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 331 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 332 | if (length == 0 && unicode_empty != NULL) { |
| 333 | Py_INCREF(unicode_empty); |
| 334 | return unicode_empty; |
| 335 | } |
| 336 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 337 | /* Ensure we won't overflow the size. */ |
| 338 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 339 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 340 | } |
| 341 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 342 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 343 | if (free_list) { |
| 344 | unicode = free_list; |
| 345 | free_list = *(PyUnicodeObject **)unicode; |
| 346 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 347 | if (unicode->str) { |
| 348 | /* Keep-Alive optimization: we only upsize the buffer, |
| 349 | never downsize it. */ |
| 350 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 351 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 352 | PyObject_DEL(unicode->str); |
| 353 | unicode->str = NULL; |
| 354 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 355 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 356 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 357 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 358 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 359 | } |
| 360 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 361 | } |
| 362 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 363 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 364 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 365 | if (unicode == NULL) |
| 366 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 367 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 368 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 371 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 372 | PyErr_NoMemory(); |
| 373 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 374 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 375 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 376 | * the caller fails before initializing str -- unicode_resize() |
| 377 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 378 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 379 | * We don't want unicode_resize to read uninitialized memory in |
| 380 | * that case. |
| 381 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 382 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 383 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 384 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 385 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 386 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 387 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 388 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 390 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 391 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 392 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 393 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 394 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 395 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 399 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 400 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 401 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 402 | case SSTATE_NOT_INTERNED: |
| 403 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 404 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 405 | case SSTATE_INTERNED_MORTAL: |
| 406 | /* revive dead object temporarily for DelItem */ |
| 407 | Py_REFCNT(unicode) = 3; |
| 408 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 409 | Py_FatalError( |
| 410 | "deletion of interned string failed"); |
| 411 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 412 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 413 | case SSTATE_INTERNED_IMMORTAL: |
| 414 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 415 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 416 | default: |
| 417 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 420 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 421 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 422 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 423 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 424 | PyObject_DEL(unicode->str); |
| 425 | unicode->str = NULL; |
| 426 | unicode->length = 0; |
| 427 | } |
| 428 | if (unicode->defenc) { |
Georg Brandl | 8ee604b | 2010-07-29 14:23:06 +0000 | [diff] [blame] | 429 | Py_CLEAR(unicode->defenc); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 430 | } |
| 431 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 432 | *(PyUnicodeObject **)unicode = free_list; |
| 433 | free_list = unicode; |
| 434 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 435 | } |
| 436 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 437 | PyObject_DEL(unicode->str); |
| 438 | Py_XDECREF(unicode->defenc); |
| 439 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 443 | static |
| 444 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 445 | { |
| 446 | register PyUnicodeObject *v; |
| 447 | |
| 448 | /* Argument checks */ |
| 449 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 450 | PyErr_BadInternalCall(); |
| 451 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 452 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 453 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 454 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 455 | PyErr_BadInternalCall(); |
| 456 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /* Resizing unicode_empty and single character objects is not |
| 460 | possible since these are being shared. We simply return a fresh |
| 461 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 462 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 463 | (v == unicode_empty || v->length == 1)) { |
| 464 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 465 | if (w == NULL) |
| 466 | return -1; |
| 467 | Py_UNICODE_COPY(w->str, v->str, |
| 468 | length < v->length ? length : v->length); |
| 469 | Py_DECREF(*unicode); |
| 470 | *unicode = w; |
| 471 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 475 | objects, since we can modify them in-place. */ |
| 476 | return unicode_resize(v, length); |
| 477 | } |
| 478 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 479 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 480 | { |
| 481 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 482 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 484 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 485 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 486 | { |
| 487 | PyUnicodeObject *unicode; |
| 488 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 489 | /* If the Unicode data is known at construction time, we can apply |
| 490 | some optimizations which share commonly used objects. */ |
| 491 | if (u != NULL) { |
| 492 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 493 | /* Optimization for empty strings */ |
| 494 | if (size == 0 && unicode_empty != NULL) { |
| 495 | Py_INCREF(unicode_empty); |
| 496 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 497 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 498 | |
| 499 | /* Single character Unicode objects in the Latin-1 range are |
| 500 | shared when using this constructor */ |
| 501 | if (size == 1 && *u < 256) { |
| 502 | unicode = unicode_latin1[*u]; |
| 503 | if (!unicode) { |
| 504 | unicode = _PyUnicode_New(1); |
| 505 | if (!unicode) |
| 506 | return NULL; |
| 507 | unicode->str[0] = *u; |
| 508 | unicode_latin1[*u] = unicode; |
| 509 | } |
| 510 | Py_INCREF(unicode); |
| 511 | return (PyObject *)unicode; |
| 512 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 513 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 514 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 515 | unicode = _PyUnicode_New(size); |
| 516 | if (!unicode) |
| 517 | return NULL; |
| 518 | |
| 519 | /* Copy the Unicode data into the new object */ |
| 520 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 521 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 522 | |
| 523 | return (PyObject *)unicode; |
| 524 | } |
| 525 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 526 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 527 | { |
| 528 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 529 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 530 | if (size < 0) { |
| 531 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 532 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 533 | return NULL; |
| 534 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 535 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 536 | /* 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] | 537 | some optimizations which share commonly used objects. |
| 538 | Also, this means the input must be UTF-8, so fall back to the |
| 539 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 540 | if (u != NULL) { |
| 541 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 542 | /* Optimization for empty strings */ |
| 543 | if (size == 0 && unicode_empty != NULL) { |
| 544 | Py_INCREF(unicode_empty); |
| 545 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 546 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 547 | |
| 548 | /* Single characters are shared when using this constructor. |
| 549 | Restrict to ASCII, since the input must be UTF-8. */ |
| 550 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 551 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 552 | if (!unicode) { |
| 553 | unicode = _PyUnicode_New(1); |
| 554 | if (!unicode) |
| 555 | return NULL; |
| 556 | unicode->str[0] = Py_CHARMASK(*u); |
| 557 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 558 | } |
| 559 | Py_INCREF(unicode); |
| 560 | return (PyObject *)unicode; |
| 561 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 562 | |
| 563 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 566 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 567 | if (!unicode) |
| 568 | return NULL; |
| 569 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 570 | return (PyObject *)unicode; |
| 571 | } |
| 572 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 573 | PyObject *PyUnicode_FromString(const char *u) |
| 574 | { |
| 575 | size_t size = strlen(u); |
| 576 | if (size > PY_SSIZE_T_MAX) { |
| 577 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 578 | return NULL; |
| 579 | } |
| 580 | |
| 581 | return PyUnicode_FromStringAndSize(u, size); |
| 582 | } |
| 583 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 584 | #ifdef HAVE_WCHAR_H |
| 585 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 586 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 587 | # define CONVERT_WCHAR_TO_SURROGATES |
| 588 | #endif |
| 589 | |
| 590 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 591 | |
| 592 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 593 | to convert from UTF32 to UTF16. */ |
| 594 | |
| 595 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 596 | Py_ssize_t size) |
| 597 | { |
| 598 | PyUnicodeObject *unicode; |
| 599 | register Py_ssize_t i; |
| 600 | Py_ssize_t alloc; |
| 601 | const wchar_t *orig_w; |
| 602 | |
| 603 | if (w == NULL) { |
| 604 | if (size == 0) |
| 605 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 606 | PyErr_BadInternalCall(); |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | if (size == -1) { |
| 611 | size = wcslen(w); |
| 612 | } |
| 613 | |
| 614 | alloc = size; |
| 615 | orig_w = w; |
| 616 | for (i = size; i > 0; i--) { |
| 617 | if (*w > 0xFFFF) |
| 618 | alloc++; |
| 619 | w++; |
| 620 | } |
| 621 | w = orig_w; |
| 622 | unicode = _PyUnicode_New(alloc); |
| 623 | if (!unicode) |
| 624 | return NULL; |
| 625 | |
| 626 | /* Copy the wchar_t data into the new object */ |
| 627 | { |
| 628 | register Py_UNICODE *u; |
| 629 | u = PyUnicode_AS_UNICODE(unicode); |
| 630 | for (i = size; i > 0; i--) { |
| 631 | if (*w > 0xFFFF) { |
| 632 | wchar_t ordinal = *w++; |
| 633 | ordinal -= 0x10000; |
| 634 | *u++ = 0xD800 | (ordinal >> 10); |
| 635 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 636 | } |
| 637 | else |
| 638 | *u++ = *w++; |
| 639 | } |
| 640 | } |
| 641 | return (PyObject *)unicode; |
| 642 | } |
| 643 | |
| 644 | #else |
| 645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 646 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 647 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 648 | { |
| 649 | PyUnicodeObject *unicode; |
| 650 | |
| 651 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 652 | if (size == 0) |
| 653 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 654 | PyErr_BadInternalCall(); |
| 655 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 658 | if (size == -1) { |
| 659 | size = wcslen(w); |
| 660 | } |
| 661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 662 | unicode = _PyUnicode_New(size); |
| 663 | if (!unicode) |
| 664 | return NULL; |
| 665 | |
| 666 | /* Copy the wchar_t data into the new object */ |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame] | 667 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 668 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 669 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 670 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 671 | register Py_UNICODE *u; |
| 672 | register Py_ssize_t i; |
| 673 | u = PyUnicode_AS_UNICODE(unicode); |
| 674 | for (i = size; i > 0; i--) |
| 675 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 676 | } |
| 677 | #endif |
| 678 | |
| 679 | return (PyObject *)unicode; |
| 680 | } |
| 681 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 682 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 683 | |
| 684 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 685 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 686 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 687 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 688 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 689 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 690 | *fmt++ = '%'; |
| 691 | if (width) { |
| 692 | if (zeropad) |
| 693 | *fmt++ = '0'; |
| 694 | fmt += sprintf(fmt, "%d", width); |
| 695 | } |
| 696 | if (precision) |
| 697 | fmt += sprintf(fmt, ".%d", precision); |
| 698 | if (longflag) |
| 699 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 700 | else if (longlongflag) { |
| 701 | /* longlongflag should only ever be nonzero on machines with |
| 702 | HAVE_LONG_LONG defined */ |
| 703 | #ifdef HAVE_LONG_LONG |
| 704 | char *f = PY_FORMAT_LONG_LONG; |
| 705 | while (*f) |
| 706 | *fmt++ = *f++; |
| 707 | #else |
| 708 | /* we shouldn't ever get here */ |
| 709 | assert(0); |
| 710 | *fmt++ = 'l'; |
| 711 | #endif |
| 712 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 713 | else if (size_tflag) { |
| 714 | char *f = PY_FORMAT_SIZE_T; |
| 715 | while (*f) |
| 716 | *fmt++ = *f++; |
| 717 | } |
| 718 | *fmt++ = c; |
| 719 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 722 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 723 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 724 | /* size of fixed-size buffer for formatting single arguments */ |
| 725 | #define ITEM_BUFFER_LEN 21 |
| 726 | /* maximum number of characters required for output of %ld. 21 characters |
| 727 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 728 | #define MAX_LONG_CHARS 21 |
| 729 | /* maximum number of characters required for output of %lld. |
| 730 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 731 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 732 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 733 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 734 | PyObject * |
| 735 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 736 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 737 | va_list count; |
| 738 | Py_ssize_t callcount = 0; |
| 739 | PyObject **callresults = NULL; |
| 740 | PyObject **callresult = NULL; |
| 741 | Py_ssize_t n = 0; |
| 742 | int width = 0; |
| 743 | int precision = 0; |
| 744 | int zeropad; |
| 745 | const char* f; |
| 746 | Py_UNICODE *s; |
| 747 | PyObject *string; |
| 748 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 749 | char buffer[ITEM_BUFFER_LEN+1]; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 750 | /* use abuffer instead of buffer, if we need more space |
| 751 | * (which can happen if there's a format specifier with width). */ |
| 752 | char *abuffer = NULL; |
| 753 | char *realbuffer; |
| 754 | Py_ssize_t abuffersize = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 755 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 756 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 757 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 758 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 759 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 760 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 761 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 762 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 763 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 764 | if (*f == '%') { |
| 765 | if (*(f+1)=='%') |
| 766 | continue; |
| 767 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 768 | ++callcount; |
| 769 | while (ISDIGIT((unsigned)*f)) |
| 770 | width = (width*10) + *f++ - '0'; |
| 771 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 772 | ; |
| 773 | if (*f == 's') |
| 774 | ++callcount; |
| 775 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 776 | } |
| 777 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 778 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 779 | if (callcount) { |
| 780 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 781 | if (!callresults) { |
| 782 | PyErr_NoMemory(); |
| 783 | return NULL; |
| 784 | } |
| 785 | callresult = callresults; |
| 786 | } |
| 787 | /* step 3: figure out how large a buffer we need */ |
| 788 | for (f = format; *f; f++) { |
| 789 | if (*f == '%') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 790 | #ifdef HAVE_LONG_LONG |
| 791 | int longlongflag = 0; |
| 792 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 793 | const char* p = f; |
| 794 | width = 0; |
| 795 | while (ISDIGIT((unsigned)*f)) |
| 796 | width = (width*10) + *f++ - '0'; |
| 797 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 798 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 799 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 800 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 801 | * they don't affect the amount of space we reserve. |
| 802 | */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 803 | if (*f == 'l') { |
| 804 | if (f[1] == 'd' || f[1] == 'u') { |
| 805 | ++f; |
| 806 | } |
| 807 | #ifdef HAVE_LONG_LONG |
| 808 | else if (f[1] == 'l' && |
| 809 | (f[2] == 'd' || f[2] == 'u')) { |
| 810 | longlongflag = 1; |
| 811 | f += 2; |
| 812 | } |
| 813 | #endif |
| 814 | } |
| 815 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 816 | ++f; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 817 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 818 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 819 | switch (*f) { |
| 820 | case 'c': |
| 821 | (void)va_arg(count, int); |
| 822 | /* fall through... */ |
| 823 | case '%': |
| 824 | n++; |
| 825 | break; |
| 826 | case 'd': case 'u': case 'i': case 'x': |
| 827 | (void) va_arg(count, int); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 828 | #ifdef HAVE_LONG_LONG |
| 829 | if (longlongflag) { |
| 830 | if (width < MAX_LONG_LONG_CHARS) |
| 831 | width = MAX_LONG_LONG_CHARS; |
| 832 | } |
| 833 | else |
| 834 | #endif |
| 835 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 836 | including sign. Decimal takes the most space. This |
| 837 | isn't enough for octal. If a width is specified we |
| 838 | need more (which we allocate later). */ |
| 839 | if (width < MAX_LONG_CHARS) |
| 840 | width = MAX_LONG_CHARS; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 841 | n += width; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 842 | /* XXX should allow for large precision here too. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 843 | if (abuffersize < width) |
| 844 | abuffersize = width; |
| 845 | break; |
| 846 | case 's': |
| 847 | { |
| 848 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 849 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 850 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 851 | if (!str) |
| 852 | goto fail; |
| 853 | n += PyUnicode_GET_SIZE(str); |
| 854 | /* Remember the str and switch to the next slot */ |
| 855 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 856 | break; |
| 857 | } |
| 858 | case 'U': |
| 859 | { |
| 860 | PyObject *obj = va_arg(count, PyObject *); |
| 861 | assert(obj && PyUnicode_Check(obj)); |
| 862 | n += PyUnicode_GET_SIZE(obj); |
| 863 | break; |
| 864 | } |
| 865 | case 'V': |
| 866 | { |
| 867 | PyObject *obj = va_arg(count, PyObject *); |
| 868 | const char *str = va_arg(count, const char *); |
| 869 | assert(obj || str); |
| 870 | assert(!obj || PyUnicode_Check(obj)); |
| 871 | if (obj) |
| 872 | n += PyUnicode_GET_SIZE(obj); |
| 873 | else |
| 874 | n += strlen(str); |
| 875 | break; |
| 876 | } |
| 877 | case 'S': |
| 878 | { |
| 879 | PyObject *obj = va_arg(count, PyObject *); |
| 880 | PyObject *str; |
| 881 | assert(obj); |
| 882 | str = PyObject_Str(obj); |
| 883 | if (!str) |
| 884 | goto fail; |
| 885 | n += PyUnicode_GET_SIZE(str); |
| 886 | /* Remember the str and switch to the next slot */ |
| 887 | *callresult++ = str; |
| 888 | break; |
| 889 | } |
| 890 | case 'R': |
| 891 | { |
| 892 | PyObject *obj = va_arg(count, PyObject *); |
| 893 | PyObject *repr; |
| 894 | assert(obj); |
| 895 | repr = PyObject_Repr(obj); |
| 896 | if (!repr) |
| 897 | goto fail; |
| 898 | n += PyUnicode_GET_SIZE(repr); |
| 899 | /* Remember the repr and switch to the next slot */ |
| 900 | *callresult++ = repr; |
| 901 | break; |
| 902 | } |
| 903 | case 'A': |
| 904 | { |
| 905 | PyObject *obj = va_arg(count, PyObject *); |
| 906 | PyObject *ascii; |
| 907 | assert(obj); |
| 908 | ascii = PyObject_ASCII(obj); |
| 909 | if (!ascii) |
| 910 | goto fail; |
| 911 | n += PyUnicode_GET_SIZE(ascii); |
| 912 | /* Remember the repr and switch to the next slot */ |
| 913 | *callresult++ = ascii; |
| 914 | break; |
| 915 | } |
| 916 | case 'p': |
| 917 | (void) va_arg(count, int); |
| 918 | /* maximum 64-bit pointer representation: |
| 919 | * 0xffffffffffffffff |
| 920 | * so 19 characters is enough. |
| 921 | * XXX I count 18 -- what's the extra for? |
| 922 | */ |
| 923 | n += 19; |
| 924 | break; |
| 925 | default: |
| 926 | /* if we stumble upon an unknown |
| 927 | formatting code, copy the rest of |
| 928 | the format string to the output |
| 929 | string. (we cannot just skip the |
| 930 | code, since there's no way to know |
| 931 | what's in the argument list) */ |
| 932 | n += strlen(p); |
| 933 | goto expand; |
| 934 | } |
| 935 | } else |
| 936 | n++; |
| 937 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 938 | expand: |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 939 | if (abuffersize > ITEM_BUFFER_LEN) { |
| 940 | /* add 1 for sprintf's trailing null byte */ |
| 941 | abuffer = PyObject_Malloc(abuffersize + 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 942 | if (!abuffer) { |
| 943 | PyErr_NoMemory(); |
| 944 | goto fail; |
| 945 | } |
| 946 | realbuffer = abuffer; |
| 947 | } |
| 948 | else |
| 949 | realbuffer = buffer; |
| 950 | /* step 4: fill the buffer */ |
| 951 | /* Since we've analyzed how much space we need for the worst case, |
| 952 | we don't have to resize the string. |
| 953 | There can be no errors beyond this point. */ |
| 954 | string = PyUnicode_FromUnicode(NULL, n); |
| 955 | if (!string) |
| 956 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 957 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 958 | s = PyUnicode_AS_UNICODE(string); |
| 959 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 960 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 961 | for (f = format; *f; f++) { |
| 962 | if (*f == '%') { |
| 963 | const char* p = f++; |
| 964 | int longflag = 0; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 965 | int longlongflag = 0; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 966 | int size_tflag = 0; |
| 967 | zeropad = (*f == '0'); |
| 968 | /* parse the width.precision part */ |
| 969 | width = 0; |
| 970 | while (ISDIGIT((unsigned)*f)) |
| 971 | width = (width*10) + *f++ - '0'; |
| 972 | precision = 0; |
| 973 | if (*f == '.') { |
| 974 | f++; |
| 975 | while (ISDIGIT((unsigned)*f)) |
| 976 | precision = (precision*10) + *f++ - '0'; |
| 977 | } |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 978 | /* Handle %ld, %lu, %lld and %llu. */ |
| 979 | if (*f == 'l') { |
| 980 | if (f[1] == 'd' || f[1] == 'u') { |
| 981 | longflag = 1; |
| 982 | ++f; |
| 983 | } |
| 984 | #ifdef HAVE_LONG_LONG |
| 985 | else if (f[1] == 'l' && |
| 986 | (f[2] == 'd' || f[2] == 'u')) { |
| 987 | longlongflag = 1; |
| 988 | f += 2; |
| 989 | } |
| 990 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 991 | } |
| 992 | /* handle the size_t flag. */ |
| 993 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 994 | size_tflag = 1; |
| 995 | ++f; |
| 996 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 997 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 998 | switch (*f) { |
| 999 | case 'c': |
| 1000 | *s++ = va_arg(vargs, int); |
| 1001 | break; |
| 1002 | case 'd': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1003 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1004 | width, precision, 'd'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1005 | if (longflag) |
| 1006 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1007 | #ifdef HAVE_LONG_LONG |
| 1008 | else if (longlongflag) |
| 1009 | sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG)); |
| 1010 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1011 | else if (size_tflag) |
| 1012 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 1013 | else |
| 1014 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1015 | appendstring(realbuffer); |
| 1016 | break; |
| 1017 | case 'u': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1018 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1019 | width, precision, 'u'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1020 | if (longflag) |
| 1021 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1022 | #ifdef HAVE_LONG_LONG |
| 1023 | else if (longlongflag) |
| 1024 | sprintf(realbuffer, fmt, va_arg(vargs, |
| 1025 | unsigned PY_LONG_LONG)); |
| 1026 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1027 | else if (size_tflag) |
| 1028 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 1029 | else |
| 1030 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 1031 | appendstring(realbuffer); |
| 1032 | break; |
| 1033 | case 'i': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1034 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1035 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1036 | appendstring(realbuffer); |
| 1037 | break; |
| 1038 | case 'x': |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1039 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1040 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 1041 | appendstring(realbuffer); |
| 1042 | break; |
| 1043 | case 's': |
| 1044 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1045 | /* unused, since we already have the result */ |
| 1046 | (void) va_arg(vargs, char *); |
| 1047 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 1048 | PyUnicode_GET_SIZE(*callresult)); |
| 1049 | s += PyUnicode_GET_SIZE(*callresult); |
| 1050 | /* We're done with the unicode()/repr() => forget it */ |
| 1051 | Py_DECREF(*callresult); |
| 1052 | /* switch to next unicode()/repr() result */ |
| 1053 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1054 | break; |
| 1055 | } |
| 1056 | case 'U': |
| 1057 | { |
| 1058 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1059 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1060 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1061 | s += size; |
| 1062 | break; |
| 1063 | } |
| 1064 | case 'V': |
| 1065 | { |
| 1066 | PyObject *obj = va_arg(vargs, PyObject *); |
| 1067 | const char *str = va_arg(vargs, const char *); |
| 1068 | if (obj) { |
| 1069 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 1070 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 1071 | s += size; |
| 1072 | } else { |
| 1073 | appendstring(str); |
| 1074 | } |
| 1075 | break; |
| 1076 | } |
| 1077 | case 'S': |
| 1078 | case 'R': |
| 1079 | { |
| 1080 | Py_UNICODE *ucopy; |
| 1081 | Py_ssize_t usize; |
| 1082 | Py_ssize_t upos; |
| 1083 | /* unused, since we already have the result */ |
| 1084 | (void) va_arg(vargs, PyObject *); |
| 1085 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1086 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1087 | for (upos = 0; upos<usize;) |
| 1088 | *s++ = ucopy[upos++]; |
| 1089 | /* We're done with the unicode()/repr() => forget it */ |
| 1090 | Py_DECREF(*callresult); |
| 1091 | /* switch to next unicode()/repr() result */ |
| 1092 | ++callresult; |
| 1093 | break; |
| 1094 | } |
| 1095 | case 'p': |
| 1096 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1097 | /* %p is ill-defined: ensure leading 0x. */ |
| 1098 | if (buffer[1] == 'X') |
| 1099 | buffer[1] = 'x'; |
| 1100 | else if (buffer[1] != 'x') { |
| 1101 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1102 | buffer[0] = '0'; |
| 1103 | buffer[1] = 'x'; |
| 1104 | } |
| 1105 | appendstring(buffer); |
| 1106 | break; |
| 1107 | case '%': |
| 1108 | *s++ = '%'; |
| 1109 | break; |
| 1110 | default: |
| 1111 | appendstring(p); |
| 1112 | goto end; |
| 1113 | } |
| 1114 | } else |
| 1115 | *s++ = *f; |
| 1116 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1117 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1118 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1119 | if (callresults) |
| 1120 | PyObject_Free(callresults); |
| 1121 | if (abuffer) |
| 1122 | PyObject_Free(abuffer); |
| 1123 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1124 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1125 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1126 | if (callresults) { |
| 1127 | PyObject **callresult2 = callresults; |
| 1128 | while (callresult2 < callresult) { |
| 1129 | Py_DECREF(*callresult2); |
| 1130 | ++callresult2; |
| 1131 | } |
| 1132 | PyObject_Free(callresults); |
| 1133 | } |
| 1134 | if (abuffer) |
| 1135 | PyObject_Free(abuffer); |
| 1136 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | #undef appendstring |
| 1140 | |
| 1141 | PyObject * |
| 1142 | PyUnicode_FromFormat(const char *format, ...) |
| 1143 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1144 | PyObject* ret; |
| 1145 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1146 | |
| 1147 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1148 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1149 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1150 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1151 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1152 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1153 | va_end(vargs); |
| 1154 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1157 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1158 | wchar_t *w, |
| 1159 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1160 | { |
| 1161 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1162 | PyErr_BadInternalCall(); |
| 1163 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1164 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1165 | |
| 1166 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1167 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1168 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1169 | |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame] | 1170 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1171 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1172 | #else |
| 1173 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1174 | register Py_UNICODE *u; |
| 1175 | register Py_ssize_t i; |
| 1176 | u = PyUnicode_AS_UNICODE(unicode); |
| 1177 | for (i = size; i > 0; i--) |
| 1178 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1179 | } |
| 1180 | #endif |
| 1181 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1182 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1183 | return PyUnicode_GET_SIZE(unicode); |
| 1184 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1185 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | #endif |
| 1189 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1190 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1191 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1192 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1193 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1194 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1195 | PyErr_SetString(PyExc_ValueError, |
| 1196 | "chr() arg not in range(0x110000)"); |
| 1197 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1198 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1199 | |
| 1200 | #ifndef Py_UNICODE_WIDE |
| 1201 | if (ordinal > 0xffff) { |
| 1202 | ordinal -= 0x10000; |
| 1203 | s[0] = 0xD800 | (ordinal >> 10); |
| 1204 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1205 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1206 | } |
| 1207 | #endif |
| 1208 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1209 | s[0] = (Py_UNICODE)ordinal; |
| 1210 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1213 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1214 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1215 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1216 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1217 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1218 | Py_INCREF(obj); |
| 1219 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1220 | } |
| 1221 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1222 | /* For a Unicode subtype that's not a Unicode object, |
| 1223 | return a true Unicode object with the same data. */ |
| 1224 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1225 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1226 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1227 | PyErr_Format(PyExc_TypeError, |
| 1228 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1229 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1230 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1234 | const char *encoding, |
| 1235 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1236 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame^] | 1237 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1238 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1240 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1241 | PyErr_BadInternalCall(); |
| 1242 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1243 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1244 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame^] | 1245 | /* Decoding bytes objects is the most common case and should be fast */ |
| 1246 | if (PyBytes_Check(obj)) { |
| 1247 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1248 | Py_INCREF(unicode_empty); |
| 1249 | v = (PyObject *) unicode_empty; |
| 1250 | } |
| 1251 | else { |
| 1252 | v = PyUnicode_Decode( |
| 1253 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 1254 | encoding, errors); |
| 1255 | } |
| 1256 | return v; |
| 1257 | } |
| 1258 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1259 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1260 | PyErr_SetString(PyExc_TypeError, |
| 1261 | "decoding str is not supported"); |
| 1262 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1263 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1264 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame^] | 1265 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 1266 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 1267 | PyErr_Format(PyExc_TypeError, |
| 1268 | "coercing to str: need bytes, bytearray " |
| 1269 | "or buffer-like object, %.80s found", |
| 1270 | Py_TYPE(obj)->tp_name); |
| 1271 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1272 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1273 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame^] | 1274 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1275 | Py_INCREF(unicode_empty); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame^] | 1276 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1277 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1278 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame^] | 1279 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame^] | 1281 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1282 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1285 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1286 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 1287 | 1 on success. */ |
| 1288 | static int |
| 1289 | normalize_encoding(const char *encoding, |
| 1290 | char *lower, |
| 1291 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1292 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1293 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1294 | char *l; |
| 1295 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1296 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1297 | e = encoding; |
| 1298 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1299 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1300 | while (*e) { |
| 1301 | if (l == l_end) |
| 1302 | return 0; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1303 | if (ISUPPER(*e)) { |
| 1304 | *l++ = TOLOWER(*e++); |
| 1305 | } |
| 1306 | else if (*e == '_') { |
| 1307 | *l++ = '-'; |
| 1308 | e++; |
| 1309 | } |
| 1310 | else { |
| 1311 | *l++ = *e++; |
| 1312 | } |
| 1313 | } |
| 1314 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1315 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | PyObject *PyUnicode_Decode(const char *s, |
| 1319 | Py_ssize_t size, |
| 1320 | const char *encoding, |
| 1321 | const char *errors) |
| 1322 | { |
| 1323 | PyObject *buffer = NULL, *unicode; |
| 1324 | Py_buffer info; |
| 1325 | char lower[11]; /* Enough for any encoding shortcut */ |
| 1326 | |
| 1327 | if (encoding == NULL) |
| 1328 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1329 | |
| 1330 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1331 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1332 | if (strcmp(lower, "utf-8") == 0) |
| 1333 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 1334 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1335 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1336 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1337 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1338 | else if (strcmp(lower, "mbcs") == 0) |
| 1339 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1340 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1341 | else if (strcmp(lower, "ascii") == 0) |
| 1342 | return PyUnicode_DecodeASCII(s, size, errors); |
| 1343 | else if (strcmp(lower, "utf-16") == 0) |
| 1344 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1345 | else if (strcmp(lower, "utf-32") == 0) |
| 1346 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 1347 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1348 | |
| 1349 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1350 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1351 | 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] | 1352 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1353 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1354 | if (buffer == NULL) |
| 1355 | goto onError; |
| 1356 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1357 | if (unicode == NULL) |
| 1358 | goto onError; |
| 1359 | if (!PyUnicode_Check(unicode)) { |
| 1360 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1361 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1362 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1363 | Py_DECREF(unicode); |
| 1364 | goto onError; |
| 1365 | } |
| 1366 | Py_DECREF(buffer); |
| 1367 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1368 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1369 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1370 | Py_XDECREF(buffer); |
| 1371 | return NULL; |
| 1372 | } |
| 1373 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1374 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1375 | const char *encoding, |
| 1376 | const char *errors) |
| 1377 | { |
| 1378 | PyObject *v; |
| 1379 | |
| 1380 | if (!PyUnicode_Check(unicode)) { |
| 1381 | PyErr_BadArgument(); |
| 1382 | goto onError; |
| 1383 | } |
| 1384 | |
| 1385 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1386 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1387 | |
| 1388 | /* Decode via the codec registry */ |
| 1389 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1390 | if (v == NULL) |
| 1391 | goto onError; |
| 1392 | return v; |
| 1393 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1394 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1395 | return NULL; |
| 1396 | } |
| 1397 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1398 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1399 | const char *encoding, |
| 1400 | const char *errors) |
| 1401 | { |
| 1402 | PyObject *v; |
| 1403 | |
| 1404 | if (!PyUnicode_Check(unicode)) { |
| 1405 | PyErr_BadArgument(); |
| 1406 | goto onError; |
| 1407 | } |
| 1408 | |
| 1409 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1410 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1411 | |
| 1412 | /* Decode via the codec registry */ |
| 1413 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1414 | if (v == NULL) |
| 1415 | goto onError; |
| 1416 | if (!PyUnicode_Check(v)) { |
| 1417 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1418 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1419 | Py_TYPE(v)->tp_name); |
| 1420 | Py_DECREF(v); |
| 1421 | goto onError; |
| 1422 | } |
| 1423 | return v; |
| 1424 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1425 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1426 | return NULL; |
| 1427 | } |
| 1428 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1429 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1430 | Py_ssize_t size, |
| 1431 | const char *encoding, |
| 1432 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1433 | { |
| 1434 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1435 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1436 | unicode = PyUnicode_FromUnicode(s, size); |
| 1437 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1438 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1439 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1440 | Py_DECREF(unicode); |
| 1441 | return v; |
| 1442 | } |
| 1443 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1444 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1445 | const char *encoding, |
| 1446 | const char *errors) |
| 1447 | { |
| 1448 | PyObject *v; |
| 1449 | |
| 1450 | if (!PyUnicode_Check(unicode)) { |
| 1451 | PyErr_BadArgument(); |
| 1452 | goto onError; |
| 1453 | } |
| 1454 | |
| 1455 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1456 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1457 | |
| 1458 | /* Encode via the codec registry */ |
| 1459 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1460 | if (v == NULL) |
| 1461 | goto onError; |
| 1462 | return v; |
| 1463 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1464 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1465 | return NULL; |
| 1466 | } |
| 1467 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1468 | PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode) |
| 1469 | { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1470 | if (Py_FileSystemDefaultEncoding) { |
| 1471 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1472 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) |
| 1473 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1474 | PyUnicode_GET_SIZE(unicode), |
| 1475 | NULL); |
| 1476 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1477 | return PyUnicode_AsEncodedString(unicode, |
| 1478 | Py_FileSystemDefaultEncoding, |
| 1479 | "surrogateescape"); |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1480 | } else |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1481 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Victor Stinner | 3119ed7 | 2010-08-18 22:26:50 +0000 | [diff] [blame] | 1482 | PyUnicode_GET_SIZE(unicode), |
| 1483 | "surrogateescape"); |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1486 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1487 | const char *encoding, |
| 1488 | const char *errors) |
| 1489 | { |
| 1490 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1491 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1493 | if (!PyUnicode_Check(unicode)) { |
| 1494 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1495 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1496 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1497 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1498 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1499 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1500 | |
| 1501 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1502 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1503 | if (strcmp(lower, "utf-8") == 0) |
| 1504 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1505 | PyUnicode_GET_SIZE(unicode), |
| 1506 | errors); |
| 1507 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1508 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1509 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1510 | PyUnicode_GET_SIZE(unicode), |
| 1511 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1512 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1513 | else if (strcmp(lower, "mbcs") == 0) |
| 1514 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1515 | PyUnicode_GET_SIZE(unicode), |
| 1516 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1517 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1518 | else if (strcmp(lower, "ascii") == 0) |
| 1519 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1520 | PyUnicode_GET_SIZE(unicode), |
| 1521 | errors); |
| 1522 | } |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1523 | /* During bootstrap, we may need to find the encodings |
| 1524 | package, to load the file system encoding, and require the |
| 1525 | file system encoding in order to load the encodings |
| 1526 | package. |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1527 | |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1528 | Break out of this dependency by assuming that the path to |
| 1529 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1530 | instead, if the file system encoding is the locale's |
| 1531 | encoding. */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1532 | if (Py_FileSystemDefaultEncoding && |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1533 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1534 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1535 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1536 | PyUnicode_GET_SIZE(unicode), |
| 1537 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1538 | |
| 1539 | /* Encode via the codec registry */ |
| 1540 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1541 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1542 | return NULL; |
| 1543 | |
| 1544 | /* The normal path */ |
| 1545 | if (PyBytes_Check(v)) |
| 1546 | return v; |
| 1547 | |
| 1548 | /* 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] | 1549 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1550 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1551 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1552 | |
| 1553 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1554 | "encoder %s returned bytearray instead of bytes", |
| 1555 | encoding); |
| 1556 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1557 | Py_DECREF(v); |
| 1558 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1559 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1560 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1561 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1562 | Py_DECREF(v); |
| 1563 | return b; |
| 1564 | } |
| 1565 | |
| 1566 | PyErr_Format(PyExc_TypeError, |
| 1567 | "encoder did not return a bytes object (type=%.400s)", |
| 1568 | Py_TYPE(v)->tp_name); |
| 1569 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1570 | return NULL; |
| 1571 | } |
| 1572 | |
| 1573 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1574 | const char *encoding, |
| 1575 | const char *errors) |
| 1576 | { |
| 1577 | PyObject *v; |
| 1578 | |
| 1579 | if (!PyUnicode_Check(unicode)) { |
| 1580 | PyErr_BadArgument(); |
| 1581 | goto onError; |
| 1582 | } |
| 1583 | |
| 1584 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1585 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1586 | |
| 1587 | /* Encode via the codec registry */ |
| 1588 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1589 | if (v == NULL) |
| 1590 | goto onError; |
| 1591 | if (!PyUnicode_Check(v)) { |
| 1592 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1593 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1594 | Py_TYPE(v)->tp_name); |
| 1595 | Py_DECREF(v); |
| 1596 | goto onError; |
| 1597 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1598 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1599 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1600 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1601 | return NULL; |
| 1602 | } |
| 1603 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1604 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1605 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1606 | { |
| 1607 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1608 | if (v) |
| 1609 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1610 | if (errors != NULL) |
| 1611 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1612 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1613 | PyUnicode_GET_SIZE(unicode), |
| 1614 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1615 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1616 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1617 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1618 | return v; |
| 1619 | } |
| 1620 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1621 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1622 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1623 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1624 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1625 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1626 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1627 | PyObject* |
| 1628 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1629 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1630 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1631 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1632 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1633 | bootstrapping process where the codecs aren't ready yet. |
| 1634 | */ |
| 1635 | if (Py_FileSystemDefaultEncoding) { |
| 1636 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1637 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1638 | return PyUnicode_DecodeMBCS(s, size, NULL); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1639 | } |
| 1640 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1641 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1642 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1643 | } |
| 1644 | #endif |
| 1645 | return PyUnicode_Decode(s, size, |
| 1646 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1647 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1648 | } |
| 1649 | else { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1650 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1654 | |
| 1655 | int |
| 1656 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1657 | { |
| 1658 | PyObject *output = NULL; |
| 1659 | Py_ssize_t size; |
| 1660 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1661 | if (arg == NULL) { |
| 1662 | Py_DECREF(*(PyObject**)addr); |
| 1663 | return 1; |
| 1664 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1665 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1666 | output = arg; |
| 1667 | Py_INCREF(output); |
| 1668 | } |
| 1669 | else { |
| 1670 | arg = PyUnicode_FromObject(arg); |
| 1671 | if (!arg) |
| 1672 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1673 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1674 | Py_DECREF(arg); |
| 1675 | if (!output) |
| 1676 | return 0; |
| 1677 | if (!PyBytes_Check(output)) { |
| 1678 | Py_DECREF(output); |
| 1679 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1680 | return 0; |
| 1681 | } |
| 1682 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1683 | size = PyBytes_GET_SIZE(output); |
| 1684 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1685 | if (size != strlen(data)) { |
| 1686 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1687 | Py_DECREF(output); |
| 1688 | return 0; |
| 1689 | } |
| 1690 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1691 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 1695 | int |
| 1696 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 1697 | { |
| 1698 | PyObject *output = NULL; |
| 1699 | Py_ssize_t size; |
| 1700 | void *data; |
| 1701 | if (arg == NULL) { |
| 1702 | Py_DECREF(*(PyObject**)addr); |
| 1703 | return 1; |
| 1704 | } |
| 1705 | if (PyUnicode_Check(arg)) { |
| 1706 | output = arg; |
| 1707 | Py_INCREF(output); |
| 1708 | } |
| 1709 | else { |
| 1710 | arg = PyBytes_FromObject(arg); |
| 1711 | if (!arg) |
| 1712 | return 0; |
| 1713 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 1714 | PyBytes_GET_SIZE(arg)); |
| 1715 | Py_DECREF(arg); |
| 1716 | if (!output) |
| 1717 | return 0; |
| 1718 | if (!PyUnicode_Check(output)) { |
| 1719 | Py_DECREF(output); |
| 1720 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 1721 | return 0; |
| 1722 | } |
| 1723 | } |
| 1724 | size = PyUnicode_GET_SIZE(output); |
| 1725 | data = PyUnicode_AS_UNICODE(output); |
| 1726 | if (size != Py_UNICODE_strlen(data)) { |
| 1727 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1728 | Py_DECREF(output); |
| 1729 | return 0; |
| 1730 | } |
| 1731 | *(PyObject**)addr = output; |
| 1732 | return Py_CLEANUP_SUPPORTED; |
| 1733 | } |
| 1734 | |
| 1735 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1736 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1737 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1738 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1739 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1740 | if (!PyUnicode_Check(unicode)) { |
| 1741 | PyErr_BadArgument(); |
| 1742 | return NULL; |
| 1743 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1744 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1745 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1746 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1747 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1748 | *psize = PyBytes_GET_SIZE(bytes); |
| 1749 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1753 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1754 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1755 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1758 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1759 | { |
| 1760 | if (!PyUnicode_Check(unicode)) { |
| 1761 | PyErr_BadArgument(); |
| 1762 | goto onError; |
| 1763 | } |
| 1764 | return PyUnicode_AS_UNICODE(unicode); |
| 1765 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1766 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1767 | return NULL; |
| 1768 | } |
| 1769 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1770 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1771 | { |
| 1772 | if (!PyUnicode_Check(unicode)) { |
| 1773 | PyErr_BadArgument(); |
| 1774 | goto onError; |
| 1775 | } |
| 1776 | return PyUnicode_GET_SIZE(unicode); |
| 1777 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1778 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1779 | return -1; |
| 1780 | } |
| 1781 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1782 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1783 | { |
| 1784 | return unicode_default_encoding; |
| 1785 | } |
| 1786 | |
| 1787 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1788 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1789 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1790 | PyErr_Format(PyExc_ValueError, |
| 1791 | "Can only set default encoding to %s", |
| 1792 | unicode_default_encoding); |
| 1793 | return -1; |
| 1794 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1795 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1798 | /* create or adjust a UnicodeDecodeError */ |
| 1799 | static void |
| 1800 | make_decode_exception(PyObject **exceptionObject, |
| 1801 | const char *encoding, |
| 1802 | const char *input, Py_ssize_t length, |
| 1803 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 1804 | const char *reason) |
| 1805 | { |
| 1806 | if (*exceptionObject == NULL) { |
| 1807 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1808 | encoding, input, length, startpos, endpos, reason); |
| 1809 | } |
| 1810 | else { |
| 1811 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 1812 | goto onError; |
| 1813 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 1814 | goto onError; |
| 1815 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1816 | goto onError; |
| 1817 | } |
| 1818 | return; |
| 1819 | |
| 1820 | onError: |
| 1821 | Py_DECREF(*exceptionObject); |
| 1822 | *exceptionObject = NULL; |
| 1823 | } |
| 1824 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1825 | /* error handling callback helper: |
| 1826 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1827 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1828 | and adjust various state variables. |
| 1829 | return 0 on success, -1 on error |
| 1830 | */ |
| 1831 | |
| 1832 | static |
| 1833 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1834 | const char *encoding, const char *reason, |
| 1835 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1836 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1837 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1838 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1839 | 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] | 1840 | |
| 1841 | PyObject *restuple = NULL; |
| 1842 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1843 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1844 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1845 | Py_ssize_t requiredsize; |
| 1846 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1847 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1848 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1849 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1850 | int res = -1; |
| 1851 | |
| 1852 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1853 | *errorHandler = PyCodec_LookupError(errors); |
| 1854 | if (*errorHandler == NULL) |
| 1855 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1858 | make_decode_exception(exceptionObject, |
| 1859 | encoding, |
| 1860 | *input, *inend - *input, |
| 1861 | *startinpos, *endinpos, |
| 1862 | reason); |
| 1863 | if (*exceptionObject == NULL) |
| 1864 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1865 | |
| 1866 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1867 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1868 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1869 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1870 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1871 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1872 | } |
| 1873 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1874 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1875 | |
| 1876 | /* Copy back the bytes variables, which might have been modified by the |
| 1877 | callback */ |
| 1878 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1879 | if (!inputobj) |
| 1880 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1881 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1882 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1883 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1884 | *input = PyBytes_AS_STRING(inputobj); |
| 1885 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1886 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1887 | /* we can DECREF safely, as the exception has another reference, |
| 1888 | so the object won't go away. */ |
| 1889 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1890 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1891 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1892 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1893 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1894 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1895 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1896 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1897 | |
| 1898 | /* need more space? (at least enough for what we |
| 1899 | have+the replacement+the rest of the string (starting |
| 1900 | at the new input position), so we won't have to check space |
| 1901 | when there are no errors in the rest of the string) */ |
| 1902 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1903 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1904 | requiredsize = *outpos + repsize + insize-newpos; |
| 1905 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1906 | if (requiredsize<2*outsize) |
| 1907 | requiredsize = 2*outsize; |
| 1908 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1909 | goto onError; |
| 1910 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1911 | } |
| 1912 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1913 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1914 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1915 | *outptr += repsize; |
| 1916 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1917 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1918 | /* we made it! */ |
| 1919 | res = 0; |
| 1920 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1921 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1922 | Py_XDECREF(restuple); |
| 1923 | return res; |
| 1924 | } |
| 1925 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1926 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1927 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1928 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1929 | |
| 1930 | /* Three simple macros defining base-64. */ |
| 1931 | |
| 1932 | /* Is c a base-64 character? */ |
| 1933 | |
| 1934 | #define IS_BASE64(c) \ |
| 1935 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1936 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1937 | ((c) >= '0' && (c) <= '9') || \ |
| 1938 | (c) == '+' || (c) == '/') |
| 1939 | |
| 1940 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1941 | |
| 1942 | #define FROM_BASE64(c) \ |
| 1943 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1944 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1945 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1946 | (c) == '+' ? 62 : 63) |
| 1947 | |
| 1948 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1949 | |
| 1950 | #define TO_BASE64(n) \ |
| 1951 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1952 | |
| 1953 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1954 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1955 | * byte not decoding to itself is the + which begins a base64 |
| 1956 | * string. */ |
| 1957 | |
| 1958 | #define DECODE_DIRECT(c) \ |
| 1959 | ((c) <= 127 && (c) != '+') |
| 1960 | |
| 1961 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1962 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1963 | * the above). See RFC2152. This array identifies these different |
| 1964 | * sets: |
| 1965 | * 0 : "Set D" |
| 1966 | * alphanumeric and '(),-./:? |
| 1967 | * 1 : "Set O" |
| 1968 | * !"#$%&*;<=>@[]^_`{|} |
| 1969 | * 2 : "whitespace" |
| 1970 | * ht nl cr sp |
| 1971 | * 3 : special (must be base64 encoded) |
| 1972 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1973 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1974 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1975 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1976 | char utf7_category[128] = { |
| 1977 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 1978 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 1979 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 1980 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1981 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 1982 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 1983 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 1984 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 1985 | /* @ A B C D E F G H I J K L M N O */ |
| 1986 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1987 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 1988 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 1989 | /* ` a b c d e f g h i j k l m n o */ |
| 1990 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1991 | /* p q r s t u v w x y z { | } ~ del */ |
| 1992 | 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] | 1993 | }; |
| 1994 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1995 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 1996 | * answer depends on whether we are encoding set O as itself, and also |
| 1997 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 1998 | * clear that the answers to these questions vary between |
| 1999 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 2000 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2001 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 2002 | ((c) < 128 && (c) > 0 && \ |
| 2003 | ((utf7_category[(c)] == 0) || \ |
| 2004 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 2005 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2006 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2007 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2008 | Py_ssize_t size, |
| 2009 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2010 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2011 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 2012 | } |
| 2013 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2014 | /* The decoder. The only state we preserve is our read position, |
| 2015 | * i.e. how many characters we have consumed. So if we end in the |
| 2016 | * middle of a shift sequence we have to back off the read position |
| 2017 | * and the output to the beginning of the sequence, otherwise we lose |
| 2018 | * all the shift state (seen bits, number of bits seen, high |
| 2019 | * surrogate). */ |
| 2020 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2021 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2022 | Py_ssize_t size, |
| 2023 | const char *errors, |
| 2024 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2025 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2026 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2027 | Py_ssize_t startinpos; |
| 2028 | Py_ssize_t endinpos; |
| 2029 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2030 | const char *e; |
| 2031 | PyUnicodeObject *unicode; |
| 2032 | Py_UNICODE *p; |
| 2033 | const char *errmsg = ""; |
| 2034 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2035 | Py_UNICODE *shiftOutStart; |
| 2036 | unsigned int base64bits = 0; |
| 2037 | unsigned long base64buffer = 0; |
| 2038 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2039 | PyObject *errorHandler = NULL; |
| 2040 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2041 | |
| 2042 | unicode = _PyUnicode_New(size); |
| 2043 | if (!unicode) |
| 2044 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2045 | if (size == 0) { |
| 2046 | if (consumed) |
| 2047 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2048 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2049 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2050 | |
| 2051 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2052 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2053 | e = s + size; |
| 2054 | |
| 2055 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2056 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2057 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 2058 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2059 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2060 | if (inShift) { /* in a base-64 section */ |
| 2061 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 2062 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 2063 | base64bits += 6; |
| 2064 | s++; |
| 2065 | if (base64bits >= 16) { |
| 2066 | /* we have enough bits for a UTF-16 value */ |
| 2067 | Py_UNICODE outCh = (Py_UNICODE) |
| 2068 | (base64buffer >> (base64bits-16)); |
| 2069 | base64bits -= 16; |
| 2070 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 2071 | if (surrogate) { |
| 2072 | /* expecting a second surrogate */ |
| 2073 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2074 | #ifdef Py_UNICODE_WIDE |
| 2075 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2076 | | (outCh & 0x3FF)) + 0x10000; |
| 2077 | #else |
| 2078 | *p++ = surrogate; |
| 2079 | *p++ = outCh; |
| 2080 | #endif |
| 2081 | surrogate = 0; |
| 2082 | } |
| 2083 | else { |
| 2084 | surrogate = 0; |
| 2085 | errmsg = "second surrogate missing"; |
| 2086 | goto utf7Error; |
| 2087 | } |
| 2088 | } |
| 2089 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2090 | /* first surrogate */ |
| 2091 | surrogate = outCh; |
| 2092 | } |
| 2093 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2094 | errmsg = "unexpected second surrogate"; |
| 2095 | goto utf7Error; |
| 2096 | } |
| 2097 | else { |
| 2098 | *p++ = outCh; |
| 2099 | } |
| 2100 | } |
| 2101 | } |
| 2102 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2103 | inShift = 0; |
| 2104 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2105 | if (surrogate) { |
| 2106 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2107 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2108 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2109 | if (base64bits > 0) { /* left-over bits */ |
| 2110 | if (base64bits >= 6) { |
| 2111 | /* We've seen at least one base-64 character */ |
| 2112 | errmsg = "partial character in shift sequence"; |
| 2113 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2114 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2115 | else { |
| 2116 | /* Some bits remain; they should be zero */ |
| 2117 | if (base64buffer != 0) { |
| 2118 | errmsg = "non-zero padding bits in shift sequence"; |
| 2119 | goto utf7Error; |
| 2120 | } |
| 2121 | } |
| 2122 | } |
| 2123 | if (ch != '-') { |
| 2124 | /* '-' is absorbed; other terminating |
| 2125 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2126 | *p++ = ch; |
| 2127 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2128 | } |
| 2129 | } |
| 2130 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2131 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2132 | s++; /* consume '+' */ |
| 2133 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2134 | s++; |
| 2135 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2136 | } |
| 2137 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2138 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2139 | shiftOutStart = p; |
| 2140 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2141 | } |
| 2142 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2143 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2144 | *p++ = ch; |
| 2145 | s++; |
| 2146 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2147 | else { |
| 2148 | startinpos = s-starts; |
| 2149 | s++; |
| 2150 | errmsg = "unexpected special character"; |
| 2151 | goto utf7Error; |
| 2152 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2153 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2154 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2155 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2156 | endinpos = s-starts; |
| 2157 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2158 | errors, &errorHandler, |
| 2159 | "utf7", errmsg, |
| 2160 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2161 | &unicode, &outpos, &p)) |
| 2162 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2165 | /* end of string */ |
| 2166 | |
| 2167 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2168 | /* if we're in an inconsistent state, that's an error */ |
| 2169 | if (surrogate || |
| 2170 | (base64bits >= 6) || |
| 2171 | (base64bits > 0 && base64buffer != 0)) { |
| 2172 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2173 | endinpos = size; |
| 2174 | if (unicode_decode_call_errorhandler( |
| 2175 | errors, &errorHandler, |
| 2176 | "utf7", "unterminated shift sequence", |
| 2177 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2178 | &unicode, &outpos, &p)) |
| 2179 | goto onError; |
| 2180 | if (s < e) |
| 2181 | goto restart; |
| 2182 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2183 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2184 | |
| 2185 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2186 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2187 | if (inShift) { |
| 2188 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2189 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2190 | } |
| 2191 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2192 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2193 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2194 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2195 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2196 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2197 | goto onError; |
| 2198 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2199 | Py_XDECREF(errorHandler); |
| 2200 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2201 | return (PyObject *)unicode; |
| 2202 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2203 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2204 | Py_XDECREF(errorHandler); |
| 2205 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2206 | Py_DECREF(unicode); |
| 2207 | return NULL; |
| 2208 | } |
| 2209 | |
| 2210 | |
| 2211 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2212 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2213 | int base64SetO, |
| 2214 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2215 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2216 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2217 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2218 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2219 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2220 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2221 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2222 | unsigned int base64bits = 0; |
| 2223 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2224 | char * out; |
| 2225 | char * start; |
| 2226 | |
| 2227 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2228 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2229 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2230 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2231 | return PyErr_NoMemory(); |
| 2232 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2233 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2234 | if (v == NULL) |
| 2235 | return NULL; |
| 2236 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2237 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2238 | for (;i < size; ++i) { |
| 2239 | Py_UNICODE ch = s[i]; |
| 2240 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2241 | if (inShift) { |
| 2242 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2243 | /* shifting out */ |
| 2244 | if (base64bits) { /* output remaining bits */ |
| 2245 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2246 | base64buffer = 0; |
| 2247 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2248 | } |
| 2249 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2250 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2251 | so no '-' is required, except if the character is itself a '-' */ |
| 2252 | if (IS_BASE64(ch) || ch == '-') { |
| 2253 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2254 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2255 | *out++ = (char) ch; |
| 2256 | } |
| 2257 | else { |
| 2258 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2259 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2260 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2261 | else { /* not in a shift sequence */ |
| 2262 | if (ch == '+') { |
| 2263 | *out++ = '+'; |
| 2264 | *out++ = '-'; |
| 2265 | } |
| 2266 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2267 | *out++ = (char) ch; |
| 2268 | } |
| 2269 | else { |
| 2270 | *out++ = '+'; |
| 2271 | inShift = 1; |
| 2272 | goto encode_char; |
| 2273 | } |
| 2274 | } |
| 2275 | continue; |
| 2276 | encode_char: |
| 2277 | #ifdef Py_UNICODE_WIDE |
| 2278 | if (ch >= 0x10000) { |
| 2279 | /* code first surrogate */ |
| 2280 | base64bits += 16; |
| 2281 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2282 | while (base64bits >= 6) { |
| 2283 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2284 | base64bits -= 6; |
| 2285 | } |
| 2286 | /* prepare second surrogate */ |
| 2287 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2288 | } |
| 2289 | #endif |
| 2290 | base64bits += 16; |
| 2291 | base64buffer = (base64buffer << 16) | ch; |
| 2292 | while (base64bits >= 6) { |
| 2293 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2294 | base64bits -= 6; |
| 2295 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2296 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2297 | if (base64bits) |
| 2298 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2299 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2300 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2301 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2302 | return NULL; |
| 2303 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2306 | #undef IS_BASE64 |
| 2307 | #undef FROM_BASE64 |
| 2308 | #undef TO_BASE64 |
| 2309 | #undef DECODE_DIRECT |
| 2310 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2311 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2312 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2313 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2314 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2315 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2316 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 2317 | illegal prefix. See RFC 3629 for details */ |
| 2318 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 2319 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2320 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2321 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2322 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2323 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2324 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2325 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 2326 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2327 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2328 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2329 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 2330 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 2331 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 2332 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 2333 | 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0-F4 + F5-FF */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2334 | }; |
| 2335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2336 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2337 | Py_ssize_t size, |
| 2338 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2339 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2340 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2341 | } |
| 2342 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2343 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2344 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2345 | |
| 2346 | /* Mask to quickly check whether a C 'long' contains a |
| 2347 | non-ASCII, UTF8-encoded char. */ |
| 2348 | #if (SIZEOF_LONG == 8) |
| 2349 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2350 | #elif (SIZEOF_LONG == 4) |
| 2351 | # define ASCII_CHAR_MASK 0x80808080L |
| 2352 | #else |
| 2353 | # error C 'long' size should be either 4 or 8! |
| 2354 | #endif |
| 2355 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2356 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2357 | Py_ssize_t size, |
| 2358 | const char *errors, |
| 2359 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2360 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2361 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2362 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2363 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2364 | Py_ssize_t startinpos; |
| 2365 | Py_ssize_t endinpos; |
| 2366 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2367 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2368 | PyUnicodeObject *unicode; |
| 2369 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2370 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2371 | PyObject *errorHandler = NULL; |
| 2372 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2373 | |
| 2374 | /* Note: size will always be longer than the resulting Unicode |
| 2375 | character count */ |
| 2376 | unicode = _PyUnicode_New(size); |
| 2377 | if (!unicode) |
| 2378 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2379 | if (size == 0) { |
| 2380 | if (consumed) |
| 2381 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2382 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2383 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2384 | |
| 2385 | /* Unpack UTF-8 encoded data */ |
| 2386 | p = unicode->str; |
| 2387 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2388 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2389 | |
| 2390 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2391 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2392 | |
| 2393 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2394 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2395 | input will consist of an overwhelming majority of ASCII |
| 2396 | characters, we try to optimize for this case by checking |
| 2397 | as many characters as a C 'long' can contain. |
| 2398 | First, check if we can do an aligned read, as most CPUs have |
| 2399 | a penalty for unaligned reads. |
| 2400 | */ |
| 2401 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2402 | /* Help register allocation */ |
| 2403 | register const char *_s = s; |
| 2404 | register Py_UNICODE *_p = p; |
| 2405 | while (_s < aligned_end) { |
| 2406 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2407 | and do a fast unrolled copy if it only contains ASCII |
| 2408 | characters. */ |
| 2409 | unsigned long data = *(unsigned long *) _s; |
| 2410 | if (data & ASCII_CHAR_MASK) |
| 2411 | break; |
| 2412 | _p[0] = (unsigned char) _s[0]; |
| 2413 | _p[1] = (unsigned char) _s[1]; |
| 2414 | _p[2] = (unsigned char) _s[2]; |
| 2415 | _p[3] = (unsigned char) _s[3]; |
| 2416 | #if (SIZEOF_LONG == 8) |
| 2417 | _p[4] = (unsigned char) _s[4]; |
| 2418 | _p[5] = (unsigned char) _s[5]; |
| 2419 | _p[6] = (unsigned char) _s[6]; |
| 2420 | _p[7] = (unsigned char) _s[7]; |
| 2421 | #endif |
| 2422 | _s += SIZEOF_LONG; |
| 2423 | _p += SIZEOF_LONG; |
| 2424 | } |
| 2425 | s = _s; |
| 2426 | p = _p; |
| 2427 | if (s == e) |
| 2428 | break; |
| 2429 | ch = (unsigned char)*s; |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2434 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2435 | s++; |
| 2436 | continue; |
| 2437 | } |
| 2438 | |
| 2439 | n = utf8_code_length[ch]; |
| 2440 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2441 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2442 | if (consumed) |
| 2443 | break; |
| 2444 | else { |
| 2445 | errmsg = "unexpected end of data"; |
| 2446 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2447 | endinpos = startinpos+1; |
| 2448 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 2449 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2450 | goto utf8Error; |
| 2451 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2452 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2453 | |
| 2454 | switch (n) { |
| 2455 | |
| 2456 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2457 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2458 | startinpos = s-starts; |
| 2459 | endinpos = startinpos+1; |
| 2460 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2461 | |
| 2462 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2463 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2464 | startinpos = s-starts; |
| 2465 | endinpos = startinpos+1; |
| 2466 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2467 | |
| 2468 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2469 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2470 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2471 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2472 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2473 | goto utf8Error; |
| 2474 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2475 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2476 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2477 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2478 | break; |
| 2479 | |
| 2480 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 2481 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2482 | will result in surrogates in range d800-dfff. Surrogates are |
| 2483 | not valid UTF-8 so they are rejected. |
| 2484 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2485 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2486 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2487 | (s[2] & 0xc0) != 0x80 || |
| 2488 | ((unsigned char)s[0] == 0xE0 && |
| 2489 | (unsigned char)s[1] < 0xA0) || |
| 2490 | ((unsigned char)s[0] == 0xED && |
| 2491 | (unsigned char)s[1] > 0x9F)) { |
| 2492 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2493 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2494 | endinpos = startinpos + 1; |
| 2495 | |
| 2496 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 2497 | continuation byte is s[2], so increment endinpos by 1, |
| 2498 | if not, s[1] is invalid and endinpos doesn't need to |
| 2499 | be incremented. */ |
| 2500 | if ((s[1] & 0xC0) == 0x80) |
| 2501 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2502 | goto utf8Error; |
| 2503 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2504 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2505 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2506 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2507 | break; |
| 2508 | |
| 2509 | case 4: |
| 2510 | if ((s[1] & 0xc0) != 0x80 || |
| 2511 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2512 | (s[3] & 0xc0) != 0x80 || |
| 2513 | ((unsigned char)s[0] == 0xF0 && |
| 2514 | (unsigned char)s[1] < 0x90) || |
| 2515 | ((unsigned char)s[0] == 0xF4 && |
| 2516 | (unsigned char)s[1] > 0x8F)) { |
| 2517 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2518 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2519 | endinpos = startinpos + 1; |
| 2520 | if ((s[1] & 0xC0) == 0x80) { |
| 2521 | endinpos++; |
| 2522 | if ((s[2] & 0xC0) == 0x80) |
| 2523 | endinpos++; |
| 2524 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2525 | goto utf8Error; |
| 2526 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2527 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2528 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2529 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2530 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2531 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2532 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2533 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2534 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2535 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2536 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2537 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2538 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2539 | /* high surrogate = top 10 bits added to D800 */ |
| 2540 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2541 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2542 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2543 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2544 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2545 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2546 | } |
| 2547 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2548 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2549 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2550 | utf8Error: |
| 2551 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2552 | if (unicode_decode_call_errorhandler( |
| 2553 | errors, &errorHandler, |
| 2554 | "utf8", errmsg, |
| 2555 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2556 | &unicode, &outpos, &p)) |
| 2557 | goto onError; |
| 2558 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2559 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2560 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2561 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2562 | |
| 2563 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2564 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2565 | goto onError; |
| 2566 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2567 | Py_XDECREF(errorHandler); |
| 2568 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2569 | return (PyObject *)unicode; |
| 2570 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2571 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2572 | Py_XDECREF(errorHandler); |
| 2573 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2574 | Py_DECREF(unicode); |
| 2575 | return NULL; |
| 2576 | } |
| 2577 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2578 | #undef ASCII_CHAR_MASK |
| 2579 | |
| 2580 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2581 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2582 | and allocate exactly as much space needed at the end. Else allocate the |
| 2583 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2584 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2585 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2586 | PyObject * |
| 2587 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2588 | Py_ssize_t size, |
| 2589 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2590 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2591 | #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] | 2592 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2593 | Py_ssize_t i; /* index into s of next input byte */ |
| 2594 | PyObject *result; /* result string object */ |
| 2595 | char *p; /* next free byte in output buffer */ |
| 2596 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2597 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2598 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2599 | PyObject *errorHandler = NULL; |
| 2600 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2601 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2602 | assert(s != NULL); |
| 2603 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2604 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2605 | if (size <= MAX_SHORT_UNICHARS) { |
| 2606 | /* Write into the stack buffer; nallocated can't overflow. |
| 2607 | * At the end, we'll allocate exactly as much heap space as it |
| 2608 | * turns out we need. |
| 2609 | */ |
| 2610 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2611 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2612 | p = stackbuf; |
| 2613 | } |
| 2614 | else { |
| 2615 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2616 | nallocated = size * 4; |
| 2617 | if (nallocated / 4 != size) /* overflow! */ |
| 2618 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2619 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2620 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2621 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2622 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2623 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2624 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2625 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2626 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2627 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2628 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2629 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2630 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2631 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2632 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2633 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2634 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2635 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2636 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2637 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2638 | /* Special case: check for high and low surrogate */ |
| 2639 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2640 | Py_UCS4 ch2 = s[i]; |
| 2641 | /* Combine the two surrogates to form a UCS4 value */ |
| 2642 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2643 | i++; |
| 2644 | |
| 2645 | /* Encode UCS4 Unicode ordinals */ |
| 2646 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2647 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2648 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2649 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2650 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2651 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2652 | Py_ssize_t newpos; |
| 2653 | PyObject *rep; |
| 2654 | Py_ssize_t repsize, k; |
| 2655 | rep = unicode_encode_call_errorhandler |
| 2656 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2657 | s, size, &exc, i-1, i, &newpos); |
| 2658 | if (!rep) |
| 2659 | goto error; |
| 2660 | |
| 2661 | if (PyBytes_Check(rep)) |
| 2662 | repsize = PyBytes_GET_SIZE(rep); |
| 2663 | else |
| 2664 | repsize = PyUnicode_GET_SIZE(rep); |
| 2665 | |
| 2666 | if (repsize > 4) { |
| 2667 | Py_ssize_t offset; |
| 2668 | |
| 2669 | if (result == NULL) |
| 2670 | offset = p - stackbuf; |
| 2671 | else |
| 2672 | offset = p - PyBytes_AS_STRING(result); |
| 2673 | |
| 2674 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2675 | /* integer overflow */ |
| 2676 | PyErr_NoMemory(); |
| 2677 | goto error; |
| 2678 | } |
| 2679 | nallocated += repsize - 4; |
| 2680 | if (result != NULL) { |
| 2681 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2682 | goto error; |
| 2683 | } else { |
| 2684 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2685 | if (result == NULL) |
| 2686 | goto error; |
| 2687 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2688 | } |
| 2689 | p = PyBytes_AS_STRING(result) + offset; |
| 2690 | } |
| 2691 | |
| 2692 | if (PyBytes_Check(rep)) { |
| 2693 | char *prep = PyBytes_AS_STRING(rep); |
| 2694 | for(k = repsize; k > 0; k--) |
| 2695 | *p++ = *prep++; |
| 2696 | } else /* rep is unicode */ { |
| 2697 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2698 | Py_UNICODE c; |
| 2699 | |
| 2700 | for(k=0; k<repsize; k++) { |
| 2701 | c = prep[k]; |
| 2702 | if (0x80 <= c) { |
| 2703 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2704 | i-1, i, "surrogates not allowed"); |
| 2705 | goto error; |
| 2706 | } |
| 2707 | *p++ = (char)prep[k]; |
| 2708 | } |
| 2709 | } |
| 2710 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2711 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2712 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2713 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2714 | } else if (ch < 0x10000) { |
| 2715 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2716 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2717 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2718 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2719 | /* Encode UCS4 Unicode ordinals */ |
| 2720 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2721 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2722 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2723 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2724 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2725 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2726 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2727 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2728 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2729 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2730 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2731 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2732 | } |
| 2733 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2734 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2735 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2736 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2737 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2738 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2739 | Py_XDECREF(errorHandler); |
| 2740 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2741 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2742 | error: |
| 2743 | Py_XDECREF(errorHandler); |
| 2744 | Py_XDECREF(exc); |
| 2745 | Py_XDECREF(result); |
| 2746 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2747 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2748 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2751 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2752 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2753 | if (!PyUnicode_Check(unicode)) { |
| 2754 | PyErr_BadArgument(); |
| 2755 | return NULL; |
| 2756 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2757 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2758 | PyUnicode_GET_SIZE(unicode), |
| 2759 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2762 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2763 | |
| 2764 | PyObject * |
| 2765 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2766 | Py_ssize_t size, |
| 2767 | const char *errors, |
| 2768 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2769 | { |
| 2770 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2771 | } |
| 2772 | |
| 2773 | PyObject * |
| 2774 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2775 | Py_ssize_t size, |
| 2776 | const char *errors, |
| 2777 | int *byteorder, |
| 2778 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2779 | { |
| 2780 | const char *starts = s; |
| 2781 | Py_ssize_t startinpos; |
| 2782 | Py_ssize_t endinpos; |
| 2783 | Py_ssize_t outpos; |
| 2784 | PyUnicodeObject *unicode; |
| 2785 | Py_UNICODE *p; |
| 2786 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2787 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2788 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2789 | #else |
| 2790 | const int pairs = 0; |
| 2791 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2792 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2793 | int bo = 0; /* assume native ordering by default */ |
| 2794 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2795 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2796 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2797 | int iorder[] = {0, 1, 2, 3}; |
| 2798 | #else |
| 2799 | int iorder[] = {3, 2, 1, 0}; |
| 2800 | #endif |
| 2801 | PyObject *errorHandler = NULL; |
| 2802 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 2803 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2804 | q = (unsigned char *)s; |
| 2805 | e = q + size; |
| 2806 | |
| 2807 | if (byteorder) |
| 2808 | bo = *byteorder; |
| 2809 | |
| 2810 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2811 | byte order setting accordingly. In native mode, the leading BOM |
| 2812 | mark is skipped, in all other modes, it is copied to the output |
| 2813 | stream as-is (giving a ZWNBSP character). */ |
| 2814 | if (bo == 0) { |
| 2815 | if (size >= 4) { |
| 2816 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2817 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2818 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2819 | if (bom == 0x0000FEFF) { |
| 2820 | q += 4; |
| 2821 | bo = -1; |
| 2822 | } |
| 2823 | else if (bom == 0xFFFE0000) { |
| 2824 | q += 4; |
| 2825 | bo = 1; |
| 2826 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2827 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2828 | if (bom == 0x0000FEFF) { |
| 2829 | q += 4; |
| 2830 | bo = 1; |
| 2831 | } |
| 2832 | else if (bom == 0xFFFE0000) { |
| 2833 | q += 4; |
| 2834 | bo = -1; |
| 2835 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2836 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2837 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | if (bo == -1) { |
| 2841 | /* force LE */ |
| 2842 | iorder[0] = 0; |
| 2843 | iorder[1] = 1; |
| 2844 | iorder[2] = 2; |
| 2845 | iorder[3] = 3; |
| 2846 | } |
| 2847 | else if (bo == 1) { |
| 2848 | /* force BE */ |
| 2849 | iorder[0] = 3; |
| 2850 | iorder[1] = 2; |
| 2851 | iorder[2] = 1; |
| 2852 | iorder[3] = 0; |
| 2853 | } |
| 2854 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2855 | /* On narrow builds we split characters outside the BMP into two |
| 2856 | codepoints => count how much extra space we need. */ |
| 2857 | #ifndef Py_UNICODE_WIDE |
| 2858 | for (qq = q; qq < e; qq += 4) |
| 2859 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 2860 | pairs++; |
| 2861 | #endif |
| 2862 | |
| 2863 | /* This might be one to much, because of a BOM */ |
| 2864 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2865 | if (!unicode) |
| 2866 | return NULL; |
| 2867 | if (size == 0) |
| 2868 | return (PyObject *)unicode; |
| 2869 | |
| 2870 | /* Unpack UTF-32 encoded data */ |
| 2871 | p = unicode->str; |
| 2872 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2873 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2874 | Py_UCS4 ch; |
| 2875 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2876 | if (e-q<4) { |
| 2877 | if (consumed) |
| 2878 | break; |
| 2879 | errmsg = "truncated data"; |
| 2880 | startinpos = ((const char *)q)-starts; |
| 2881 | endinpos = ((const char *)e)-starts; |
| 2882 | goto utf32Error; |
| 2883 | /* The remaining input chars are ignored if the callback |
| 2884 | chooses to skip the input */ |
| 2885 | } |
| 2886 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2887 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2888 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2889 | if (ch >= 0x110000) |
| 2890 | { |
| 2891 | errmsg = "codepoint not in range(0x110000)"; |
| 2892 | startinpos = ((const char *)q)-starts; |
| 2893 | endinpos = startinpos+4; |
| 2894 | goto utf32Error; |
| 2895 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2896 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2897 | if (ch >= 0x10000) |
| 2898 | { |
| 2899 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2900 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2901 | } |
| 2902 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2903 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2904 | *p++ = ch; |
| 2905 | q += 4; |
| 2906 | continue; |
| 2907 | utf32Error: |
| 2908 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2909 | if (unicode_decode_call_errorhandler( |
| 2910 | errors, &errorHandler, |
| 2911 | "utf32", errmsg, |
| 2912 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2913 | &unicode, &outpos, &p)) |
| 2914 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | if (byteorder) |
| 2918 | *byteorder = bo; |
| 2919 | |
| 2920 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2921 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2922 | |
| 2923 | /* Adjust length */ |
| 2924 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2925 | goto onError; |
| 2926 | |
| 2927 | Py_XDECREF(errorHandler); |
| 2928 | Py_XDECREF(exc); |
| 2929 | return (PyObject *)unicode; |
| 2930 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2931 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2932 | Py_DECREF(unicode); |
| 2933 | Py_XDECREF(errorHandler); |
| 2934 | Py_XDECREF(exc); |
| 2935 | return NULL; |
| 2936 | } |
| 2937 | |
| 2938 | PyObject * |
| 2939 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2940 | Py_ssize_t size, |
| 2941 | const char *errors, |
| 2942 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2943 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2944 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2945 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2946 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2947 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2948 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2949 | #else |
| 2950 | const int pairs = 0; |
| 2951 | #endif |
| 2952 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2953 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2954 | int iorder[] = {0, 1, 2, 3}; |
| 2955 | #else |
| 2956 | int iorder[] = {3, 2, 1, 0}; |
| 2957 | #endif |
| 2958 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2959 | #define STORECHAR(CH) \ |
| 2960 | do { \ |
| 2961 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2962 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2963 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2964 | p[iorder[0]] = (CH) & 0xff; \ |
| 2965 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2966 | } while(0) |
| 2967 | |
| 2968 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2969 | so we need less space. */ |
| 2970 | #ifndef Py_UNICODE_WIDE |
| 2971 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2972 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2973 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2974 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2975 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2976 | nsize = (size - pairs + (byteorder == 0)); |
| 2977 | bytesize = nsize * 4; |
| 2978 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2979 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2980 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2981 | if (v == NULL) |
| 2982 | return NULL; |
| 2983 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2984 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2985 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2986 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2987 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2988 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2989 | |
| 2990 | if (byteorder == -1) { |
| 2991 | /* force LE */ |
| 2992 | iorder[0] = 0; |
| 2993 | iorder[1] = 1; |
| 2994 | iorder[2] = 2; |
| 2995 | iorder[3] = 3; |
| 2996 | } |
| 2997 | else if (byteorder == 1) { |
| 2998 | /* force BE */ |
| 2999 | iorder[0] = 3; |
| 3000 | iorder[1] = 2; |
| 3001 | iorder[2] = 1; |
| 3002 | iorder[3] = 0; |
| 3003 | } |
| 3004 | |
| 3005 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3006 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3007 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3008 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 3009 | Py_UCS4 ch2 = *s; |
| 3010 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 3011 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 3012 | s++; |
| 3013 | size--; |
| 3014 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3015 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3016 | #endif |
| 3017 | STORECHAR(ch); |
| 3018 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3019 | |
| 3020 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3021 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3022 | #undef STORECHAR |
| 3023 | } |
| 3024 | |
| 3025 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 3026 | { |
| 3027 | if (!PyUnicode_Check(unicode)) { |
| 3028 | PyErr_BadArgument(); |
| 3029 | return NULL; |
| 3030 | } |
| 3031 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3032 | PyUnicode_GET_SIZE(unicode), |
| 3033 | NULL, |
| 3034 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 3038 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3039 | PyObject * |
| 3040 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3041 | Py_ssize_t size, |
| 3042 | const char *errors, |
| 3043 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3044 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3045 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 3046 | } |
| 3047 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3048 | /* Two masks for fast checking of whether a C 'long' may contain |
| 3049 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 3050 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 3051 | rare in most input. |
| 3052 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 3053 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3054 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3055 | #if (SIZEOF_LONG == 8) |
| 3056 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 3057 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 3058 | #elif (SIZEOF_LONG == 4) |
| 3059 | # define FAST_CHAR_MASK 0x80008000L |
| 3060 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 3061 | #else |
| 3062 | # error C 'long' size should be either 4 or 8! |
| 3063 | #endif |
| 3064 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3065 | PyObject * |
| 3066 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3067 | Py_ssize_t size, |
| 3068 | const char *errors, |
| 3069 | int *byteorder, |
| 3070 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3071 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3072 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3073 | Py_ssize_t startinpos; |
| 3074 | Py_ssize_t endinpos; |
| 3075 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3076 | PyUnicodeObject *unicode; |
| 3077 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3078 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3079 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3080 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3081 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3082 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3083 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3084 | int ihi = 1, ilo = 0; |
| 3085 | #else |
| 3086 | int ihi = 0, ilo = 1; |
| 3087 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3088 | PyObject *errorHandler = NULL; |
| 3089 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | |
| 3091 | /* Note: size will always be longer than the resulting Unicode |
| 3092 | character count */ |
| 3093 | unicode = _PyUnicode_New(size); |
| 3094 | if (!unicode) |
| 3095 | return NULL; |
| 3096 | if (size == 0) |
| 3097 | return (PyObject *)unicode; |
| 3098 | |
| 3099 | /* Unpack UTF-16 encoded data */ |
| 3100 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3101 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3102 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3103 | |
| 3104 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3105 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3106 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3107 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3108 | byte order setting accordingly. In native mode, the leading BOM |
| 3109 | mark is skipped, in all other modes, it is copied to the output |
| 3110 | stream as-is (giving a ZWNBSP character). */ |
| 3111 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3112 | if (size >= 2) { |
| 3113 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3114 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3115 | if (bom == 0xFEFF) { |
| 3116 | q += 2; |
| 3117 | bo = -1; |
| 3118 | } |
| 3119 | else if (bom == 0xFFFE) { |
| 3120 | q += 2; |
| 3121 | bo = 1; |
| 3122 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3123 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3124 | if (bom == 0xFEFF) { |
| 3125 | q += 2; |
| 3126 | bo = 1; |
| 3127 | } |
| 3128 | else if (bom == 0xFFFE) { |
| 3129 | q += 2; |
| 3130 | bo = -1; |
| 3131 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3132 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3133 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3134 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3135 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3136 | if (bo == -1) { |
| 3137 | /* force LE */ |
| 3138 | ihi = 1; |
| 3139 | ilo = 0; |
| 3140 | } |
| 3141 | else if (bo == 1) { |
| 3142 | /* force BE */ |
| 3143 | ihi = 0; |
| 3144 | ilo = 1; |
| 3145 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3146 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3147 | native_ordering = ilo < ihi; |
| 3148 | #else |
| 3149 | native_ordering = ilo > ihi; |
| 3150 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3151 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3152 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3153 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3154 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3155 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3156 | reads are more expensive, better to defer to another iteration. */ |
| 3157 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3158 | /* Fast path for runs of non-surrogate chars. */ |
| 3159 | register const unsigned char *_q = q; |
| 3160 | Py_UNICODE *_p = p; |
| 3161 | if (native_ordering) { |
| 3162 | /* Native ordering is simple: as long as the input cannot |
| 3163 | possibly contain a surrogate char, do an unrolled copy |
| 3164 | of several 16-bit code points to the target object. |
| 3165 | The non-surrogate check is done on several input bytes |
| 3166 | at a time (as many as a C 'long' can contain). */ |
| 3167 | while (_q < aligned_end) { |
| 3168 | unsigned long data = * (unsigned long *) _q; |
| 3169 | if (data & FAST_CHAR_MASK) |
| 3170 | break; |
| 3171 | _p[0] = ((unsigned short *) _q)[0]; |
| 3172 | _p[1] = ((unsigned short *) _q)[1]; |
| 3173 | #if (SIZEOF_LONG == 8) |
| 3174 | _p[2] = ((unsigned short *) _q)[2]; |
| 3175 | _p[3] = ((unsigned short *) _q)[3]; |
| 3176 | #endif |
| 3177 | _q += SIZEOF_LONG; |
| 3178 | _p += SIZEOF_LONG / 2; |
| 3179 | } |
| 3180 | } |
| 3181 | else { |
| 3182 | /* Byteswapped ordering is similar, but we must decompose |
| 3183 | the copy bytewise, and take care of zero'ing out the |
| 3184 | upper bytes if the target object is in 32-bit units |
| 3185 | (that is, in UCS-4 builds). */ |
| 3186 | while (_q < aligned_end) { |
| 3187 | unsigned long data = * (unsigned long *) _q; |
| 3188 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3189 | break; |
| 3190 | /* Zero upper bytes in UCS-4 builds */ |
| 3191 | #if (Py_UNICODE_SIZE > 2) |
| 3192 | _p[0] = 0; |
| 3193 | _p[1] = 0; |
| 3194 | #if (SIZEOF_LONG == 8) |
| 3195 | _p[2] = 0; |
| 3196 | _p[3] = 0; |
| 3197 | #endif |
| 3198 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3199 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3200 | fill the two last bytes of each 4-byte unit. */ |
| 3201 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3202 | # define OFF 2 |
| 3203 | #else |
| 3204 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3205 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3206 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3207 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3208 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3209 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3210 | #if (SIZEOF_LONG == 8) |
| 3211 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3212 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3213 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3214 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3215 | #endif |
| 3216 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3217 | _q += SIZEOF_LONG; |
| 3218 | _p += SIZEOF_LONG / 2; |
| 3219 | } |
| 3220 | } |
| 3221 | p = _p; |
| 3222 | q = _q; |
| 3223 | if (q >= e) |
| 3224 | break; |
| 3225 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3226 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3227 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3228 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3229 | |
| 3230 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3231 | *p++ = ch; |
| 3232 | continue; |
| 3233 | } |
| 3234 | |
| 3235 | /* UTF-16 code pair: */ |
| 3236 | if (q > e) { |
| 3237 | errmsg = "unexpected end of data"; |
| 3238 | startinpos = (((const char *)q) - 2) - starts; |
| 3239 | endinpos = ((const char *)e) + 1 - starts; |
| 3240 | goto utf16Error; |
| 3241 | } |
| 3242 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3243 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3244 | q += 2; |
| 3245 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3246 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3247 | *p++ = ch; |
| 3248 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3249 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3250 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3251 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3252 | continue; |
| 3253 | } |
| 3254 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3255 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3256 | startinpos = (((const char *)q)-4)-starts; |
| 3257 | endinpos = startinpos+2; |
| 3258 | goto utf16Error; |
| 3259 | } |
| 3260 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3261 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3262 | errmsg = "illegal encoding"; |
| 3263 | startinpos = (((const char *)q)-2)-starts; |
| 3264 | endinpos = startinpos+2; |
| 3265 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3266 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3267 | utf16Error: |
| 3268 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3269 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3270 | errors, |
| 3271 | &errorHandler, |
| 3272 | "utf16", errmsg, |
| 3273 | &starts, |
| 3274 | (const char **)&e, |
| 3275 | &startinpos, |
| 3276 | &endinpos, |
| 3277 | &exc, |
| 3278 | (const char **)&q, |
| 3279 | &unicode, |
| 3280 | &outpos, |
| 3281 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3282 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3283 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3284 | /* remaining byte at the end? (size should be even) */ |
| 3285 | if (e == q) { |
| 3286 | if (!consumed) { |
| 3287 | errmsg = "truncated data"; |
| 3288 | startinpos = ((const char *)q) - starts; |
| 3289 | endinpos = ((const char *)e) + 1 - starts; |
| 3290 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3291 | if (unicode_decode_call_errorhandler( |
| 3292 | errors, |
| 3293 | &errorHandler, |
| 3294 | "utf16", errmsg, |
| 3295 | &starts, |
| 3296 | (const char **)&e, |
| 3297 | &startinpos, |
| 3298 | &endinpos, |
| 3299 | &exc, |
| 3300 | (const char **)&q, |
| 3301 | &unicode, |
| 3302 | &outpos, |
| 3303 | &p)) |
| 3304 | goto onError; |
| 3305 | /* The remaining input chars are ignored if the callback |
| 3306 | chooses to skip the input */ |
| 3307 | } |
| 3308 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3309 | |
| 3310 | if (byteorder) |
| 3311 | *byteorder = bo; |
| 3312 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3313 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3314 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3315 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3316 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3317 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3318 | goto onError; |
| 3319 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3320 | Py_XDECREF(errorHandler); |
| 3321 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3322 | return (PyObject *)unicode; |
| 3323 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3324 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3325 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3326 | Py_XDECREF(errorHandler); |
| 3327 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3328 | return NULL; |
| 3329 | } |
| 3330 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3331 | #undef FAST_CHAR_MASK |
| 3332 | #undef SWAPPED_FAST_CHAR_MASK |
| 3333 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3334 | PyObject * |
| 3335 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3336 | Py_ssize_t size, |
| 3337 | const char *errors, |
| 3338 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3339 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3340 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3341 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3342 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3343 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3344 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3345 | #else |
| 3346 | const int pairs = 0; |
| 3347 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3348 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3349 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3350 | int ihi = 1, ilo = 0; |
| 3351 | #else |
| 3352 | int ihi = 0, ilo = 1; |
| 3353 | #endif |
| 3354 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3355 | #define STORECHAR(CH) \ |
| 3356 | do { \ |
| 3357 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3358 | p[ilo] = (CH) & 0xff; \ |
| 3359 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3360 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3361 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3362 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3363 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3364 | if (s[i] >= 0x10000) |
| 3365 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3366 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3367 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3368 | if (size > PY_SSIZE_T_MAX || |
| 3369 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3370 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3371 | nsize = size + pairs + (byteorder == 0); |
| 3372 | bytesize = nsize * 2; |
| 3373 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3374 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3375 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3376 | if (v == NULL) |
| 3377 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3379 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3380 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3381 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3382 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3383 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3384 | |
| 3385 | if (byteorder == -1) { |
| 3386 | /* force LE */ |
| 3387 | ihi = 1; |
| 3388 | ilo = 0; |
| 3389 | } |
| 3390 | else if (byteorder == 1) { |
| 3391 | /* force BE */ |
| 3392 | ihi = 0; |
| 3393 | ilo = 1; |
| 3394 | } |
| 3395 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3396 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3397 | Py_UNICODE ch = *s++; |
| 3398 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3399 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3400 | if (ch >= 0x10000) { |
| 3401 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3402 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3403 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3404 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3405 | STORECHAR(ch); |
| 3406 | if (ch2) |
| 3407 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3408 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3409 | |
| 3410 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3411 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3412 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3416 | { |
| 3417 | if (!PyUnicode_Check(unicode)) { |
| 3418 | PyErr_BadArgument(); |
| 3419 | return NULL; |
| 3420 | } |
| 3421 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3422 | PyUnicode_GET_SIZE(unicode), |
| 3423 | NULL, |
| 3424 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3428 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3429 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3430 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3431 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3432 | Py_ssize_t size, |
| 3433 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3434 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3435 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3436 | Py_ssize_t startinpos; |
| 3437 | Py_ssize_t endinpos; |
| 3438 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3439 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3440 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3441 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3442 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3443 | char* message; |
| 3444 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3445 | PyObject *errorHandler = NULL; |
| 3446 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3447 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3448 | /* Escaped strings will always be longer than the resulting |
| 3449 | 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] | 3450 | length after conversion to the true value. |
| 3451 | (but if the error callback returns a long replacement string |
| 3452 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3453 | v = _PyUnicode_New(size); |
| 3454 | if (v == NULL) |
| 3455 | goto onError; |
| 3456 | if (size == 0) |
| 3457 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3458 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3459 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3460 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3461 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3462 | while (s < end) { |
| 3463 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3464 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3465 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3466 | |
| 3467 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3468 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3469 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3470 | continue; |
| 3471 | } |
| 3472 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3473 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3474 | /* \ - Escapes */ |
| 3475 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3476 | c = *s++; |
| 3477 | if (s > end) |
| 3478 | c = '\0'; /* Invalid after \ */ |
| 3479 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3480 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3481 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3482 | case '\n': break; |
| 3483 | case '\\': *p++ = '\\'; break; |
| 3484 | case '\'': *p++ = '\''; break; |
| 3485 | case '\"': *p++ = '\"'; break; |
| 3486 | case 'b': *p++ = '\b'; break; |
| 3487 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3488 | case 't': *p++ = '\t'; break; |
| 3489 | case 'n': *p++ = '\n'; break; |
| 3490 | case 'r': *p++ = '\r'; break; |
| 3491 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3492 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3493 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3494 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3495 | case '0': case '1': case '2': case '3': |
| 3496 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3497 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3498 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3499 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3500 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3501 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3502 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3503 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3504 | break; |
| 3505 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3506 | /* hex escapes */ |
| 3507 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3508 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3509 | digits = 2; |
| 3510 | message = "truncated \\xXX escape"; |
| 3511 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3512 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3513 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3514 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3515 | digits = 4; |
| 3516 | message = "truncated \\uXXXX escape"; |
| 3517 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3518 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3519 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3520 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3521 | digits = 8; |
| 3522 | message = "truncated \\UXXXXXXXX escape"; |
| 3523 | hexescape: |
| 3524 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3525 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3526 | if (s+digits>end) { |
| 3527 | endinpos = size; |
| 3528 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3529 | errors, &errorHandler, |
| 3530 | "unicodeescape", "end of string in escape sequence", |
| 3531 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3532 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3533 | goto onError; |
| 3534 | goto nextByte; |
| 3535 | } |
| 3536 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3537 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3538 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3539 | endinpos = (s+i+1)-starts; |
| 3540 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3541 | errors, &errorHandler, |
| 3542 | "unicodeescape", message, |
| 3543 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3544 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3545 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3546 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3547 | } |
| 3548 | chr = (chr<<4) & ~0xF; |
| 3549 | if (c >= '0' && c <= '9') |
| 3550 | chr += c - '0'; |
| 3551 | else if (c >= 'a' && c <= 'f') |
| 3552 | chr += 10 + c - 'a'; |
| 3553 | else |
| 3554 | chr += 10 + c - 'A'; |
| 3555 | } |
| 3556 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3557 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3558 | /* _decoding_error will have already written into the |
| 3559 | target buffer. */ |
| 3560 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3561 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3562 | /* when we get here, chr is a 32-bit unicode character */ |
| 3563 | if (chr <= 0xffff) |
| 3564 | /* UCS-2 character */ |
| 3565 | *p++ = (Py_UNICODE) chr; |
| 3566 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3567 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3568 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3569 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3570 | *p++ = chr; |
| 3571 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3572 | chr -= 0x10000L; |
| 3573 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3574 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3575 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3576 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3577 | endinpos = s-starts; |
| 3578 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3579 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3580 | errors, &errorHandler, |
| 3581 | "unicodeescape", "illegal Unicode character", |
| 3582 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3583 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3584 | goto onError; |
| 3585 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3586 | break; |
| 3587 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3588 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3589 | case 'N': |
| 3590 | message = "malformed \\N character escape"; |
| 3591 | if (ucnhash_CAPI == NULL) { |
| 3592 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3593 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3594 | if (ucnhash_CAPI == NULL) |
| 3595 | goto ucnhashError; |
| 3596 | } |
| 3597 | if (*s == '{') { |
| 3598 | const char *start = s+1; |
| 3599 | /* look for the closing brace */ |
| 3600 | while (*s != '}' && s < end) |
| 3601 | s++; |
| 3602 | if (s > start && s < end && *s == '}') { |
| 3603 | /* found a name. look it up in the unicode database */ |
| 3604 | message = "unknown Unicode character name"; |
| 3605 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3606 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3607 | goto store; |
| 3608 | } |
| 3609 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3610 | endinpos = s-starts; |
| 3611 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3612 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3613 | errors, &errorHandler, |
| 3614 | "unicodeescape", message, |
| 3615 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3616 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3617 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3618 | break; |
| 3619 | |
| 3620 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3621 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3622 | message = "\\ at end of string"; |
| 3623 | s--; |
| 3624 | endinpos = s-starts; |
| 3625 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3626 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3627 | errors, &errorHandler, |
| 3628 | "unicodeescape", message, |
| 3629 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3630 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3631 | goto onError; |
| 3632 | } |
| 3633 | else { |
| 3634 | *p++ = '\\'; |
| 3635 | *p++ = (unsigned char)s[-1]; |
| 3636 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3637 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3638 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3639 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3640 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3641 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3642 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3643 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3644 | Py_XDECREF(errorHandler); |
| 3645 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3646 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3647 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3648 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3649 | PyErr_SetString( |
| 3650 | PyExc_UnicodeError, |
| 3651 | "\\N escapes not supported (can't load unicodedata module)" |
| 3652 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3653 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3654 | Py_XDECREF(errorHandler); |
| 3655 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3656 | return NULL; |
| 3657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3658 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3659 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3660 | Py_XDECREF(errorHandler); |
| 3661 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3662 | return NULL; |
| 3663 | } |
| 3664 | |
| 3665 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3666 | |
| 3667 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3668 | appropriate. |
| 3669 | |
| 3670 | */ |
| 3671 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3672 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3673 | Py_ssize_t size, |
| 3674 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3675 | { |
| 3676 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3677 | |
| 3678 | while (size-- > 0) { |
| 3679 | if (*s == ch) |
| 3680 | return s; |
| 3681 | s++; |
| 3682 | } |
| 3683 | |
| 3684 | return NULL; |
| 3685 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3686 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3687 | static const char *hexdigits = "0123456789abcdef"; |
| 3688 | |
| 3689 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3690 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3691 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3692 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3693 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3694 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3695 | #ifdef Py_UNICODE_WIDE |
| 3696 | const Py_ssize_t expandsize = 10; |
| 3697 | #else |
| 3698 | const Py_ssize_t expandsize = 6; |
| 3699 | #endif |
| 3700 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3701 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3702 | better to choose a different scheme. Perhaps scan the |
| 3703 | first N-chars of the string and allocate based on that size. |
| 3704 | */ |
| 3705 | /* Initial allocation is based on the longest-possible unichr |
| 3706 | escape. |
| 3707 | |
| 3708 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3709 | unichr, so in this case it's the longest unichr escape. In |
| 3710 | narrow (UTF-16) builds this is five chars per source unichr |
| 3711 | since there are two unichrs in the surrogate pair, so in narrow |
| 3712 | (UTF-16) builds it's not the longest unichr escape. |
| 3713 | |
| 3714 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3715 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3716 | escape. |
| 3717 | */ |
| 3718 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3719 | if (size == 0) |
| 3720 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3721 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3722 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3723 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3724 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3725 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3726 | 2 |
| 3727 | + expandsize*size |
| 3728 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3729 | if (repr == NULL) |
| 3730 | return NULL; |
| 3731 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3732 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3733 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3734 | while (size-- > 0) { |
| 3735 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3736 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3737 | /* Escape backslashes */ |
| 3738 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3739 | *p++ = '\\'; |
| 3740 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3741 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3742 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3743 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3744 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3745 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3746 | else if (ch >= 0x10000) { |
| 3747 | *p++ = '\\'; |
| 3748 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3749 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3750 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3751 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3752 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3753 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3754 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3755 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3756 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3757 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3758 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3759 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3760 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3761 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3762 | Py_UNICODE ch2; |
| 3763 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3764 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3765 | ch2 = *s++; |
| 3766 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 3767 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3768 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3769 | *p++ = '\\'; |
| 3770 | *p++ = 'U'; |
| 3771 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3772 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3773 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3774 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3775 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3776 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3777 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3778 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3779 | continue; |
| 3780 | } |
| 3781 | /* Fall through: isolated surrogates are copied as-is */ |
| 3782 | s--; |
| 3783 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3784 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3785 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3786 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3787 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3788 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3789 | *p++ = '\\'; |
| 3790 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3791 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3792 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3793 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3794 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3795 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3796 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3797 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3798 | else if (ch == '\t') { |
| 3799 | *p++ = '\\'; |
| 3800 | *p++ = 't'; |
| 3801 | } |
| 3802 | else if (ch == '\n') { |
| 3803 | *p++ = '\\'; |
| 3804 | *p++ = 'n'; |
| 3805 | } |
| 3806 | else if (ch == '\r') { |
| 3807 | *p++ = '\\'; |
| 3808 | *p++ = 'r'; |
| 3809 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3810 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3811 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3812 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3813 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3814 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3815 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3816 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3817 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3819 | /* Copy everything else as-is */ |
| 3820 | else |
| 3821 | *p++ = (char) ch; |
| 3822 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3823 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3824 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3825 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3826 | return NULL; |
| 3827 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3830 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3831 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3832 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3833 | if (!PyUnicode_Check(unicode)) { |
| 3834 | PyErr_BadArgument(); |
| 3835 | return NULL; |
| 3836 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3837 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3838 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3839 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3843 | |
| 3844 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3845 | Py_ssize_t size, |
| 3846 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3847 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3848 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3849 | Py_ssize_t startinpos; |
| 3850 | Py_ssize_t endinpos; |
| 3851 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3852 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3853 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3854 | const char *end; |
| 3855 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3856 | PyObject *errorHandler = NULL; |
| 3857 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3858 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3859 | /* Escaped strings will always be longer than the resulting |
| 3860 | 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] | 3861 | length after conversion to the true value. (But decoding error |
| 3862 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3863 | v = _PyUnicode_New(size); |
| 3864 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3865 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3866 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3867 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3868 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3869 | end = s + size; |
| 3870 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3871 | unsigned char c; |
| 3872 | Py_UCS4 x; |
| 3873 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3874 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3875 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3876 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3877 | if (*s != '\\') { |
| 3878 | *p++ = (unsigned char)*s++; |
| 3879 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3880 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3881 | startinpos = s-starts; |
| 3882 | |
| 3883 | /* \u-escapes are only interpreted iff the number of leading |
| 3884 | backslashes if odd */ |
| 3885 | bs = s; |
| 3886 | for (;s < end;) { |
| 3887 | if (*s != '\\') |
| 3888 | break; |
| 3889 | *p++ = (unsigned char)*s++; |
| 3890 | } |
| 3891 | if (((s - bs) & 1) == 0 || |
| 3892 | s >= end || |
| 3893 | (*s != 'u' && *s != 'U')) { |
| 3894 | continue; |
| 3895 | } |
| 3896 | p--; |
| 3897 | count = *s=='u' ? 4 : 8; |
| 3898 | s++; |
| 3899 | |
| 3900 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3901 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3902 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3903 | c = (unsigned char)*s; |
| 3904 | if (!ISXDIGIT(c)) { |
| 3905 | endinpos = s-starts; |
| 3906 | if (unicode_decode_call_errorhandler( |
| 3907 | errors, &errorHandler, |
| 3908 | "rawunicodeescape", "truncated \\uXXXX", |
| 3909 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3910 | &v, &outpos, &p)) |
| 3911 | goto onError; |
| 3912 | goto nextByte; |
| 3913 | } |
| 3914 | x = (x<<4) & ~0xF; |
| 3915 | if (c >= '0' && c <= '9') |
| 3916 | x += c - '0'; |
| 3917 | else if (c >= 'a' && c <= 'f') |
| 3918 | x += 10 + c - 'a'; |
| 3919 | else |
| 3920 | x += 10 + c - 'A'; |
| 3921 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3922 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3923 | /* UCS-2 character */ |
| 3924 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3925 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3926 | /* UCS-4 character. Either store directly, or as |
| 3927 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3928 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3929 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3930 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3931 | x -= 0x10000L; |
| 3932 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3933 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3934 | #endif |
| 3935 | } else { |
| 3936 | endinpos = s-starts; |
| 3937 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3938 | if (unicode_decode_call_errorhandler( |
| 3939 | errors, &errorHandler, |
| 3940 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3941 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3942 | &v, &outpos, &p)) |
| 3943 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3944 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3945 | nextByte: |
| 3946 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3947 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3948 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3949 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3950 | Py_XDECREF(errorHandler); |
| 3951 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3952 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3953 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3954 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3955 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3956 | Py_XDECREF(errorHandler); |
| 3957 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3958 | return NULL; |
| 3959 | } |
| 3960 | |
| 3961 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3962 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3963 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3964 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3965 | char *p; |
| 3966 | char *q; |
| 3967 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3968 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3969 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3970 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3971 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3972 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3973 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3974 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3975 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3976 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3977 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3978 | if (repr == NULL) |
| 3979 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3980 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3981 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3982 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3983 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3984 | while (size-- > 0) { |
| 3985 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3986 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3987 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3988 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3989 | *p++ = '\\'; |
| 3990 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3991 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3992 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3993 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3994 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3995 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3996 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3997 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3998 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3999 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4000 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4001 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4002 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4003 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 4004 | Py_UNICODE ch2; |
| 4005 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4006 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4007 | ch2 = *s++; |
| 4008 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4009 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4010 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4011 | *p++ = '\\'; |
| 4012 | *p++ = 'U'; |
| 4013 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 4014 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 4015 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 4016 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 4017 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 4018 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 4019 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 4020 | *p++ = hexdigits[ucs & 0xf]; |
| 4021 | continue; |
| 4022 | } |
| 4023 | /* Fall through: isolated surrogates are copied as-is */ |
| 4024 | s--; |
| 4025 | size++; |
| 4026 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4027 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4028 | /* Map 16-bit characters to '\uxxxx' */ |
| 4029 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4030 | *p++ = '\\'; |
| 4031 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4032 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4033 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4034 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4035 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4036 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4037 | /* Copy everything else as-is */ |
| 4038 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4039 | *p++ = (char) ch; |
| 4040 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4041 | size = p - q; |
| 4042 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4043 | assert(size > 0); |
| 4044 | if (_PyBytes_Resize(&repr, size) < 0) |
| 4045 | return NULL; |
| 4046 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 4050 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4051 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4052 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4053 | PyErr_BadArgument(); |
| 4054 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4055 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4056 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4057 | PyUnicode_GET_SIZE(unicode)); |
| 4058 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4059 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4062 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 4063 | |
| 4064 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4065 | Py_ssize_t size, |
| 4066 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4067 | { |
| 4068 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4069 | Py_ssize_t startinpos; |
| 4070 | Py_ssize_t endinpos; |
| 4071 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4072 | PyUnicodeObject *v; |
| 4073 | Py_UNICODE *p; |
| 4074 | const char *end; |
| 4075 | const char *reason; |
| 4076 | PyObject *errorHandler = NULL; |
| 4077 | PyObject *exc = NULL; |
| 4078 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4079 | #ifdef Py_UNICODE_WIDE |
| 4080 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4081 | #endif |
| 4082 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4083 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4084 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4085 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4086 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4087 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4088 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4089 | p = PyUnicode_AS_UNICODE(v); |
| 4090 | end = s + size; |
| 4091 | |
| 4092 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4093 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4094 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4095 | some malformed UCS-4 data. */ |
| 4096 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4097 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4098 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4099 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4100 | end-s < Py_UNICODE_SIZE |
| 4101 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4102 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4103 | startinpos = s - starts; |
| 4104 | if (end-s < Py_UNICODE_SIZE) { |
| 4105 | endinpos = end-starts; |
| 4106 | reason = "truncated input"; |
| 4107 | } |
| 4108 | else { |
| 4109 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4110 | reason = "illegal code point (> 0x10FFFF)"; |
| 4111 | } |
| 4112 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4113 | if (unicode_decode_call_errorhandler( |
| 4114 | errors, &errorHandler, |
| 4115 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4116 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4117 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4118 | goto onError; |
| 4119 | } |
| 4120 | } |
| 4121 | else { |
| 4122 | p++; |
| 4123 | s += Py_UNICODE_SIZE; |
| 4124 | } |
| 4125 | } |
| 4126 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4127 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4128 | goto onError; |
| 4129 | Py_XDECREF(errorHandler); |
| 4130 | Py_XDECREF(exc); |
| 4131 | return (PyObject *)v; |
| 4132 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4133 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4134 | Py_XDECREF(v); |
| 4135 | Py_XDECREF(errorHandler); |
| 4136 | Py_XDECREF(exc); |
| 4137 | return NULL; |
| 4138 | } |
| 4139 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4140 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4141 | |
| 4142 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4143 | Py_ssize_t size, |
| 4144 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4145 | { |
| 4146 | PyUnicodeObject *v; |
| 4147 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4148 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4150 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4151 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4152 | Py_UNICODE r = *(unsigned char*)s; |
| 4153 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4156 | v = _PyUnicode_New(size); |
| 4157 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4158 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4159 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4160 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4161 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4162 | e = s + size; |
| 4163 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4164 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4165 | unrolled_end = e - 4; |
| 4166 | while (s < unrolled_end) { |
| 4167 | p[0] = (unsigned char) s[0]; |
| 4168 | p[1] = (unsigned char) s[1]; |
| 4169 | p[2] = (unsigned char) s[2]; |
| 4170 | p[3] = (unsigned char) s[3]; |
| 4171 | s += 4; |
| 4172 | p += 4; |
| 4173 | } |
| 4174 | while (s < e) |
| 4175 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4176 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4177 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4178 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4179 | Py_XDECREF(v); |
| 4180 | return NULL; |
| 4181 | } |
| 4182 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4183 | /* create or adjust a UnicodeEncodeError */ |
| 4184 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4185 | const char *encoding, |
| 4186 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4187 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4188 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4189 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4190 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4191 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4192 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4193 | } |
| 4194 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4195 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4196 | goto onError; |
| 4197 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4198 | goto onError; |
| 4199 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4200 | goto onError; |
| 4201 | return; |
| 4202 | onError: |
| 4203 | Py_DECREF(*exceptionObject); |
| 4204 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4205 | } |
| 4206 | } |
| 4207 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4208 | /* raises a UnicodeEncodeError */ |
| 4209 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4210 | const char *encoding, |
| 4211 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4212 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4213 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4214 | { |
| 4215 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4216 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4217 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4218 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | /* error handling callback helper: |
| 4222 | build arguments, call the callback and check the arguments, |
| 4223 | put the result into newpos and return the replacement string, which |
| 4224 | has to be freed by the caller */ |
| 4225 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4226 | PyObject **errorHandler, |
| 4227 | const char *encoding, const char *reason, |
| 4228 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4229 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4230 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4231 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4232 | 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] | 4233 | |
| 4234 | PyObject *restuple; |
| 4235 | PyObject *resunicode; |
| 4236 | |
| 4237 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4238 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4239 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4240 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4244 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4245 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4246 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4247 | |
| 4248 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4249 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4250 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4251 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4252 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4253 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4254 | Py_DECREF(restuple); |
| 4255 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4256 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4257 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4258 | &resunicode, newpos)) { |
| 4259 | Py_DECREF(restuple); |
| 4260 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4261 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4262 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4263 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4264 | Py_DECREF(restuple); |
| 4265 | return NULL; |
| 4266 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4267 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4268 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4269 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4270 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4271 | Py_DECREF(restuple); |
| 4272 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4273 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4274 | Py_INCREF(resunicode); |
| 4275 | Py_DECREF(restuple); |
| 4276 | return resunicode; |
| 4277 | } |
| 4278 | |
| 4279 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4280 | Py_ssize_t size, |
| 4281 | const char *errors, |
| 4282 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4283 | { |
| 4284 | /* output object */ |
| 4285 | PyObject *res; |
| 4286 | /* pointers to the beginning and end+1 of input */ |
| 4287 | const Py_UNICODE *startp = p; |
| 4288 | const Py_UNICODE *endp = p + size; |
| 4289 | /* pointer to the beginning of the unencodable characters */ |
| 4290 | /* const Py_UNICODE *badp = NULL; */ |
| 4291 | /* pointer into the output */ |
| 4292 | char *str; |
| 4293 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4294 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4295 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4296 | 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] | 4297 | PyObject *errorHandler = NULL; |
| 4298 | PyObject *exc = NULL; |
| 4299 | /* the following variable is used for caching string comparisons |
| 4300 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4301 | int known_errorHandler = -1; |
| 4302 | |
| 4303 | /* allocate enough for a simple encoding without |
| 4304 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4305 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4306 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4307 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4308 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4309 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4310 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4311 | ressize = size; |
| 4312 | |
| 4313 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4314 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4315 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4316 | /* can we encode this? */ |
| 4317 | if (c<limit) { |
| 4318 | /* no overflow check, because we know that the space is enough */ |
| 4319 | *str++ = (char)c; |
| 4320 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4321 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4322 | else { |
| 4323 | Py_ssize_t unicodepos = p-startp; |
| 4324 | Py_ssize_t requiredsize; |
| 4325 | PyObject *repunicode; |
| 4326 | Py_ssize_t repsize; |
| 4327 | Py_ssize_t newpos; |
| 4328 | Py_ssize_t respos; |
| 4329 | Py_UNICODE *uni2; |
| 4330 | /* startpos for collecting unencodable chars */ |
| 4331 | const Py_UNICODE *collstart = p; |
| 4332 | const Py_UNICODE *collend = p; |
| 4333 | /* find all unecodable characters */ |
| 4334 | while ((collend < endp) && ((*collend)>=limit)) |
| 4335 | ++collend; |
| 4336 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4337 | if (known_errorHandler==-1) { |
| 4338 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4339 | known_errorHandler = 1; |
| 4340 | else if (!strcmp(errors, "replace")) |
| 4341 | known_errorHandler = 2; |
| 4342 | else if (!strcmp(errors, "ignore")) |
| 4343 | known_errorHandler = 3; |
| 4344 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4345 | known_errorHandler = 4; |
| 4346 | else |
| 4347 | known_errorHandler = 0; |
| 4348 | } |
| 4349 | switch (known_errorHandler) { |
| 4350 | case 1: /* strict */ |
| 4351 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4352 | goto onError; |
| 4353 | case 2: /* replace */ |
| 4354 | while (collstart++<collend) |
| 4355 | *str++ = '?'; /* fall through */ |
| 4356 | case 3: /* ignore */ |
| 4357 | p = collend; |
| 4358 | break; |
| 4359 | case 4: /* xmlcharrefreplace */ |
| 4360 | respos = str - PyBytes_AS_STRING(res); |
| 4361 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4362 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4363 | if (*p<10) |
| 4364 | repsize += 2+1+1; |
| 4365 | else if (*p<100) |
| 4366 | repsize += 2+2+1; |
| 4367 | else if (*p<1000) |
| 4368 | repsize += 2+3+1; |
| 4369 | else if (*p<10000) |
| 4370 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4371 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4372 | else |
| 4373 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4374 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4375 | else if (*p<100000) |
| 4376 | repsize += 2+5+1; |
| 4377 | else if (*p<1000000) |
| 4378 | repsize += 2+6+1; |
| 4379 | else |
| 4380 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4381 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4382 | } |
| 4383 | requiredsize = respos+repsize+(endp-collend); |
| 4384 | if (requiredsize > ressize) { |
| 4385 | if (requiredsize<2*ressize) |
| 4386 | requiredsize = 2*ressize; |
| 4387 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4388 | goto onError; |
| 4389 | str = PyBytes_AS_STRING(res) + respos; |
| 4390 | ressize = requiredsize; |
| 4391 | } |
| 4392 | /* generate replacement (temporarily (mis)uses p) */ |
| 4393 | for (p = collstart; p < collend; ++p) { |
| 4394 | str += sprintf(str, "&#%d;", (int)*p); |
| 4395 | } |
| 4396 | p = collend; |
| 4397 | break; |
| 4398 | default: |
| 4399 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4400 | encoding, reason, startp, size, &exc, |
| 4401 | collstart-startp, collend-startp, &newpos); |
| 4402 | if (repunicode == NULL) |
| 4403 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4404 | if (PyBytes_Check(repunicode)) { |
| 4405 | /* Directly copy bytes result to output. */ |
| 4406 | repsize = PyBytes_Size(repunicode); |
| 4407 | if (repsize > 1) { |
| 4408 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4409 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4410 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4411 | Py_DECREF(repunicode); |
| 4412 | goto onError; |
| 4413 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4414 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4415 | ressize += repsize-1; |
| 4416 | } |
| 4417 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4418 | str += repsize; |
| 4419 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4420 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4421 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4422 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4423 | /* need more space? (at least enough for what we |
| 4424 | have+the replacement+the rest of the string, so |
| 4425 | we won't have to check space for encodable characters) */ |
| 4426 | respos = str - PyBytes_AS_STRING(res); |
| 4427 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4428 | requiredsize = respos+repsize+(endp-collend); |
| 4429 | if (requiredsize > ressize) { |
| 4430 | if (requiredsize<2*ressize) |
| 4431 | requiredsize = 2*ressize; |
| 4432 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4433 | Py_DECREF(repunicode); |
| 4434 | goto onError; |
| 4435 | } |
| 4436 | str = PyBytes_AS_STRING(res) + respos; |
| 4437 | ressize = requiredsize; |
| 4438 | } |
| 4439 | /* check if there is anything unencodable in the replacement |
| 4440 | and copy it to the output */ |
| 4441 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4442 | c = *uni2; |
| 4443 | if (c >= limit) { |
| 4444 | raise_encode_exception(&exc, encoding, startp, size, |
| 4445 | unicodepos, unicodepos+1, reason); |
| 4446 | Py_DECREF(repunicode); |
| 4447 | goto onError; |
| 4448 | } |
| 4449 | *str = (char)c; |
| 4450 | } |
| 4451 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4452 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4453 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4454 | } |
| 4455 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4456 | /* Resize if we allocated to much */ |
| 4457 | size = str - PyBytes_AS_STRING(res); |
| 4458 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4459 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4460 | if (_PyBytes_Resize(&res, size) < 0) |
| 4461 | goto onError; |
| 4462 | } |
| 4463 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4464 | Py_XDECREF(errorHandler); |
| 4465 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4466 | return res; |
| 4467 | |
| 4468 | onError: |
| 4469 | Py_XDECREF(res); |
| 4470 | Py_XDECREF(errorHandler); |
| 4471 | Py_XDECREF(exc); |
| 4472 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4475 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4476 | Py_ssize_t size, |
| 4477 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4478 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4479 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
| 4482 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4483 | { |
| 4484 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4485 | PyErr_BadArgument(); |
| 4486 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4487 | } |
| 4488 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4489 | PyUnicode_GET_SIZE(unicode), |
| 4490 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4494 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4495 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4496 | Py_ssize_t size, |
| 4497 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4498 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4499 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4500 | PyUnicodeObject *v; |
| 4501 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4502 | Py_ssize_t startinpos; |
| 4503 | Py_ssize_t endinpos; |
| 4504 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4505 | const char *e; |
| 4506 | PyObject *errorHandler = NULL; |
| 4507 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4508 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4509 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4510 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4511 | Py_UNICODE r = *(unsigned char*)s; |
| 4512 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4513 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4514 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4515 | v = _PyUnicode_New(size); |
| 4516 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4517 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4518 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4519 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4520 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4521 | e = s + size; |
| 4522 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4523 | register unsigned char c = (unsigned char)*s; |
| 4524 | if (c < 128) { |
| 4525 | *p++ = c; |
| 4526 | ++s; |
| 4527 | } |
| 4528 | else { |
| 4529 | startinpos = s-starts; |
| 4530 | endinpos = startinpos + 1; |
| 4531 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4532 | if (unicode_decode_call_errorhandler( |
| 4533 | errors, &errorHandler, |
| 4534 | "ascii", "ordinal not in range(128)", |
| 4535 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4536 | &v, &outpos, &p)) |
| 4537 | goto onError; |
| 4538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4539 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4540 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4541 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4542 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4543 | Py_XDECREF(errorHandler); |
| 4544 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4545 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4546 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4547 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4548 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4549 | Py_XDECREF(errorHandler); |
| 4550 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4551 | return NULL; |
| 4552 | } |
| 4553 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4554 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4555 | Py_ssize_t size, |
| 4556 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4557 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4558 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4562 | { |
| 4563 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4564 | PyErr_BadArgument(); |
| 4565 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4566 | } |
| 4567 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4568 | PyUnicode_GET_SIZE(unicode), |
| 4569 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4572 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4573 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4574 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4575 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4576 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4577 | #define NEED_RETRY |
| 4578 | #endif |
| 4579 | |
| 4580 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4581 | a) it assumes an incomplete character consists of a single byte, and |
| 4582 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4583 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4584 | |
| 4585 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4586 | { |
| 4587 | const char *curr = s + offset; |
| 4588 | |
| 4589 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4590 | const char *prev = CharPrev(s, curr); |
| 4591 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4592 | } |
| 4593 | return 0; |
| 4594 | } |
| 4595 | |
| 4596 | /* |
| 4597 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4598 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4599 | */ |
| 4600 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4601 | const char *s, /* MBCS string */ |
| 4602 | int size, /* sizeof MBCS string */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4603 | int final, |
| 4604 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4605 | { |
| 4606 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4607 | Py_ssize_t n; |
| 4608 | DWORD usize; |
| 4609 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4610 | |
| 4611 | assert(size >= 0); |
| 4612 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4613 | /* check and handle 'errors' arg */ |
| 4614 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 4615 | flags = MB_ERR_INVALID_CHARS; |
| 4616 | else if (strcmp(errors, "ignore")==0) |
| 4617 | flags = 0; |
| 4618 | else { |
| 4619 | PyErr_Format(PyExc_ValueError, |
| 4620 | "mbcs encoding does not support errors='%s'", |
| 4621 | errors); |
| 4622 | return -1; |
| 4623 | } |
| 4624 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4625 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4626 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4627 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4628 | |
| 4629 | /* First get the size of the result */ |
| 4630 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4631 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 4632 | if (usize==0) |
| 4633 | goto mbcs_decode_error; |
| 4634 | } else |
| 4635 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4636 | |
| 4637 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4638 | /* Create unicode object */ |
| 4639 | *v = _PyUnicode_New(usize); |
| 4640 | if (*v == NULL) |
| 4641 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4642 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4643 | } |
| 4644 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4645 | /* Extend unicode object */ |
| 4646 | n = PyUnicode_GET_SIZE(*v); |
| 4647 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4648 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4649 | } |
| 4650 | |
| 4651 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4652 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4653 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4654 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 4655 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4656 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4657 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4658 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4659 | |
| 4660 | mbcs_decode_error: |
| 4661 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 4662 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 4663 | windows error |
| 4664 | */ |
| 4665 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 4666 | /* Ideally, we should get reason from FormatMessage - this |
| 4667 | is the Windows 2000 English version of the message |
| 4668 | */ |
| 4669 | PyObject *exc = NULL; |
| 4670 | const char *reason = "No mapping for the Unicode character exists " |
| 4671 | "in the target multi-byte code page."; |
| 4672 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 4673 | if (exc != NULL) { |
| 4674 | PyCodec_StrictErrors(exc); |
| 4675 | Py_DECREF(exc); |
| 4676 | } |
| 4677 | } else { |
| 4678 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4679 | } |
| 4680 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4684 | Py_ssize_t size, |
| 4685 | const char *errors, |
| 4686 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4687 | { |
| 4688 | PyUnicodeObject *v = NULL; |
| 4689 | int done; |
| 4690 | |
| 4691 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4692 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4693 | |
| 4694 | #ifdef NEED_RETRY |
| 4695 | retry: |
| 4696 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4697 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4698 | else |
| 4699 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4700 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4701 | |
| 4702 | if (done < 0) { |
| 4703 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4704 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4708 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4709 | |
| 4710 | #ifdef NEED_RETRY |
| 4711 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4712 | s += done; |
| 4713 | size -= done; |
| 4714 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4715 | } |
| 4716 | #endif |
| 4717 | |
| 4718 | return (PyObject *)v; |
| 4719 | } |
| 4720 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4721 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4722 | Py_ssize_t size, |
| 4723 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4724 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4725 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4726 | } |
| 4727 | |
| 4728 | /* |
| 4729 | * Convert unicode into string object (MBCS). |
| 4730 | * Returns 0 if succeed, -1 otherwise. |
| 4731 | */ |
| 4732 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4733 | const Py_UNICODE *p, /* unicode */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4734 | int size, /* size of unicode */ |
| 4735 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4736 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4737 | BOOL usedDefaultChar = FALSE; |
| 4738 | BOOL *pusedDefaultChar; |
| 4739 | int mbcssize; |
| 4740 | Py_ssize_t n; |
| 4741 | PyObject *exc = NULL; |
| 4742 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4743 | |
| 4744 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4745 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4746 | /* check and handle 'errors' arg */ |
| 4747 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 4748 | flags = WC_NO_BEST_FIT_CHARS; |
| 4749 | pusedDefaultChar = &usedDefaultChar; |
| 4750 | } else if (strcmp(errors, "replace")==0) { |
| 4751 | flags = 0; |
| 4752 | pusedDefaultChar = NULL; |
| 4753 | } else { |
| 4754 | PyErr_Format(PyExc_ValueError, |
| 4755 | "mbcs encoding does not support errors='%s'", |
| 4756 | errors); |
| 4757 | return -1; |
| 4758 | } |
| 4759 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4760 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4761 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4762 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 4763 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4764 | if (mbcssize == 0) { |
| 4765 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4766 | return -1; |
| 4767 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4768 | /* If we used a default char, then we failed! */ |
| 4769 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4770 | goto mbcs_encode_error; |
| 4771 | } else { |
| 4772 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4775 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4776 | /* Create string object */ |
| 4777 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4778 | if (*repr == NULL) |
| 4779 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4780 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4781 | } |
| 4782 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4783 | /* Extend string object */ |
| 4784 | n = PyBytes_Size(*repr); |
| 4785 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4786 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
| 4789 | /* Do the conversion */ |
| 4790 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4791 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4792 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 4793 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4794 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4795 | return -1; |
| 4796 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4797 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4798 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4799 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4800 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4801 | |
| 4802 | mbcs_encode_error: |
| 4803 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 4804 | Py_XDECREF(exc); |
| 4805 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4809 | Py_ssize_t size, |
| 4810 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4811 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4812 | PyObject *repr = NULL; |
| 4813 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4814 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4815 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4816 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4817 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4818 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4819 | else |
| 4820 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4821 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4822 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4823 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4824 | Py_XDECREF(repr); |
| 4825 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4826 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4827 | |
| 4828 | #ifdef NEED_RETRY |
| 4829 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4830 | p += INT_MAX; |
| 4831 | size -= INT_MAX; |
| 4832 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4833 | } |
| 4834 | #endif |
| 4835 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4836 | return repr; |
| 4837 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4838 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4839 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4840 | { |
| 4841 | if (!PyUnicode_Check(unicode)) { |
| 4842 | PyErr_BadArgument(); |
| 4843 | return NULL; |
| 4844 | } |
| 4845 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4846 | PyUnicode_GET_SIZE(unicode), |
| 4847 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4850 | #undef NEED_RETRY |
| 4851 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4852 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4854 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4855 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4856 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4857 | Py_ssize_t size, |
| 4858 | PyObject *mapping, |
| 4859 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4860 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4861 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4862 | Py_ssize_t startinpos; |
| 4863 | Py_ssize_t endinpos; |
| 4864 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4865 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4866 | PyUnicodeObject *v; |
| 4867 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4868 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4869 | PyObject *errorHandler = NULL; |
| 4870 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4871 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4872 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4873 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4874 | /* Default to Latin-1 */ |
| 4875 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4876 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4877 | |
| 4878 | v = _PyUnicode_New(size); |
| 4879 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4880 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4881 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4882 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4883 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4884 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4885 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4886 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4887 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4888 | while (s < e) { |
| 4889 | unsigned char ch = *s; |
| 4890 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4891 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4892 | if (ch < maplen) |
| 4893 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4894 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4895 | if (x == 0xfffe) { |
| 4896 | /* undefined mapping */ |
| 4897 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4898 | startinpos = s-starts; |
| 4899 | endinpos = startinpos+1; |
| 4900 | if (unicode_decode_call_errorhandler( |
| 4901 | errors, &errorHandler, |
| 4902 | "charmap", "character maps to <undefined>", |
| 4903 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4904 | &v, &outpos, &p)) { |
| 4905 | goto onError; |
| 4906 | } |
| 4907 | continue; |
| 4908 | } |
| 4909 | *p++ = x; |
| 4910 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4911 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4912 | } |
| 4913 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4914 | while (s < e) { |
| 4915 | unsigned char ch = *s; |
| 4916 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4917 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4918 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4919 | w = PyLong_FromLong((long)ch); |
| 4920 | if (w == NULL) |
| 4921 | goto onError; |
| 4922 | x = PyObject_GetItem(mapping, w); |
| 4923 | Py_DECREF(w); |
| 4924 | if (x == NULL) { |
| 4925 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4926 | /* No mapping found means: mapping is undefined. */ |
| 4927 | PyErr_Clear(); |
| 4928 | x = Py_None; |
| 4929 | Py_INCREF(x); |
| 4930 | } else |
| 4931 | goto onError; |
| 4932 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4933 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4934 | /* Apply mapping */ |
| 4935 | if (PyLong_Check(x)) { |
| 4936 | long value = PyLong_AS_LONG(x); |
| 4937 | if (value < 0 || value > 65535) { |
| 4938 | PyErr_SetString(PyExc_TypeError, |
| 4939 | "character mapping must be in range(65536)"); |
| 4940 | Py_DECREF(x); |
| 4941 | goto onError; |
| 4942 | } |
| 4943 | *p++ = (Py_UNICODE)value; |
| 4944 | } |
| 4945 | else if (x == Py_None) { |
| 4946 | /* undefined mapping */ |
| 4947 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4948 | startinpos = s-starts; |
| 4949 | endinpos = startinpos+1; |
| 4950 | if (unicode_decode_call_errorhandler( |
| 4951 | errors, &errorHandler, |
| 4952 | "charmap", "character maps to <undefined>", |
| 4953 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4954 | &v, &outpos, &p)) { |
| 4955 | Py_DECREF(x); |
| 4956 | goto onError; |
| 4957 | } |
| 4958 | Py_DECREF(x); |
| 4959 | continue; |
| 4960 | } |
| 4961 | else if (PyUnicode_Check(x)) { |
| 4962 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4963 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4964 | if (targetsize == 1) |
| 4965 | /* 1-1 mapping */ |
| 4966 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4967 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4968 | else if (targetsize > 1) { |
| 4969 | /* 1-n mapping */ |
| 4970 | if (targetsize > extrachars) { |
| 4971 | /* resize first */ |
| 4972 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4973 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4974 | (targetsize << 2); |
| 4975 | extrachars += needed; |
| 4976 | /* XXX overflow detection missing */ |
| 4977 | if (_PyUnicode_Resize(&v, |
| 4978 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4979 | Py_DECREF(x); |
| 4980 | goto onError; |
| 4981 | } |
| 4982 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4983 | } |
| 4984 | Py_UNICODE_COPY(p, |
| 4985 | PyUnicode_AS_UNICODE(x), |
| 4986 | targetsize); |
| 4987 | p += targetsize; |
| 4988 | extrachars -= targetsize; |
| 4989 | } |
| 4990 | /* 1-0 mapping: skip the character */ |
| 4991 | } |
| 4992 | else { |
| 4993 | /* wrong return value */ |
| 4994 | PyErr_SetString(PyExc_TypeError, |
| 4995 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4996 | Py_DECREF(x); |
| 4997 | goto onError; |
| 4998 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4999 | Py_DECREF(x); |
| 5000 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5001 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5002 | } |
| 5003 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5004 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 5005 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5006 | Py_XDECREF(errorHandler); |
| 5007 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5008 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5009 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5010 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5011 | Py_XDECREF(errorHandler); |
| 5012 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5013 | Py_XDECREF(v); |
| 5014 | return NULL; |
| 5015 | } |
| 5016 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5017 | /* Charmap encoding: the lookup table */ |
| 5018 | |
| 5019 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5020 | PyObject_HEAD |
| 5021 | unsigned char level1[32]; |
| 5022 | int count2, count3; |
| 5023 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5024 | }; |
| 5025 | |
| 5026 | static PyObject* |
| 5027 | encoding_map_size(PyObject *obj, PyObject* args) |
| 5028 | { |
| 5029 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5030 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5031 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5035 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5036 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 5037 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5038 | }; |
| 5039 | |
| 5040 | static void |
| 5041 | encoding_map_dealloc(PyObject* o) |
| 5042 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5043 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5047 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5048 | "EncodingMap", /*tp_name*/ |
| 5049 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 5050 | 0, /*tp_itemsize*/ |
| 5051 | /* methods */ |
| 5052 | encoding_map_dealloc, /*tp_dealloc*/ |
| 5053 | 0, /*tp_print*/ |
| 5054 | 0, /*tp_getattr*/ |
| 5055 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 5056 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5057 | 0, /*tp_repr*/ |
| 5058 | 0, /*tp_as_number*/ |
| 5059 | 0, /*tp_as_sequence*/ |
| 5060 | 0, /*tp_as_mapping*/ |
| 5061 | 0, /*tp_hash*/ |
| 5062 | 0, /*tp_call*/ |
| 5063 | 0, /*tp_str*/ |
| 5064 | 0, /*tp_getattro*/ |
| 5065 | 0, /*tp_setattro*/ |
| 5066 | 0, /*tp_as_buffer*/ |
| 5067 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5068 | 0, /*tp_doc*/ |
| 5069 | 0, /*tp_traverse*/ |
| 5070 | 0, /*tp_clear*/ |
| 5071 | 0, /*tp_richcompare*/ |
| 5072 | 0, /*tp_weaklistoffset*/ |
| 5073 | 0, /*tp_iter*/ |
| 5074 | 0, /*tp_iternext*/ |
| 5075 | encoding_map_methods, /*tp_methods*/ |
| 5076 | 0, /*tp_members*/ |
| 5077 | 0, /*tp_getset*/ |
| 5078 | 0, /*tp_base*/ |
| 5079 | 0, /*tp_dict*/ |
| 5080 | 0, /*tp_descr_get*/ |
| 5081 | 0, /*tp_descr_set*/ |
| 5082 | 0, /*tp_dictoffset*/ |
| 5083 | 0, /*tp_init*/ |
| 5084 | 0, /*tp_alloc*/ |
| 5085 | 0, /*tp_new*/ |
| 5086 | 0, /*tp_free*/ |
| 5087 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5088 | }; |
| 5089 | |
| 5090 | PyObject* |
| 5091 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 5092 | { |
| 5093 | Py_UNICODE *decode; |
| 5094 | PyObject *result; |
| 5095 | struct encoding_map *mresult; |
| 5096 | int i; |
| 5097 | int need_dict = 0; |
| 5098 | unsigned char level1[32]; |
| 5099 | unsigned char level2[512]; |
| 5100 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 5101 | int count2 = 0, count3 = 0; |
| 5102 | |
| 5103 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 5104 | PyErr_BadArgument(); |
| 5105 | return NULL; |
| 5106 | } |
| 5107 | decode = PyUnicode_AS_UNICODE(string); |
| 5108 | memset(level1, 0xFF, sizeof level1); |
| 5109 | memset(level2, 0xFF, sizeof level2); |
| 5110 | |
| 5111 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 5112 | or if there are non-BMP characters, we need to use |
| 5113 | a mapping dictionary. */ |
| 5114 | if (decode[0] != 0) |
| 5115 | need_dict = 1; |
| 5116 | for (i = 1; i < 256; i++) { |
| 5117 | int l1, l2; |
| 5118 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5119 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5120 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5121 | #endif |
| 5122 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5123 | need_dict = 1; |
| 5124 | break; |
| 5125 | } |
| 5126 | if (decode[i] == 0xFFFE) |
| 5127 | /* unmapped character */ |
| 5128 | continue; |
| 5129 | l1 = decode[i] >> 11; |
| 5130 | l2 = decode[i] >> 7; |
| 5131 | if (level1[l1] == 0xFF) |
| 5132 | level1[l1] = count2++; |
| 5133 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5134 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
| 5137 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 5138 | need_dict = 1; |
| 5139 | |
| 5140 | if (need_dict) { |
| 5141 | PyObject *result = PyDict_New(); |
| 5142 | PyObject *key, *value; |
| 5143 | if (!result) |
| 5144 | return NULL; |
| 5145 | for (i = 0; i < 256; i++) { |
| 5146 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5147 | key = PyLong_FromLong(decode[i]); |
| 5148 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5149 | if (!key || !value) |
| 5150 | goto failed1; |
| 5151 | if (PyDict_SetItem(result, key, value) == -1) |
| 5152 | goto failed1; |
| 5153 | Py_DECREF(key); |
| 5154 | Py_DECREF(value); |
| 5155 | } |
| 5156 | return result; |
| 5157 | failed1: |
| 5158 | Py_XDECREF(key); |
| 5159 | Py_XDECREF(value); |
| 5160 | Py_DECREF(result); |
| 5161 | return NULL; |
| 5162 | } |
| 5163 | |
| 5164 | /* Create a three-level trie */ |
| 5165 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5166 | 16*count2 + 128*count3 - 1); |
| 5167 | if (!result) |
| 5168 | return PyErr_NoMemory(); |
| 5169 | PyObject_Init(result, &EncodingMapType); |
| 5170 | mresult = (struct encoding_map*)result; |
| 5171 | mresult->count2 = count2; |
| 5172 | mresult->count3 = count3; |
| 5173 | mlevel1 = mresult->level1; |
| 5174 | mlevel2 = mresult->level23; |
| 5175 | mlevel3 = mresult->level23 + 16*count2; |
| 5176 | memcpy(mlevel1, level1, 32); |
| 5177 | memset(mlevel2, 0xFF, 16*count2); |
| 5178 | memset(mlevel3, 0, 128*count3); |
| 5179 | count3 = 0; |
| 5180 | for (i = 1; i < 256; i++) { |
| 5181 | int o1, o2, o3, i2, i3; |
| 5182 | if (decode[i] == 0xFFFE) |
| 5183 | /* unmapped character */ |
| 5184 | continue; |
| 5185 | o1 = decode[i]>>11; |
| 5186 | o2 = (decode[i]>>7) & 0xF; |
| 5187 | i2 = 16*mlevel1[o1] + o2; |
| 5188 | if (mlevel2[i2] == 0xFF) |
| 5189 | mlevel2[i2] = count3++; |
| 5190 | o3 = decode[i] & 0x7F; |
| 5191 | i3 = 128*mlevel2[i2] + o3; |
| 5192 | mlevel3[i3] = i; |
| 5193 | } |
| 5194 | return result; |
| 5195 | } |
| 5196 | |
| 5197 | static int |
| 5198 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5199 | { |
| 5200 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5201 | int l1 = c>>11; |
| 5202 | int l2 = (c>>7) & 0xF; |
| 5203 | int l3 = c & 0x7F; |
| 5204 | int i; |
| 5205 | |
| 5206 | #ifdef Py_UNICODE_WIDE |
| 5207 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5208 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5209 | } |
| 5210 | #endif |
| 5211 | if (c == 0) |
| 5212 | return 0; |
| 5213 | /* level 1*/ |
| 5214 | i = map->level1[l1]; |
| 5215 | if (i == 0xFF) { |
| 5216 | return -1; |
| 5217 | } |
| 5218 | /* level 2*/ |
| 5219 | i = map->level23[16*i+l2]; |
| 5220 | if (i == 0xFF) { |
| 5221 | return -1; |
| 5222 | } |
| 5223 | /* level 3 */ |
| 5224 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5225 | if (i == 0) { |
| 5226 | return -1; |
| 5227 | } |
| 5228 | return i; |
| 5229 | } |
| 5230 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5231 | /* Lookup the character ch in the mapping. If the character |
| 5232 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5233 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5234 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5235 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5236 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5237 | PyObject *x; |
| 5238 | |
| 5239 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5240 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5241 | x = PyObject_GetItem(mapping, w); |
| 5242 | Py_DECREF(w); |
| 5243 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5244 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5245 | /* No mapping found means: mapping is undefined. */ |
| 5246 | PyErr_Clear(); |
| 5247 | x = Py_None; |
| 5248 | Py_INCREF(x); |
| 5249 | return x; |
| 5250 | } else |
| 5251 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5252 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5253 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5254 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5255 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5256 | long value = PyLong_AS_LONG(x); |
| 5257 | if (value < 0 || value > 255) { |
| 5258 | PyErr_SetString(PyExc_TypeError, |
| 5259 | "character mapping must be in range(256)"); |
| 5260 | Py_DECREF(x); |
| 5261 | return NULL; |
| 5262 | } |
| 5263 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5264 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5265 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5266 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5267 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5268 | /* wrong return value */ |
| 5269 | PyErr_Format(PyExc_TypeError, |
| 5270 | "character mapping must return integer, bytes or None, not %.400s", |
| 5271 | x->ob_type->tp_name); |
| 5272 | Py_DECREF(x); |
| 5273 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5274 | } |
| 5275 | } |
| 5276 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5277 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5278 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5279 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5280 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5281 | /* exponentially overallocate to minimize reallocations */ |
| 5282 | if (requiredsize < 2*outsize) |
| 5283 | requiredsize = 2*outsize; |
| 5284 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5285 | return -1; |
| 5286 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5289 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5290 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5291 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5292 | /* 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] | 5293 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5294 | space is available. Return a new reference to the object that |
| 5295 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5296 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5297 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5298 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5299 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5300 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5301 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5302 | PyObject *rep; |
| 5303 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5304 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5305 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5306 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5307 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5308 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5309 | if (res == -1) |
| 5310 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5311 | if (outsize<requiredsize) |
| 5312 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5313 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5314 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5315 | outstart[(*outpos)++] = (char)res; |
| 5316 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5320 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5321 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5322 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5323 | Py_DECREF(rep); |
| 5324 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5325 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5326 | if (PyLong_Check(rep)) { |
| 5327 | Py_ssize_t requiredsize = *outpos+1; |
| 5328 | if (outsize<requiredsize) |
| 5329 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5330 | Py_DECREF(rep); |
| 5331 | return enc_EXCEPTION; |
| 5332 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5333 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5334 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5335 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5336 | else { |
| 5337 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5338 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5339 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5340 | if (outsize<requiredsize) |
| 5341 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5342 | Py_DECREF(rep); |
| 5343 | return enc_EXCEPTION; |
| 5344 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5345 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5346 | memcpy(outstart + *outpos, repchars, repsize); |
| 5347 | *outpos += repsize; |
| 5348 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5349 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5350 | Py_DECREF(rep); |
| 5351 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | /* handle an error in PyUnicode_EncodeCharmap |
| 5355 | Return 0 on success, -1 on error */ |
| 5356 | static |
| 5357 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5358 | 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] | 5359 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5360 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5361 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5362 | { |
| 5363 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5364 | Py_ssize_t repsize; |
| 5365 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5366 | Py_UNICODE *uni2; |
| 5367 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5368 | Py_ssize_t collstartpos = *inpos; |
| 5369 | Py_ssize_t collendpos = *inpos+1; |
| 5370 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5371 | char *encoding = "charmap"; |
| 5372 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5373 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5374 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5375 | /* find all unencodable characters */ |
| 5376 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5377 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5378 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5380 | if (res != -1) |
| 5381 | break; |
| 5382 | ++collendpos; |
| 5383 | continue; |
| 5384 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5385 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5386 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5387 | if (rep==NULL) |
| 5388 | return -1; |
| 5389 | else if (rep!=Py_None) { |
| 5390 | Py_DECREF(rep); |
| 5391 | break; |
| 5392 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5393 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5394 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5395 | } |
| 5396 | /* cache callback name lookup |
| 5397 | * (if not done yet, i.e. it's the first error) */ |
| 5398 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5399 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5400 | *known_errorHandler = 1; |
| 5401 | else if (!strcmp(errors, "replace")) |
| 5402 | *known_errorHandler = 2; |
| 5403 | else if (!strcmp(errors, "ignore")) |
| 5404 | *known_errorHandler = 3; |
| 5405 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5406 | *known_errorHandler = 4; |
| 5407 | else |
| 5408 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5409 | } |
| 5410 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5411 | case 1: /* strict */ |
| 5412 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5413 | return -1; |
| 5414 | case 2: /* replace */ |
| 5415 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5416 | x = charmapencode_output('?', mapping, res, respos); |
| 5417 | if (x==enc_EXCEPTION) { |
| 5418 | return -1; |
| 5419 | } |
| 5420 | else if (x==enc_FAILED) { |
| 5421 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5422 | return -1; |
| 5423 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5424 | } |
| 5425 | /* fall through */ |
| 5426 | case 3: /* ignore */ |
| 5427 | *inpos = collendpos; |
| 5428 | break; |
| 5429 | case 4: /* xmlcharrefreplace */ |
| 5430 | /* generate replacement (temporarily (mis)uses p) */ |
| 5431 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5432 | char buffer[2+29+1+1]; |
| 5433 | char *cp; |
| 5434 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5435 | for (cp = buffer; *cp; ++cp) { |
| 5436 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5437 | if (x==enc_EXCEPTION) |
| 5438 | return -1; |
| 5439 | else if (x==enc_FAILED) { |
| 5440 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5441 | return -1; |
| 5442 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5443 | } |
| 5444 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5445 | *inpos = collendpos; |
| 5446 | break; |
| 5447 | default: |
| 5448 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5449 | encoding, reason, p, size, exceptionObject, |
| 5450 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5451 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5452 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5453 | if (PyBytes_Check(repunicode)) { |
| 5454 | /* Directly copy bytes result to output. */ |
| 5455 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5456 | Py_ssize_t requiredsize; |
| 5457 | repsize = PyBytes_Size(repunicode); |
| 5458 | requiredsize = *respos + repsize; |
| 5459 | if (requiredsize > outsize) |
| 5460 | /* Make room for all additional bytes. */ |
| 5461 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5462 | Py_DECREF(repunicode); |
| 5463 | return -1; |
| 5464 | } |
| 5465 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5466 | PyBytes_AsString(repunicode), repsize); |
| 5467 | *respos += repsize; |
| 5468 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5469 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5470 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5471 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5472 | /* generate replacement */ |
| 5473 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5474 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5475 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5476 | if (x==enc_EXCEPTION) { |
| 5477 | return -1; |
| 5478 | } |
| 5479 | else if (x==enc_FAILED) { |
| 5480 | Py_DECREF(repunicode); |
| 5481 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5482 | return -1; |
| 5483 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5484 | } |
| 5485 | *inpos = newpos; |
| 5486 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5487 | } |
| 5488 | return 0; |
| 5489 | } |
| 5490 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5491 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5492 | Py_ssize_t size, |
| 5493 | PyObject *mapping, |
| 5494 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5495 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5496 | /* output object */ |
| 5497 | PyObject *res = NULL; |
| 5498 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5499 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5500 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5501 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5502 | PyObject *errorHandler = NULL; |
| 5503 | PyObject *exc = NULL; |
| 5504 | /* the following variable is used for caching string comparisons |
| 5505 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5506 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5507 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5508 | |
| 5509 | /* Default to Latin-1 */ |
| 5510 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5511 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5512 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5513 | /* allocate enough for a simple encoding without |
| 5514 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5515 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5516 | if (res == NULL) |
| 5517 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5518 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5519 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5520 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5521 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5522 | /* try to encode it */ |
| 5523 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5524 | if (x==enc_EXCEPTION) /* error */ |
| 5525 | goto onError; |
| 5526 | if (x==enc_FAILED) { /* unencodable character */ |
| 5527 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5528 | &exc, |
| 5529 | &known_errorHandler, &errorHandler, errors, |
| 5530 | &res, &respos)) { |
| 5531 | goto onError; |
| 5532 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5533 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5534 | else |
| 5535 | /* done with this character => adjust input position */ |
| 5536 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5537 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5539 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5540 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5541 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5542 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5543 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5544 | Py_XDECREF(exc); |
| 5545 | Py_XDECREF(errorHandler); |
| 5546 | return res; |
| 5547 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5548 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5549 | Py_XDECREF(res); |
| 5550 | Py_XDECREF(exc); |
| 5551 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5552 | return NULL; |
| 5553 | } |
| 5554 | |
| 5555 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5556 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5557 | { |
| 5558 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5559 | PyErr_BadArgument(); |
| 5560 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5561 | } |
| 5562 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5563 | PyUnicode_GET_SIZE(unicode), |
| 5564 | mapping, |
| 5565 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5566 | } |
| 5567 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5568 | /* create or adjust a UnicodeTranslateError */ |
| 5569 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5570 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5571 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5572 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5573 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5574 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5575 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5576 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5577 | } |
| 5578 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5579 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5580 | goto onError; |
| 5581 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5582 | goto onError; |
| 5583 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5584 | goto onError; |
| 5585 | return; |
| 5586 | onError: |
| 5587 | Py_DECREF(*exceptionObject); |
| 5588 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5589 | } |
| 5590 | } |
| 5591 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5592 | /* raises a UnicodeTranslateError */ |
| 5593 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5594 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5595 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5596 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5597 | { |
| 5598 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5599 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5600 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5601 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5602 | } |
| 5603 | |
| 5604 | /* error handling callback helper: |
| 5605 | build arguments, call the callback and check the arguments, |
| 5606 | put the result into newpos and return the replacement string, which |
| 5607 | has to be freed by the caller */ |
| 5608 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5609 | PyObject **errorHandler, |
| 5610 | const char *reason, |
| 5611 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5612 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5613 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5614 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5615 | 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] | 5616 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5617 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5618 | PyObject *restuple; |
| 5619 | PyObject *resunicode; |
| 5620 | |
| 5621 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5622 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5623 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5624 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5625 | } |
| 5626 | |
| 5627 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5628 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5629 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5630 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5631 | |
| 5632 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5633 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5634 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5635 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5636 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5637 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5638 | Py_DECREF(restuple); |
| 5639 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5640 | } |
| 5641 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5642 | &resunicode, &i_newpos)) { |
| 5643 | Py_DECREF(restuple); |
| 5644 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5645 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5646 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5647 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5648 | else |
| 5649 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5650 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5651 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5652 | Py_DECREF(restuple); |
| 5653 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5654 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5655 | Py_INCREF(resunicode); |
| 5656 | Py_DECREF(restuple); |
| 5657 | return resunicode; |
| 5658 | } |
| 5659 | |
| 5660 | /* Lookup the character ch in the mapping and put the result in result, |
| 5661 | which must be decrefed by the caller. |
| 5662 | Return 0 on success, -1 on error */ |
| 5663 | static |
| 5664 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5665 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5666 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5667 | PyObject *x; |
| 5668 | |
| 5669 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5670 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5671 | x = PyObject_GetItem(mapping, w); |
| 5672 | Py_DECREF(w); |
| 5673 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5674 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5675 | /* No mapping found means: use 1:1 mapping. */ |
| 5676 | PyErr_Clear(); |
| 5677 | *result = NULL; |
| 5678 | return 0; |
| 5679 | } else |
| 5680 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5681 | } |
| 5682 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5683 | *result = x; |
| 5684 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5685 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5686 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | long value = PyLong_AS_LONG(x); |
| 5688 | long max = PyUnicode_GetMax(); |
| 5689 | if (value < 0 || value > max) { |
| 5690 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5691 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5692 | Py_DECREF(x); |
| 5693 | return -1; |
| 5694 | } |
| 5695 | *result = x; |
| 5696 | return 0; |
| 5697 | } |
| 5698 | else if (PyUnicode_Check(x)) { |
| 5699 | *result = x; |
| 5700 | return 0; |
| 5701 | } |
| 5702 | else { |
| 5703 | /* wrong return value */ |
| 5704 | PyErr_SetString(PyExc_TypeError, |
| 5705 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5706 | Py_DECREF(x); |
| 5707 | return -1; |
| 5708 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5709 | } |
| 5710 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5711 | if not reallocate and adjust various state variables. |
| 5712 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5713 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5714 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5715 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5716 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5717 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5718 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5719 | /* remember old output position */ |
| 5720 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5721 | /* exponentially overallocate to minimize reallocations */ |
| 5722 | if (requiredsize < 2 * oldsize) |
| 5723 | requiredsize = 2 * oldsize; |
| 5724 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5725 | return -1; |
| 5726 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5727 | } |
| 5728 | return 0; |
| 5729 | } |
| 5730 | /* lookup the character, put the result in the output string and adjust |
| 5731 | various state variables. Return a new reference to the object that |
| 5732 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5733 | undefined (in which case no character was written). |
| 5734 | The called must decref result. |
| 5735 | Return 0 on success, -1 on error. */ |
| 5736 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5737 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5738 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5739 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5740 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5741 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5742 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5743 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5744 | /* not found => default to 1:1 mapping */ |
| 5745 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5746 | } |
| 5747 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5748 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5749 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5750 | /* no overflow check, because we know that the space is enough */ |
| 5751 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5752 | } |
| 5753 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5754 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5755 | if (repsize==1) { |
| 5756 | /* no overflow check, because we know that the space is enough */ |
| 5757 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5758 | } |
| 5759 | else if (repsize!=0) { |
| 5760 | /* more than one character */ |
| 5761 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5762 | (insize - (curinp-startinp)) + |
| 5763 | repsize - 1; |
| 5764 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5765 | return -1; |
| 5766 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5767 | *outp += repsize; |
| 5768 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5769 | } |
| 5770 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5771 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5772 | return 0; |
| 5773 | } |
| 5774 | |
| 5775 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5776 | Py_ssize_t size, |
| 5777 | PyObject *mapping, |
| 5778 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5779 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5780 | /* output object */ |
| 5781 | PyObject *res = NULL; |
| 5782 | /* pointers to the beginning and end+1 of input */ |
| 5783 | const Py_UNICODE *startp = p; |
| 5784 | const Py_UNICODE *endp = p + size; |
| 5785 | /* pointer into the output */ |
| 5786 | Py_UNICODE *str; |
| 5787 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5788 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5789 | char *reason = "character maps to <undefined>"; |
| 5790 | PyObject *errorHandler = NULL; |
| 5791 | PyObject *exc = NULL; |
| 5792 | /* the following variable is used for caching string comparisons |
| 5793 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5794 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5795 | int known_errorHandler = -1; |
| 5796 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5797 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5798 | PyErr_BadArgument(); |
| 5799 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5800 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5801 | |
| 5802 | /* allocate enough for a simple 1:1 translation without |
| 5803 | replacements, if we need more, we'll resize */ |
| 5804 | res = PyUnicode_FromUnicode(NULL, size); |
| 5805 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5806 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5807 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5808 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5809 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5810 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5811 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5812 | /* try to encode it */ |
| 5813 | PyObject *x = NULL; |
| 5814 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5815 | Py_XDECREF(x); |
| 5816 | goto onError; |
| 5817 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5818 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5819 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5820 | ++p; |
| 5821 | else { /* untranslatable character */ |
| 5822 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5823 | Py_ssize_t repsize; |
| 5824 | Py_ssize_t newpos; |
| 5825 | Py_UNICODE *uni2; |
| 5826 | /* startpos for collecting untranslatable chars */ |
| 5827 | const Py_UNICODE *collstart = p; |
| 5828 | const Py_UNICODE *collend = p+1; |
| 5829 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5830 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5831 | /* find all untranslatable characters */ |
| 5832 | while (collend < endp) { |
| 5833 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5834 | goto onError; |
| 5835 | Py_XDECREF(x); |
| 5836 | if (x!=Py_None) |
| 5837 | break; |
| 5838 | ++collend; |
| 5839 | } |
| 5840 | /* cache callback name lookup |
| 5841 | * (if not done yet, i.e. it's the first error) */ |
| 5842 | if (known_errorHandler==-1) { |
| 5843 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5844 | known_errorHandler = 1; |
| 5845 | else if (!strcmp(errors, "replace")) |
| 5846 | known_errorHandler = 2; |
| 5847 | else if (!strcmp(errors, "ignore")) |
| 5848 | known_errorHandler = 3; |
| 5849 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5850 | known_errorHandler = 4; |
| 5851 | else |
| 5852 | known_errorHandler = 0; |
| 5853 | } |
| 5854 | switch (known_errorHandler) { |
| 5855 | case 1: /* strict */ |
| 5856 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5857 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5858 | case 2: /* replace */ |
| 5859 | /* No need to check for space, this is a 1:1 replacement */ |
| 5860 | for (coll = collstart; coll<collend; ++coll) |
| 5861 | *str++ = '?'; |
| 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 | char buffer[2+29+1+1]; |
| 5870 | char *cp; |
| 5871 | sprintf(buffer, "&#%d;", (int)*p); |
| 5872 | if (charmaptranslate_makespace(&res, &str, |
| 5873 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5874 | goto onError; |
| 5875 | for (cp = buffer; *cp; ++cp) |
| 5876 | *str++ = *cp; |
| 5877 | } |
| 5878 | p = collend; |
| 5879 | break; |
| 5880 | default: |
| 5881 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5882 | reason, startp, size, &exc, |
| 5883 | collstart-startp, collend-startp, &newpos); |
| 5884 | if (repunicode == NULL) |
| 5885 | goto onError; |
| 5886 | /* generate replacement */ |
| 5887 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5888 | if (charmaptranslate_makespace(&res, &str, |
| 5889 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5890 | Py_DECREF(repunicode); |
| 5891 | goto onError; |
| 5892 | } |
| 5893 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5894 | *str++ = *uni2; |
| 5895 | p = startp + newpos; |
| 5896 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5897 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5898 | } |
| 5899 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5900 | /* Resize if we allocated to much */ |
| 5901 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5902 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5903 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5904 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5905 | } |
| 5906 | Py_XDECREF(exc); |
| 5907 | Py_XDECREF(errorHandler); |
| 5908 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5910 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5911 | Py_XDECREF(res); |
| 5912 | Py_XDECREF(exc); |
| 5913 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | return NULL; |
| 5915 | } |
| 5916 | |
| 5917 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5918 | PyObject *mapping, |
| 5919 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | { |
| 5921 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5922 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | str = PyUnicode_FromObject(str); |
| 5924 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5925 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5927 | PyUnicode_GET_SIZE(str), |
| 5928 | mapping, |
| 5929 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | Py_DECREF(str); |
| 5931 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5932 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5933 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | Py_XDECREF(str); |
| 5935 | return NULL; |
| 5936 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5937 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5938 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5939 | |
| 5940 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5941 | Py_ssize_t length, |
| 5942 | char *output, |
| 5943 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5944 | { |
| 5945 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5946 | PyObject *errorHandler = NULL; |
| 5947 | PyObject *exc = NULL; |
| 5948 | const char *encoding = "decimal"; |
| 5949 | const char *reason = "invalid decimal Unicode string"; |
| 5950 | /* the following variable is used for caching string comparisons |
| 5951 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5952 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5953 | |
| 5954 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5955 | PyErr_BadArgument(); |
| 5956 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5957 | } |
| 5958 | |
| 5959 | p = s; |
| 5960 | end = s + length; |
| 5961 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5962 | register Py_UNICODE ch = *p; |
| 5963 | int decimal; |
| 5964 | PyObject *repunicode; |
| 5965 | Py_ssize_t repsize; |
| 5966 | Py_ssize_t newpos; |
| 5967 | Py_UNICODE *uni2; |
| 5968 | Py_UNICODE *collstart; |
| 5969 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5970 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5971 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5972 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5973 | ++p; |
| 5974 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5975 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5976 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5977 | if (decimal >= 0) { |
| 5978 | *output++ = '0' + decimal; |
| 5979 | ++p; |
| 5980 | continue; |
| 5981 | } |
| 5982 | if (0 < ch && ch < 256) { |
| 5983 | *output++ = (char)ch; |
| 5984 | ++p; |
| 5985 | continue; |
| 5986 | } |
| 5987 | /* All other characters are considered unencodable */ |
| 5988 | collstart = p; |
| 5989 | collend = p+1; |
| 5990 | while (collend < end) { |
| 5991 | if ((0 < *collend && *collend < 256) || |
| 5992 | !Py_UNICODE_ISSPACE(*collend) || |
| 5993 | Py_UNICODE_TODECIMAL(*collend)) |
| 5994 | break; |
| 5995 | } |
| 5996 | /* cache callback name lookup |
| 5997 | * (if not done yet, i.e. it's the first error) */ |
| 5998 | if (known_errorHandler==-1) { |
| 5999 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6000 | known_errorHandler = 1; |
| 6001 | else if (!strcmp(errors, "replace")) |
| 6002 | known_errorHandler = 2; |
| 6003 | else if (!strcmp(errors, "ignore")) |
| 6004 | known_errorHandler = 3; |
| 6005 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6006 | known_errorHandler = 4; |
| 6007 | else |
| 6008 | known_errorHandler = 0; |
| 6009 | } |
| 6010 | switch (known_errorHandler) { |
| 6011 | case 1: /* strict */ |
| 6012 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 6013 | goto onError; |
| 6014 | case 2: /* replace */ |
| 6015 | for (p = collstart; p < collend; ++p) |
| 6016 | *output++ = '?'; |
| 6017 | /* fall through */ |
| 6018 | case 3: /* ignore */ |
| 6019 | p = collend; |
| 6020 | break; |
| 6021 | case 4: /* xmlcharrefreplace */ |
| 6022 | /* generate replacement (temporarily (mis)uses p) */ |
| 6023 | for (p = collstart; p < collend; ++p) |
| 6024 | output += sprintf(output, "&#%d;", (int)*p); |
| 6025 | p = collend; |
| 6026 | break; |
| 6027 | default: |
| 6028 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6029 | encoding, reason, s, length, &exc, |
| 6030 | collstart-s, collend-s, &newpos); |
| 6031 | if (repunicode == NULL) |
| 6032 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6033 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6034 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6035 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 6036 | Py_DECREF(repunicode); |
| 6037 | goto onError; |
| 6038 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6039 | /* generate replacement */ |
| 6040 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6041 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 6042 | Py_UNICODE ch = *uni2; |
| 6043 | if (Py_UNICODE_ISSPACE(ch)) |
| 6044 | *output++ = ' '; |
| 6045 | else { |
| 6046 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6047 | if (decimal >= 0) |
| 6048 | *output++ = '0' + decimal; |
| 6049 | else if (0 < ch && ch < 256) |
| 6050 | *output++ = (char)ch; |
| 6051 | else { |
| 6052 | Py_DECREF(repunicode); |
| 6053 | raise_encode_exception(&exc, encoding, |
| 6054 | s, length, collstart-s, collend-s, reason); |
| 6055 | goto onError; |
| 6056 | } |
| 6057 | } |
| 6058 | } |
| 6059 | p = s + newpos; |
| 6060 | Py_DECREF(repunicode); |
| 6061 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6062 | } |
| 6063 | /* 0-terminate the output string */ |
| 6064 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6065 | Py_XDECREF(exc); |
| 6066 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6067 | return 0; |
| 6068 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6069 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6070 | Py_XDECREF(exc); |
| 6071 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6072 | return -1; |
| 6073 | } |
| 6074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 | /* --- Helpers ------------------------------------------------------------ */ |
| 6076 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 6077 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6078 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6079 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6080 | #include "stringlib/count.h" |
| 6081 | #include "stringlib/find.h" |
| 6082 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6083 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6084 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6085 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 6086 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6087 | #include "stringlib/localeutil.h" |
| 6088 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6089 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6090 | #define ADJUST_INDICES(start, end, len) \ |
| 6091 | if (end > len) \ |
| 6092 | end = len; \ |
| 6093 | else if (end < 0) { \ |
| 6094 | end += len; \ |
| 6095 | if (end < 0) \ |
| 6096 | end = 0; \ |
| 6097 | } \ |
| 6098 | if (start < 0) { \ |
| 6099 | start += len; \ |
| 6100 | if (start < 0) \ |
| 6101 | start = 0; \ |
| 6102 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6103 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6104 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6105 | PyObject *substr, |
| 6106 | Py_ssize_t start, |
| 6107 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6108 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6109 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6110 | PyUnicodeObject* str_obj; |
| 6111 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6112 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6113 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 6114 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6115 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6116 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 6117 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | Py_DECREF(str_obj); |
| 6119 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6120 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6121 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6122 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6123 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6124 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 6125 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6126 | ); |
| 6127 | |
| 6128 | Py_DECREF(sub_obj); |
| 6129 | Py_DECREF(str_obj); |
| 6130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6131 | return result; |
| 6132 | } |
| 6133 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6134 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6135 | PyObject *sub, |
| 6136 | Py_ssize_t start, |
| 6137 | Py_ssize_t end, |
| 6138 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6139 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6140 | Py_ssize_t result; |
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 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6143 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6144 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6145 | sub = PyUnicode_FromObject(sub); |
| 6146 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6147 | Py_DECREF(str); |
| 6148 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6150 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6151 | if (direction > 0) |
| 6152 | result = stringlib_find_slice( |
| 6153 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6154 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6155 | start, end |
| 6156 | ); |
| 6157 | else |
| 6158 | result = stringlib_rfind_slice( |
| 6159 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6160 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6161 | start, end |
| 6162 | ); |
| 6163 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6164 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6165 | Py_DECREF(sub); |
| 6166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6167 | return result; |
| 6168 | } |
| 6169 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6170 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6171 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | PyUnicodeObject *substring, |
| 6173 | Py_ssize_t start, |
| 6174 | Py_ssize_t end, |
| 6175 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6176 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6177 | if (substring->length == 0) |
| 6178 | return 1; |
| 6179 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6180 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 | end -= substring->length; |
| 6182 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6183 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | |
| 6185 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6186 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6187 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | } else { |
| 6189 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6190 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6191 | } |
| 6192 | |
| 6193 | return 0; |
| 6194 | } |
| 6195 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6196 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6197 | PyObject *substr, |
| 6198 | Py_ssize_t start, |
| 6199 | Py_ssize_t end, |
| 6200 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6201 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6202 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6204 | str = PyUnicode_FromObject(str); |
| 6205 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6206 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6207 | substr = PyUnicode_FromObject(substr); |
| 6208 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6209 | Py_DECREF(str); |
| 6210 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6211 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | (PyUnicodeObject *)substr, |
| 6215 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | Py_DECREF(str); |
| 6217 | Py_DECREF(substr); |
| 6218 | return result; |
| 6219 | } |
| 6220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | /* Apply fixfct filter to the Unicode object self and return a |
| 6222 | reference to the modified object */ |
| 6223 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6224 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6225 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6226 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6227 | { |
| 6228 | |
| 6229 | PyUnicodeObject *u; |
| 6230 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6231 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6233 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6234 | |
| 6235 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6236 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6237 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6238 | /* fixfct should return TRUE if it modified the buffer. If |
| 6239 | FALSE, return a reference to the original buffer instead |
| 6240 | (to save space, not time) */ |
| 6241 | Py_INCREF(self); |
| 6242 | Py_DECREF(u); |
| 6243 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6244 | } |
| 6245 | return (PyObject*) u; |
| 6246 | } |
| 6247 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6248 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6249 | int fixupper(PyUnicodeObject *self) |
| 6250 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6251 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | Py_UNICODE *s = self->str; |
| 6253 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6255 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6256 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6257 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6258 | ch = Py_UNICODE_TOUPPER(*s); |
| 6259 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6260 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6261 | *s = ch; |
| 6262 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6263 | s++; |
| 6264 | } |
| 6265 | |
| 6266 | return status; |
| 6267 | } |
| 6268 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6269 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6270 | int fixlower(PyUnicodeObject *self) |
| 6271 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6272 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6273 | Py_UNICODE *s = self->str; |
| 6274 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6276 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6278 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6279 | ch = Py_UNICODE_TOLOWER(*s); |
| 6280 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6281 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6282 | *s = ch; |
| 6283 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6284 | s++; |
| 6285 | } |
| 6286 | |
| 6287 | return status; |
| 6288 | } |
| 6289 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6290 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6291 | int fixswapcase(PyUnicodeObject *self) |
| 6292 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6293 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6294 | Py_UNICODE *s = self->str; |
| 6295 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6296 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6297 | while (len-- > 0) { |
| 6298 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6299 | *s = Py_UNICODE_TOLOWER(*s); |
| 6300 | status = 1; |
| 6301 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6302 | *s = Py_UNICODE_TOUPPER(*s); |
| 6303 | status = 1; |
| 6304 | } |
| 6305 | s++; |
| 6306 | } |
| 6307 | |
| 6308 | return status; |
| 6309 | } |
| 6310 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6311 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6312 | int fixcapitalize(PyUnicodeObject *self) |
| 6313 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6314 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6315 | Py_UNICODE *s = self->str; |
| 6316 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6317 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6318 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6319 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6320 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6321 | *s = Py_UNICODE_TOUPPER(*s); |
| 6322 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6323 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6324 | s++; |
| 6325 | while (--len > 0) { |
| 6326 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6327 | *s = Py_UNICODE_TOLOWER(*s); |
| 6328 | status = 1; |
| 6329 | } |
| 6330 | s++; |
| 6331 | } |
| 6332 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6333 | } |
| 6334 | |
| 6335 | static |
| 6336 | int fixtitle(PyUnicodeObject *self) |
| 6337 | { |
| 6338 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6339 | register Py_UNICODE *e; |
| 6340 | int previous_is_cased; |
| 6341 | |
| 6342 | /* Shortcut for single character strings */ |
| 6343 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6344 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6345 | if (*p != ch) { |
| 6346 | *p = ch; |
| 6347 | return 1; |
| 6348 | } |
| 6349 | else |
| 6350 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6351 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6352 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | e = p + PyUnicode_GET_SIZE(self); |
| 6354 | previous_is_cased = 0; |
| 6355 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6356 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6357 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6358 | if (previous_is_cased) |
| 6359 | *p = Py_UNICODE_TOLOWER(ch); |
| 6360 | else |
| 6361 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6362 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6363 | if (Py_UNICODE_ISLOWER(ch) || |
| 6364 | Py_UNICODE_ISUPPER(ch) || |
| 6365 | Py_UNICODE_ISTITLE(ch)) |
| 6366 | previous_is_cased = 1; |
| 6367 | else |
| 6368 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6369 | } |
| 6370 | return 1; |
| 6371 | } |
| 6372 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6373 | PyObject * |
| 6374 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6375 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6376 | const Py_UNICODE blank = ' '; |
| 6377 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6378 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6379 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6380 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6381 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6382 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6383 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6384 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6385 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6387 | fseq = PySequence_Fast(seq, ""); |
| 6388 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6389 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6392 | /* NOTE: the following code can't call back into Python code, |
| 6393 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6394 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6395 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6396 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6397 | /* If empty sequence, return u"". */ |
| 6398 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6399 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6400 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6401 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6402 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6403 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6404 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6405 | item = items[0]; |
| 6406 | if (PyUnicode_CheckExact(item)) { |
| 6407 | Py_INCREF(item); |
| 6408 | res = (PyUnicodeObject *)item; |
| 6409 | goto Done; |
| 6410 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6411 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6412 | else { |
| 6413 | /* Set up sep and seplen */ |
| 6414 | if (separator == NULL) { |
| 6415 | sep = ␣ |
| 6416 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6417 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6418 | else { |
| 6419 | if (!PyUnicode_Check(separator)) { |
| 6420 | PyErr_Format(PyExc_TypeError, |
| 6421 | "separator: expected str instance," |
| 6422 | " %.80s found", |
| 6423 | Py_TYPE(separator)->tp_name); |
| 6424 | goto onError; |
| 6425 | } |
| 6426 | sep = PyUnicode_AS_UNICODE(separator); |
| 6427 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6428 | } |
| 6429 | } |
| 6430 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6431 | /* There are at least two things to join, or else we have a subclass |
| 6432 | * of str in the sequence. |
| 6433 | * Do a pre-pass to figure out the total amount of space we'll |
| 6434 | * need (sz), and see whether all argument are strings. |
| 6435 | */ |
| 6436 | sz = 0; |
| 6437 | for (i = 0; i < seqlen; i++) { |
| 6438 | const Py_ssize_t old_sz = sz; |
| 6439 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6440 | if (!PyUnicode_Check(item)) { |
| 6441 | PyErr_Format(PyExc_TypeError, |
| 6442 | "sequence item %zd: expected str instance," |
| 6443 | " %.80s found", |
| 6444 | i, Py_TYPE(item)->tp_name); |
| 6445 | goto onError; |
| 6446 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6447 | sz += PyUnicode_GET_SIZE(item); |
| 6448 | if (i != 0) |
| 6449 | sz += seplen; |
| 6450 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6451 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6452 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6453 | goto onError; |
| 6454 | } |
| 6455 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6456 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6457 | res = _PyUnicode_New(sz); |
| 6458 | if (res == NULL) |
| 6459 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6460 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6461 | /* Catenate everything. */ |
| 6462 | res_p = PyUnicode_AS_UNICODE(res); |
| 6463 | for (i = 0; i < seqlen; ++i) { |
| 6464 | Py_ssize_t itemlen; |
| 6465 | item = items[i]; |
| 6466 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6467 | /* Copy item, and maybe the separator. */ |
| 6468 | if (i) { |
| 6469 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6470 | res_p += seplen; |
| 6471 | } |
| 6472 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6473 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6474 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6475 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6476 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6477 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6478 | return (PyObject *)res; |
| 6479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6480 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6481 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6482 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6483 | return NULL; |
| 6484 | } |
| 6485 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6486 | static |
| 6487 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6488 | Py_ssize_t left, |
| 6489 | Py_ssize_t right, |
| 6490 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6491 | { |
| 6492 | PyUnicodeObject *u; |
| 6493 | |
| 6494 | if (left < 0) |
| 6495 | left = 0; |
| 6496 | if (right < 0) |
| 6497 | right = 0; |
| 6498 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6499 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6500 | Py_INCREF(self); |
| 6501 | return self; |
| 6502 | } |
| 6503 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6504 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6505 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6506 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6507 | return NULL; |
| 6508 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | u = _PyUnicode_New(left + self->length + right); |
| 6510 | if (u) { |
| 6511 | if (left) |
| 6512 | Py_UNICODE_FILL(u->str, fill, left); |
| 6513 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6514 | if (right) |
| 6515 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6516 | } |
| 6517 | |
| 6518 | return u; |
| 6519 | } |
| 6520 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6521 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6522 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6523 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6524 | |
| 6525 | string = PyUnicode_FromObject(string); |
| 6526 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6527 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6528 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6529 | list = stringlib_splitlines( |
| 6530 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6531 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6532 | |
| 6533 | Py_DECREF(string); |
| 6534 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6537 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6538 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6539 | PyUnicodeObject *substring, |
| 6540 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6541 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6542 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6543 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6545 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6546 | return stringlib_split_whitespace( |
| 6547 | (PyObject*) self, self->str, self->length, maxcount |
| 6548 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6549 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6550 | return stringlib_split( |
| 6551 | (PyObject*) self, self->str, self->length, |
| 6552 | substring->str, substring->length, |
| 6553 | maxcount |
| 6554 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6555 | } |
| 6556 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6557 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6558 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6559 | PyUnicodeObject *substring, |
| 6560 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6561 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6562 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6563 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6564 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6565 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6566 | return stringlib_rsplit_whitespace( |
| 6567 | (PyObject*) self, self->str, self->length, maxcount |
| 6568 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6569 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6570 | return stringlib_rsplit( |
| 6571 | (PyObject*) self, self->str, self->length, |
| 6572 | substring->str, substring->length, |
| 6573 | maxcount |
| 6574 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6575 | } |
| 6576 | |
| 6577 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6578 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6579 | PyUnicodeObject *str1, |
| 6580 | PyUnicodeObject *str2, |
| 6581 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6582 | { |
| 6583 | PyUnicodeObject *u; |
| 6584 | |
| 6585 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6586 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6587 | else if (maxcount == 0 || self->length == 0) |
| 6588 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6589 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6590 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6591 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6592 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6593 | if (str1->length == 0) |
| 6594 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6595 | if (str1->length == 1) { |
| 6596 | /* replace characters */ |
| 6597 | Py_UNICODE u1, u2; |
| 6598 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6599 | goto nothing; |
| 6600 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6601 | if (!u) |
| 6602 | return NULL; |
| 6603 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6604 | u1 = str1->str[0]; |
| 6605 | u2 = str2->str[0]; |
| 6606 | for (i = 0; i < u->length; i++) |
| 6607 | if (u->str[i] == u1) { |
| 6608 | if (--maxcount < 0) |
| 6609 | break; |
| 6610 | u->str[i] = u2; |
| 6611 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6612 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6613 | i = stringlib_find( |
| 6614 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6615 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6616 | if (i < 0) |
| 6617 | goto nothing; |
| 6618 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6619 | if (!u) |
| 6620 | return NULL; |
| 6621 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6622 | |
| 6623 | /* change everything in-place, starting with this one */ |
| 6624 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6625 | i += str1->length; |
| 6626 | |
| 6627 | while ( --maxcount > 0) { |
| 6628 | i = stringlib_find(self->str+i, self->length-i, |
| 6629 | str1->str, str1->length, |
| 6630 | i); |
| 6631 | if (i == -1) |
| 6632 | break; |
| 6633 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6634 | i += str1->length; |
| 6635 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6637 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6638 | |
| 6639 | Py_ssize_t n, i, j, e; |
| 6640 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | Py_UNICODE *p; |
| 6642 | |
| 6643 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6644 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6645 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6646 | if (n == 0) |
| 6647 | goto nothing; |
| 6648 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6649 | delta = (str2->length - str1->length); |
| 6650 | if (delta == 0) { |
| 6651 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6652 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6653 | product = n * (str2->length - str1->length); |
| 6654 | if ((product / (str2->length - str1->length)) != n) { |
| 6655 | PyErr_SetString(PyExc_OverflowError, |
| 6656 | "replace string is too long"); |
| 6657 | return NULL; |
| 6658 | } |
| 6659 | new_size = self->length + product; |
| 6660 | if (new_size < 0) { |
| 6661 | PyErr_SetString(PyExc_OverflowError, |
| 6662 | "replace string is too long"); |
| 6663 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6664 | } |
| 6665 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6666 | u = _PyUnicode_New(new_size); |
| 6667 | if (!u) |
| 6668 | return NULL; |
| 6669 | i = 0; |
| 6670 | p = u->str; |
| 6671 | e = self->length - str1->length; |
| 6672 | if (str1->length > 0) { |
| 6673 | while (n-- > 0) { |
| 6674 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6675 | j = stringlib_find(self->str+i, self->length-i, |
| 6676 | str1->str, str1->length, |
| 6677 | i); |
| 6678 | if (j == -1) |
| 6679 | break; |
| 6680 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6681 | /* copy unchanged part [i:j] */ |
| 6682 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6683 | p += j - i; |
| 6684 | } |
| 6685 | /* copy substitution string */ |
| 6686 | if (str2->length > 0) { |
| 6687 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6688 | p += str2->length; |
| 6689 | } |
| 6690 | i = j + str1->length; |
| 6691 | } |
| 6692 | if (i < self->length) |
| 6693 | /* copy tail [i:] */ |
| 6694 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6695 | } else { |
| 6696 | /* interleave */ |
| 6697 | while (n > 0) { |
| 6698 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6699 | p += str2->length; |
| 6700 | if (--n <= 0) |
| 6701 | break; |
| 6702 | *p++ = self->str[i++]; |
| 6703 | } |
| 6704 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6705 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6706 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6708 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6709 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6710 | /* nothing to replace; return original string (when possible) */ |
| 6711 | if (PyUnicode_CheckExact(self)) { |
| 6712 | Py_INCREF(self); |
| 6713 | return (PyObject *) self; |
| 6714 | } |
| 6715 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6716 | } |
| 6717 | |
| 6718 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6719 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6720 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6721 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6722 | \n\ |
| 6723 | 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] | 6724 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | |
| 6726 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6727 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6728 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | return fixup(self, fixtitle); |
| 6730 | } |
| 6731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6732 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6733 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6734 | \n\ |
| 6735 | Return a capitalized version of S, i.e. make the first character\n\ |
Senthil Kumaran | e51ee8a | 2010-07-05 12:00:56 +0000 | [diff] [blame] | 6736 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6737 | |
| 6738 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6739 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6740 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6741 | return fixup(self, fixcapitalize); |
| 6742 | } |
| 6743 | |
| 6744 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6745 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6746 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6747 | \n\ |
| 6748 | 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] | 6749 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6750 | |
| 6751 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6752 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6753 | { |
| 6754 | PyObject *list; |
| 6755 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6756 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6757 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | /* Split into words */ |
| 6759 | list = split(self, NULL, -1); |
| 6760 | if (!list) |
| 6761 | return NULL; |
| 6762 | |
| 6763 | /* Capitalize each word */ |
| 6764 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6765 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6766 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6767 | if (item == NULL) |
| 6768 | goto onError; |
| 6769 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6770 | PyList_SET_ITEM(list, i, item); |
| 6771 | } |
| 6772 | |
| 6773 | /* Join the words to form a new string */ |
| 6774 | item = PyUnicode_Join(NULL, list); |
| 6775 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6776 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | Py_DECREF(list); |
| 6778 | return (PyObject *)item; |
| 6779 | } |
| 6780 | #endif |
| 6781 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6782 | /* Argument converter. Coerces to a single unicode character */ |
| 6783 | |
| 6784 | static int |
| 6785 | convert_uc(PyObject *obj, void *addr) |
| 6786 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6787 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6788 | PyObject *uniobj; |
| 6789 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6790 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6791 | uniobj = PyUnicode_FromObject(obj); |
| 6792 | if (uniobj == NULL) { |
| 6793 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6794 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6795 | return 0; |
| 6796 | } |
| 6797 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6798 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6799 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6800 | Py_DECREF(uniobj); |
| 6801 | return 0; |
| 6802 | } |
| 6803 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6804 | *fillcharloc = unistr[0]; |
| 6805 | Py_DECREF(uniobj); |
| 6806 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6807 | } |
| 6808 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6809 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6810 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6812 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6813 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6814 | |
| 6815 | static PyObject * |
| 6816 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6817 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6818 | Py_ssize_t marg, left; |
| 6819 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6820 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6821 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6822 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6823 | return NULL; |
| 6824 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6825 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | Py_INCREF(self); |
| 6827 | return (PyObject*) self; |
| 6828 | } |
| 6829 | |
| 6830 | marg = width - self->length; |
| 6831 | left = marg / 2 + (marg & width & 1); |
| 6832 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6833 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6836 | #if 0 |
| 6837 | |
| 6838 | /* This code should go into some future Unicode collation support |
| 6839 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6840 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6841 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6842 | /* speedy UTF-16 code point order comparison */ |
| 6843 | /* gleaned from: */ |
| 6844 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6845 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6846 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6847 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6848 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6849 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6850 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6851 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6852 | }; |
| 6853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | static int |
| 6855 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6856 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6857 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6858 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6859 | Py_UNICODE *s1 = str1->str; |
| 6860 | Py_UNICODE *s2 = str2->str; |
| 6861 | |
| 6862 | len1 = str1->length; |
| 6863 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6865 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6866 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6867 | |
| 6868 | c1 = *s1++; |
| 6869 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6870 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6871 | if (c1 > (1<<11) * 26) |
| 6872 | c1 += utf16Fixup[c1>>11]; |
| 6873 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6874 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6875 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6876 | |
| 6877 | if (c1 != c2) |
| 6878 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6879 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6880 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6884 | } |
| 6885 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6886 | #else |
| 6887 | |
| 6888 | static int |
| 6889 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6890 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6891 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6892 | |
| 6893 | Py_UNICODE *s1 = str1->str; |
| 6894 | Py_UNICODE *s2 = str2->str; |
| 6895 | |
| 6896 | len1 = str1->length; |
| 6897 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6898 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6899 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6900 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6901 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6902 | c1 = *s1++; |
| 6903 | c2 = *s2++; |
| 6904 | |
| 6905 | if (c1 != c2) |
| 6906 | return (c1 < c2) ? -1 : 1; |
| 6907 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6908 | len1--; len2--; |
| 6909 | } |
| 6910 | |
| 6911 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6912 | } |
| 6913 | |
| 6914 | #endif |
| 6915 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6916 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6917 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6918 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6919 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6920 | return unicode_compare((PyUnicodeObject *)left, |
| 6921 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6922 | PyErr_Format(PyExc_TypeError, |
| 6923 | "Can't compare %.100s and %.100s", |
| 6924 | left->ob_type->tp_name, |
| 6925 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6926 | return -1; |
| 6927 | } |
| 6928 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6929 | int |
| 6930 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6931 | { |
| 6932 | int i; |
| 6933 | Py_UNICODE *id; |
| 6934 | assert(PyUnicode_Check(uni)); |
| 6935 | id = PyUnicode_AS_UNICODE(uni); |
| 6936 | /* Compare Unicode string and source character set string */ |
| 6937 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6938 | if (id[i] != str[i]) |
| 6939 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 6940 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 6941 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 6942 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6943 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6944 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6945 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6946 | return 0; |
| 6947 | } |
| 6948 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6949 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6950 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6951 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6952 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6953 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6954 | PyObject *right, |
| 6955 | int op) |
| 6956 | { |
| 6957 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6958 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6959 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6960 | PyObject *v; |
| 6961 | if (((PyUnicodeObject *) left)->length != |
| 6962 | ((PyUnicodeObject *) right)->length) { |
| 6963 | if (op == Py_EQ) { |
| 6964 | Py_INCREF(Py_False); |
| 6965 | return Py_False; |
| 6966 | } |
| 6967 | if (op == Py_NE) { |
| 6968 | Py_INCREF(Py_True); |
| 6969 | return Py_True; |
| 6970 | } |
| 6971 | } |
| 6972 | if (left == right) |
| 6973 | result = 0; |
| 6974 | else |
| 6975 | result = unicode_compare((PyUnicodeObject *)left, |
| 6976 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6977 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6978 | /* Convert the return value to a Boolean */ |
| 6979 | switch (op) { |
| 6980 | case Py_EQ: |
| 6981 | v = TEST_COND(result == 0); |
| 6982 | break; |
| 6983 | case Py_NE: |
| 6984 | v = TEST_COND(result != 0); |
| 6985 | break; |
| 6986 | case Py_LE: |
| 6987 | v = TEST_COND(result <= 0); |
| 6988 | break; |
| 6989 | case Py_GE: |
| 6990 | v = TEST_COND(result >= 0); |
| 6991 | break; |
| 6992 | case Py_LT: |
| 6993 | v = TEST_COND(result == -1); |
| 6994 | break; |
| 6995 | case Py_GT: |
| 6996 | v = TEST_COND(result == 1); |
| 6997 | break; |
| 6998 | default: |
| 6999 | PyErr_BadArgument(); |
| 7000 | return NULL; |
| 7001 | } |
| 7002 | Py_INCREF(v); |
| 7003 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7004 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7005 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7006 | Py_INCREF(Py_NotImplemented); |
| 7007 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7008 | } |
| 7009 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7010 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7011 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7012 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7013 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7014 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7015 | |
| 7016 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7017 | sub = PyUnicode_FromObject(element); |
| 7018 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7019 | PyErr_Format(PyExc_TypeError, |
| 7020 | "'in <string>' requires string as left operand, not %s", |
| 7021 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7022 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7023 | } |
| 7024 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7025 | str = PyUnicode_FromObject(container); |
| 7026 | if (!str) { |
| 7027 | Py_DECREF(sub); |
| 7028 | return -1; |
| 7029 | } |
| 7030 | |
| 7031 | result = stringlib_contains_obj(str, sub); |
| 7032 | |
| 7033 | Py_DECREF(str); |
| 7034 | Py_DECREF(sub); |
| 7035 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7036 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7037 | } |
| 7038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7040 | |
| 7041 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7042 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7043 | { |
| 7044 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7045 | |
| 7046 | /* Coerce the two arguments */ |
| 7047 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7048 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7049 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7051 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7052 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7053 | |
| 7054 | /* Shortcuts */ |
| 7055 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7056 | Py_DECREF(v); |
| 7057 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7058 | } |
| 7059 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7060 | Py_DECREF(u); |
| 7061 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | } |
| 7063 | |
| 7064 | /* Concat the two Unicode strings */ |
| 7065 | w = _PyUnicode_New(u->length + v->length); |
| 7066 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7067 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7068 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7069 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7070 | |
| 7071 | Py_DECREF(u); |
| 7072 | Py_DECREF(v); |
| 7073 | return (PyObject *)w; |
| 7074 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7075 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7076 | Py_XDECREF(u); |
| 7077 | Py_XDECREF(v); |
| 7078 | return NULL; |
| 7079 | } |
| 7080 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7081 | void |
| 7082 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7083 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7084 | PyObject *new; |
| 7085 | if (*pleft == NULL) |
| 7086 | return; |
| 7087 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7088 | Py_DECREF(*pleft); |
| 7089 | *pleft = NULL; |
| 7090 | return; |
| 7091 | } |
| 7092 | new = PyUnicode_Concat(*pleft, right); |
| 7093 | Py_DECREF(*pleft); |
| 7094 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
| 7097 | void |
| 7098 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7099 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7100 | PyUnicode_Append(pleft, right); |
| 7101 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7104 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7105 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7106 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7107 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7108 | 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] | 7109 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7110 | |
| 7111 | static PyObject * |
| 7112 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7113 | { |
| 7114 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7115 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7116 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7117 | PyObject *result; |
| 7118 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7119 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7120 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7121 | return NULL; |
| 7122 | |
| 7123 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7124 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7125 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7126 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7127 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7128 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7129 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7130 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7131 | substring->str, substring->length, |
| 7132 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7133 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7134 | |
| 7135 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7137 | return result; |
| 7138 | } |
| 7139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7140 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7141 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7142 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7143 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7144 | 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] | 7145 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7146 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7147 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7148 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | |
| 7150 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7151 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7152 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7153 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | char *encoding = NULL; |
| 7155 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7156 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7157 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7158 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7159 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7160 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7161 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7162 | if (v == NULL) |
| 7163 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7164 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7165 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7166 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7167 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7168 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7169 | Py_DECREF(v); |
| 7170 | return NULL; |
| 7171 | } |
| 7172 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7173 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7174 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7175 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7178 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7179 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7180 | \n\ |
| 7181 | 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] | 7182 | 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] | 7183 | |
| 7184 | static PyObject* |
| 7185 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7186 | { |
| 7187 | Py_UNICODE *e; |
| 7188 | Py_UNICODE *p; |
| 7189 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7190 | Py_UNICODE *qe; |
| 7191 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7192 | PyUnicodeObject *u; |
| 7193 | int tabsize = 8; |
| 7194 | |
| 7195 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7196 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7197 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7198 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7199 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7200 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7201 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7202 | for (p = self->str; p < e; p++) |
| 7203 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7204 | if (tabsize > 0) { |
| 7205 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7206 | if (j > PY_SSIZE_T_MAX - incr) |
| 7207 | goto overflow1; |
| 7208 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7209 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7210 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7211 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7212 | if (j > PY_SSIZE_T_MAX - 1) |
| 7213 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7214 | j++; |
| 7215 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7216 | if (i > PY_SSIZE_T_MAX - j) |
| 7217 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7218 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7219 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7220 | } |
| 7221 | } |
| 7222 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7223 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7224 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7226 | /* Second pass: create output string and fill it */ |
| 7227 | u = _PyUnicode_New(i + j); |
| 7228 | if (!u) |
| 7229 | return NULL; |
| 7230 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7231 | j = 0; /* same as in first pass */ |
| 7232 | q = u->str; /* next output char */ |
| 7233 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7234 | |
| 7235 | for (p = self->str; p < e; p++) |
| 7236 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7237 | if (tabsize > 0) { |
| 7238 | i = tabsize - (j % tabsize); |
| 7239 | j += i; |
| 7240 | while (i--) { |
| 7241 | if (q >= qe) |
| 7242 | goto overflow2; |
| 7243 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7244 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7245 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7246 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7247 | else { |
| 7248 | if (q >= qe) |
| 7249 | goto overflow2; |
| 7250 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7251 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7252 | if (*p == '\n' || *p == '\r') |
| 7253 | j = 0; |
| 7254 | } |
| 7255 | |
| 7256 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7257 | |
| 7258 | overflow2: |
| 7259 | Py_DECREF(u); |
| 7260 | overflow1: |
| 7261 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7262 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7263 | } |
| 7264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7265 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7266 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7267 | \n\ |
| 7268 | 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] | 7269 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7270 | arguments start and end are interpreted as in slice notation.\n\ |
| 7271 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7272 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7273 | |
| 7274 | static PyObject * |
| 7275 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7276 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7277 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7278 | Py_ssize_t start; |
| 7279 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7280 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7281 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7282 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7283 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7284 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7285 | result = stringlib_find_slice( |
| 7286 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7287 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7288 | start, end |
| 7289 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7290 | |
| 7291 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7292 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7293 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
| 7296 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7297 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | { |
| 7299 | if (index < 0 || index >= self->length) { |
| 7300 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7301 | return NULL; |
| 7302 | } |
| 7303 | |
| 7304 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7305 | } |
| 7306 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7307 | /* Believe it or not, this produces the same value for ASCII strings |
| 7308 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7309 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7310 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7311 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7312 | Py_ssize_t len; |
| 7313 | Py_UNICODE *p; |
| 7314 | long x; |
| 7315 | |
| 7316 | if (self->hash != -1) |
| 7317 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7318 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7319 | p = self->str; |
| 7320 | x = *p << 7; |
| 7321 | while (--len >= 0) |
| 7322 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7323 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7324 | if (x == -1) |
| 7325 | x = -2; |
| 7326 | self->hash = x; |
| 7327 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7328 | } |
| 7329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7330 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7331 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7332 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7333 | 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] | 7334 | |
| 7335 | static PyObject * |
| 7336 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7337 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7338 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7339 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7340 | Py_ssize_t start; |
| 7341 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7342 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7343 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7344 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7345 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7346 | result = stringlib_find_slice( |
| 7347 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7348 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7349 | start, end |
| 7350 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7351 | |
| 7352 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7354 | if (result < 0) { |
| 7355 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7356 | return NULL; |
| 7357 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7358 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7359 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7360 | } |
| 7361 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7362 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7363 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7364 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7365 | 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] | 7366 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7367 | |
| 7368 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7369 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7370 | { |
| 7371 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7372 | register const Py_UNICODE *e; |
| 7373 | int cased; |
| 7374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7375 | /* Shortcut for single character strings */ |
| 7376 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7377 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7378 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7379 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7380 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7381 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7382 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7383 | e = p + PyUnicode_GET_SIZE(self); |
| 7384 | cased = 0; |
| 7385 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7386 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7387 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7388 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7389 | return PyBool_FromLong(0); |
| 7390 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7391 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7392 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7393 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7394 | } |
| 7395 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7396 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7397 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7399 | 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] | 7400 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7401 | |
| 7402 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7403 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7404 | { |
| 7405 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7406 | register const Py_UNICODE *e; |
| 7407 | int cased; |
| 7408 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7409 | /* Shortcut for single character strings */ |
| 7410 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7411 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7412 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7413 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7414 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7415 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7416 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7417 | e = p + PyUnicode_GET_SIZE(self); |
| 7418 | cased = 0; |
| 7419 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7420 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7421 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7422 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7423 | return PyBool_FromLong(0); |
| 7424 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7425 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7426 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7427 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7428 | } |
| 7429 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7430 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7431 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7432 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7433 | Return True if S is a titlecased string and there is at least one\n\ |
| 7434 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7435 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7436 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | |
| 7438 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7439 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | { |
| 7441 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7442 | register const Py_UNICODE *e; |
| 7443 | int cased, previous_is_cased; |
| 7444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7445 | /* Shortcut for single character strings */ |
| 7446 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7447 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7448 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7449 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7450 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7451 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7452 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7453 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7454 | e = p + PyUnicode_GET_SIZE(self); |
| 7455 | cased = 0; |
| 7456 | previous_is_cased = 0; |
| 7457 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7458 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7459 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7460 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7461 | if (previous_is_cased) |
| 7462 | return PyBool_FromLong(0); |
| 7463 | previous_is_cased = 1; |
| 7464 | cased = 1; |
| 7465 | } |
| 7466 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7467 | if (!previous_is_cased) |
| 7468 | return PyBool_FromLong(0); |
| 7469 | previous_is_cased = 1; |
| 7470 | cased = 1; |
| 7471 | } |
| 7472 | else |
| 7473 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7474 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7475 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | } |
| 7477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7478 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7479 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7480 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7481 | Return True if all characters in S are whitespace\n\ |
| 7482 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | |
| 7484 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7485 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7486 | { |
| 7487 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7488 | register const Py_UNICODE *e; |
| 7489 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7490 | /* Shortcut for single character strings */ |
| 7491 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7492 | Py_UNICODE_ISSPACE(*p)) |
| 7493 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7494 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7495 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7496 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7497 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7499 | e = p + PyUnicode_GET_SIZE(self); |
| 7500 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7501 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7502 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7503 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7504 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | } |
| 7506 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7507 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7508 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7509 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7510 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7511 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7512 | |
| 7513 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7514 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7515 | { |
| 7516 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7517 | register const Py_UNICODE *e; |
| 7518 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7519 | /* Shortcut for single character strings */ |
| 7520 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7521 | Py_UNICODE_ISALPHA(*p)) |
| 7522 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7523 | |
| 7524 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7525 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7526 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7527 | |
| 7528 | e = p + PyUnicode_GET_SIZE(self); |
| 7529 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7531 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7532 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7533 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7534 | } |
| 7535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7536 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7538 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7539 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7540 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7541 | |
| 7542 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7543 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7544 | { |
| 7545 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7546 | register const Py_UNICODE *e; |
| 7547 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7548 | /* Shortcut for single character strings */ |
| 7549 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7550 | Py_UNICODE_ISALNUM(*p)) |
| 7551 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7552 | |
| 7553 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7554 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7555 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7556 | |
| 7557 | e = p + PyUnicode_GET_SIZE(self); |
| 7558 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7559 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7560 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7561 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7562 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7563 | } |
| 7564 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7565 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7566 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7567 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7568 | 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] | 7569 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7570 | |
| 7571 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7572 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | { |
| 7574 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7575 | register const Py_UNICODE *e; |
| 7576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7577 | /* Shortcut for single character strings */ |
| 7578 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7579 | Py_UNICODE_ISDECIMAL(*p)) |
| 7580 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7582 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7583 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7584 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7585 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7586 | e = p + PyUnicode_GET_SIZE(self); |
| 7587 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7588 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7589 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7590 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7591 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7592 | } |
| 7593 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7594 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7595 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7596 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7597 | Return True if all characters in S are digits\n\ |
| 7598 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | |
| 7600 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7601 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7602 | { |
| 7603 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7604 | register const Py_UNICODE *e; |
| 7605 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7606 | /* Shortcut for single character strings */ |
| 7607 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7608 | Py_UNICODE_ISDIGIT(*p)) |
| 7609 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7610 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7611 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7612 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7613 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7614 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7615 | e = p + PyUnicode_GET_SIZE(self); |
| 7616 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7617 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7618 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7619 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7620 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7621 | } |
| 7622 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7623 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7624 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7625 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7626 | 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] | 7627 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7628 | |
| 7629 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7630 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7631 | { |
| 7632 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7633 | register const Py_UNICODE *e; |
| 7634 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7635 | /* Shortcut for single character strings */ |
| 7636 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7637 | Py_UNICODE_ISNUMERIC(*p)) |
| 7638 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7639 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7640 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7641 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7642 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7643 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7644 | e = p + PyUnicode_GET_SIZE(self); |
| 7645 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7646 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7647 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7648 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7649 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7650 | } |
| 7651 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7652 | int |
| 7653 | PyUnicode_IsIdentifier(PyObject *self) |
| 7654 | { |
| 7655 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7656 | register const Py_UNICODE *e; |
| 7657 | |
| 7658 | /* Special case for empty strings */ |
| 7659 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7660 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7661 | |
| 7662 | /* PEP 3131 says that the first character must be in |
| 7663 | XID_Start and subsequent characters in XID_Continue, |
| 7664 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7665 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7666 | letters, digits, underscore). However, given the current |
| 7667 | definition of XID_Start and XID_Continue, it is sufficient |
| 7668 | to check just for these, except that _ must be allowed |
| 7669 | as starting an identifier. */ |
| 7670 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7671 | return 0; |
| 7672 | |
| 7673 | e = p + PyUnicode_GET_SIZE(self); |
| 7674 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7675 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7676 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7677 | } |
| 7678 | return 1; |
| 7679 | } |
| 7680 | |
| 7681 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7682 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7683 | \n\ |
| 7684 | Return True if S is a valid identifier according\n\ |
| 7685 | to the language definition."); |
| 7686 | |
| 7687 | static PyObject* |
| 7688 | unicode_isidentifier(PyObject *self) |
| 7689 | { |
| 7690 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7691 | } |
| 7692 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7693 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7694 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7695 | \n\ |
| 7696 | Return True if all characters in S are considered\n\ |
| 7697 | printable in repr() or S is empty, False otherwise."); |
| 7698 | |
| 7699 | static PyObject* |
| 7700 | unicode_isprintable(PyObject *self) |
| 7701 | { |
| 7702 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7703 | register const Py_UNICODE *e; |
| 7704 | |
| 7705 | /* Shortcut for single character strings */ |
| 7706 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7707 | Py_RETURN_TRUE; |
| 7708 | } |
| 7709 | |
| 7710 | e = p + PyUnicode_GET_SIZE(self); |
| 7711 | for (; p < e; p++) { |
| 7712 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7713 | Py_RETURN_FALSE; |
| 7714 | } |
| 7715 | } |
| 7716 | Py_RETURN_TRUE; |
| 7717 | } |
| 7718 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7719 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7720 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7721 | \n\ |
| 7722 | 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] | 7723 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7724 | |
| 7725 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7726 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7727 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7728 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7729 | } |
| 7730 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7731 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7732 | unicode_length(PyUnicodeObject *self) |
| 7733 | { |
| 7734 | return self->length; |
| 7735 | } |
| 7736 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7737 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7738 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7739 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7740 | 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] | 7741 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7742 | |
| 7743 | static PyObject * |
| 7744 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7745 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7746 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7747 | Py_UNICODE fillchar = ' '; |
| 7748 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7749 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7750 | return NULL; |
| 7751 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7752 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7753 | Py_INCREF(self); |
| 7754 | return (PyObject*) self; |
| 7755 | } |
| 7756 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7757 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7758 | } |
| 7759 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7760 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7761 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7762 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7763 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7764 | |
| 7765 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7766 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7767 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7768 | return fixup(self, fixlower); |
| 7769 | } |
| 7770 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7771 | #define LEFTSTRIP 0 |
| 7772 | #define RIGHTSTRIP 1 |
| 7773 | #define BOTHSTRIP 2 |
| 7774 | |
| 7775 | /* Arrays indexed by above */ |
| 7776 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7777 | |
| 7778 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7779 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7780 | /* externally visible for str.strip(unicode) */ |
| 7781 | PyObject * |
| 7782 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7783 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7784 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7785 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7786 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7787 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7788 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7789 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7790 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7791 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7792 | i = 0; |
| 7793 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7794 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7795 | i++; |
| 7796 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7797 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7798 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7799 | j = len; |
| 7800 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7801 | do { |
| 7802 | j--; |
| 7803 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7804 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7805 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7806 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7807 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7808 | Py_INCREF(self); |
| 7809 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7810 | } |
| 7811 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7812 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7815 | |
| 7816 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7817 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7818 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7819 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7820 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7821 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7822 | i = 0; |
| 7823 | if (striptype != RIGHTSTRIP) { |
| 7824 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7825 | i++; |
| 7826 | } |
| 7827 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7828 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7829 | j = len; |
| 7830 | if (striptype != LEFTSTRIP) { |
| 7831 | do { |
| 7832 | j--; |
| 7833 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7834 | j++; |
| 7835 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7836 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7837 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7838 | Py_INCREF(self); |
| 7839 | return (PyObject*)self; |
| 7840 | } |
| 7841 | else |
| 7842 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7843 | } |
| 7844 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7845 | |
| 7846 | static PyObject * |
| 7847 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7848 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7849 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7850 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7851 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7852 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7853 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7854 | if (sep != NULL && sep != Py_None) { |
| 7855 | if (PyUnicode_Check(sep)) |
| 7856 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7857 | else { |
| 7858 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7859 | "%s arg must be None or str", |
| 7860 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7861 | return NULL; |
| 7862 | } |
| 7863 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7864 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7865 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7866 | } |
| 7867 | |
| 7868 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7869 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7870 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7871 | \n\ |
| 7872 | Return a copy of the string S with leading and trailing\n\ |
| 7873 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7874 | 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] | 7875 | |
| 7876 | static PyObject * |
| 7877 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7878 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7879 | if (PyTuple_GET_SIZE(args) == 0) |
| 7880 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7881 | else |
| 7882 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7883 | } |
| 7884 | |
| 7885 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7886 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7887 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7888 | \n\ |
| 7889 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7890 | 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] | 7891 | |
| 7892 | static PyObject * |
| 7893 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7894 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7895 | if (PyTuple_GET_SIZE(args) == 0) |
| 7896 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7897 | else |
| 7898 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7899 | } |
| 7900 | |
| 7901 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7902 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7904 | \n\ |
| 7905 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7906 | 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] | 7907 | |
| 7908 | static PyObject * |
| 7909 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7910 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7911 | if (PyTuple_GET_SIZE(args) == 0) |
| 7912 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7913 | else |
| 7914 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7915 | } |
| 7916 | |
| 7917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7918 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7919 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7920 | { |
| 7921 | PyUnicodeObject *u; |
| 7922 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7923 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7924 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7925 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7926 | if (len < 1) { |
| 7927 | Py_INCREF(unicode_empty); |
| 7928 | return (PyObject *)unicode_empty; |
| 7929 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7930 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7931 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7932 | /* no repeat, return original string */ |
| 7933 | Py_INCREF(str); |
| 7934 | return (PyObject*) str; |
| 7935 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7936 | |
| 7937 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7938 | * needed doesn't overflow size_t |
| 7939 | */ |
| 7940 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7941 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7942 | PyErr_SetString(PyExc_OverflowError, |
| 7943 | "repeated string is too long"); |
| 7944 | return NULL; |
| 7945 | } |
| 7946 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7947 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7948 | PyErr_SetString(PyExc_OverflowError, |
| 7949 | "repeated string is too long"); |
| 7950 | return NULL; |
| 7951 | } |
| 7952 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7953 | if (!u) |
| 7954 | return NULL; |
| 7955 | |
| 7956 | p = u->str; |
| 7957 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7958 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7959 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7960 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7961 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7962 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7963 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7964 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7965 | Py_UNICODE_COPY(p+done, p, n); |
| 7966 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7967 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7968 | } |
| 7969 | |
| 7970 | return (PyObject*) u; |
| 7971 | } |
| 7972 | |
| 7973 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7974 | PyObject *subobj, |
| 7975 | PyObject *replobj, |
| 7976 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7977 | { |
| 7978 | PyObject *self; |
| 7979 | PyObject *str1; |
| 7980 | PyObject *str2; |
| 7981 | PyObject *result; |
| 7982 | |
| 7983 | self = PyUnicode_FromObject(obj); |
| 7984 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7986 | str1 = PyUnicode_FromObject(subobj); |
| 7987 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7988 | Py_DECREF(self); |
| 7989 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7990 | } |
| 7991 | str2 = PyUnicode_FromObject(replobj); |
| 7992 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7993 | Py_DECREF(self); |
| 7994 | Py_DECREF(str1); |
| 7995 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7996 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7997 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7998 | (PyUnicodeObject *)str1, |
| 7999 | (PyUnicodeObject *)str2, |
| 8000 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8001 | Py_DECREF(self); |
| 8002 | Py_DECREF(str1); |
| 8003 | Py_DECREF(str2); |
| 8004 | return result; |
| 8005 | } |
| 8006 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8007 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 8008 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8009 | \n\ |
| 8010 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8011 | old replaced by new. If the optional argument count is\n\ |
| 8012 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8013 | |
| 8014 | static PyObject* |
| 8015 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8016 | { |
| 8017 | PyUnicodeObject *str1; |
| 8018 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8019 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8020 | PyObject *result; |
| 8021 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8022 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8023 | return NULL; |
| 8024 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8025 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8026 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8027 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8028 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8029 | Py_DECREF(str1); |
| 8030 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8031 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8032 | |
| 8033 | result = replace(self, str1, str2, maxcount); |
| 8034 | |
| 8035 | Py_DECREF(str1); |
| 8036 | Py_DECREF(str2); |
| 8037 | return result; |
| 8038 | } |
| 8039 | |
| 8040 | static |
| 8041 | PyObject *unicode_repr(PyObject *unicode) |
| 8042 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8043 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8044 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8045 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8046 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8047 | |
| 8048 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8049 | better to choose a different scheme. Perhaps scan the |
| 8050 | first N-chars of the string and allocate based on that size. |
| 8051 | */ |
| 8052 | /* Initial allocation is based on the longest-possible unichr |
| 8053 | escape. |
| 8054 | |
| 8055 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8056 | unichr, so in this case it's the longest unichr escape. In |
| 8057 | narrow (UTF-16) builds this is five chars per source unichr |
| 8058 | since there are two unichrs in the surrogate pair, so in narrow |
| 8059 | (UTF-16) builds it's not the longest unichr escape. |
| 8060 | |
| 8061 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8062 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8063 | escape. |
| 8064 | */ |
| 8065 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8066 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8067 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8068 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8069 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8070 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8072 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8073 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8074 | if (repr == NULL) |
| 8075 | return NULL; |
| 8076 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8077 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8078 | |
| 8079 | /* Add quote */ |
| 8080 | *p++ = (findchar(s, size, '\'') && |
| 8081 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8082 | while (size-- > 0) { |
| 8083 | Py_UNICODE ch = *s++; |
| 8084 | |
| 8085 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8086 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8087 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8088 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8089 | continue; |
| 8090 | } |
| 8091 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8092 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8093 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8094 | *p++ = '\\'; |
| 8095 | *p++ = 't'; |
| 8096 | } |
| 8097 | else if (ch == '\n') { |
| 8098 | *p++ = '\\'; |
| 8099 | *p++ = 'n'; |
| 8100 | } |
| 8101 | else if (ch == '\r') { |
| 8102 | *p++ = '\\'; |
| 8103 | *p++ = 'r'; |
| 8104 | } |
| 8105 | |
| 8106 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8107 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8108 | *p++ = '\\'; |
| 8109 | *p++ = 'x'; |
| 8110 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8111 | *p++ = hexdigits[ch & 0x000F]; |
| 8112 | } |
| 8113 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8114 | /* Copy ASCII characters as-is */ |
| 8115 | else if (ch < 0x7F) { |
| 8116 | *p++ = ch; |
| 8117 | } |
| 8118 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8119 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8120 | else { |
| 8121 | Py_UCS4 ucs = ch; |
| 8122 | |
| 8123 | #ifndef Py_UNICODE_WIDE |
| 8124 | Py_UNICODE ch2 = 0; |
| 8125 | /* Get code point from surrogate pair */ |
| 8126 | if (size > 0) { |
| 8127 | ch2 = *s; |
| 8128 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8129 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8130 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8131 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8132 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8133 | size--; |
| 8134 | } |
| 8135 | } |
| 8136 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8137 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8138 | (categories Z* and C* except ASCII space) |
| 8139 | */ |
| 8140 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8141 | /* Map 8-bit characters to '\xhh' */ |
| 8142 | if (ucs <= 0xff) { |
| 8143 | *p++ = '\\'; |
| 8144 | *p++ = 'x'; |
| 8145 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8146 | *p++ = hexdigits[ch & 0x000F]; |
| 8147 | } |
| 8148 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8149 | else if (ucs >= 0x10000) { |
| 8150 | *p++ = '\\'; |
| 8151 | *p++ = 'U'; |
| 8152 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8153 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8154 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8155 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8156 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8157 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8158 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8159 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8160 | } |
| 8161 | /* Map 16-bit characters to '\uxxxx' */ |
| 8162 | else { |
| 8163 | *p++ = '\\'; |
| 8164 | *p++ = 'u'; |
| 8165 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8166 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8167 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8168 | *p++ = hexdigits[ucs & 0x000F]; |
| 8169 | } |
| 8170 | } |
| 8171 | /* Copy characters as-is */ |
| 8172 | else { |
| 8173 | *p++ = ch; |
| 8174 | #ifndef Py_UNICODE_WIDE |
| 8175 | if (ucs >= 0x10000) |
| 8176 | *p++ = ch2; |
| 8177 | #endif |
| 8178 | } |
| 8179 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8180 | } |
| 8181 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8182 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8183 | |
| 8184 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8185 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8186 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | } |
| 8188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8189 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8190 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8191 | \n\ |
| 8192 | 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] | 8193 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8194 | arguments start and end are interpreted as in slice notation.\n\ |
| 8195 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8196 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8197 | |
| 8198 | static PyObject * |
| 8199 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8200 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8201 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8202 | Py_ssize_t start; |
| 8203 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8204 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8205 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8206 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8207 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8208 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8209 | result = stringlib_rfind_slice( |
| 8210 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8211 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8212 | start, end |
| 8213 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8214 | |
| 8215 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8216 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8217 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8218 | } |
| 8219 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8220 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8221 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8222 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8223 | 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] | 8224 | |
| 8225 | static PyObject * |
| 8226 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8227 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8228 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8229 | Py_ssize_t start; |
| 8230 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8231 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8232 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8233 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8234 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8235 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8236 | result = stringlib_rfind_slice( |
| 8237 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8238 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8239 | start, end |
| 8240 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8241 | |
| 8242 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8243 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8244 | if (result < 0) { |
| 8245 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8246 | return NULL; |
| 8247 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8248 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8249 | } |
| 8250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8251 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8252 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8253 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8254 | 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] | 8255 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8256 | |
| 8257 | static PyObject * |
| 8258 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8259 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8260 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8261 | Py_UNICODE fillchar = ' '; |
| 8262 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8263 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8264 | return NULL; |
| 8265 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8266 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8267 | Py_INCREF(self); |
| 8268 | return (PyObject*) self; |
| 8269 | } |
| 8270 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8271 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | } |
| 8273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8274 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8275 | PyObject *sep, |
| 8276 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8277 | { |
| 8278 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8280 | s = PyUnicode_FromObject(s); |
| 8281 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8282 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8283 | if (sep != NULL) { |
| 8284 | sep = PyUnicode_FromObject(sep); |
| 8285 | if (sep == NULL) { |
| 8286 | Py_DECREF(s); |
| 8287 | return NULL; |
| 8288 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8289 | } |
| 8290 | |
| 8291 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8292 | |
| 8293 | Py_DECREF(s); |
| 8294 | Py_XDECREF(sep); |
| 8295 | return result; |
| 8296 | } |
| 8297 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8298 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8299 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8300 | \n\ |
| 8301 | Return a list of the words in S, using sep as the\n\ |
| 8302 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8303 | 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] | 8304 | whitespace string is a separator and empty strings are\n\ |
| 8305 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8306 | |
| 8307 | static PyObject* |
| 8308 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8309 | { |
| 8310 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8311 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8312 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8313 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8314 | return NULL; |
| 8315 | |
| 8316 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8317 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8318 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8319 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8320 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8321 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8322 | } |
| 8323 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8324 | PyObject * |
| 8325 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8326 | { |
| 8327 | PyObject* str_obj; |
| 8328 | PyObject* sep_obj; |
| 8329 | PyObject* out; |
| 8330 | |
| 8331 | str_obj = PyUnicode_FromObject(str_in); |
| 8332 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8334 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8335 | if (!sep_obj) { |
| 8336 | Py_DECREF(str_obj); |
| 8337 | return NULL; |
| 8338 | } |
| 8339 | |
| 8340 | out = stringlib_partition( |
| 8341 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8342 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8343 | ); |
| 8344 | |
| 8345 | Py_DECREF(sep_obj); |
| 8346 | Py_DECREF(str_obj); |
| 8347 | |
| 8348 | return out; |
| 8349 | } |
| 8350 | |
| 8351 | |
| 8352 | PyObject * |
| 8353 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8354 | { |
| 8355 | PyObject* str_obj; |
| 8356 | PyObject* sep_obj; |
| 8357 | PyObject* out; |
| 8358 | |
| 8359 | str_obj = PyUnicode_FromObject(str_in); |
| 8360 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8361 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8362 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8363 | if (!sep_obj) { |
| 8364 | Py_DECREF(str_obj); |
| 8365 | return NULL; |
| 8366 | } |
| 8367 | |
| 8368 | out = stringlib_rpartition( |
| 8369 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8370 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8371 | ); |
| 8372 | |
| 8373 | Py_DECREF(sep_obj); |
| 8374 | Py_DECREF(str_obj); |
| 8375 | |
| 8376 | return out; |
| 8377 | } |
| 8378 | |
| 8379 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8381 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8382 | 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] | 8383 | 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] | 8384 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8385 | |
| 8386 | static PyObject* |
| 8387 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8388 | { |
| 8389 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8390 | } |
| 8391 | |
| 8392 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8393 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8394 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8395 | 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] | 8396 | 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] | 8397 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8398 | |
| 8399 | static PyObject* |
| 8400 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8401 | { |
| 8402 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8403 | } |
| 8404 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8405 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8406 | PyObject *sep, |
| 8407 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8408 | { |
| 8409 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8410 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8411 | s = PyUnicode_FromObject(s); |
| 8412 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8413 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8414 | if (sep != NULL) { |
| 8415 | sep = PyUnicode_FromObject(sep); |
| 8416 | if (sep == NULL) { |
| 8417 | Py_DECREF(s); |
| 8418 | return NULL; |
| 8419 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8420 | } |
| 8421 | |
| 8422 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8423 | |
| 8424 | Py_DECREF(s); |
| 8425 | Py_XDECREF(sep); |
| 8426 | return result; |
| 8427 | } |
| 8428 | |
| 8429 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8430 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8431 | \n\ |
| 8432 | Return a list of the words in S, using sep as the\n\ |
| 8433 | delimiter string, starting at the end of the string and\n\ |
| 8434 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8435 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8436 | is a separator."); |
| 8437 | |
| 8438 | static PyObject* |
| 8439 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8440 | { |
| 8441 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8442 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8443 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8444 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8445 | return NULL; |
| 8446 | |
| 8447 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8448 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8449 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8450 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8451 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8453 | } |
| 8454 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8455 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8457 | \n\ |
| 8458 | 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] | 8459 | 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] | 8460 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8461 | |
| 8462 | static PyObject* |
| 8463 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8464 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8465 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8466 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8467 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8468 | return NULL; |
| 8469 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8470 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8471 | } |
| 8472 | |
| 8473 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8474 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8475 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8476 | if (PyUnicode_CheckExact(self)) { |
| 8477 | Py_INCREF(self); |
| 8478 | return self; |
| 8479 | } else |
| 8480 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8481 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8482 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8483 | } |
| 8484 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8485 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8486 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8487 | \n\ |
| 8488 | 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] | 8489 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8490 | |
| 8491 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8492 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8493 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8494 | return fixup(self, fixswapcase); |
| 8495 | } |
| 8496 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8497 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8498 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8499 | \n\ |
| 8500 | Return a translation table usable for str.translate().\n\ |
| 8501 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8502 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8503 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8504 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8505 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8506 | character at the same position in y. If there is a third argument, it\n\ |
| 8507 | must be a string, whose characters will be mapped to None in the result."); |
| 8508 | |
| 8509 | static PyObject* |
| 8510 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8511 | { |
| 8512 | PyObject *x, *y = NULL, *z = NULL; |
| 8513 | PyObject *new = NULL, *key, *value; |
| 8514 | Py_ssize_t i = 0; |
| 8515 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8516 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8517 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8518 | return NULL; |
| 8519 | new = PyDict_New(); |
| 8520 | if (!new) |
| 8521 | return NULL; |
| 8522 | if (y != NULL) { |
| 8523 | /* x must be a string too, of equal length */ |
| 8524 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8525 | if (!PyUnicode_Check(x)) { |
| 8526 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8527 | "be a string if there is a second argument"); |
| 8528 | goto err; |
| 8529 | } |
| 8530 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8531 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8532 | "arguments must have equal length"); |
| 8533 | goto err; |
| 8534 | } |
| 8535 | /* create entries for translating chars in x to those in y */ |
| 8536 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8537 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8538 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8539 | if (!key || !value) |
| 8540 | goto err; |
| 8541 | res = PyDict_SetItem(new, key, value); |
| 8542 | Py_DECREF(key); |
| 8543 | Py_DECREF(value); |
| 8544 | if (res < 0) |
| 8545 | goto err; |
| 8546 | } |
| 8547 | /* create entries for deleting chars in z */ |
| 8548 | if (z != NULL) { |
| 8549 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8550 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8551 | if (!key) |
| 8552 | goto err; |
| 8553 | res = PyDict_SetItem(new, key, Py_None); |
| 8554 | Py_DECREF(key); |
| 8555 | if (res < 0) |
| 8556 | goto err; |
| 8557 | } |
| 8558 | } |
| 8559 | } else { |
| 8560 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8561 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8562 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8563 | "to maketrans it must be a dict"); |
| 8564 | goto err; |
| 8565 | } |
| 8566 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8567 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8568 | if (PyUnicode_Check(key)) { |
| 8569 | /* convert string keys to integer keys */ |
| 8570 | PyObject *newkey; |
| 8571 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8572 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8573 | "table must be of length 1"); |
| 8574 | goto err; |
| 8575 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8576 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8577 | if (!newkey) |
| 8578 | goto err; |
| 8579 | res = PyDict_SetItem(new, newkey, value); |
| 8580 | Py_DECREF(newkey); |
| 8581 | if (res < 0) |
| 8582 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8583 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8584 | /* just keep integer keys */ |
| 8585 | if (PyDict_SetItem(new, key, value) < 0) |
| 8586 | goto err; |
| 8587 | } else { |
| 8588 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8589 | "be strings or integers"); |
| 8590 | goto err; |
| 8591 | } |
| 8592 | } |
| 8593 | } |
| 8594 | return new; |
| 8595 | err: |
| 8596 | Py_DECREF(new); |
| 8597 | return NULL; |
| 8598 | } |
| 8599 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8600 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8601 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8602 | \n\ |
| 8603 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8604 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8605 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8606 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8607 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8608 | |
| 8609 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8610 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8611 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8612 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8613 | } |
| 8614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8615 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8616 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8617 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8618 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8619 | |
| 8620 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8621 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8622 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8623 | return fixup(self, fixupper); |
| 8624 | } |
| 8625 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8626 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8627 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8628 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8629 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8630 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8631 | |
| 8632 | static PyObject * |
| 8633 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8634 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8635 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8636 | PyUnicodeObject *u; |
| 8637 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8638 | Py_ssize_t width; |
| 8639 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8640 | return NULL; |
| 8641 | |
| 8642 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8643 | if (PyUnicode_CheckExact(self)) { |
| 8644 | Py_INCREF(self); |
| 8645 | return (PyObject*) self; |
| 8646 | } |
| 8647 | else |
| 8648 | return PyUnicode_FromUnicode( |
| 8649 | PyUnicode_AS_UNICODE(self), |
| 8650 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8651 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8652 | } |
| 8653 | |
| 8654 | fill = width - self->length; |
| 8655 | |
| 8656 | u = pad(self, fill, 0, '0'); |
| 8657 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8658 | if (u == NULL) |
| 8659 | return NULL; |
| 8660 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8661 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8662 | /* move sign to beginning of string */ |
| 8663 | u->str[0] = u->str[fill]; |
| 8664 | u->str[fill] = '0'; |
| 8665 | } |
| 8666 | |
| 8667 | return (PyObject*) u; |
| 8668 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8669 | |
| 8670 | #if 0 |
| 8671 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8672 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8673 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8674 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8675 | } |
| 8676 | #endif |
| 8677 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8678 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8679 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8680 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8681 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8682 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8683 | With optional end, stop comparing S at that position.\n\ |
| 8684 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8685 | |
| 8686 | static PyObject * |
| 8687 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8688 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8689 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8690 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8691 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8692 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8693 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8694 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8695 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8696 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8697 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8698 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8699 | if (PyTuple_Check(subobj)) { |
| 8700 | Py_ssize_t i; |
| 8701 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8702 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8703 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8704 | if (substring == NULL) |
| 8705 | return NULL; |
| 8706 | result = tailmatch(self, substring, start, end, -1); |
| 8707 | Py_DECREF(substring); |
| 8708 | if (result) { |
| 8709 | Py_RETURN_TRUE; |
| 8710 | } |
| 8711 | } |
| 8712 | /* nothing matched */ |
| 8713 | Py_RETURN_FALSE; |
| 8714 | } |
| 8715 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8717 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8718 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8719 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8720 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8721 | } |
| 8722 | |
| 8723 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8724 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8725 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8726 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8727 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8728 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8729 | With optional end, stop comparing S at that position.\n\ |
| 8730 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8731 | |
| 8732 | static PyObject * |
| 8733 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8734 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8735 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8736 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8737 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8738 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8739 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8740 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8741 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8742 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8743 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8744 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8745 | if (PyTuple_Check(subobj)) { |
| 8746 | Py_ssize_t i; |
| 8747 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8748 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8749 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8750 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8751 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8752 | result = tailmatch(self, substring, start, end, +1); |
| 8753 | Py_DECREF(substring); |
| 8754 | if (result) { |
| 8755 | Py_RETURN_TRUE; |
| 8756 | } |
| 8757 | } |
| 8758 | Py_RETURN_FALSE; |
| 8759 | } |
| 8760 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8761 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8762 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8763 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8764 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8765 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8766 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8767 | } |
| 8768 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8769 | #include "stringlib/string_format.h" |
| 8770 | |
| 8771 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8772 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8773 | \n\ |
| 8774 | "); |
| 8775 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8776 | static PyObject * |
| 8777 | unicode__format__(PyObject* self, PyObject* args) |
| 8778 | { |
| 8779 | PyObject *format_spec; |
| 8780 | |
| 8781 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8782 | return NULL; |
| 8783 | |
| 8784 | return _PyUnicode_FormatAdvanced(self, |
| 8785 | PyUnicode_AS_UNICODE(format_spec), |
| 8786 | PyUnicode_GET_SIZE(format_spec)); |
| 8787 | } |
| 8788 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8789 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8790 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8791 | \n\ |
| 8792 | "); |
| 8793 | |
| 8794 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8795 | unicode__sizeof__(PyUnicodeObject *v) |
| 8796 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8797 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8798 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8799 | } |
| 8800 | |
| 8801 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8802 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8803 | |
| 8804 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8805 | unicode_getnewargs(PyUnicodeObject *v) |
| 8806 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8807 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8808 | } |
| 8809 | |
| 8810 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8811 | static PyMethodDef unicode_methods[] = { |
| 8812 | |
| 8813 | /* Order is according to common usage: often used methods should |
| 8814 | appear first, since lookup is done sequentially. */ |
| 8815 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8816 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8817 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8818 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8819 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8820 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8821 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8822 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8823 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8824 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8825 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8826 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8827 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8828 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8829 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8830 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8831 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8832 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8833 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8834 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8835 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8836 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8837 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8838 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8839 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8840 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8841 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8842 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8843 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8844 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8845 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8846 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8847 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8848 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8849 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8850 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8851 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8852 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8853 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8854 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8855 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8856 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8857 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8858 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8859 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8860 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8861 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8862 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8863 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8864 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8865 | #endif |
| 8866 | |
| 8867 | #if 0 |
| 8868 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8869 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8870 | #endif |
| 8871 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8872 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8873 | {NULL, NULL} |
| 8874 | }; |
| 8875 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8876 | static PyObject * |
| 8877 | unicode_mod(PyObject *v, PyObject *w) |
| 8878 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8879 | if (!PyUnicode_Check(v)) { |
| 8880 | Py_INCREF(Py_NotImplemented); |
| 8881 | return Py_NotImplemented; |
| 8882 | } |
| 8883 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8884 | } |
| 8885 | |
| 8886 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8887 | 0, /*nb_add*/ |
| 8888 | 0, /*nb_subtract*/ |
| 8889 | 0, /*nb_multiply*/ |
| 8890 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8891 | }; |
| 8892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8893 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8894 | (lenfunc) unicode_length, /* sq_length */ |
| 8895 | PyUnicode_Concat, /* sq_concat */ |
| 8896 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8897 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8898 | 0, /* sq_slice */ |
| 8899 | 0, /* sq_ass_item */ |
| 8900 | 0, /* sq_ass_slice */ |
| 8901 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8902 | }; |
| 8903 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8904 | static PyObject* |
| 8905 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8906 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8907 | if (PyIndex_Check(item)) { |
| 8908 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8909 | if (i == -1 && PyErr_Occurred()) |
| 8910 | return NULL; |
| 8911 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8912 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8913 | return unicode_getitem(self, i); |
| 8914 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8915 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8916 | Py_UNICODE* source_buf; |
| 8917 | Py_UNICODE* result_buf; |
| 8918 | PyObject* result; |
| 8919 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8920 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8921 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8922 | return NULL; |
| 8923 | } |
| 8924 | |
| 8925 | if (slicelength <= 0) { |
| 8926 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8927 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8928 | PyUnicode_CheckExact(self)) { |
| 8929 | Py_INCREF(self); |
| 8930 | return (PyObject *)self; |
| 8931 | } else if (step == 1) { |
| 8932 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8933 | } else { |
| 8934 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8935 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8936 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8937 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8938 | if (result_buf == NULL) |
| 8939 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8940 | |
| 8941 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8942 | result_buf[i] = source_buf[cur]; |
| 8943 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8944 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8945 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8946 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8947 | return result; |
| 8948 | } |
| 8949 | } else { |
| 8950 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8951 | return NULL; |
| 8952 | } |
| 8953 | } |
| 8954 | |
| 8955 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8956 | (lenfunc)unicode_length, /* mp_length */ |
| 8957 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8958 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8959 | }; |
| 8960 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8962 | /* Helpers for PyUnicode_Format() */ |
| 8963 | |
| 8964 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8965 | 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] | 8966 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8967 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8968 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8969 | (*p_argidx)++; |
| 8970 | if (arglen < 0) |
| 8971 | return args; |
| 8972 | else |
| 8973 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8974 | } |
| 8975 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8976 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8977 | return NULL; |
| 8978 | } |
| 8979 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8980 | /* 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] | 8981 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8982 | static PyObject * |
| 8983 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8984 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8985 | char *p; |
| 8986 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8987 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8988 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8989 | x = PyFloat_AsDouble(v); |
| 8990 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8991 | return NULL; |
| 8992 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8994 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8995 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8996 | p = PyOS_double_to_string(x, type, prec, |
| 8997 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8998 | if (p == NULL) |
| 8999 | return NULL; |
| 9000 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9001 | PyMem_Free(p); |
| 9002 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9003 | } |
| 9004 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9005 | static PyObject* |
| 9006 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9007 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9008 | char *buf; |
| 9009 | int len; |
| 9010 | PyObject *str; /* temporary string object. */ |
| 9011 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9012 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9013 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9014 | if (!str) |
| 9015 | return NULL; |
| 9016 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9017 | Py_DECREF(str); |
| 9018 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9019 | } |
| 9020 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9021 | static int |
| 9022 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9023 | size_t buflen, |
| 9024 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9025 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9026 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9027 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9028 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9029 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9030 | buf[1] = '\0'; |
| 9031 | return 1; |
| 9032 | } |
| 9033 | #ifndef Py_UNICODE_WIDE |
| 9034 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9035 | /* Decode a valid surrogate pair */ |
| 9036 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9037 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9038 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9039 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9040 | buf[0] = c0; |
| 9041 | buf[1] = c1; |
| 9042 | buf[2] = '\0'; |
| 9043 | return 2; |
| 9044 | } |
| 9045 | } |
| 9046 | #endif |
| 9047 | goto onError; |
| 9048 | } |
| 9049 | else { |
| 9050 | /* Integer input truncated to a character */ |
| 9051 | long x; |
| 9052 | x = PyLong_AsLong(v); |
| 9053 | if (x == -1 && PyErr_Occurred()) |
| 9054 | goto onError; |
| 9055 | |
| 9056 | if (x < 0 || x > 0x10ffff) { |
| 9057 | PyErr_SetString(PyExc_OverflowError, |
| 9058 | "%c arg not in range(0x110000)"); |
| 9059 | return -1; |
| 9060 | } |
| 9061 | |
| 9062 | #ifndef Py_UNICODE_WIDE |
| 9063 | if (x > 0xffff) { |
| 9064 | x -= 0x10000; |
| 9065 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9066 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9067 | return 2; |
| 9068 | } |
| 9069 | #endif |
| 9070 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9071 | buf[1] = '\0'; |
| 9072 | return 1; |
| 9073 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9074 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9075 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9076 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9077 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9078 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9079 | } |
| 9080 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9081 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9082 | 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] | 9083 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9084 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9085 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9086 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9087 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9088 | { |
| 9089 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9090 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9091 | int args_owned = 0; |
| 9092 | PyUnicodeObject *result = NULL; |
| 9093 | PyObject *dict = NULL; |
| 9094 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9096 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9097 | PyErr_BadInternalCall(); |
| 9098 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9099 | } |
| 9100 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9101 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9102 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9103 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9104 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9105 | |
| 9106 | reslen = rescnt = fmtcnt + 100; |
| 9107 | result = _PyUnicode_New(reslen); |
| 9108 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9109 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | res = PyUnicode_AS_UNICODE(result); |
| 9111 | |
| 9112 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9113 | arglen = PyTuple_Size(args); |
| 9114 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9115 | } |
| 9116 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9117 | arglen = -1; |
| 9118 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9119 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9120 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9121 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9122 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9123 | |
| 9124 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9125 | if (*fmt != '%') { |
| 9126 | if (--rescnt < 0) { |
| 9127 | rescnt = fmtcnt + 100; |
| 9128 | reslen += rescnt; |
| 9129 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9130 | goto onError; |
| 9131 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9132 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9133 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9134 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9135 | } |
| 9136 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9137 | /* Got a format specifier */ |
| 9138 | int flags = 0; |
| 9139 | Py_ssize_t width = -1; |
| 9140 | int prec = -1; |
| 9141 | Py_UNICODE c = '\0'; |
| 9142 | Py_UNICODE fill; |
| 9143 | int isnumok; |
| 9144 | PyObject *v = NULL; |
| 9145 | PyObject *temp = NULL; |
| 9146 | Py_UNICODE *pbuf; |
| 9147 | Py_UNICODE sign; |
| 9148 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9149 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9150 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9151 | fmt++; |
| 9152 | if (*fmt == '(') { |
| 9153 | Py_UNICODE *keystart; |
| 9154 | Py_ssize_t keylen; |
| 9155 | PyObject *key; |
| 9156 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9157 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9158 | if (dict == NULL) { |
| 9159 | PyErr_SetString(PyExc_TypeError, |
| 9160 | "format requires a mapping"); |
| 9161 | goto onError; |
| 9162 | } |
| 9163 | ++fmt; |
| 9164 | --fmtcnt; |
| 9165 | keystart = fmt; |
| 9166 | /* Skip over balanced parentheses */ |
| 9167 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9168 | if (*fmt == ')') |
| 9169 | --pcount; |
| 9170 | else if (*fmt == '(') |
| 9171 | ++pcount; |
| 9172 | fmt++; |
| 9173 | } |
| 9174 | keylen = fmt - keystart - 1; |
| 9175 | if (fmtcnt < 0 || pcount > 0) { |
| 9176 | PyErr_SetString(PyExc_ValueError, |
| 9177 | "incomplete format key"); |
| 9178 | goto onError; |
| 9179 | } |
| 9180 | #if 0 |
| 9181 | /* keys are converted to strings using UTF-8 and |
| 9182 | then looked up since Python uses strings to hold |
| 9183 | variables names etc. in its namespaces and we |
| 9184 | wouldn't want to break common idioms. */ |
| 9185 | key = PyUnicode_EncodeUTF8(keystart, |
| 9186 | keylen, |
| 9187 | NULL); |
| 9188 | #else |
| 9189 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9190 | #endif |
| 9191 | if (key == NULL) |
| 9192 | goto onError; |
| 9193 | if (args_owned) { |
| 9194 | Py_DECREF(args); |
| 9195 | args_owned = 0; |
| 9196 | } |
| 9197 | args = PyObject_GetItem(dict, key); |
| 9198 | Py_DECREF(key); |
| 9199 | if (args == NULL) { |
| 9200 | goto onError; |
| 9201 | } |
| 9202 | args_owned = 1; |
| 9203 | arglen = -1; |
| 9204 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9205 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9206 | while (--fmtcnt >= 0) { |
| 9207 | switch (c = *fmt++) { |
| 9208 | case '-': flags |= F_LJUST; continue; |
| 9209 | case '+': flags |= F_SIGN; continue; |
| 9210 | case ' ': flags |= F_BLANK; continue; |
| 9211 | case '#': flags |= F_ALT; continue; |
| 9212 | case '0': flags |= F_ZERO; continue; |
| 9213 | } |
| 9214 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9215 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9216 | if (c == '*') { |
| 9217 | v = getnextarg(args, arglen, &argidx); |
| 9218 | if (v == NULL) |
| 9219 | goto onError; |
| 9220 | if (!PyLong_Check(v)) { |
| 9221 | PyErr_SetString(PyExc_TypeError, |
| 9222 | "* wants int"); |
| 9223 | goto onError; |
| 9224 | } |
| 9225 | width = PyLong_AsLong(v); |
| 9226 | if (width == -1 && PyErr_Occurred()) |
| 9227 | goto onError; |
| 9228 | if (width < 0) { |
| 9229 | flags |= F_LJUST; |
| 9230 | width = -width; |
| 9231 | } |
| 9232 | if (--fmtcnt >= 0) |
| 9233 | c = *fmt++; |
| 9234 | } |
| 9235 | else if (c >= '0' && c <= '9') { |
| 9236 | width = c - '0'; |
| 9237 | while (--fmtcnt >= 0) { |
| 9238 | c = *fmt++; |
| 9239 | if (c < '0' || c > '9') |
| 9240 | break; |
| 9241 | if ((width*10) / 10 != width) { |
| 9242 | PyErr_SetString(PyExc_ValueError, |
| 9243 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9244 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9245 | } |
| 9246 | width = width*10 + (c - '0'); |
| 9247 | } |
| 9248 | } |
| 9249 | if (c == '.') { |
| 9250 | prec = 0; |
| 9251 | if (--fmtcnt >= 0) |
| 9252 | c = *fmt++; |
| 9253 | if (c == '*') { |
| 9254 | v = getnextarg(args, arglen, &argidx); |
| 9255 | if (v == NULL) |
| 9256 | goto onError; |
| 9257 | if (!PyLong_Check(v)) { |
| 9258 | PyErr_SetString(PyExc_TypeError, |
| 9259 | "* wants int"); |
| 9260 | goto onError; |
| 9261 | } |
| 9262 | prec = PyLong_AsLong(v); |
| 9263 | if (prec == -1 && PyErr_Occurred()) |
| 9264 | goto onError; |
| 9265 | if (prec < 0) |
| 9266 | prec = 0; |
| 9267 | if (--fmtcnt >= 0) |
| 9268 | c = *fmt++; |
| 9269 | } |
| 9270 | else if (c >= '0' && c <= '9') { |
| 9271 | prec = c - '0'; |
| 9272 | while (--fmtcnt >= 0) { |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 9273 | c = *fmt++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9274 | if (c < '0' || c > '9') |
| 9275 | break; |
| 9276 | if ((prec*10) / 10 != prec) { |
| 9277 | PyErr_SetString(PyExc_ValueError, |
| 9278 | "prec too big"); |
| 9279 | goto onError; |
| 9280 | } |
| 9281 | prec = prec*10 + (c - '0'); |
| 9282 | } |
| 9283 | } |
| 9284 | } /* prec */ |
| 9285 | if (fmtcnt >= 0) { |
| 9286 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9287 | if (--fmtcnt >= 0) |
| 9288 | c = *fmt++; |
| 9289 | } |
| 9290 | } |
| 9291 | if (fmtcnt < 0) { |
| 9292 | PyErr_SetString(PyExc_ValueError, |
| 9293 | "incomplete format"); |
| 9294 | goto onError; |
| 9295 | } |
| 9296 | if (c != '%') { |
| 9297 | v = getnextarg(args, arglen, &argidx); |
| 9298 | if (v == NULL) |
| 9299 | goto onError; |
| 9300 | } |
| 9301 | sign = 0; |
| 9302 | fill = ' '; |
| 9303 | switch (c) { |
| 9304 | |
| 9305 | case '%': |
| 9306 | pbuf = formatbuf; |
| 9307 | /* presume that buffer length is at least 1 */ |
| 9308 | pbuf[0] = '%'; |
| 9309 | len = 1; |
| 9310 | break; |
| 9311 | |
| 9312 | case 's': |
| 9313 | case 'r': |
| 9314 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9315 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9316 | temp = v; |
| 9317 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9318 | } |
| 9319 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9320 | if (c == 's') |
| 9321 | temp = PyObject_Str(v); |
| 9322 | else if (c == 'r') |
| 9323 | temp = PyObject_Repr(v); |
| 9324 | else |
| 9325 | temp = PyObject_ASCII(v); |
| 9326 | if (temp == NULL) |
| 9327 | goto onError; |
| 9328 | if (PyUnicode_Check(temp)) |
| 9329 | /* nothing to do */; |
| 9330 | else { |
| 9331 | Py_DECREF(temp); |
| 9332 | PyErr_SetString(PyExc_TypeError, |
| 9333 | "%s argument has non-string str()"); |
| 9334 | goto onError; |
| 9335 | } |
| 9336 | } |
| 9337 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9338 | len = PyUnicode_GET_SIZE(temp); |
| 9339 | if (prec >= 0 && len > prec) |
| 9340 | len = prec; |
| 9341 | break; |
| 9342 | |
| 9343 | case 'i': |
| 9344 | case 'd': |
| 9345 | case 'u': |
| 9346 | case 'o': |
| 9347 | case 'x': |
| 9348 | case 'X': |
| 9349 | if (c == 'i') |
| 9350 | c = 'd'; |
| 9351 | isnumok = 0; |
| 9352 | if (PyNumber_Check(v)) { |
| 9353 | PyObject *iobj=NULL; |
| 9354 | |
| 9355 | if (PyLong_Check(v)) { |
| 9356 | iobj = v; |
| 9357 | Py_INCREF(iobj); |
| 9358 | } |
| 9359 | else { |
| 9360 | iobj = PyNumber_Long(v); |
| 9361 | } |
| 9362 | if (iobj!=NULL) { |
| 9363 | if (PyLong_Check(iobj)) { |
| 9364 | isnumok = 1; |
| 9365 | temp = formatlong(iobj, flags, prec, c); |
| 9366 | Py_DECREF(iobj); |
| 9367 | if (!temp) |
| 9368 | goto onError; |
| 9369 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9370 | len = PyUnicode_GET_SIZE(temp); |
| 9371 | sign = 1; |
| 9372 | } |
| 9373 | else { |
| 9374 | Py_DECREF(iobj); |
| 9375 | } |
| 9376 | } |
| 9377 | } |
| 9378 | if (!isnumok) { |
| 9379 | PyErr_Format(PyExc_TypeError, |
| 9380 | "%%%c format: a number is required, " |
| 9381 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9382 | goto onError; |
| 9383 | } |
| 9384 | if (flags & F_ZERO) |
| 9385 | fill = '0'; |
| 9386 | break; |
| 9387 | |
| 9388 | case 'e': |
| 9389 | case 'E': |
| 9390 | case 'f': |
| 9391 | case 'F': |
| 9392 | case 'g': |
| 9393 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9394 | temp = formatfloat(v, flags, prec, c); |
| 9395 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9396 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9397 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9398 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9399 | sign = 1; |
| 9400 | if (flags & F_ZERO) |
| 9401 | fill = '0'; |
| 9402 | break; |
| 9403 | |
| 9404 | case 'c': |
| 9405 | pbuf = formatbuf; |
| 9406 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9407 | if (len < 0) |
| 9408 | goto onError; |
| 9409 | break; |
| 9410 | |
| 9411 | default: |
| 9412 | PyErr_Format(PyExc_ValueError, |
| 9413 | "unsupported format character '%c' (0x%x) " |
| 9414 | "at index %zd", |
| 9415 | (31<=c && c<=126) ? (char)c : '?', |
| 9416 | (int)c, |
| 9417 | (Py_ssize_t)(fmt - 1 - |
| 9418 | PyUnicode_AS_UNICODE(uformat))); |
| 9419 | goto onError; |
| 9420 | } |
| 9421 | if (sign) { |
| 9422 | if (*pbuf == '-' || *pbuf == '+') { |
| 9423 | sign = *pbuf++; |
| 9424 | len--; |
| 9425 | } |
| 9426 | else if (flags & F_SIGN) |
| 9427 | sign = '+'; |
| 9428 | else if (flags & F_BLANK) |
| 9429 | sign = ' '; |
| 9430 | else |
| 9431 | sign = 0; |
| 9432 | } |
| 9433 | if (width < len) |
| 9434 | width = len; |
| 9435 | if (rescnt - (sign != 0) < width) { |
| 9436 | reslen -= rescnt; |
| 9437 | rescnt = width + fmtcnt + 100; |
| 9438 | reslen += rescnt; |
| 9439 | if (reslen < 0) { |
| 9440 | Py_XDECREF(temp); |
| 9441 | PyErr_NoMemory(); |
| 9442 | goto onError; |
| 9443 | } |
| 9444 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9445 | Py_XDECREF(temp); |
| 9446 | goto onError; |
| 9447 | } |
| 9448 | res = PyUnicode_AS_UNICODE(result) |
| 9449 | + reslen - rescnt; |
| 9450 | } |
| 9451 | if (sign) { |
| 9452 | if (fill != ' ') |
| 9453 | *res++ = sign; |
| 9454 | rescnt--; |
| 9455 | if (width > len) |
| 9456 | width--; |
| 9457 | } |
| 9458 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9459 | assert(pbuf[0] == '0'); |
| 9460 | assert(pbuf[1] == c); |
| 9461 | if (fill != ' ') { |
| 9462 | *res++ = *pbuf++; |
| 9463 | *res++ = *pbuf++; |
| 9464 | } |
| 9465 | rescnt -= 2; |
| 9466 | width -= 2; |
| 9467 | if (width < 0) |
| 9468 | width = 0; |
| 9469 | len -= 2; |
| 9470 | } |
| 9471 | if (width > len && !(flags & F_LJUST)) { |
| 9472 | do { |
| 9473 | --rescnt; |
| 9474 | *res++ = fill; |
| 9475 | } while (--width > len); |
| 9476 | } |
| 9477 | if (fill == ' ') { |
| 9478 | if (sign) |
| 9479 | *res++ = sign; |
| 9480 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9481 | assert(pbuf[0] == '0'); |
| 9482 | assert(pbuf[1] == c); |
| 9483 | *res++ = *pbuf++; |
| 9484 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9485 | } |
| 9486 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9487 | Py_UNICODE_COPY(res, pbuf, len); |
| 9488 | res += len; |
| 9489 | rescnt -= len; |
| 9490 | while (--width >= len) { |
| 9491 | --rescnt; |
| 9492 | *res++ = ' '; |
| 9493 | } |
| 9494 | if (dict && (argidx < arglen) && c != '%') { |
| 9495 | PyErr_SetString(PyExc_TypeError, |
| 9496 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9497 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9498 | goto onError; |
| 9499 | } |
| 9500 | Py_XDECREF(temp); |
| 9501 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9502 | } /* until end */ |
| 9503 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9504 | PyErr_SetString(PyExc_TypeError, |
| 9505 | "not all arguments converted during string formatting"); |
| 9506 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9507 | } |
| 9508 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9509 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9510 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9511 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9512 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9513 | } |
| 9514 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9515 | return (PyObject *)result; |
| 9516 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9517 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9518 | Py_XDECREF(result); |
| 9519 | Py_DECREF(uformat); |
| 9520 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9521 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9522 | } |
| 9523 | return NULL; |
| 9524 | } |
| 9525 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9526 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9527 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9528 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9529 | static PyObject * |
| 9530 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9531 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9532 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9533 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9534 | char *encoding = NULL; |
| 9535 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9536 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9537 | if (type != &PyUnicode_Type) |
| 9538 | return unicode_subtype_new(type, args, kwds); |
| 9539 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9540 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9541 | return NULL; |
| 9542 | if (x == NULL) |
| 9543 | return (PyObject *)_PyUnicode_New(0); |
| 9544 | if (encoding == NULL && errors == NULL) |
| 9545 | return PyObject_Str(x); |
| 9546 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9547 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9548 | } |
| 9549 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9550 | static PyObject * |
| 9551 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9552 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9553 | PyUnicodeObject *tmp, *pnew; |
| 9554 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9555 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9556 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9557 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9558 | if (tmp == NULL) |
| 9559 | return NULL; |
| 9560 | assert(PyUnicode_Check(tmp)); |
| 9561 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9562 | if (pnew == NULL) { |
| 9563 | Py_DECREF(tmp); |
| 9564 | return NULL; |
| 9565 | } |
| 9566 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9567 | if (pnew->str == NULL) { |
| 9568 | _Py_ForgetReference((PyObject *)pnew); |
| 9569 | PyObject_Del(pnew); |
| 9570 | Py_DECREF(tmp); |
| 9571 | return PyErr_NoMemory(); |
| 9572 | } |
| 9573 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9574 | pnew->length = n; |
| 9575 | pnew->hash = tmp->hash; |
| 9576 | Py_DECREF(tmp); |
| 9577 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9578 | } |
| 9579 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9580 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9581 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9582 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9583 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9584 | encoding defaults to the current default string encoding.\n\ |
| 9585 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9586 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9587 | static PyObject *unicode_iter(PyObject *seq); |
| 9588 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9589 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9590 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9591 | "str", /* tp_name */ |
| 9592 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9593 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9594 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9595 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9596 | 0, /* tp_print */ |
| 9597 | 0, /* tp_getattr */ |
| 9598 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9599 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9600 | unicode_repr, /* tp_repr */ |
| 9601 | &unicode_as_number, /* tp_as_number */ |
| 9602 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9603 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9604 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9605 | 0, /* tp_call*/ |
| 9606 | (reprfunc) unicode_str, /* tp_str */ |
| 9607 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9608 | 0, /* tp_setattro */ |
| 9609 | 0, /* tp_as_buffer */ |
| 9610 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9611 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9612 | unicode_doc, /* tp_doc */ |
| 9613 | 0, /* tp_traverse */ |
| 9614 | 0, /* tp_clear */ |
| 9615 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9616 | 0, /* tp_weaklistoffset */ |
| 9617 | unicode_iter, /* tp_iter */ |
| 9618 | 0, /* tp_iternext */ |
| 9619 | unicode_methods, /* tp_methods */ |
| 9620 | 0, /* tp_members */ |
| 9621 | 0, /* tp_getset */ |
| 9622 | &PyBaseObject_Type, /* tp_base */ |
| 9623 | 0, /* tp_dict */ |
| 9624 | 0, /* tp_descr_get */ |
| 9625 | 0, /* tp_descr_set */ |
| 9626 | 0, /* tp_dictoffset */ |
| 9627 | 0, /* tp_init */ |
| 9628 | 0, /* tp_alloc */ |
| 9629 | unicode_new, /* tp_new */ |
| 9630 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9631 | }; |
| 9632 | |
| 9633 | /* Initialize the Unicode implementation */ |
| 9634 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9635 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9636 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9637 | int i; |
| 9638 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9639 | /* XXX - move this array to unicodectype.c ? */ |
| 9640 | Py_UNICODE linebreak[] = { |
| 9641 | 0x000A, /* LINE FEED */ |
| 9642 | 0x000D, /* CARRIAGE RETURN */ |
| 9643 | 0x001C, /* FILE SEPARATOR */ |
| 9644 | 0x001D, /* GROUP SEPARATOR */ |
| 9645 | 0x001E, /* RECORD SEPARATOR */ |
| 9646 | 0x0085, /* NEXT LINE */ |
| 9647 | 0x2028, /* LINE SEPARATOR */ |
| 9648 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9649 | }; |
| 9650 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9651 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9652 | free_list = NULL; |
| 9653 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9654 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9655 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9656 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9657 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9658 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9659 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9660 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9661 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9662 | |
| 9663 | /* initialize the linebreak bloom filter */ |
| 9664 | bloom_linebreak = make_bloom_mask( |
| 9665 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9666 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9667 | |
| 9668 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9669 | } |
| 9670 | |
| 9671 | /* Finalize the Unicode implementation */ |
| 9672 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9673 | int |
| 9674 | PyUnicode_ClearFreeList(void) |
| 9675 | { |
| 9676 | int freelist_size = numfree; |
| 9677 | PyUnicodeObject *u; |
| 9678 | |
| 9679 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9680 | PyUnicodeObject *v = u; |
| 9681 | u = *(PyUnicodeObject **)u; |
| 9682 | if (v->str) |
| 9683 | PyObject_DEL(v->str); |
| 9684 | Py_XDECREF(v->defenc); |
| 9685 | PyObject_Del(v); |
| 9686 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9687 | } |
| 9688 | free_list = NULL; |
| 9689 | assert(numfree == 0); |
| 9690 | return freelist_size; |
| 9691 | } |
| 9692 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9693 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9694 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9695 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9696 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9697 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9698 | Py_XDECREF(unicode_empty); |
| 9699 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9700 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9701 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9702 | if (unicode_latin1[i]) { |
| 9703 | Py_DECREF(unicode_latin1[i]); |
| 9704 | unicode_latin1[i] = NULL; |
| 9705 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9706 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9707 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9708 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9709 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9710 | void |
| 9711 | PyUnicode_InternInPlace(PyObject **p) |
| 9712 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9713 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9714 | PyObject *t; |
| 9715 | if (s == NULL || !PyUnicode_Check(s)) |
| 9716 | Py_FatalError( |
| 9717 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9718 | /* If it's a subclass, we don't really know what putting |
| 9719 | it in the interned dict might do. */ |
| 9720 | if (!PyUnicode_CheckExact(s)) |
| 9721 | return; |
| 9722 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9723 | return; |
| 9724 | if (interned == NULL) { |
| 9725 | interned = PyDict_New(); |
| 9726 | if (interned == NULL) { |
| 9727 | PyErr_Clear(); /* Don't leave an exception */ |
| 9728 | return; |
| 9729 | } |
| 9730 | } |
| 9731 | /* It might be that the GetItem call fails even |
| 9732 | though the key is present in the dictionary, |
| 9733 | namely when this happens during a stack overflow. */ |
| 9734 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9735 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9736 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9737 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9738 | if (t) { |
| 9739 | Py_INCREF(t); |
| 9740 | Py_DECREF(*p); |
| 9741 | *p = t; |
| 9742 | return; |
| 9743 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9744 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9745 | PyThreadState_GET()->recursion_critical = 1; |
| 9746 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9747 | PyErr_Clear(); |
| 9748 | PyThreadState_GET()->recursion_critical = 0; |
| 9749 | return; |
| 9750 | } |
| 9751 | PyThreadState_GET()->recursion_critical = 0; |
| 9752 | /* The two references in interned are not counted by refcnt. |
| 9753 | The deallocator will take care of this */ |
| 9754 | Py_REFCNT(s) -= 2; |
| 9755 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9756 | } |
| 9757 | |
| 9758 | void |
| 9759 | PyUnicode_InternImmortal(PyObject **p) |
| 9760 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9761 | PyUnicode_InternInPlace(p); |
| 9762 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9763 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9764 | Py_INCREF(*p); |
| 9765 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9766 | } |
| 9767 | |
| 9768 | PyObject * |
| 9769 | PyUnicode_InternFromString(const char *cp) |
| 9770 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9771 | PyObject *s = PyUnicode_FromString(cp); |
| 9772 | if (s == NULL) |
| 9773 | return NULL; |
| 9774 | PyUnicode_InternInPlace(&s); |
| 9775 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9776 | } |
| 9777 | |
| 9778 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9779 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9780 | PyObject *keys; |
| 9781 | PyUnicodeObject *s; |
| 9782 | Py_ssize_t i, n; |
| 9783 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9784 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9785 | if (interned == NULL || !PyDict_Check(interned)) |
| 9786 | return; |
| 9787 | keys = PyDict_Keys(interned); |
| 9788 | if (keys == NULL || !PyList_Check(keys)) { |
| 9789 | PyErr_Clear(); |
| 9790 | return; |
| 9791 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9792 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9793 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9794 | detector, interned unicode strings are not forcibly deallocated; |
| 9795 | rather, we give them their stolen references back, and then clear |
| 9796 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9797 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9798 | n = PyList_GET_SIZE(keys); |
| 9799 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9800 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9801 | for (i = 0; i < n; i++) { |
| 9802 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9803 | switch (s->state) { |
| 9804 | case SSTATE_NOT_INTERNED: |
| 9805 | /* XXX Shouldn't happen */ |
| 9806 | break; |
| 9807 | case SSTATE_INTERNED_IMMORTAL: |
| 9808 | Py_REFCNT(s) += 1; |
| 9809 | immortal_size += s->length; |
| 9810 | break; |
| 9811 | case SSTATE_INTERNED_MORTAL: |
| 9812 | Py_REFCNT(s) += 2; |
| 9813 | mortal_size += s->length; |
| 9814 | break; |
| 9815 | default: |
| 9816 | Py_FatalError("Inconsistent interned string state."); |
| 9817 | } |
| 9818 | s->state = SSTATE_NOT_INTERNED; |
| 9819 | } |
| 9820 | fprintf(stderr, "total size of all interned strings: " |
| 9821 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9822 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9823 | Py_DECREF(keys); |
| 9824 | PyDict_Clear(interned); |
| 9825 | Py_DECREF(interned); |
| 9826 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9827 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9828 | |
| 9829 | |
| 9830 | /********************* Unicode Iterator **************************/ |
| 9831 | |
| 9832 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9833 | PyObject_HEAD |
| 9834 | Py_ssize_t it_index; |
| 9835 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9836 | } unicodeiterobject; |
| 9837 | |
| 9838 | static void |
| 9839 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9840 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9841 | _PyObject_GC_UNTRACK(it); |
| 9842 | Py_XDECREF(it->it_seq); |
| 9843 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9844 | } |
| 9845 | |
| 9846 | static int |
| 9847 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9848 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9849 | Py_VISIT(it->it_seq); |
| 9850 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9851 | } |
| 9852 | |
| 9853 | static PyObject * |
| 9854 | unicodeiter_next(unicodeiterobject *it) |
| 9855 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9856 | PyUnicodeObject *seq; |
| 9857 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9858 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9859 | assert(it != NULL); |
| 9860 | seq = it->it_seq; |
| 9861 | if (seq == NULL) |
| 9862 | return NULL; |
| 9863 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9864 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9865 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9866 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9867 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9868 | if (item != NULL) |
| 9869 | ++it->it_index; |
| 9870 | return item; |
| 9871 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9872 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9873 | Py_DECREF(seq); |
| 9874 | it->it_seq = NULL; |
| 9875 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9876 | } |
| 9877 | |
| 9878 | static PyObject * |
| 9879 | unicodeiter_len(unicodeiterobject *it) |
| 9880 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9881 | Py_ssize_t len = 0; |
| 9882 | if (it->it_seq) |
| 9883 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9884 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9885 | } |
| 9886 | |
| 9887 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9888 | |
| 9889 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9890 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9891 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9892 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9893 | }; |
| 9894 | |
| 9895 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9896 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9897 | "str_iterator", /* tp_name */ |
| 9898 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9899 | 0, /* tp_itemsize */ |
| 9900 | /* methods */ |
| 9901 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9902 | 0, /* tp_print */ |
| 9903 | 0, /* tp_getattr */ |
| 9904 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9905 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9906 | 0, /* tp_repr */ |
| 9907 | 0, /* tp_as_number */ |
| 9908 | 0, /* tp_as_sequence */ |
| 9909 | 0, /* tp_as_mapping */ |
| 9910 | 0, /* tp_hash */ |
| 9911 | 0, /* tp_call */ |
| 9912 | 0, /* tp_str */ |
| 9913 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9914 | 0, /* tp_setattro */ |
| 9915 | 0, /* tp_as_buffer */ |
| 9916 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9917 | 0, /* tp_doc */ |
| 9918 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9919 | 0, /* tp_clear */ |
| 9920 | 0, /* tp_richcompare */ |
| 9921 | 0, /* tp_weaklistoffset */ |
| 9922 | PyObject_SelfIter, /* tp_iter */ |
| 9923 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9924 | unicodeiter_methods, /* tp_methods */ |
| 9925 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9926 | }; |
| 9927 | |
| 9928 | static PyObject * |
| 9929 | unicode_iter(PyObject *seq) |
| 9930 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9931 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9932 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9933 | if (!PyUnicode_Check(seq)) { |
| 9934 | PyErr_BadInternalCall(); |
| 9935 | return NULL; |
| 9936 | } |
| 9937 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9938 | if (it == NULL) |
| 9939 | return NULL; |
| 9940 | it->it_index = 0; |
| 9941 | Py_INCREF(seq); |
| 9942 | it->it_seq = (PyUnicodeObject *)seq; |
| 9943 | _PyObject_GC_TRACK(it); |
| 9944 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9945 | } |
| 9946 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9947 | size_t |
| 9948 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9949 | { |
| 9950 | int res = 0; |
| 9951 | while(*u++) |
| 9952 | res++; |
| 9953 | return res; |
| 9954 | } |
| 9955 | |
| 9956 | Py_UNICODE* |
| 9957 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9958 | { |
| 9959 | Py_UNICODE *u = s1; |
| 9960 | while ((*u++ = *s2++)); |
| 9961 | return s1; |
| 9962 | } |
| 9963 | |
| 9964 | Py_UNICODE* |
| 9965 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9966 | { |
| 9967 | Py_UNICODE *u = s1; |
| 9968 | while ((*u++ = *s2++)) |
| 9969 | if (n-- == 0) |
| 9970 | break; |
| 9971 | return s1; |
| 9972 | } |
| 9973 | |
| 9974 | int |
| 9975 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9976 | { |
| 9977 | while (*s1 && *s2 && *s1 == *s2) |
| 9978 | s1++, s2++; |
| 9979 | if (*s1 && *s2) |
| 9980 | return (*s1 < *s2) ? -1 : +1; |
| 9981 | if (*s1) |
| 9982 | return 1; |
| 9983 | if (*s2) |
| 9984 | return -1; |
| 9985 | return 0; |
| 9986 | } |
| 9987 | |
Victor Stinner | ef8d95c | 2010-08-16 22:03:11 +0000 | [diff] [blame] | 9988 | int |
| 9989 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9990 | { |
| 9991 | register Py_UNICODE u1, u2; |
| 9992 | for (; n != 0; n--) { |
| 9993 | u1 = *s1; |
| 9994 | u2 = *s2; |
| 9995 | if (u1 != u2) |
| 9996 | return (u1 < u2) ? -1 : +1; |
| 9997 | if (u1 == '\0') |
| 9998 | return 0; |
| 9999 | s1++; |
| 10000 | s2++; |
| 10001 | } |
| 10002 | return 0; |
| 10003 | } |
| 10004 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10005 | Py_UNICODE* |
| 10006 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10007 | { |
| 10008 | const Py_UNICODE *p; |
| 10009 | for (p = s; *p; p++) |
| 10010 | if (*p == c) |
| 10011 | return (Py_UNICODE*)p; |
| 10012 | return NULL; |
| 10013 | } |
| 10014 | |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 10015 | Py_UNICODE* |
| 10016 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10017 | { |
| 10018 | const Py_UNICODE *p; |
| 10019 | p = s + Py_UNICODE_strlen(s); |
| 10020 | while (p != s) { |
| 10021 | p--; |
| 10022 | if (*p == c) |
| 10023 | return (Py_UNICODE*)p; |
| 10024 | } |
| 10025 | return NULL; |
| 10026 | } |
| 10027 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10028 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10029 | #ifdef __cplusplus |
| 10030 | } |
| 10031 | #endif |