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 */ |
| 667 | #ifdef HAVE_USABLE_WCHAR_T |
| 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 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1170 | #ifdef HAVE_USABLE_WCHAR_T |
| 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 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1237 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1238 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1239 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1241 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1242 | PyErr_BadInternalCall(); |
| 1243 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1244 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1245 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1246 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1247 | PyErr_SetString(PyExc_TypeError, |
| 1248 | "decoding str is not supported"); |
| 1249 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1250 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1251 | |
| 1252 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1253 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1254 | s = PyBytes_AS_STRING(obj); |
| 1255 | len = PyBytes_GET_SIZE(obj); |
| 1256 | } |
| 1257 | else if (PyByteArray_Check(obj)) { |
| 1258 | s = PyByteArray_AS_STRING(obj); |
| 1259 | len = PyByteArray_GET_SIZE(obj); |
| 1260 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1261 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1262 | /* Overwrite the error message with something more useful in |
| 1263 | case of a TypeError. */ |
| 1264 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1265 | PyErr_Format(PyExc_TypeError, |
Georg Brandl | 952867a | 2010-06-27 10:17:12 +0000 | [diff] [blame] | 1266 | "coercing to str: need bytes, bytearray or char buffer, " |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1267 | "%.80s found", |
| 1268 | Py_TYPE(obj)->tp_name); |
| 1269 | goto onError; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1270 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1271 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1272 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1273 | if (len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1274 | Py_INCREF(unicode_empty); |
| 1275 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1276 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1277 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1278 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1279 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1280 | return v; |
| 1281 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1282 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1283 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1286 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1287 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 1288 | 1 on success. */ |
| 1289 | static int |
| 1290 | normalize_encoding(const char *encoding, |
| 1291 | char *lower, |
| 1292 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1293 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1294 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1295 | char *l; |
| 1296 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1297 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1298 | e = encoding; |
| 1299 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1300 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1301 | while (*e) { |
| 1302 | if (l == l_end) |
| 1303 | return 0; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1304 | if (ISUPPER(*e)) { |
| 1305 | *l++ = TOLOWER(*e++); |
| 1306 | } |
| 1307 | else if (*e == '_') { |
| 1308 | *l++ = '-'; |
| 1309 | e++; |
| 1310 | } |
| 1311 | else { |
| 1312 | *l++ = *e++; |
| 1313 | } |
| 1314 | } |
| 1315 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1316 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | PyObject *PyUnicode_Decode(const char *s, |
| 1320 | Py_ssize_t size, |
| 1321 | const char *encoding, |
| 1322 | const char *errors) |
| 1323 | { |
| 1324 | PyObject *buffer = NULL, *unicode; |
| 1325 | Py_buffer info; |
| 1326 | char lower[11]; /* Enough for any encoding shortcut */ |
| 1327 | |
| 1328 | if (encoding == NULL) |
| 1329 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1330 | |
| 1331 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1332 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1333 | if (strcmp(lower, "utf-8") == 0) |
| 1334 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 1335 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1336 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1337 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1338 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1339 | else if (strcmp(lower, "mbcs") == 0) |
| 1340 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1341 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1342 | else if (strcmp(lower, "ascii") == 0) |
| 1343 | return PyUnicode_DecodeASCII(s, size, errors); |
| 1344 | else if (strcmp(lower, "utf-16") == 0) |
| 1345 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1346 | else if (strcmp(lower, "utf-32") == 0) |
| 1347 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 1348 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1349 | |
| 1350 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1351 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1352 | 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] | 1353 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1354 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1355 | if (buffer == NULL) |
| 1356 | goto onError; |
| 1357 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1358 | if (unicode == NULL) |
| 1359 | goto onError; |
| 1360 | if (!PyUnicode_Check(unicode)) { |
| 1361 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1362 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1363 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1364 | Py_DECREF(unicode); |
| 1365 | goto onError; |
| 1366 | } |
| 1367 | Py_DECREF(buffer); |
| 1368 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1369 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1370 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1371 | Py_XDECREF(buffer); |
| 1372 | return NULL; |
| 1373 | } |
| 1374 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1375 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1376 | const char *encoding, |
| 1377 | const char *errors) |
| 1378 | { |
| 1379 | PyObject *v; |
| 1380 | |
| 1381 | if (!PyUnicode_Check(unicode)) { |
| 1382 | PyErr_BadArgument(); |
| 1383 | goto onError; |
| 1384 | } |
| 1385 | |
| 1386 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1387 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1388 | |
| 1389 | /* Decode via the codec registry */ |
| 1390 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1391 | if (v == NULL) |
| 1392 | goto onError; |
| 1393 | return v; |
| 1394 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1395 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1396 | return NULL; |
| 1397 | } |
| 1398 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1399 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1400 | const char *encoding, |
| 1401 | const char *errors) |
| 1402 | { |
| 1403 | PyObject *v; |
| 1404 | |
| 1405 | if (!PyUnicode_Check(unicode)) { |
| 1406 | PyErr_BadArgument(); |
| 1407 | goto onError; |
| 1408 | } |
| 1409 | |
| 1410 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1411 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1412 | |
| 1413 | /* Decode via the codec registry */ |
| 1414 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1415 | if (v == NULL) |
| 1416 | goto onError; |
| 1417 | if (!PyUnicode_Check(v)) { |
| 1418 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1419 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1420 | Py_TYPE(v)->tp_name); |
| 1421 | Py_DECREF(v); |
| 1422 | goto onError; |
| 1423 | } |
| 1424 | return v; |
| 1425 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1426 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1427 | return NULL; |
| 1428 | } |
| 1429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1430 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1431 | Py_ssize_t size, |
| 1432 | const char *encoding, |
| 1433 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1434 | { |
| 1435 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1437 | unicode = PyUnicode_FromUnicode(s, size); |
| 1438 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1439 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1440 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1441 | Py_DECREF(unicode); |
| 1442 | return v; |
| 1443 | } |
| 1444 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1445 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1446 | const char *encoding, |
| 1447 | const char *errors) |
| 1448 | { |
| 1449 | PyObject *v; |
| 1450 | |
| 1451 | if (!PyUnicode_Check(unicode)) { |
| 1452 | PyErr_BadArgument(); |
| 1453 | goto onError; |
| 1454 | } |
| 1455 | |
| 1456 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1457 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1458 | |
| 1459 | /* Encode via the codec registry */ |
| 1460 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1461 | if (v == NULL) |
| 1462 | goto onError; |
| 1463 | return v; |
| 1464 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1465 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1466 | return NULL; |
| 1467 | } |
| 1468 | |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1469 | PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode) |
| 1470 | { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1471 | if (Py_FileSystemDefaultEncoding) { |
| 1472 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1473 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) |
| 1474 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1475 | PyUnicode_GET_SIZE(unicode), |
| 1476 | NULL); |
| 1477 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1478 | return PyUnicode_AsEncodedString(unicode, |
| 1479 | Py_FileSystemDefaultEncoding, |
| 1480 | "surrogateescape"); |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1481 | } else |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1482 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Victor Stinner | 3119ed7 | 2010-08-18 22:26:50 +0000 | [diff] [blame^] | 1483 | PyUnicode_GET_SIZE(unicode), |
| 1484 | "surrogateescape"); |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1487 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1488 | const char *encoding, |
| 1489 | const char *errors) |
| 1490 | { |
| 1491 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 1492 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1493 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1494 | if (!PyUnicode_Check(unicode)) { |
| 1495 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1496 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1497 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1498 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1499 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1500 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1501 | |
| 1502 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1503 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
| 1504 | if (strcmp(lower, "utf-8") == 0) |
| 1505 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1506 | PyUnicode_GET_SIZE(unicode), |
| 1507 | errors); |
| 1508 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1509 | (strcmp(lower, "iso-8859-1") == 0)) |
| 1510 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 1511 | PyUnicode_GET_SIZE(unicode), |
| 1512 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1513 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1514 | else if (strcmp(lower, "mbcs") == 0) |
| 1515 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 1516 | PyUnicode_GET_SIZE(unicode), |
| 1517 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1518 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1519 | else if (strcmp(lower, "ascii") == 0) |
| 1520 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1521 | PyUnicode_GET_SIZE(unicode), |
| 1522 | errors); |
| 1523 | } |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1524 | /* During bootstrap, we may need to find the encodings |
| 1525 | package, to load the file system encoding, and require the |
| 1526 | file system encoding in order to load the encodings |
| 1527 | package. |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1528 | |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1529 | Break out of this dependency by assuming that the path to |
| 1530 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1531 | instead, if the file system encoding is the locale's |
| 1532 | encoding. */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 1533 | if (Py_FileSystemDefaultEncoding && |
Victor Stinner | 59e62db | 2010-05-15 13:14:32 +0000 | [diff] [blame] | 1534 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1535 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1536 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 1537 | PyUnicode_GET_SIZE(unicode), |
| 1538 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1539 | |
| 1540 | /* Encode via the codec registry */ |
| 1541 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1542 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1543 | return NULL; |
| 1544 | |
| 1545 | /* The normal path */ |
| 1546 | if (PyBytes_Check(v)) |
| 1547 | return v; |
| 1548 | |
| 1549 | /* 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] | 1550 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1551 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1552 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1553 | |
| 1554 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1555 | "encoder %s returned bytearray instead of bytes", |
| 1556 | encoding); |
| 1557 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1558 | Py_DECREF(v); |
| 1559 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1560 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1561 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1562 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1563 | Py_DECREF(v); |
| 1564 | return b; |
| 1565 | } |
| 1566 | |
| 1567 | PyErr_Format(PyExc_TypeError, |
| 1568 | "encoder did not return a bytes object (type=%.400s)", |
| 1569 | Py_TYPE(v)->tp_name); |
| 1570 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1571 | return NULL; |
| 1572 | } |
| 1573 | |
| 1574 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1575 | const char *encoding, |
| 1576 | const char *errors) |
| 1577 | { |
| 1578 | PyObject *v; |
| 1579 | |
| 1580 | if (!PyUnicode_Check(unicode)) { |
| 1581 | PyErr_BadArgument(); |
| 1582 | goto onError; |
| 1583 | } |
| 1584 | |
| 1585 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1586 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1587 | |
| 1588 | /* Encode via the codec registry */ |
| 1589 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1590 | if (v == NULL) |
| 1591 | goto onError; |
| 1592 | if (!PyUnicode_Check(v)) { |
| 1593 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1594 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1595 | Py_TYPE(v)->tp_name); |
| 1596 | Py_DECREF(v); |
| 1597 | goto onError; |
| 1598 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1599 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1600 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1601 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1602 | return NULL; |
| 1603 | } |
| 1604 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1605 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1606 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1607 | { |
| 1608 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1609 | if (v) |
| 1610 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1611 | if (errors != NULL) |
| 1612 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1613 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1614 | PyUnicode_GET_SIZE(unicode), |
| 1615 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1616 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1617 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1618 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1619 | return v; |
| 1620 | } |
| 1621 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1622 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1623 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1624 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1625 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1626 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1627 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1628 | PyObject* |
| 1629 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1630 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1631 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1632 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1633 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1634 | bootstrapping process where the codecs aren't ready yet. |
| 1635 | */ |
| 1636 | if (Py_FileSystemDefaultEncoding) { |
| 1637 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1638 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 1639 | return PyUnicode_DecodeMBCS(s, size, NULL); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1640 | } |
| 1641 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1642 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1643 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1644 | } |
| 1645 | #endif |
| 1646 | return PyUnicode_Decode(s, size, |
| 1647 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1648 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1649 | } |
| 1650 | else { |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 1651 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1655 | |
| 1656 | int |
| 1657 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1658 | { |
| 1659 | PyObject *output = NULL; |
| 1660 | Py_ssize_t size; |
| 1661 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1662 | if (arg == NULL) { |
| 1663 | Py_DECREF(*(PyObject**)addr); |
| 1664 | return 1; |
| 1665 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 1666 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1667 | output = arg; |
| 1668 | Py_INCREF(output); |
| 1669 | } |
| 1670 | else { |
| 1671 | arg = PyUnicode_FromObject(arg); |
| 1672 | if (!arg) |
| 1673 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 1674 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1675 | Py_DECREF(arg); |
| 1676 | if (!output) |
| 1677 | return 0; |
| 1678 | if (!PyBytes_Check(output)) { |
| 1679 | Py_DECREF(output); |
| 1680 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1681 | return 0; |
| 1682 | } |
| 1683 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 1684 | size = PyBytes_GET_SIZE(output); |
| 1685 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1686 | if (size != strlen(data)) { |
| 1687 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1688 | Py_DECREF(output); |
| 1689 | return 0; |
| 1690 | } |
| 1691 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1692 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 1696 | int |
| 1697 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 1698 | { |
| 1699 | PyObject *output = NULL; |
| 1700 | Py_ssize_t size; |
| 1701 | void *data; |
| 1702 | if (arg == NULL) { |
| 1703 | Py_DECREF(*(PyObject**)addr); |
| 1704 | return 1; |
| 1705 | } |
| 1706 | if (PyUnicode_Check(arg)) { |
| 1707 | output = arg; |
| 1708 | Py_INCREF(output); |
| 1709 | } |
| 1710 | else { |
| 1711 | arg = PyBytes_FromObject(arg); |
| 1712 | if (!arg) |
| 1713 | return 0; |
| 1714 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 1715 | PyBytes_GET_SIZE(arg)); |
| 1716 | Py_DECREF(arg); |
| 1717 | if (!output) |
| 1718 | return 0; |
| 1719 | if (!PyUnicode_Check(output)) { |
| 1720 | Py_DECREF(output); |
| 1721 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 1722 | return 0; |
| 1723 | } |
| 1724 | } |
| 1725 | size = PyUnicode_GET_SIZE(output); |
| 1726 | data = PyUnicode_AS_UNICODE(output); |
| 1727 | if (size != Py_UNICODE_strlen(data)) { |
| 1728 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1729 | Py_DECREF(output); |
| 1730 | return 0; |
| 1731 | } |
| 1732 | *(PyObject**)addr = output; |
| 1733 | return Py_CLEANUP_SUPPORTED; |
| 1734 | } |
| 1735 | |
| 1736 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1737 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1738 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1739 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1740 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1741 | if (!PyUnicode_Check(unicode)) { |
| 1742 | PyErr_BadArgument(); |
| 1743 | return NULL; |
| 1744 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1745 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1746 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1747 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1748 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1749 | *psize = PyBytes_GET_SIZE(bytes); |
| 1750 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1754 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1755 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1756 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1759 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1760 | { |
| 1761 | if (!PyUnicode_Check(unicode)) { |
| 1762 | PyErr_BadArgument(); |
| 1763 | goto onError; |
| 1764 | } |
| 1765 | return PyUnicode_AS_UNICODE(unicode); |
| 1766 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1767 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1768 | return NULL; |
| 1769 | } |
| 1770 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1771 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1772 | { |
| 1773 | if (!PyUnicode_Check(unicode)) { |
| 1774 | PyErr_BadArgument(); |
| 1775 | goto onError; |
| 1776 | } |
| 1777 | return PyUnicode_GET_SIZE(unicode); |
| 1778 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1779 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1780 | return -1; |
| 1781 | } |
| 1782 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1783 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1784 | { |
| 1785 | return unicode_default_encoding; |
| 1786 | } |
| 1787 | |
| 1788 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1789 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1790 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1791 | PyErr_Format(PyExc_ValueError, |
| 1792 | "Can only set default encoding to %s", |
| 1793 | unicode_default_encoding); |
| 1794 | return -1; |
| 1795 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1796 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1799 | /* create or adjust a UnicodeDecodeError */ |
| 1800 | static void |
| 1801 | make_decode_exception(PyObject **exceptionObject, |
| 1802 | const char *encoding, |
| 1803 | const char *input, Py_ssize_t length, |
| 1804 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 1805 | const char *reason) |
| 1806 | { |
| 1807 | if (*exceptionObject == NULL) { |
| 1808 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1809 | encoding, input, length, startpos, endpos, reason); |
| 1810 | } |
| 1811 | else { |
| 1812 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 1813 | goto onError; |
| 1814 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 1815 | goto onError; |
| 1816 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1817 | goto onError; |
| 1818 | } |
| 1819 | return; |
| 1820 | |
| 1821 | onError: |
| 1822 | Py_DECREF(*exceptionObject); |
| 1823 | *exceptionObject = NULL; |
| 1824 | } |
| 1825 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1826 | /* error handling callback helper: |
| 1827 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1828 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1829 | and adjust various state variables. |
| 1830 | return 0 on success, -1 on error |
| 1831 | */ |
| 1832 | |
| 1833 | static |
| 1834 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1835 | const char *encoding, const char *reason, |
| 1836 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1837 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1838 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1839 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1840 | 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] | 1841 | |
| 1842 | PyObject *restuple = NULL; |
| 1843 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1844 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1845 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1846 | Py_ssize_t requiredsize; |
| 1847 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1848 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1849 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1850 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1851 | int res = -1; |
| 1852 | |
| 1853 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1854 | *errorHandler = PyCodec_LookupError(errors); |
| 1855 | if (*errorHandler == NULL) |
| 1856 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1859 | make_decode_exception(exceptionObject, |
| 1860 | encoding, |
| 1861 | *input, *inend - *input, |
| 1862 | *startinpos, *endinpos, |
| 1863 | reason); |
| 1864 | if (*exceptionObject == NULL) |
| 1865 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1866 | |
| 1867 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1868 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1869 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1870 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1871 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1872 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1873 | } |
| 1874 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1875 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1876 | |
| 1877 | /* Copy back the bytes variables, which might have been modified by the |
| 1878 | callback */ |
| 1879 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1880 | if (!inputobj) |
| 1881 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1882 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1883 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1884 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1885 | *input = PyBytes_AS_STRING(inputobj); |
| 1886 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1887 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1888 | /* we can DECREF safely, as the exception has another reference, |
| 1889 | so the object won't go away. */ |
| 1890 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1891 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1892 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1893 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1894 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1895 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1896 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1897 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1898 | |
| 1899 | /* need more space? (at least enough for what we |
| 1900 | have+the replacement+the rest of the string (starting |
| 1901 | at the new input position), so we won't have to check space |
| 1902 | when there are no errors in the rest of the string) */ |
| 1903 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1904 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1905 | requiredsize = *outpos + repsize + insize-newpos; |
| 1906 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1907 | if (requiredsize<2*outsize) |
| 1908 | requiredsize = 2*outsize; |
| 1909 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1910 | goto onError; |
| 1911 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1912 | } |
| 1913 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1914 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1915 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1916 | *outptr += repsize; |
| 1917 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1918 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1919 | /* we made it! */ |
| 1920 | res = 0; |
| 1921 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1922 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1923 | Py_XDECREF(restuple); |
| 1924 | return res; |
| 1925 | } |
| 1926 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1927 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1928 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1929 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1930 | |
| 1931 | /* Three simple macros defining base-64. */ |
| 1932 | |
| 1933 | /* Is c a base-64 character? */ |
| 1934 | |
| 1935 | #define IS_BASE64(c) \ |
| 1936 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1937 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1938 | ((c) >= '0' && (c) <= '9') || \ |
| 1939 | (c) == '+' || (c) == '/') |
| 1940 | |
| 1941 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1942 | |
| 1943 | #define FROM_BASE64(c) \ |
| 1944 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1945 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1946 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1947 | (c) == '+' ? 62 : 63) |
| 1948 | |
| 1949 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1950 | |
| 1951 | #define TO_BASE64(n) \ |
| 1952 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1953 | |
| 1954 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1955 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1956 | * byte not decoding to itself is the + which begins a base64 |
| 1957 | * string. */ |
| 1958 | |
| 1959 | #define DECODE_DIRECT(c) \ |
| 1960 | ((c) <= 127 && (c) != '+') |
| 1961 | |
| 1962 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1963 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1964 | * the above). See RFC2152. This array identifies these different |
| 1965 | * sets: |
| 1966 | * 0 : "Set D" |
| 1967 | * alphanumeric and '(),-./:? |
| 1968 | * 1 : "Set O" |
| 1969 | * !"#$%&*;<=>@[]^_`{|} |
| 1970 | * 2 : "whitespace" |
| 1971 | * ht nl cr sp |
| 1972 | * 3 : special (must be base64 encoded) |
| 1973 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1974 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1975 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1976 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1977 | char utf7_category[128] = { |
| 1978 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 1979 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 1980 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 1981 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1982 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 1983 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 1984 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 1985 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 1986 | /* @ A B C D E F G H I J K L M N O */ |
| 1987 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1988 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 1989 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 1990 | /* ` a b c d e f g h i j k l m n o */ |
| 1991 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1992 | /* p q r s t u v w x y z { | } ~ del */ |
| 1993 | 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] | 1994 | }; |
| 1995 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1996 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 1997 | * answer depends on whether we are encoding set O as itself, and also |
| 1998 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 1999 | * clear that the answers to these questions vary between |
| 2000 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 2001 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2002 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 2003 | ((c) < 128 && (c) > 0 && \ |
| 2004 | ((utf7_category[(c)] == 0) || \ |
| 2005 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 2006 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2007 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2008 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2009 | Py_ssize_t size, |
| 2010 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2011 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2012 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 2013 | } |
| 2014 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2015 | /* The decoder. The only state we preserve is our read position, |
| 2016 | * i.e. how many characters we have consumed. So if we end in the |
| 2017 | * middle of a shift sequence we have to back off the read position |
| 2018 | * and the output to the beginning of the sequence, otherwise we lose |
| 2019 | * all the shift state (seen bits, number of bits seen, high |
| 2020 | * surrogate). */ |
| 2021 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2022 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2023 | Py_ssize_t size, |
| 2024 | const char *errors, |
| 2025 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2026 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2027 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2028 | Py_ssize_t startinpos; |
| 2029 | Py_ssize_t endinpos; |
| 2030 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2031 | const char *e; |
| 2032 | PyUnicodeObject *unicode; |
| 2033 | Py_UNICODE *p; |
| 2034 | const char *errmsg = ""; |
| 2035 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2036 | Py_UNICODE *shiftOutStart; |
| 2037 | unsigned int base64bits = 0; |
| 2038 | unsigned long base64buffer = 0; |
| 2039 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2040 | PyObject *errorHandler = NULL; |
| 2041 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2042 | |
| 2043 | unicode = _PyUnicode_New(size); |
| 2044 | if (!unicode) |
| 2045 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2046 | if (size == 0) { |
| 2047 | if (consumed) |
| 2048 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2049 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2050 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2051 | |
| 2052 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2053 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2054 | e = s + size; |
| 2055 | |
| 2056 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2057 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2058 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 2059 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2061 | if (inShift) { /* in a base-64 section */ |
| 2062 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 2063 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 2064 | base64bits += 6; |
| 2065 | s++; |
| 2066 | if (base64bits >= 16) { |
| 2067 | /* we have enough bits for a UTF-16 value */ |
| 2068 | Py_UNICODE outCh = (Py_UNICODE) |
| 2069 | (base64buffer >> (base64bits-16)); |
| 2070 | base64bits -= 16; |
| 2071 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 2072 | if (surrogate) { |
| 2073 | /* expecting a second surrogate */ |
| 2074 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2075 | #ifdef Py_UNICODE_WIDE |
| 2076 | *p++ = (((surrogate & 0x3FF)<<10) |
| 2077 | | (outCh & 0x3FF)) + 0x10000; |
| 2078 | #else |
| 2079 | *p++ = surrogate; |
| 2080 | *p++ = outCh; |
| 2081 | #endif |
| 2082 | surrogate = 0; |
| 2083 | } |
| 2084 | else { |
| 2085 | surrogate = 0; |
| 2086 | errmsg = "second surrogate missing"; |
| 2087 | goto utf7Error; |
| 2088 | } |
| 2089 | } |
| 2090 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 2091 | /* first surrogate */ |
| 2092 | surrogate = outCh; |
| 2093 | } |
| 2094 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 2095 | errmsg = "unexpected second surrogate"; |
| 2096 | goto utf7Error; |
| 2097 | } |
| 2098 | else { |
| 2099 | *p++ = outCh; |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2104 | inShift = 0; |
| 2105 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2106 | if (surrogate) { |
| 2107 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2108 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2109 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2110 | if (base64bits > 0) { /* left-over bits */ |
| 2111 | if (base64bits >= 6) { |
| 2112 | /* We've seen at least one base-64 character */ |
| 2113 | errmsg = "partial character in shift sequence"; |
| 2114 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2115 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2116 | else { |
| 2117 | /* Some bits remain; they should be zero */ |
| 2118 | if (base64buffer != 0) { |
| 2119 | errmsg = "non-zero padding bits in shift sequence"; |
| 2120 | goto utf7Error; |
| 2121 | } |
| 2122 | } |
| 2123 | } |
| 2124 | if (ch != '-') { |
| 2125 | /* '-' is absorbed; other terminating |
| 2126 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2127 | *p++ = ch; |
| 2128 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2129 | } |
| 2130 | } |
| 2131 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2132 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2133 | s++; /* consume '+' */ |
| 2134 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2135 | s++; |
| 2136 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2137 | } |
| 2138 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2139 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2140 | shiftOutStart = p; |
| 2141 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2142 | } |
| 2143 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2144 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2145 | *p++ = ch; |
| 2146 | s++; |
| 2147 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2148 | else { |
| 2149 | startinpos = s-starts; |
| 2150 | s++; |
| 2151 | errmsg = "unexpected special character"; |
| 2152 | goto utf7Error; |
| 2153 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2154 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2155 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2156 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2157 | endinpos = s-starts; |
| 2158 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2159 | errors, &errorHandler, |
| 2160 | "utf7", errmsg, |
| 2161 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2162 | &unicode, &outpos, &p)) |
| 2163 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2166 | /* end of string */ |
| 2167 | |
| 2168 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 2169 | /* if we're in an inconsistent state, that's an error */ |
| 2170 | if (surrogate || |
| 2171 | (base64bits >= 6) || |
| 2172 | (base64bits > 0 && base64buffer != 0)) { |
| 2173 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2174 | endinpos = size; |
| 2175 | if (unicode_decode_call_errorhandler( |
| 2176 | errors, &errorHandler, |
| 2177 | "utf7", "unterminated shift sequence", |
| 2178 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2179 | &unicode, &outpos, &p)) |
| 2180 | goto onError; |
| 2181 | if (s < e) |
| 2182 | goto restart; |
| 2183 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2184 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2185 | |
| 2186 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2187 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2188 | if (inShift) { |
| 2189 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2190 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2191 | } |
| 2192 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2193 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2194 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2195 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2196 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2197 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2198 | goto onError; |
| 2199 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2200 | Py_XDECREF(errorHandler); |
| 2201 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2202 | return (PyObject *)unicode; |
| 2203 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2204 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2205 | Py_XDECREF(errorHandler); |
| 2206 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2207 | Py_DECREF(unicode); |
| 2208 | return NULL; |
| 2209 | } |
| 2210 | |
| 2211 | |
| 2212 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2213 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2214 | int base64SetO, |
| 2215 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2216 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2217 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2218 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2219 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2220 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2221 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2222 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2223 | unsigned int base64bits = 0; |
| 2224 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2225 | char * out; |
| 2226 | char * start; |
| 2227 | |
| 2228 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2229 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2230 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 2231 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2232 | return PyErr_NoMemory(); |
| 2233 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2234 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2235 | if (v == NULL) |
| 2236 | return NULL; |
| 2237 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2238 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2239 | for (;i < size; ++i) { |
| 2240 | Py_UNICODE ch = s[i]; |
| 2241 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2242 | if (inShift) { |
| 2243 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2244 | /* shifting out */ |
| 2245 | if (base64bits) { /* output remaining bits */ |
| 2246 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2247 | base64buffer = 0; |
| 2248 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2249 | } |
| 2250 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2251 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2252 | so no '-' is required, except if the character is itself a '-' */ |
| 2253 | if (IS_BASE64(ch) || ch == '-') { |
| 2254 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2255 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2256 | *out++ = (char) ch; |
| 2257 | } |
| 2258 | else { |
| 2259 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2260 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2261 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2262 | else { /* not in a shift sequence */ |
| 2263 | if (ch == '+') { |
| 2264 | *out++ = '+'; |
| 2265 | *out++ = '-'; |
| 2266 | } |
| 2267 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2268 | *out++ = (char) ch; |
| 2269 | } |
| 2270 | else { |
| 2271 | *out++ = '+'; |
| 2272 | inShift = 1; |
| 2273 | goto encode_char; |
| 2274 | } |
| 2275 | } |
| 2276 | continue; |
| 2277 | encode_char: |
| 2278 | #ifdef Py_UNICODE_WIDE |
| 2279 | if (ch >= 0x10000) { |
| 2280 | /* code first surrogate */ |
| 2281 | base64bits += 16; |
| 2282 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2283 | while (base64bits >= 6) { |
| 2284 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2285 | base64bits -= 6; |
| 2286 | } |
| 2287 | /* prepare second surrogate */ |
| 2288 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2289 | } |
| 2290 | #endif |
| 2291 | base64bits += 16; |
| 2292 | base64buffer = (base64buffer << 16) | ch; |
| 2293 | while (base64bits >= 6) { |
| 2294 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2295 | base64bits -= 6; |
| 2296 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2297 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2298 | if (base64bits) |
| 2299 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2300 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2301 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2302 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2303 | return NULL; |
| 2304 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2307 | #undef IS_BASE64 |
| 2308 | #undef FROM_BASE64 |
| 2309 | #undef TO_BASE64 |
| 2310 | #undef DECODE_DIRECT |
| 2311 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2313 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2314 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2315 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2316 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2317 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 2318 | illegal prefix. See RFC 3629 for details */ |
| 2319 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 2320 | 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] | 2321 | 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] | 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, |
| 2325 | 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] | 2326 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 2327 | 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] | 2328 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2329 | 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] | 2330 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 2331 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 2332 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 2333 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 2334 | 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] | 2335 | }; |
| 2336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2337 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2338 | Py_ssize_t size, |
| 2339 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2340 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2341 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2342 | } |
| 2343 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2344 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2345 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2346 | |
| 2347 | /* Mask to quickly check whether a C 'long' contains a |
| 2348 | non-ASCII, UTF8-encoded char. */ |
| 2349 | #if (SIZEOF_LONG == 8) |
| 2350 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2351 | #elif (SIZEOF_LONG == 4) |
| 2352 | # define ASCII_CHAR_MASK 0x80808080L |
| 2353 | #else |
| 2354 | # error C 'long' size should be either 4 or 8! |
| 2355 | #endif |
| 2356 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2357 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2358 | Py_ssize_t size, |
| 2359 | const char *errors, |
| 2360 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2361 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2362 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2363 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2364 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2365 | Py_ssize_t startinpos; |
| 2366 | Py_ssize_t endinpos; |
| 2367 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2368 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2369 | PyUnicodeObject *unicode; |
| 2370 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2371 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2372 | PyObject *errorHandler = NULL; |
| 2373 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2374 | |
| 2375 | /* Note: size will always be longer than the resulting Unicode |
| 2376 | character count */ |
| 2377 | unicode = _PyUnicode_New(size); |
| 2378 | if (!unicode) |
| 2379 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2380 | if (size == 0) { |
| 2381 | if (consumed) |
| 2382 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2383 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2384 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2385 | |
| 2386 | /* Unpack UTF-8 encoded data */ |
| 2387 | p = unicode->str; |
| 2388 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2389 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2390 | |
| 2391 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2392 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2393 | |
| 2394 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2395 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2396 | input will consist of an overwhelming majority of ASCII |
| 2397 | characters, we try to optimize for this case by checking |
| 2398 | as many characters as a C 'long' can contain. |
| 2399 | First, check if we can do an aligned read, as most CPUs have |
| 2400 | a penalty for unaligned reads. |
| 2401 | */ |
| 2402 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2403 | /* Help register allocation */ |
| 2404 | register const char *_s = s; |
| 2405 | register Py_UNICODE *_p = p; |
| 2406 | while (_s < aligned_end) { |
| 2407 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2408 | and do a fast unrolled copy if it only contains ASCII |
| 2409 | characters. */ |
| 2410 | unsigned long data = *(unsigned long *) _s; |
| 2411 | if (data & ASCII_CHAR_MASK) |
| 2412 | break; |
| 2413 | _p[0] = (unsigned char) _s[0]; |
| 2414 | _p[1] = (unsigned char) _s[1]; |
| 2415 | _p[2] = (unsigned char) _s[2]; |
| 2416 | _p[3] = (unsigned char) _s[3]; |
| 2417 | #if (SIZEOF_LONG == 8) |
| 2418 | _p[4] = (unsigned char) _s[4]; |
| 2419 | _p[5] = (unsigned char) _s[5]; |
| 2420 | _p[6] = (unsigned char) _s[6]; |
| 2421 | _p[7] = (unsigned char) _s[7]; |
| 2422 | #endif |
| 2423 | _s += SIZEOF_LONG; |
| 2424 | _p += SIZEOF_LONG; |
| 2425 | } |
| 2426 | s = _s; |
| 2427 | p = _p; |
| 2428 | if (s == e) |
| 2429 | break; |
| 2430 | ch = (unsigned char)*s; |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2435 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2436 | s++; |
| 2437 | continue; |
| 2438 | } |
| 2439 | |
| 2440 | n = utf8_code_length[ch]; |
| 2441 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2442 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2443 | if (consumed) |
| 2444 | break; |
| 2445 | else { |
| 2446 | errmsg = "unexpected end of data"; |
| 2447 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2448 | endinpos = startinpos+1; |
| 2449 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 2450 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2451 | goto utf8Error; |
| 2452 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2453 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2454 | |
| 2455 | switch (n) { |
| 2456 | |
| 2457 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2458 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2459 | startinpos = s-starts; |
| 2460 | endinpos = startinpos+1; |
| 2461 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2462 | |
| 2463 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2464 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2465 | startinpos = s-starts; |
| 2466 | endinpos = startinpos+1; |
| 2467 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2468 | |
| 2469 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2470 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2471 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2472 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2473 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2474 | goto utf8Error; |
| 2475 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2476 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2477 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2478 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2479 | break; |
| 2480 | |
| 2481 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 2482 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2483 | will result in surrogates in range d800-dfff. Surrogates are |
| 2484 | not valid UTF-8 so they are rejected. |
| 2485 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2486 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2487 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2488 | (s[2] & 0xc0) != 0x80 || |
| 2489 | ((unsigned char)s[0] == 0xE0 && |
| 2490 | (unsigned char)s[1] < 0xA0) || |
| 2491 | ((unsigned char)s[0] == 0xED && |
| 2492 | (unsigned char)s[1] > 0x9F)) { |
| 2493 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2494 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2495 | endinpos = startinpos + 1; |
| 2496 | |
| 2497 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 2498 | continuation byte is s[2], so increment endinpos by 1, |
| 2499 | if not, s[1] is invalid and endinpos doesn't need to |
| 2500 | be incremented. */ |
| 2501 | if ((s[1] & 0xC0) == 0x80) |
| 2502 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2503 | goto utf8Error; |
| 2504 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2505 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2506 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2507 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2508 | break; |
| 2509 | |
| 2510 | case 4: |
| 2511 | if ((s[1] & 0xc0) != 0x80 || |
| 2512 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2513 | (s[3] & 0xc0) != 0x80 || |
| 2514 | ((unsigned char)s[0] == 0xF0 && |
| 2515 | (unsigned char)s[1] < 0x90) || |
| 2516 | ((unsigned char)s[0] == 0xF4 && |
| 2517 | (unsigned char)s[1] > 0x8F)) { |
| 2518 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2519 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2520 | endinpos = startinpos + 1; |
| 2521 | if ((s[1] & 0xC0) == 0x80) { |
| 2522 | endinpos++; |
| 2523 | if ((s[2] & 0xC0) == 0x80) |
| 2524 | endinpos++; |
| 2525 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2526 | goto utf8Error; |
| 2527 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2528 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 2529 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2530 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2531 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2532 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2533 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2534 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2535 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2536 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2537 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2538 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2539 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2540 | /* high surrogate = top 10 bits added to D800 */ |
| 2541 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2542 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2543 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2544 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2545 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2546 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2547 | } |
| 2548 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2549 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2550 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2551 | utf8Error: |
| 2552 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2553 | if (unicode_decode_call_errorhandler( |
| 2554 | errors, &errorHandler, |
| 2555 | "utf8", errmsg, |
| 2556 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2557 | &unicode, &outpos, &p)) |
| 2558 | goto onError; |
| 2559 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2560 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2561 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2562 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2563 | |
| 2564 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2565 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2566 | goto onError; |
| 2567 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2568 | Py_XDECREF(errorHandler); |
| 2569 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2570 | return (PyObject *)unicode; |
| 2571 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2572 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2573 | Py_XDECREF(errorHandler); |
| 2574 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2575 | Py_DECREF(unicode); |
| 2576 | return NULL; |
| 2577 | } |
| 2578 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2579 | #undef ASCII_CHAR_MASK |
| 2580 | |
| 2581 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2582 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2583 | and allocate exactly as much space needed at the end. Else allocate the |
| 2584 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2585 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2586 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2587 | PyObject * |
| 2588 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2589 | Py_ssize_t size, |
| 2590 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2591 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2592 | #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] | 2593 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2594 | Py_ssize_t i; /* index into s of next input byte */ |
| 2595 | PyObject *result; /* result string object */ |
| 2596 | char *p; /* next free byte in output buffer */ |
| 2597 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2598 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2599 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2600 | PyObject *errorHandler = NULL; |
| 2601 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2602 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2603 | assert(s != NULL); |
| 2604 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2605 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2606 | if (size <= MAX_SHORT_UNICHARS) { |
| 2607 | /* Write into the stack buffer; nallocated can't overflow. |
| 2608 | * At the end, we'll allocate exactly as much heap space as it |
| 2609 | * turns out we need. |
| 2610 | */ |
| 2611 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2612 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2613 | p = stackbuf; |
| 2614 | } |
| 2615 | else { |
| 2616 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2617 | nallocated = size * 4; |
| 2618 | if (nallocated / 4 != size) /* overflow! */ |
| 2619 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2620 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2621 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2622 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2623 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2624 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2625 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2626 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2627 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2628 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2629 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2630 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2631 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2632 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2633 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2634 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2635 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2636 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2637 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2638 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2639 | /* Special case: check for high and low surrogate */ |
| 2640 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2641 | Py_UCS4 ch2 = s[i]; |
| 2642 | /* Combine the two surrogates to form a UCS4 value */ |
| 2643 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2644 | i++; |
| 2645 | |
| 2646 | /* Encode UCS4 Unicode ordinals */ |
| 2647 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2648 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2649 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2650 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2651 | } else { |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2652 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2653 | Py_ssize_t newpos; |
| 2654 | PyObject *rep; |
| 2655 | Py_ssize_t repsize, k; |
| 2656 | rep = unicode_encode_call_errorhandler |
| 2657 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2658 | s, size, &exc, i-1, i, &newpos); |
| 2659 | if (!rep) |
| 2660 | goto error; |
| 2661 | |
| 2662 | if (PyBytes_Check(rep)) |
| 2663 | repsize = PyBytes_GET_SIZE(rep); |
| 2664 | else |
| 2665 | repsize = PyUnicode_GET_SIZE(rep); |
| 2666 | |
| 2667 | if (repsize > 4) { |
| 2668 | Py_ssize_t offset; |
| 2669 | |
| 2670 | if (result == NULL) |
| 2671 | offset = p - stackbuf; |
| 2672 | else |
| 2673 | offset = p - PyBytes_AS_STRING(result); |
| 2674 | |
| 2675 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2676 | /* integer overflow */ |
| 2677 | PyErr_NoMemory(); |
| 2678 | goto error; |
| 2679 | } |
| 2680 | nallocated += repsize - 4; |
| 2681 | if (result != NULL) { |
| 2682 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2683 | goto error; |
| 2684 | } else { |
| 2685 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2686 | if (result == NULL) |
| 2687 | goto error; |
| 2688 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2689 | } |
| 2690 | p = PyBytes_AS_STRING(result) + offset; |
| 2691 | } |
| 2692 | |
| 2693 | if (PyBytes_Check(rep)) { |
| 2694 | char *prep = PyBytes_AS_STRING(rep); |
| 2695 | for(k = repsize; k > 0; k--) |
| 2696 | *p++ = *prep++; |
| 2697 | } else /* rep is unicode */ { |
| 2698 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2699 | Py_UNICODE c; |
| 2700 | |
| 2701 | for(k=0; k<repsize; k++) { |
| 2702 | c = prep[k]; |
| 2703 | if (0x80 <= c) { |
| 2704 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2705 | i-1, i, "surrogates not allowed"); |
| 2706 | goto error; |
| 2707 | } |
| 2708 | *p++ = (char)prep[k]; |
| 2709 | } |
| 2710 | } |
| 2711 | Py_DECREF(rep); |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2712 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2713 | } |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 2714 | #endif |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 2715 | } else if (ch < 0x10000) { |
| 2716 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2717 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2718 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2719 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2720 | /* Encode UCS4 Unicode ordinals */ |
| 2721 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2722 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2723 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2724 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2725 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2726 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2727 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2728 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2729 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2730 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2731 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2732 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2733 | } |
| 2734 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2735 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2736 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2737 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2738 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2739 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2740 | Py_XDECREF(errorHandler); |
| 2741 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2742 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2743 | error: |
| 2744 | Py_XDECREF(errorHandler); |
| 2745 | Py_XDECREF(exc); |
| 2746 | Py_XDECREF(result); |
| 2747 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2748 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2749 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2752 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2753 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2754 | if (!PyUnicode_Check(unicode)) { |
| 2755 | PyErr_BadArgument(); |
| 2756 | return NULL; |
| 2757 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2758 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2759 | PyUnicode_GET_SIZE(unicode), |
| 2760 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2763 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2764 | |
| 2765 | PyObject * |
| 2766 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2767 | Py_ssize_t size, |
| 2768 | const char *errors, |
| 2769 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2770 | { |
| 2771 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2772 | } |
| 2773 | |
| 2774 | PyObject * |
| 2775 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2776 | Py_ssize_t size, |
| 2777 | const char *errors, |
| 2778 | int *byteorder, |
| 2779 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2780 | { |
| 2781 | const char *starts = s; |
| 2782 | Py_ssize_t startinpos; |
| 2783 | Py_ssize_t endinpos; |
| 2784 | Py_ssize_t outpos; |
| 2785 | PyUnicodeObject *unicode; |
| 2786 | Py_UNICODE *p; |
| 2787 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2788 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2789 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2790 | #else |
| 2791 | const int pairs = 0; |
| 2792 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 2793 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2794 | int bo = 0; /* assume native ordering by default */ |
| 2795 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2796 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2797 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2798 | int iorder[] = {0, 1, 2, 3}; |
| 2799 | #else |
| 2800 | int iorder[] = {3, 2, 1, 0}; |
| 2801 | #endif |
| 2802 | PyObject *errorHandler = NULL; |
| 2803 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 2804 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2805 | q = (unsigned char *)s; |
| 2806 | e = q + size; |
| 2807 | |
| 2808 | if (byteorder) |
| 2809 | bo = *byteorder; |
| 2810 | |
| 2811 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2812 | byte order setting accordingly. In native mode, the leading BOM |
| 2813 | mark is skipped, in all other modes, it is copied to the output |
| 2814 | stream as-is (giving a ZWNBSP character). */ |
| 2815 | if (bo == 0) { |
| 2816 | if (size >= 4) { |
| 2817 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2818 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2819 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2820 | if (bom == 0x0000FEFF) { |
| 2821 | q += 4; |
| 2822 | bo = -1; |
| 2823 | } |
| 2824 | else if (bom == 0xFFFE0000) { |
| 2825 | q += 4; |
| 2826 | bo = 1; |
| 2827 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2828 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2829 | if (bom == 0x0000FEFF) { |
| 2830 | q += 4; |
| 2831 | bo = 1; |
| 2832 | } |
| 2833 | else if (bom == 0xFFFE0000) { |
| 2834 | q += 4; |
| 2835 | bo = -1; |
| 2836 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2837 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2838 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | if (bo == -1) { |
| 2842 | /* force LE */ |
| 2843 | iorder[0] = 0; |
| 2844 | iorder[1] = 1; |
| 2845 | iorder[2] = 2; |
| 2846 | iorder[3] = 3; |
| 2847 | } |
| 2848 | else if (bo == 1) { |
| 2849 | /* force BE */ |
| 2850 | iorder[0] = 3; |
| 2851 | iorder[1] = 2; |
| 2852 | iorder[2] = 1; |
| 2853 | iorder[3] = 0; |
| 2854 | } |
| 2855 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 2856 | /* On narrow builds we split characters outside the BMP into two |
| 2857 | codepoints => count how much extra space we need. */ |
| 2858 | #ifndef Py_UNICODE_WIDE |
| 2859 | for (qq = q; qq < e; qq += 4) |
| 2860 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 2861 | pairs++; |
| 2862 | #endif |
| 2863 | |
| 2864 | /* This might be one to much, because of a BOM */ |
| 2865 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2866 | if (!unicode) |
| 2867 | return NULL; |
| 2868 | if (size == 0) |
| 2869 | return (PyObject *)unicode; |
| 2870 | |
| 2871 | /* Unpack UTF-32 encoded data */ |
| 2872 | p = unicode->str; |
| 2873 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2874 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2875 | Py_UCS4 ch; |
| 2876 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2877 | if (e-q<4) { |
| 2878 | if (consumed) |
| 2879 | break; |
| 2880 | errmsg = "truncated data"; |
| 2881 | startinpos = ((const char *)q)-starts; |
| 2882 | endinpos = ((const char *)e)-starts; |
| 2883 | goto utf32Error; |
| 2884 | /* The remaining input chars are ignored if the callback |
| 2885 | chooses to skip the input */ |
| 2886 | } |
| 2887 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2888 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2889 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2890 | if (ch >= 0x110000) |
| 2891 | { |
| 2892 | errmsg = "codepoint not in range(0x110000)"; |
| 2893 | startinpos = ((const char *)q)-starts; |
| 2894 | endinpos = startinpos+4; |
| 2895 | goto utf32Error; |
| 2896 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2897 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2898 | if (ch >= 0x10000) |
| 2899 | { |
| 2900 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2901 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2902 | } |
| 2903 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2904 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2905 | *p++ = ch; |
| 2906 | q += 4; |
| 2907 | continue; |
| 2908 | utf32Error: |
| 2909 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2910 | if (unicode_decode_call_errorhandler( |
| 2911 | errors, &errorHandler, |
| 2912 | "utf32", errmsg, |
| 2913 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2914 | &unicode, &outpos, &p)) |
| 2915 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | if (byteorder) |
| 2919 | *byteorder = bo; |
| 2920 | |
| 2921 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2922 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2923 | |
| 2924 | /* Adjust length */ |
| 2925 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2926 | goto onError; |
| 2927 | |
| 2928 | Py_XDECREF(errorHandler); |
| 2929 | Py_XDECREF(exc); |
| 2930 | return (PyObject *)unicode; |
| 2931 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2932 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2933 | Py_DECREF(unicode); |
| 2934 | Py_XDECREF(errorHandler); |
| 2935 | Py_XDECREF(exc); |
| 2936 | return NULL; |
| 2937 | } |
| 2938 | |
| 2939 | PyObject * |
| 2940 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2941 | Py_ssize_t size, |
| 2942 | const char *errors, |
| 2943 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2944 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2945 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2946 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2947 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2948 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2949 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2950 | #else |
| 2951 | const int pairs = 0; |
| 2952 | #endif |
| 2953 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2954 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2955 | int iorder[] = {0, 1, 2, 3}; |
| 2956 | #else |
| 2957 | int iorder[] = {3, 2, 1, 0}; |
| 2958 | #endif |
| 2959 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2960 | #define STORECHAR(CH) \ |
| 2961 | do { \ |
| 2962 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2963 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2964 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2965 | p[iorder[0]] = (CH) & 0xff; \ |
| 2966 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2967 | } while(0) |
| 2968 | |
| 2969 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2970 | so we need less space. */ |
| 2971 | #ifndef Py_UNICODE_WIDE |
| 2972 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2973 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2974 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2975 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2976 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2977 | nsize = (size - pairs + (byteorder == 0)); |
| 2978 | bytesize = nsize * 4; |
| 2979 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2980 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2981 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2982 | if (v == NULL) |
| 2983 | return NULL; |
| 2984 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2985 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2986 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2987 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2988 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2989 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2990 | |
| 2991 | if (byteorder == -1) { |
| 2992 | /* force LE */ |
| 2993 | iorder[0] = 0; |
| 2994 | iorder[1] = 1; |
| 2995 | iorder[2] = 2; |
| 2996 | iorder[3] = 3; |
| 2997 | } |
| 2998 | else if (byteorder == 1) { |
| 2999 | /* force BE */ |
| 3000 | iorder[0] = 3; |
| 3001 | iorder[1] = 2; |
| 3002 | iorder[2] = 1; |
| 3003 | iorder[3] = 0; |
| 3004 | } |
| 3005 | |
| 3006 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3007 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3008 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3009 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 3010 | Py_UCS4 ch2 = *s; |
| 3011 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 3012 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 3013 | s++; |
| 3014 | size--; |
| 3015 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3016 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3017 | #endif |
| 3018 | STORECHAR(ch); |
| 3019 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3020 | |
| 3021 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3022 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3023 | #undef STORECHAR |
| 3024 | } |
| 3025 | |
| 3026 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 3027 | { |
| 3028 | if (!PyUnicode_Check(unicode)) { |
| 3029 | PyErr_BadArgument(); |
| 3030 | return NULL; |
| 3031 | } |
| 3032 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3033 | PyUnicode_GET_SIZE(unicode), |
| 3034 | NULL, |
| 3035 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3038 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 3039 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3040 | PyObject * |
| 3041 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3042 | Py_ssize_t size, |
| 3043 | const char *errors, |
| 3044 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3046 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 3047 | } |
| 3048 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3049 | /* Two masks for fast checking of whether a C 'long' may contain |
| 3050 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 3051 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 3052 | rare in most input. |
| 3053 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 3054 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3055 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3056 | #if (SIZEOF_LONG == 8) |
| 3057 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 3058 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 3059 | #elif (SIZEOF_LONG == 4) |
| 3060 | # define FAST_CHAR_MASK 0x80008000L |
| 3061 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 3062 | #else |
| 3063 | # error C 'long' size should be either 4 or 8! |
| 3064 | #endif |
| 3065 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3066 | PyObject * |
| 3067 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3068 | Py_ssize_t size, |
| 3069 | const char *errors, |
| 3070 | int *byteorder, |
| 3071 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3072 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3073 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3074 | Py_ssize_t startinpos; |
| 3075 | Py_ssize_t endinpos; |
| 3076 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3077 | PyUnicodeObject *unicode; |
| 3078 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3079 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3080 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3081 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3082 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3083 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 3084 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3085 | int ihi = 1, ilo = 0; |
| 3086 | #else |
| 3087 | int ihi = 0, ilo = 1; |
| 3088 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3089 | PyObject *errorHandler = NULL; |
| 3090 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3091 | |
| 3092 | /* Note: size will always be longer than the resulting Unicode |
| 3093 | character count */ |
| 3094 | unicode = _PyUnicode_New(size); |
| 3095 | if (!unicode) |
| 3096 | return NULL; |
| 3097 | if (size == 0) |
| 3098 | return (PyObject *)unicode; |
| 3099 | |
| 3100 | /* Unpack UTF-16 encoded data */ |
| 3101 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3102 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3103 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3104 | |
| 3105 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3106 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3107 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3108 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 3109 | byte order setting accordingly. In native mode, the leading BOM |
| 3110 | mark is skipped, in all other modes, it is copied to the output |
| 3111 | stream as-is (giving a ZWNBSP character). */ |
| 3112 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3113 | if (size >= 2) { |
| 3114 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3115 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3116 | if (bom == 0xFEFF) { |
| 3117 | q += 2; |
| 3118 | bo = -1; |
| 3119 | } |
| 3120 | else if (bom == 0xFFFE) { |
| 3121 | q += 2; |
| 3122 | bo = 1; |
| 3123 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3124 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3125 | if (bom == 0xFEFF) { |
| 3126 | q += 2; |
| 3127 | bo = 1; |
| 3128 | } |
| 3129 | else if (bom == 0xFFFE) { |
| 3130 | q += 2; |
| 3131 | bo = -1; |
| 3132 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3133 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3134 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 3135 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3136 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3137 | if (bo == -1) { |
| 3138 | /* force LE */ |
| 3139 | ihi = 1; |
| 3140 | ilo = 0; |
| 3141 | } |
| 3142 | else if (bo == 1) { |
| 3143 | /* force BE */ |
| 3144 | ihi = 0; |
| 3145 | ilo = 1; |
| 3146 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3147 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3148 | native_ordering = ilo < ihi; |
| 3149 | #else |
| 3150 | native_ordering = ilo > ihi; |
| 3151 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3152 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3153 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3154 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3155 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3156 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 3157 | reads are more expensive, better to defer to another iteration. */ |
| 3158 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 3159 | /* Fast path for runs of non-surrogate chars. */ |
| 3160 | register const unsigned char *_q = q; |
| 3161 | Py_UNICODE *_p = p; |
| 3162 | if (native_ordering) { |
| 3163 | /* Native ordering is simple: as long as the input cannot |
| 3164 | possibly contain a surrogate char, do an unrolled copy |
| 3165 | of several 16-bit code points to the target object. |
| 3166 | The non-surrogate check is done on several input bytes |
| 3167 | at a time (as many as a C 'long' can contain). */ |
| 3168 | while (_q < aligned_end) { |
| 3169 | unsigned long data = * (unsigned long *) _q; |
| 3170 | if (data & FAST_CHAR_MASK) |
| 3171 | break; |
| 3172 | _p[0] = ((unsigned short *) _q)[0]; |
| 3173 | _p[1] = ((unsigned short *) _q)[1]; |
| 3174 | #if (SIZEOF_LONG == 8) |
| 3175 | _p[2] = ((unsigned short *) _q)[2]; |
| 3176 | _p[3] = ((unsigned short *) _q)[3]; |
| 3177 | #endif |
| 3178 | _q += SIZEOF_LONG; |
| 3179 | _p += SIZEOF_LONG / 2; |
| 3180 | } |
| 3181 | } |
| 3182 | else { |
| 3183 | /* Byteswapped ordering is similar, but we must decompose |
| 3184 | the copy bytewise, and take care of zero'ing out the |
| 3185 | upper bytes if the target object is in 32-bit units |
| 3186 | (that is, in UCS-4 builds). */ |
| 3187 | while (_q < aligned_end) { |
| 3188 | unsigned long data = * (unsigned long *) _q; |
| 3189 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3190 | break; |
| 3191 | /* Zero upper bytes in UCS-4 builds */ |
| 3192 | #if (Py_UNICODE_SIZE > 2) |
| 3193 | _p[0] = 0; |
| 3194 | _p[1] = 0; |
| 3195 | #if (SIZEOF_LONG == 8) |
| 3196 | _p[2] = 0; |
| 3197 | _p[3] = 0; |
| 3198 | #endif |
| 3199 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3200 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3201 | fill the two last bytes of each 4-byte unit. */ |
| 3202 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3203 | # define OFF 2 |
| 3204 | #else |
| 3205 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3206 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3207 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3208 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3209 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3210 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3211 | #if (SIZEOF_LONG == 8) |
| 3212 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3213 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3214 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3215 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3216 | #endif |
| 3217 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3218 | _q += SIZEOF_LONG; |
| 3219 | _p += SIZEOF_LONG / 2; |
| 3220 | } |
| 3221 | } |
| 3222 | p = _p; |
| 3223 | q = _q; |
| 3224 | if (q >= e) |
| 3225 | break; |
| 3226 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3227 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3228 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3229 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3230 | |
| 3231 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3232 | *p++ = ch; |
| 3233 | continue; |
| 3234 | } |
| 3235 | |
| 3236 | /* UTF-16 code pair: */ |
| 3237 | if (q > e) { |
| 3238 | errmsg = "unexpected end of data"; |
| 3239 | startinpos = (((const char *)q) - 2) - starts; |
| 3240 | endinpos = ((const char *)e) + 1 - starts; |
| 3241 | goto utf16Error; |
| 3242 | } |
| 3243 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3244 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3245 | q += 2; |
| 3246 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3247 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3248 | *p++ = ch; |
| 3249 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3250 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3251 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3252 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3253 | continue; |
| 3254 | } |
| 3255 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3256 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3257 | startinpos = (((const char *)q)-4)-starts; |
| 3258 | endinpos = startinpos+2; |
| 3259 | goto utf16Error; |
| 3260 | } |
| 3261 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3262 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3263 | errmsg = "illegal encoding"; |
| 3264 | startinpos = (((const char *)q)-2)-starts; |
| 3265 | endinpos = startinpos+2; |
| 3266 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3267 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3268 | utf16Error: |
| 3269 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3270 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3271 | errors, |
| 3272 | &errorHandler, |
| 3273 | "utf16", errmsg, |
| 3274 | &starts, |
| 3275 | (const char **)&e, |
| 3276 | &startinpos, |
| 3277 | &endinpos, |
| 3278 | &exc, |
| 3279 | (const char **)&q, |
| 3280 | &unicode, |
| 3281 | &outpos, |
| 3282 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3283 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3284 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3285 | /* remaining byte at the end? (size should be even) */ |
| 3286 | if (e == q) { |
| 3287 | if (!consumed) { |
| 3288 | errmsg = "truncated data"; |
| 3289 | startinpos = ((const char *)q) - starts; |
| 3290 | endinpos = ((const char *)e) + 1 - starts; |
| 3291 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3292 | if (unicode_decode_call_errorhandler( |
| 3293 | errors, |
| 3294 | &errorHandler, |
| 3295 | "utf16", errmsg, |
| 3296 | &starts, |
| 3297 | (const char **)&e, |
| 3298 | &startinpos, |
| 3299 | &endinpos, |
| 3300 | &exc, |
| 3301 | (const char **)&q, |
| 3302 | &unicode, |
| 3303 | &outpos, |
| 3304 | &p)) |
| 3305 | goto onError; |
| 3306 | /* The remaining input chars are ignored if the callback |
| 3307 | chooses to skip the input */ |
| 3308 | } |
| 3309 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3310 | |
| 3311 | if (byteorder) |
| 3312 | *byteorder = bo; |
| 3313 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3314 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3315 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3318 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3319 | goto onError; |
| 3320 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3321 | Py_XDECREF(errorHandler); |
| 3322 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3323 | return (PyObject *)unicode; |
| 3324 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3325 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3326 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3327 | Py_XDECREF(errorHandler); |
| 3328 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3329 | return NULL; |
| 3330 | } |
| 3331 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3332 | #undef FAST_CHAR_MASK |
| 3333 | #undef SWAPPED_FAST_CHAR_MASK |
| 3334 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3335 | PyObject * |
| 3336 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3337 | Py_ssize_t size, |
| 3338 | const char *errors, |
| 3339 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3340 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3341 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3342 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3343 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3344 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3345 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3346 | #else |
| 3347 | const int pairs = 0; |
| 3348 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3349 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3350 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3351 | int ihi = 1, ilo = 0; |
| 3352 | #else |
| 3353 | int ihi = 0, ilo = 1; |
| 3354 | #endif |
| 3355 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3356 | #define STORECHAR(CH) \ |
| 3357 | do { \ |
| 3358 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3359 | p[ilo] = (CH) & 0xff; \ |
| 3360 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3361 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3362 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3363 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3364 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3365 | if (s[i] >= 0x10000) |
| 3366 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3367 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3368 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3369 | if (size > PY_SSIZE_T_MAX || |
| 3370 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3371 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3372 | nsize = size + pairs + (byteorder == 0); |
| 3373 | bytesize = nsize * 2; |
| 3374 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3375 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3376 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3377 | if (v == NULL) |
| 3378 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3379 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3380 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3381 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3382 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3383 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3384 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3385 | |
| 3386 | if (byteorder == -1) { |
| 3387 | /* force LE */ |
| 3388 | ihi = 1; |
| 3389 | ilo = 0; |
| 3390 | } |
| 3391 | else if (byteorder == 1) { |
| 3392 | /* force BE */ |
| 3393 | ihi = 0; |
| 3394 | ilo = 1; |
| 3395 | } |
| 3396 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3397 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3398 | Py_UNICODE ch = *s++; |
| 3399 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3400 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3401 | if (ch >= 0x10000) { |
| 3402 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3403 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3404 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3405 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3406 | STORECHAR(ch); |
| 3407 | if (ch2) |
| 3408 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3409 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3410 | |
| 3411 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3412 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3413 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3417 | { |
| 3418 | if (!PyUnicode_Check(unicode)) { |
| 3419 | PyErr_BadArgument(); |
| 3420 | return NULL; |
| 3421 | } |
| 3422 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3423 | PyUnicode_GET_SIZE(unicode), |
| 3424 | NULL, |
| 3425 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3429 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3430 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3432 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3433 | Py_ssize_t size, |
| 3434 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3435 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3436 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3437 | Py_ssize_t startinpos; |
| 3438 | Py_ssize_t endinpos; |
| 3439 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3440 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3441 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3442 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3443 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3444 | char* message; |
| 3445 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3446 | PyObject *errorHandler = NULL; |
| 3447 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3449 | /* Escaped strings will always be longer than the resulting |
| 3450 | 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] | 3451 | length after conversion to the true value. |
| 3452 | (but if the error callback returns a long replacement string |
| 3453 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3454 | v = _PyUnicode_New(size); |
| 3455 | if (v == NULL) |
| 3456 | goto onError; |
| 3457 | if (size == 0) |
| 3458 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3459 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3460 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3461 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3463 | while (s < end) { |
| 3464 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3465 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3466 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3467 | |
| 3468 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3469 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3470 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3471 | continue; |
| 3472 | } |
| 3473 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3474 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3475 | /* \ - Escapes */ |
| 3476 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3477 | c = *s++; |
| 3478 | if (s > end) |
| 3479 | c = '\0'; /* Invalid after \ */ |
| 3480 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3481 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3482 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3483 | case '\n': break; |
| 3484 | case '\\': *p++ = '\\'; break; |
| 3485 | case '\'': *p++ = '\''; break; |
| 3486 | case '\"': *p++ = '\"'; break; |
| 3487 | case 'b': *p++ = '\b'; break; |
| 3488 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3489 | case 't': *p++ = '\t'; break; |
| 3490 | case 'n': *p++ = '\n'; break; |
| 3491 | case 'r': *p++ = '\r'; break; |
| 3492 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3493 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3494 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3495 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3496 | case '0': case '1': case '2': case '3': |
| 3497 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3498 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3499 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3500 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3501 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3502 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3503 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3504 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3505 | break; |
| 3506 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3507 | /* hex escapes */ |
| 3508 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3509 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3510 | digits = 2; |
| 3511 | message = "truncated \\xXX escape"; |
| 3512 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3513 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3514 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3515 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3516 | digits = 4; |
| 3517 | message = "truncated \\uXXXX escape"; |
| 3518 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3519 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3520 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3521 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3522 | digits = 8; |
| 3523 | message = "truncated \\UXXXXXXXX escape"; |
| 3524 | hexescape: |
| 3525 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3526 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3527 | if (s+digits>end) { |
| 3528 | endinpos = size; |
| 3529 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3530 | errors, &errorHandler, |
| 3531 | "unicodeescape", "end of string in escape sequence", |
| 3532 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3533 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3534 | goto onError; |
| 3535 | goto nextByte; |
| 3536 | } |
| 3537 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3538 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3539 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3540 | endinpos = (s+i+1)-starts; |
| 3541 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3542 | errors, &errorHandler, |
| 3543 | "unicodeescape", message, |
| 3544 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3545 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3546 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3547 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3548 | } |
| 3549 | chr = (chr<<4) & ~0xF; |
| 3550 | if (c >= '0' && c <= '9') |
| 3551 | chr += c - '0'; |
| 3552 | else if (c >= 'a' && c <= 'f') |
| 3553 | chr += 10 + c - 'a'; |
| 3554 | else |
| 3555 | chr += 10 + c - 'A'; |
| 3556 | } |
| 3557 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3558 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3559 | /* _decoding_error will have already written into the |
| 3560 | target buffer. */ |
| 3561 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3562 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3563 | /* when we get here, chr is a 32-bit unicode character */ |
| 3564 | if (chr <= 0xffff) |
| 3565 | /* UCS-2 character */ |
| 3566 | *p++ = (Py_UNICODE) chr; |
| 3567 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3568 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3569 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3570 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3571 | *p++ = chr; |
| 3572 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3573 | chr -= 0x10000L; |
| 3574 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3575 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3576 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3577 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3578 | endinpos = s-starts; |
| 3579 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3580 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3581 | errors, &errorHandler, |
| 3582 | "unicodeescape", "illegal Unicode character", |
| 3583 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3584 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3585 | goto onError; |
| 3586 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3587 | break; |
| 3588 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3589 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3590 | case 'N': |
| 3591 | message = "malformed \\N character escape"; |
| 3592 | if (ucnhash_CAPI == NULL) { |
| 3593 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3594 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3595 | if (ucnhash_CAPI == NULL) |
| 3596 | goto ucnhashError; |
| 3597 | } |
| 3598 | if (*s == '{') { |
| 3599 | const char *start = s+1; |
| 3600 | /* look for the closing brace */ |
| 3601 | while (*s != '}' && s < end) |
| 3602 | s++; |
| 3603 | if (s > start && s < end && *s == '}') { |
| 3604 | /* found a name. look it up in the unicode database */ |
| 3605 | message = "unknown Unicode character name"; |
| 3606 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3607 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3608 | goto store; |
| 3609 | } |
| 3610 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3611 | endinpos = s-starts; |
| 3612 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3613 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3614 | errors, &errorHandler, |
| 3615 | "unicodeescape", message, |
| 3616 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3617 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3618 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3619 | break; |
| 3620 | |
| 3621 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3622 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3623 | message = "\\ at end of string"; |
| 3624 | s--; |
| 3625 | endinpos = s-starts; |
| 3626 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3627 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3628 | errors, &errorHandler, |
| 3629 | "unicodeescape", message, |
| 3630 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3631 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3632 | goto onError; |
| 3633 | } |
| 3634 | else { |
| 3635 | *p++ = '\\'; |
| 3636 | *p++ = (unsigned char)s[-1]; |
| 3637 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3638 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3639 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3640 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3641 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3642 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3643 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3644 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3645 | Py_XDECREF(errorHandler); |
| 3646 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3647 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3648 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3649 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3650 | PyErr_SetString( |
| 3651 | PyExc_UnicodeError, |
| 3652 | "\\N escapes not supported (can't load unicodedata module)" |
| 3653 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3654 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3655 | Py_XDECREF(errorHandler); |
| 3656 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3657 | return NULL; |
| 3658 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3659 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3660 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3661 | Py_XDECREF(errorHandler); |
| 3662 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3663 | return NULL; |
| 3664 | } |
| 3665 | |
| 3666 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3667 | |
| 3668 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3669 | appropriate. |
| 3670 | |
| 3671 | */ |
| 3672 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3673 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3674 | Py_ssize_t size, |
| 3675 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3676 | { |
| 3677 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3678 | |
| 3679 | while (size-- > 0) { |
| 3680 | if (*s == ch) |
| 3681 | return s; |
| 3682 | s++; |
| 3683 | } |
| 3684 | |
| 3685 | return NULL; |
| 3686 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3687 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3688 | static const char *hexdigits = "0123456789abcdef"; |
| 3689 | |
| 3690 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3691 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3692 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3693 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3694 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3696 | #ifdef Py_UNICODE_WIDE |
| 3697 | const Py_ssize_t expandsize = 10; |
| 3698 | #else |
| 3699 | const Py_ssize_t expandsize = 6; |
| 3700 | #endif |
| 3701 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3702 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3703 | better to choose a different scheme. Perhaps scan the |
| 3704 | first N-chars of the string and allocate based on that size. |
| 3705 | */ |
| 3706 | /* Initial allocation is based on the longest-possible unichr |
| 3707 | escape. |
| 3708 | |
| 3709 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3710 | unichr, so in this case it's the longest unichr escape. In |
| 3711 | narrow (UTF-16) builds this is five chars per source unichr |
| 3712 | since there are two unichrs in the surrogate pair, so in narrow |
| 3713 | (UTF-16) builds it's not the longest unichr escape. |
| 3714 | |
| 3715 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3716 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3717 | escape. |
| 3718 | */ |
| 3719 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3720 | if (size == 0) |
| 3721 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3722 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3723 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3724 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3725 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3726 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3727 | 2 |
| 3728 | + expandsize*size |
| 3729 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3730 | if (repr == NULL) |
| 3731 | return NULL; |
| 3732 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3733 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3735 | while (size-- > 0) { |
| 3736 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3737 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3738 | /* Escape backslashes */ |
| 3739 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3740 | *p++ = '\\'; |
| 3741 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3742 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3743 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3744 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3745 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3746 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3747 | else if (ch >= 0x10000) { |
| 3748 | *p++ = '\\'; |
| 3749 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3750 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3751 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3752 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3753 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3754 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3755 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3756 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3757 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3758 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3759 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3760 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3761 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3762 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3763 | Py_UNICODE ch2; |
| 3764 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3765 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3766 | ch2 = *s++; |
| 3767 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 3768 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3769 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3770 | *p++ = '\\'; |
| 3771 | *p++ = 'U'; |
| 3772 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3773 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3774 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3775 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3776 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3777 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3778 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3779 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3780 | continue; |
| 3781 | } |
| 3782 | /* Fall through: isolated surrogates are copied as-is */ |
| 3783 | s--; |
| 3784 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3785 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3786 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3788 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3789 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3790 | *p++ = '\\'; |
| 3791 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3792 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3793 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3794 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3795 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3796 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3797 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3798 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3799 | else if (ch == '\t') { |
| 3800 | *p++ = '\\'; |
| 3801 | *p++ = 't'; |
| 3802 | } |
| 3803 | else if (ch == '\n') { |
| 3804 | *p++ = '\\'; |
| 3805 | *p++ = 'n'; |
| 3806 | } |
| 3807 | else if (ch == '\r') { |
| 3808 | *p++ = '\\'; |
| 3809 | *p++ = 'r'; |
| 3810 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3811 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3812 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3813 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3814 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3815 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3816 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3817 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3818 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3819 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3820 | /* Copy everything else as-is */ |
| 3821 | else |
| 3822 | *p++ = (char) ch; |
| 3823 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3824 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3825 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3826 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3827 | return NULL; |
| 3828 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3831 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3832 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3833 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3834 | if (!PyUnicode_Check(unicode)) { |
| 3835 | PyErr_BadArgument(); |
| 3836 | return NULL; |
| 3837 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3838 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3839 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3840 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3844 | |
| 3845 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3846 | Py_ssize_t size, |
| 3847 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3848 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3849 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3850 | Py_ssize_t startinpos; |
| 3851 | Py_ssize_t endinpos; |
| 3852 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3853 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3854 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | const char *end; |
| 3856 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3857 | PyObject *errorHandler = NULL; |
| 3858 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3860 | /* Escaped strings will always be longer than the resulting |
| 3861 | 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] | 3862 | length after conversion to the true value. (But decoding error |
| 3863 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3864 | v = _PyUnicode_New(size); |
| 3865 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3866 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3867 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3868 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3869 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3870 | end = s + size; |
| 3871 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3872 | unsigned char c; |
| 3873 | Py_UCS4 x; |
| 3874 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3875 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3876 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3877 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3878 | if (*s != '\\') { |
| 3879 | *p++ = (unsigned char)*s++; |
| 3880 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3881 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3882 | startinpos = s-starts; |
| 3883 | |
| 3884 | /* \u-escapes are only interpreted iff the number of leading |
| 3885 | backslashes if odd */ |
| 3886 | bs = s; |
| 3887 | for (;s < end;) { |
| 3888 | if (*s != '\\') |
| 3889 | break; |
| 3890 | *p++ = (unsigned char)*s++; |
| 3891 | } |
| 3892 | if (((s - bs) & 1) == 0 || |
| 3893 | s >= end || |
| 3894 | (*s != 'u' && *s != 'U')) { |
| 3895 | continue; |
| 3896 | } |
| 3897 | p--; |
| 3898 | count = *s=='u' ? 4 : 8; |
| 3899 | s++; |
| 3900 | |
| 3901 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3902 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3903 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3904 | c = (unsigned char)*s; |
| 3905 | if (!ISXDIGIT(c)) { |
| 3906 | endinpos = s-starts; |
| 3907 | if (unicode_decode_call_errorhandler( |
| 3908 | errors, &errorHandler, |
| 3909 | "rawunicodeescape", "truncated \\uXXXX", |
| 3910 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3911 | &v, &outpos, &p)) |
| 3912 | goto onError; |
| 3913 | goto nextByte; |
| 3914 | } |
| 3915 | x = (x<<4) & ~0xF; |
| 3916 | if (c >= '0' && c <= '9') |
| 3917 | x += c - '0'; |
| 3918 | else if (c >= 'a' && c <= 'f') |
| 3919 | x += 10 + c - 'a'; |
| 3920 | else |
| 3921 | x += 10 + c - 'A'; |
| 3922 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3923 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3924 | /* UCS-2 character */ |
| 3925 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3926 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3927 | /* UCS-4 character. Either store directly, or as |
| 3928 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3929 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3930 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3931 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3932 | x -= 0x10000L; |
| 3933 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3934 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3935 | #endif |
| 3936 | } else { |
| 3937 | endinpos = s-starts; |
| 3938 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3939 | if (unicode_decode_call_errorhandler( |
| 3940 | errors, &errorHandler, |
| 3941 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3942 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3943 | &v, &outpos, &p)) |
| 3944 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3945 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3946 | nextByte: |
| 3947 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3948 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3949 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3950 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3951 | Py_XDECREF(errorHandler); |
| 3952 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3953 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3954 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3955 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3956 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3957 | Py_XDECREF(errorHandler); |
| 3958 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3959 | return NULL; |
| 3960 | } |
| 3961 | |
| 3962 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3963 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3964 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3965 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3966 | char *p; |
| 3967 | char *q; |
| 3968 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3969 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3970 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3971 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3972 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3973 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3974 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3975 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3976 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3977 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3978 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3979 | if (repr == NULL) |
| 3980 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3981 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3982 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3983 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3984 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3985 | while (size-- > 0) { |
| 3986 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3987 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3988 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3989 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3990 | *p++ = '\\'; |
| 3991 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3992 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3993 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3994 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3995 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3996 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3997 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3998 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3999 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4000 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4001 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4002 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4003 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 4004 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 4005 | Py_UNICODE ch2; |
| 4006 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 4007 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4008 | ch2 = *s++; |
| 4009 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 4010 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4011 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 4012 | *p++ = '\\'; |
| 4013 | *p++ = 'U'; |
| 4014 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 4015 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 4016 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 4017 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 4018 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 4019 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 4020 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 4021 | *p++ = hexdigits[ucs & 0xf]; |
| 4022 | continue; |
| 4023 | } |
| 4024 | /* Fall through: isolated surrogates are copied as-is */ |
| 4025 | s--; |
| 4026 | size++; |
| 4027 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 4028 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4029 | /* Map 16-bit characters to '\uxxxx' */ |
| 4030 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4031 | *p++ = '\\'; |
| 4032 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 4033 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 4034 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 4035 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 4036 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4037 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4038 | /* Copy everything else as-is */ |
| 4039 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4040 | *p++ = (char) ch; |
| 4041 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4042 | size = p - q; |
| 4043 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4044 | assert(size > 0); |
| 4045 | if (_PyBytes_Resize(&repr, size) < 0) |
| 4046 | return NULL; |
| 4047 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 4051 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4052 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4053 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4054 | PyErr_BadArgument(); |
| 4055 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4056 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 4057 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 4058 | PyUnicode_GET_SIZE(unicode)); |
| 4059 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 4060 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4061 | } |
| 4062 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4063 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 4064 | |
| 4065 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4066 | Py_ssize_t size, |
| 4067 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4068 | { |
| 4069 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4070 | Py_ssize_t startinpos; |
| 4071 | Py_ssize_t endinpos; |
| 4072 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4073 | PyUnicodeObject *v; |
| 4074 | Py_UNICODE *p; |
| 4075 | const char *end; |
| 4076 | const char *reason; |
| 4077 | PyObject *errorHandler = NULL; |
| 4078 | PyObject *exc = NULL; |
| 4079 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 4080 | #ifdef Py_UNICODE_WIDE |
| 4081 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 4082 | #endif |
| 4083 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4084 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4085 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 4086 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4087 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4088 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4089 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4090 | p = PyUnicode_AS_UNICODE(v); |
| 4091 | end = s + size; |
| 4092 | |
| 4093 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4094 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4095 | /* We have to sanity check the raw data, otherwise doom looms for |
| 4096 | some malformed UCS-4 data. */ |
| 4097 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4098 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4099 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4100 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4101 | end-s < Py_UNICODE_SIZE |
| 4102 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4103 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4104 | startinpos = s - starts; |
| 4105 | if (end-s < Py_UNICODE_SIZE) { |
| 4106 | endinpos = end-starts; |
| 4107 | reason = "truncated input"; |
| 4108 | } |
| 4109 | else { |
| 4110 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 4111 | reason = "illegal code point (> 0x10FFFF)"; |
| 4112 | } |
| 4113 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 4114 | if (unicode_decode_call_errorhandler( |
| 4115 | errors, &errorHandler, |
| 4116 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4117 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4118 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4119 | goto onError; |
| 4120 | } |
| 4121 | } |
| 4122 | else { |
| 4123 | p++; |
| 4124 | s += Py_UNICODE_SIZE; |
| 4125 | } |
| 4126 | } |
| 4127 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4128 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4129 | goto onError; |
| 4130 | Py_XDECREF(errorHandler); |
| 4131 | Py_XDECREF(exc); |
| 4132 | return (PyObject *)v; |
| 4133 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4134 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 4135 | Py_XDECREF(v); |
| 4136 | Py_XDECREF(errorHandler); |
| 4137 | Py_XDECREF(exc); |
| 4138 | return NULL; |
| 4139 | } |
| 4140 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4141 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 4142 | |
| 4143 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4144 | Py_ssize_t size, |
| 4145 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4146 | { |
| 4147 | PyUnicodeObject *v; |
| 4148 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4149 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4150 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4151 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4152 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4153 | Py_UNICODE r = *(unsigned char*)s; |
| 4154 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4157 | v = _PyUnicode_New(size); |
| 4158 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4159 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4160 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4161 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4162 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4163 | e = s + size; |
| 4164 | /* Unrolling the copy makes it much faster by reducing the looping |
| 4165 | overhead. This is similar to what many memcpy() implementations do. */ |
| 4166 | unrolled_end = e - 4; |
| 4167 | while (s < unrolled_end) { |
| 4168 | p[0] = (unsigned char) s[0]; |
| 4169 | p[1] = (unsigned char) s[1]; |
| 4170 | p[2] = (unsigned char) s[2]; |
| 4171 | p[3] = (unsigned char) s[3]; |
| 4172 | s += 4; |
| 4173 | p += 4; |
| 4174 | } |
| 4175 | while (s < e) |
| 4176 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4177 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4178 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4179 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4180 | Py_XDECREF(v); |
| 4181 | return NULL; |
| 4182 | } |
| 4183 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4184 | /* create or adjust a UnicodeEncodeError */ |
| 4185 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4186 | const char *encoding, |
| 4187 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4188 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4189 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4190 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4191 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4192 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4193 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4194 | } |
| 4195 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4196 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4197 | goto onError; |
| 4198 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4199 | goto onError; |
| 4200 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4201 | goto onError; |
| 4202 | return; |
| 4203 | onError: |
| 4204 | Py_DECREF(*exceptionObject); |
| 4205 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4206 | } |
| 4207 | } |
| 4208 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4209 | /* raises a UnicodeEncodeError */ |
| 4210 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4211 | const char *encoding, |
| 4212 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4213 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4214 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4215 | { |
| 4216 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4217 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4218 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4219 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | /* error handling callback helper: |
| 4223 | build arguments, call the callback and check the arguments, |
| 4224 | put the result into newpos and return the replacement string, which |
| 4225 | has to be freed by the caller */ |
| 4226 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4227 | PyObject **errorHandler, |
| 4228 | const char *encoding, const char *reason, |
| 4229 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4230 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4231 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4232 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4233 | 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] | 4234 | |
| 4235 | PyObject *restuple; |
| 4236 | PyObject *resunicode; |
| 4237 | |
| 4238 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4239 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4240 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4241 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4245 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4246 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4247 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4248 | |
| 4249 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4250 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4251 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4252 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4253 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4254 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4255 | Py_DECREF(restuple); |
| 4256 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4257 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4258 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4259 | &resunicode, newpos)) { |
| 4260 | Py_DECREF(restuple); |
| 4261 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4262 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4263 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4264 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4265 | Py_DECREF(restuple); |
| 4266 | return NULL; |
| 4267 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4268 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4269 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4270 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4271 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4272 | Py_DECREF(restuple); |
| 4273 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4274 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4275 | Py_INCREF(resunicode); |
| 4276 | Py_DECREF(restuple); |
| 4277 | return resunicode; |
| 4278 | } |
| 4279 | |
| 4280 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4281 | Py_ssize_t size, |
| 4282 | const char *errors, |
| 4283 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4284 | { |
| 4285 | /* output object */ |
| 4286 | PyObject *res; |
| 4287 | /* pointers to the beginning and end+1 of input */ |
| 4288 | const Py_UNICODE *startp = p; |
| 4289 | const Py_UNICODE *endp = p + size; |
| 4290 | /* pointer to the beginning of the unencodable characters */ |
| 4291 | /* const Py_UNICODE *badp = NULL; */ |
| 4292 | /* pointer into the output */ |
| 4293 | char *str; |
| 4294 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4295 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4296 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4297 | 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] | 4298 | PyObject *errorHandler = NULL; |
| 4299 | PyObject *exc = NULL; |
| 4300 | /* the following variable is used for caching string comparisons |
| 4301 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4302 | int known_errorHandler = -1; |
| 4303 | |
| 4304 | /* allocate enough for a simple encoding without |
| 4305 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4306 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4307 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4308 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4309 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4310 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4311 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4312 | ressize = size; |
| 4313 | |
| 4314 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4315 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4316 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4317 | /* can we encode this? */ |
| 4318 | if (c<limit) { |
| 4319 | /* no overflow check, because we know that the space is enough */ |
| 4320 | *str++ = (char)c; |
| 4321 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4322 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4323 | else { |
| 4324 | Py_ssize_t unicodepos = p-startp; |
| 4325 | Py_ssize_t requiredsize; |
| 4326 | PyObject *repunicode; |
| 4327 | Py_ssize_t repsize; |
| 4328 | Py_ssize_t newpos; |
| 4329 | Py_ssize_t respos; |
| 4330 | Py_UNICODE *uni2; |
| 4331 | /* startpos for collecting unencodable chars */ |
| 4332 | const Py_UNICODE *collstart = p; |
| 4333 | const Py_UNICODE *collend = p; |
| 4334 | /* find all unecodable characters */ |
| 4335 | while ((collend < endp) && ((*collend)>=limit)) |
| 4336 | ++collend; |
| 4337 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4338 | if (known_errorHandler==-1) { |
| 4339 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4340 | known_errorHandler = 1; |
| 4341 | else if (!strcmp(errors, "replace")) |
| 4342 | known_errorHandler = 2; |
| 4343 | else if (!strcmp(errors, "ignore")) |
| 4344 | known_errorHandler = 3; |
| 4345 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4346 | known_errorHandler = 4; |
| 4347 | else |
| 4348 | known_errorHandler = 0; |
| 4349 | } |
| 4350 | switch (known_errorHandler) { |
| 4351 | case 1: /* strict */ |
| 4352 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4353 | goto onError; |
| 4354 | case 2: /* replace */ |
| 4355 | while (collstart++<collend) |
| 4356 | *str++ = '?'; /* fall through */ |
| 4357 | case 3: /* ignore */ |
| 4358 | p = collend; |
| 4359 | break; |
| 4360 | case 4: /* xmlcharrefreplace */ |
| 4361 | respos = str - PyBytes_AS_STRING(res); |
| 4362 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4363 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4364 | if (*p<10) |
| 4365 | repsize += 2+1+1; |
| 4366 | else if (*p<100) |
| 4367 | repsize += 2+2+1; |
| 4368 | else if (*p<1000) |
| 4369 | repsize += 2+3+1; |
| 4370 | else if (*p<10000) |
| 4371 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4372 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4373 | else |
| 4374 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4375 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4376 | else if (*p<100000) |
| 4377 | repsize += 2+5+1; |
| 4378 | else if (*p<1000000) |
| 4379 | repsize += 2+6+1; |
| 4380 | else |
| 4381 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4382 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4383 | } |
| 4384 | requiredsize = respos+repsize+(endp-collend); |
| 4385 | if (requiredsize > ressize) { |
| 4386 | if (requiredsize<2*ressize) |
| 4387 | requiredsize = 2*ressize; |
| 4388 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4389 | goto onError; |
| 4390 | str = PyBytes_AS_STRING(res) + respos; |
| 4391 | ressize = requiredsize; |
| 4392 | } |
| 4393 | /* generate replacement (temporarily (mis)uses p) */ |
| 4394 | for (p = collstart; p < collend; ++p) { |
| 4395 | str += sprintf(str, "&#%d;", (int)*p); |
| 4396 | } |
| 4397 | p = collend; |
| 4398 | break; |
| 4399 | default: |
| 4400 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4401 | encoding, reason, startp, size, &exc, |
| 4402 | collstart-startp, collend-startp, &newpos); |
| 4403 | if (repunicode == NULL) |
| 4404 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4405 | if (PyBytes_Check(repunicode)) { |
| 4406 | /* Directly copy bytes result to output. */ |
| 4407 | repsize = PyBytes_Size(repunicode); |
| 4408 | if (repsize > 1) { |
| 4409 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4410 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4411 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4412 | Py_DECREF(repunicode); |
| 4413 | goto onError; |
| 4414 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 4415 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4416 | ressize += repsize-1; |
| 4417 | } |
| 4418 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4419 | str += repsize; |
| 4420 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4421 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4422 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4423 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4424 | /* need more space? (at least enough for what we |
| 4425 | have+the replacement+the rest of the string, so |
| 4426 | we won't have to check space for encodable characters) */ |
| 4427 | respos = str - PyBytes_AS_STRING(res); |
| 4428 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4429 | requiredsize = respos+repsize+(endp-collend); |
| 4430 | if (requiredsize > ressize) { |
| 4431 | if (requiredsize<2*ressize) |
| 4432 | requiredsize = 2*ressize; |
| 4433 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4434 | Py_DECREF(repunicode); |
| 4435 | goto onError; |
| 4436 | } |
| 4437 | str = PyBytes_AS_STRING(res) + respos; |
| 4438 | ressize = requiredsize; |
| 4439 | } |
| 4440 | /* check if there is anything unencodable in the replacement |
| 4441 | and copy it to the output */ |
| 4442 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4443 | c = *uni2; |
| 4444 | if (c >= limit) { |
| 4445 | raise_encode_exception(&exc, encoding, startp, size, |
| 4446 | unicodepos, unicodepos+1, reason); |
| 4447 | Py_DECREF(repunicode); |
| 4448 | goto onError; |
| 4449 | } |
| 4450 | *str = (char)c; |
| 4451 | } |
| 4452 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4453 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4454 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4455 | } |
| 4456 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4457 | /* Resize if we allocated to much */ |
| 4458 | size = str - PyBytes_AS_STRING(res); |
| 4459 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4460 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4461 | if (_PyBytes_Resize(&res, size) < 0) |
| 4462 | goto onError; |
| 4463 | } |
| 4464 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4465 | Py_XDECREF(errorHandler); |
| 4466 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4467 | return res; |
| 4468 | |
| 4469 | onError: |
| 4470 | Py_XDECREF(res); |
| 4471 | Py_XDECREF(errorHandler); |
| 4472 | Py_XDECREF(exc); |
| 4473 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4477 | Py_ssize_t size, |
| 4478 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4479 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4480 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4484 | { |
| 4485 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4486 | PyErr_BadArgument(); |
| 4487 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4488 | } |
| 4489 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4490 | PyUnicode_GET_SIZE(unicode), |
| 4491 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4496 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4497 | Py_ssize_t size, |
| 4498 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4499 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4500 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4501 | PyUnicodeObject *v; |
| 4502 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4503 | Py_ssize_t startinpos; |
| 4504 | Py_ssize_t endinpos; |
| 4505 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4506 | const char *e; |
| 4507 | PyObject *errorHandler = NULL; |
| 4508 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4509 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4510 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4511 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4512 | Py_UNICODE r = *(unsigned char*)s; |
| 4513 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4514 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4515 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4516 | v = _PyUnicode_New(size); |
| 4517 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4518 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4519 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4520 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4521 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4522 | e = s + size; |
| 4523 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4524 | register unsigned char c = (unsigned char)*s; |
| 4525 | if (c < 128) { |
| 4526 | *p++ = c; |
| 4527 | ++s; |
| 4528 | } |
| 4529 | else { |
| 4530 | startinpos = s-starts; |
| 4531 | endinpos = startinpos + 1; |
| 4532 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4533 | if (unicode_decode_call_errorhandler( |
| 4534 | errors, &errorHandler, |
| 4535 | "ascii", "ordinal not in range(128)", |
| 4536 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4537 | &v, &outpos, &p)) |
| 4538 | goto onError; |
| 4539 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4540 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4541 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4542 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4543 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4544 | Py_XDECREF(errorHandler); |
| 4545 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4546 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4547 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4548 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4549 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4550 | Py_XDECREF(errorHandler); |
| 4551 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4552 | return NULL; |
| 4553 | } |
| 4554 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4555 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4556 | Py_ssize_t size, |
| 4557 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4558 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4559 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4563 | { |
| 4564 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4565 | PyErr_BadArgument(); |
| 4566 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4567 | } |
| 4568 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4569 | PyUnicode_GET_SIZE(unicode), |
| 4570 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4573 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4574 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4575 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4576 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4577 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4578 | #define NEED_RETRY |
| 4579 | #endif |
| 4580 | |
| 4581 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4582 | a) it assumes an incomplete character consists of a single byte, and |
| 4583 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4584 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4585 | |
| 4586 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4587 | { |
| 4588 | const char *curr = s + offset; |
| 4589 | |
| 4590 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4591 | const char *prev = CharPrev(s, curr); |
| 4592 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4593 | } |
| 4594 | return 0; |
| 4595 | } |
| 4596 | |
| 4597 | /* |
| 4598 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4599 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4600 | */ |
| 4601 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4602 | const char *s, /* MBCS string */ |
| 4603 | int size, /* sizeof MBCS string */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4604 | int final, |
| 4605 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4606 | { |
| 4607 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4608 | Py_ssize_t n; |
| 4609 | DWORD usize; |
| 4610 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4611 | |
| 4612 | assert(size >= 0); |
| 4613 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4614 | /* check and handle 'errors' arg */ |
| 4615 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 4616 | flags = MB_ERR_INVALID_CHARS; |
| 4617 | else if (strcmp(errors, "ignore")==0) |
| 4618 | flags = 0; |
| 4619 | else { |
| 4620 | PyErr_Format(PyExc_ValueError, |
| 4621 | "mbcs encoding does not support errors='%s'", |
| 4622 | errors); |
| 4623 | return -1; |
| 4624 | } |
| 4625 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4626 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4627 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4628 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4629 | |
| 4630 | /* First get the size of the result */ |
| 4631 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4632 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 4633 | if (usize==0) |
| 4634 | goto mbcs_decode_error; |
| 4635 | } else |
| 4636 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4637 | |
| 4638 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4639 | /* Create unicode object */ |
| 4640 | *v = _PyUnicode_New(usize); |
| 4641 | if (*v == NULL) |
| 4642 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4643 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4644 | } |
| 4645 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4646 | /* Extend unicode object */ |
| 4647 | n = PyUnicode_GET_SIZE(*v); |
| 4648 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4649 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4653 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4654 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4655 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 4656 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4657 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4658 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4659 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4660 | |
| 4661 | mbcs_decode_error: |
| 4662 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 4663 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 4664 | windows error |
| 4665 | */ |
| 4666 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 4667 | /* Ideally, we should get reason from FormatMessage - this |
| 4668 | is the Windows 2000 English version of the message |
| 4669 | */ |
| 4670 | PyObject *exc = NULL; |
| 4671 | const char *reason = "No mapping for the Unicode character exists " |
| 4672 | "in the target multi-byte code page."; |
| 4673 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 4674 | if (exc != NULL) { |
| 4675 | PyCodec_StrictErrors(exc); |
| 4676 | Py_DECREF(exc); |
| 4677 | } |
| 4678 | } else { |
| 4679 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4680 | } |
| 4681 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4682 | } |
| 4683 | |
| 4684 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4685 | Py_ssize_t size, |
| 4686 | const char *errors, |
| 4687 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4688 | { |
| 4689 | PyUnicodeObject *v = NULL; |
| 4690 | int done; |
| 4691 | |
| 4692 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4693 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4694 | |
| 4695 | #ifdef NEED_RETRY |
| 4696 | retry: |
| 4697 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4698 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4699 | else |
| 4700 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4701 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4702 | |
| 4703 | if (done < 0) { |
| 4704 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4705 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
| 4708 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4709 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4710 | |
| 4711 | #ifdef NEED_RETRY |
| 4712 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4713 | s += done; |
| 4714 | size -= done; |
| 4715 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4716 | } |
| 4717 | #endif |
| 4718 | |
| 4719 | return (PyObject *)v; |
| 4720 | } |
| 4721 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4722 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4723 | Py_ssize_t size, |
| 4724 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4725 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4726 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4727 | } |
| 4728 | |
| 4729 | /* |
| 4730 | * Convert unicode into string object (MBCS). |
| 4731 | * Returns 0 if succeed, -1 otherwise. |
| 4732 | */ |
| 4733 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4734 | const Py_UNICODE *p, /* unicode */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4735 | int size, /* size of unicode */ |
| 4736 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4737 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4738 | BOOL usedDefaultChar = FALSE; |
| 4739 | BOOL *pusedDefaultChar; |
| 4740 | int mbcssize; |
| 4741 | Py_ssize_t n; |
| 4742 | PyObject *exc = NULL; |
| 4743 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4744 | |
| 4745 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4746 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4747 | /* check and handle 'errors' arg */ |
| 4748 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 4749 | flags = WC_NO_BEST_FIT_CHARS; |
| 4750 | pusedDefaultChar = &usedDefaultChar; |
| 4751 | } else if (strcmp(errors, "replace")==0) { |
| 4752 | flags = 0; |
| 4753 | pusedDefaultChar = NULL; |
| 4754 | } else { |
| 4755 | PyErr_Format(PyExc_ValueError, |
| 4756 | "mbcs encoding does not support errors='%s'", |
| 4757 | errors); |
| 4758 | return -1; |
| 4759 | } |
| 4760 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4761 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4762 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4763 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 4764 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4765 | if (mbcssize == 0) { |
| 4766 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4767 | return -1; |
| 4768 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4769 | /* If we used a default char, then we failed! */ |
| 4770 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4771 | goto mbcs_encode_error; |
| 4772 | } else { |
| 4773 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4776 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4777 | /* Create string object */ |
| 4778 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4779 | if (*repr == NULL) |
| 4780 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4781 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4782 | } |
| 4783 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4784 | /* Extend string object */ |
| 4785 | n = PyBytes_Size(*repr); |
| 4786 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4787 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | /* Do the conversion */ |
| 4791 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4792 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4793 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 4794 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4795 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4796 | return -1; |
| 4797 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4798 | if (pusedDefaultChar && *pusedDefaultChar) |
| 4799 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4800 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4801 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4802 | |
| 4803 | mbcs_encode_error: |
| 4804 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 4805 | Py_XDECREF(exc); |
| 4806 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4810 | Py_ssize_t size, |
| 4811 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4812 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4813 | PyObject *repr = NULL; |
| 4814 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4815 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4816 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4817 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4818 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4819 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4820 | else |
| 4821 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4822 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4823 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4824 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4825 | Py_XDECREF(repr); |
| 4826 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4827 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4828 | |
| 4829 | #ifdef NEED_RETRY |
| 4830 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4831 | p += INT_MAX; |
| 4832 | size -= INT_MAX; |
| 4833 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4834 | } |
| 4835 | #endif |
| 4836 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4837 | return repr; |
| 4838 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4839 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4840 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4841 | { |
| 4842 | if (!PyUnicode_Check(unicode)) { |
| 4843 | PyErr_BadArgument(); |
| 4844 | return NULL; |
| 4845 | } |
| 4846 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4847 | PyUnicode_GET_SIZE(unicode), |
| 4848 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4851 | #undef NEED_RETRY |
| 4852 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4853 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4857 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4858 | Py_ssize_t size, |
| 4859 | PyObject *mapping, |
| 4860 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4861 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4862 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4863 | Py_ssize_t startinpos; |
| 4864 | Py_ssize_t endinpos; |
| 4865 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4866 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4867 | PyUnicodeObject *v; |
| 4868 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4869 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4870 | PyObject *errorHandler = NULL; |
| 4871 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4872 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4873 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4874 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4875 | /* Default to Latin-1 */ |
| 4876 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4877 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4878 | |
| 4879 | v = _PyUnicode_New(size); |
| 4880 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4881 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4882 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4883 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4884 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4885 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4886 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4887 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4888 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4889 | while (s < e) { |
| 4890 | unsigned char ch = *s; |
| 4891 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4892 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4893 | if (ch < maplen) |
| 4894 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4895 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4896 | if (x == 0xfffe) { |
| 4897 | /* undefined mapping */ |
| 4898 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4899 | startinpos = s-starts; |
| 4900 | endinpos = startinpos+1; |
| 4901 | if (unicode_decode_call_errorhandler( |
| 4902 | errors, &errorHandler, |
| 4903 | "charmap", "character maps to <undefined>", |
| 4904 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4905 | &v, &outpos, &p)) { |
| 4906 | goto onError; |
| 4907 | } |
| 4908 | continue; |
| 4909 | } |
| 4910 | *p++ = x; |
| 4911 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4912 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4913 | } |
| 4914 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4915 | while (s < e) { |
| 4916 | unsigned char ch = *s; |
| 4917 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4918 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4919 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4920 | w = PyLong_FromLong((long)ch); |
| 4921 | if (w == NULL) |
| 4922 | goto onError; |
| 4923 | x = PyObject_GetItem(mapping, w); |
| 4924 | Py_DECREF(w); |
| 4925 | if (x == NULL) { |
| 4926 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4927 | /* No mapping found means: mapping is undefined. */ |
| 4928 | PyErr_Clear(); |
| 4929 | x = Py_None; |
| 4930 | Py_INCREF(x); |
| 4931 | } else |
| 4932 | goto onError; |
| 4933 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4934 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4935 | /* Apply mapping */ |
| 4936 | if (PyLong_Check(x)) { |
| 4937 | long value = PyLong_AS_LONG(x); |
| 4938 | if (value < 0 || value > 65535) { |
| 4939 | PyErr_SetString(PyExc_TypeError, |
| 4940 | "character mapping must be in range(65536)"); |
| 4941 | Py_DECREF(x); |
| 4942 | goto onError; |
| 4943 | } |
| 4944 | *p++ = (Py_UNICODE)value; |
| 4945 | } |
| 4946 | else if (x == Py_None) { |
| 4947 | /* undefined mapping */ |
| 4948 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4949 | startinpos = s-starts; |
| 4950 | endinpos = startinpos+1; |
| 4951 | if (unicode_decode_call_errorhandler( |
| 4952 | errors, &errorHandler, |
| 4953 | "charmap", "character maps to <undefined>", |
| 4954 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4955 | &v, &outpos, &p)) { |
| 4956 | Py_DECREF(x); |
| 4957 | goto onError; |
| 4958 | } |
| 4959 | Py_DECREF(x); |
| 4960 | continue; |
| 4961 | } |
| 4962 | else if (PyUnicode_Check(x)) { |
| 4963 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4964 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4965 | if (targetsize == 1) |
| 4966 | /* 1-1 mapping */ |
| 4967 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4968 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4969 | else if (targetsize > 1) { |
| 4970 | /* 1-n mapping */ |
| 4971 | if (targetsize > extrachars) { |
| 4972 | /* resize first */ |
| 4973 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4974 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4975 | (targetsize << 2); |
| 4976 | extrachars += needed; |
| 4977 | /* XXX overflow detection missing */ |
| 4978 | if (_PyUnicode_Resize(&v, |
| 4979 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4980 | Py_DECREF(x); |
| 4981 | goto onError; |
| 4982 | } |
| 4983 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4984 | } |
| 4985 | Py_UNICODE_COPY(p, |
| 4986 | PyUnicode_AS_UNICODE(x), |
| 4987 | targetsize); |
| 4988 | p += targetsize; |
| 4989 | extrachars -= targetsize; |
| 4990 | } |
| 4991 | /* 1-0 mapping: skip the character */ |
| 4992 | } |
| 4993 | else { |
| 4994 | /* wrong return value */ |
| 4995 | PyErr_SetString(PyExc_TypeError, |
| 4996 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4997 | Py_DECREF(x); |
| 4998 | goto onError; |
| 4999 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5000 | Py_DECREF(x); |
| 5001 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5002 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5003 | } |
| 5004 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5005 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 5006 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5007 | Py_XDECREF(errorHandler); |
| 5008 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5009 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5010 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5011 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5012 | Py_XDECREF(errorHandler); |
| 5013 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5014 | Py_XDECREF(v); |
| 5015 | return NULL; |
| 5016 | } |
| 5017 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5018 | /* Charmap encoding: the lookup table */ |
| 5019 | |
| 5020 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5021 | PyObject_HEAD |
| 5022 | unsigned char level1[32]; |
| 5023 | int count2, count3; |
| 5024 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5025 | }; |
| 5026 | |
| 5027 | static PyObject* |
| 5028 | encoding_map_size(PyObject *obj, PyObject* args) |
| 5029 | { |
| 5030 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5031 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5032 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5036 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5037 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 5038 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5039 | }; |
| 5040 | |
| 5041 | static void |
| 5042 | encoding_map_dealloc(PyObject* o) |
| 5043 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5044 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5048 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5049 | "EncodingMap", /*tp_name*/ |
| 5050 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 5051 | 0, /*tp_itemsize*/ |
| 5052 | /* methods */ |
| 5053 | encoding_map_dealloc, /*tp_dealloc*/ |
| 5054 | 0, /*tp_print*/ |
| 5055 | 0, /*tp_getattr*/ |
| 5056 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 5057 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5058 | 0, /*tp_repr*/ |
| 5059 | 0, /*tp_as_number*/ |
| 5060 | 0, /*tp_as_sequence*/ |
| 5061 | 0, /*tp_as_mapping*/ |
| 5062 | 0, /*tp_hash*/ |
| 5063 | 0, /*tp_call*/ |
| 5064 | 0, /*tp_str*/ |
| 5065 | 0, /*tp_getattro*/ |
| 5066 | 0, /*tp_setattro*/ |
| 5067 | 0, /*tp_as_buffer*/ |
| 5068 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5069 | 0, /*tp_doc*/ |
| 5070 | 0, /*tp_traverse*/ |
| 5071 | 0, /*tp_clear*/ |
| 5072 | 0, /*tp_richcompare*/ |
| 5073 | 0, /*tp_weaklistoffset*/ |
| 5074 | 0, /*tp_iter*/ |
| 5075 | 0, /*tp_iternext*/ |
| 5076 | encoding_map_methods, /*tp_methods*/ |
| 5077 | 0, /*tp_members*/ |
| 5078 | 0, /*tp_getset*/ |
| 5079 | 0, /*tp_base*/ |
| 5080 | 0, /*tp_dict*/ |
| 5081 | 0, /*tp_descr_get*/ |
| 5082 | 0, /*tp_descr_set*/ |
| 5083 | 0, /*tp_dictoffset*/ |
| 5084 | 0, /*tp_init*/ |
| 5085 | 0, /*tp_alloc*/ |
| 5086 | 0, /*tp_new*/ |
| 5087 | 0, /*tp_free*/ |
| 5088 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5089 | }; |
| 5090 | |
| 5091 | PyObject* |
| 5092 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 5093 | { |
| 5094 | Py_UNICODE *decode; |
| 5095 | PyObject *result; |
| 5096 | struct encoding_map *mresult; |
| 5097 | int i; |
| 5098 | int need_dict = 0; |
| 5099 | unsigned char level1[32]; |
| 5100 | unsigned char level2[512]; |
| 5101 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 5102 | int count2 = 0, count3 = 0; |
| 5103 | |
| 5104 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 5105 | PyErr_BadArgument(); |
| 5106 | return NULL; |
| 5107 | } |
| 5108 | decode = PyUnicode_AS_UNICODE(string); |
| 5109 | memset(level1, 0xFF, sizeof level1); |
| 5110 | memset(level2, 0xFF, sizeof level2); |
| 5111 | |
| 5112 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 5113 | or if there are non-BMP characters, we need to use |
| 5114 | a mapping dictionary. */ |
| 5115 | if (decode[0] != 0) |
| 5116 | need_dict = 1; |
| 5117 | for (i = 1; i < 256; i++) { |
| 5118 | int l1, l2; |
| 5119 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5120 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5121 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5122 | #endif |
| 5123 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5124 | need_dict = 1; |
| 5125 | break; |
| 5126 | } |
| 5127 | if (decode[i] == 0xFFFE) |
| 5128 | /* unmapped character */ |
| 5129 | continue; |
| 5130 | l1 = decode[i] >> 11; |
| 5131 | l2 = decode[i] >> 7; |
| 5132 | if (level1[l1] == 0xFF) |
| 5133 | level1[l1] = count2++; |
| 5134 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5135 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5136 | } |
| 5137 | |
| 5138 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 5139 | need_dict = 1; |
| 5140 | |
| 5141 | if (need_dict) { |
| 5142 | PyObject *result = PyDict_New(); |
| 5143 | PyObject *key, *value; |
| 5144 | if (!result) |
| 5145 | return NULL; |
| 5146 | for (i = 0; i < 256; i++) { |
| 5147 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5148 | key = PyLong_FromLong(decode[i]); |
| 5149 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5150 | if (!key || !value) |
| 5151 | goto failed1; |
| 5152 | if (PyDict_SetItem(result, key, value) == -1) |
| 5153 | goto failed1; |
| 5154 | Py_DECREF(key); |
| 5155 | Py_DECREF(value); |
| 5156 | } |
| 5157 | return result; |
| 5158 | failed1: |
| 5159 | Py_XDECREF(key); |
| 5160 | Py_XDECREF(value); |
| 5161 | Py_DECREF(result); |
| 5162 | return NULL; |
| 5163 | } |
| 5164 | |
| 5165 | /* Create a three-level trie */ |
| 5166 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 5167 | 16*count2 + 128*count3 - 1); |
| 5168 | if (!result) |
| 5169 | return PyErr_NoMemory(); |
| 5170 | PyObject_Init(result, &EncodingMapType); |
| 5171 | mresult = (struct encoding_map*)result; |
| 5172 | mresult->count2 = count2; |
| 5173 | mresult->count3 = count3; |
| 5174 | mlevel1 = mresult->level1; |
| 5175 | mlevel2 = mresult->level23; |
| 5176 | mlevel3 = mresult->level23 + 16*count2; |
| 5177 | memcpy(mlevel1, level1, 32); |
| 5178 | memset(mlevel2, 0xFF, 16*count2); |
| 5179 | memset(mlevel3, 0, 128*count3); |
| 5180 | count3 = 0; |
| 5181 | for (i = 1; i < 256; i++) { |
| 5182 | int o1, o2, o3, i2, i3; |
| 5183 | if (decode[i] == 0xFFFE) |
| 5184 | /* unmapped character */ |
| 5185 | continue; |
| 5186 | o1 = decode[i]>>11; |
| 5187 | o2 = (decode[i]>>7) & 0xF; |
| 5188 | i2 = 16*mlevel1[o1] + o2; |
| 5189 | if (mlevel2[i2] == 0xFF) |
| 5190 | mlevel2[i2] = count3++; |
| 5191 | o3 = decode[i] & 0x7F; |
| 5192 | i3 = 128*mlevel2[i2] + o3; |
| 5193 | mlevel3[i3] = i; |
| 5194 | } |
| 5195 | return result; |
| 5196 | } |
| 5197 | |
| 5198 | static int |
| 5199 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 5200 | { |
| 5201 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 5202 | int l1 = c>>11; |
| 5203 | int l2 = (c>>7) & 0xF; |
| 5204 | int l3 = c & 0x7F; |
| 5205 | int i; |
| 5206 | |
| 5207 | #ifdef Py_UNICODE_WIDE |
| 5208 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5209 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5210 | } |
| 5211 | #endif |
| 5212 | if (c == 0) |
| 5213 | return 0; |
| 5214 | /* level 1*/ |
| 5215 | i = map->level1[l1]; |
| 5216 | if (i == 0xFF) { |
| 5217 | return -1; |
| 5218 | } |
| 5219 | /* level 2*/ |
| 5220 | i = map->level23[16*i+l2]; |
| 5221 | if (i == 0xFF) { |
| 5222 | return -1; |
| 5223 | } |
| 5224 | /* level 3 */ |
| 5225 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 5226 | if (i == 0) { |
| 5227 | return -1; |
| 5228 | } |
| 5229 | return i; |
| 5230 | } |
| 5231 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5232 | /* Lookup the character ch in the mapping. If the character |
| 5233 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 5234 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5235 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5236 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5237 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5238 | PyObject *x; |
| 5239 | |
| 5240 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5241 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5242 | x = PyObject_GetItem(mapping, w); |
| 5243 | Py_DECREF(w); |
| 5244 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5245 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5246 | /* No mapping found means: mapping is undefined. */ |
| 5247 | PyErr_Clear(); |
| 5248 | x = Py_None; |
| 5249 | Py_INCREF(x); |
| 5250 | return x; |
| 5251 | } else |
| 5252 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5253 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5254 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5255 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5256 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5257 | long value = PyLong_AS_LONG(x); |
| 5258 | if (value < 0 || value > 255) { |
| 5259 | PyErr_SetString(PyExc_TypeError, |
| 5260 | "character mapping must be in range(256)"); |
| 5261 | Py_DECREF(x); |
| 5262 | return NULL; |
| 5263 | } |
| 5264 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5266 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5267 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5268 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5269 | /* wrong return value */ |
| 5270 | PyErr_Format(PyExc_TypeError, |
| 5271 | "character mapping must return integer, bytes or None, not %.400s", |
| 5272 | x->ob_type->tp_name); |
| 5273 | Py_DECREF(x); |
| 5274 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5275 | } |
| 5276 | } |
| 5277 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5278 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5279 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5280 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5281 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5282 | /* exponentially overallocate to minimize reallocations */ |
| 5283 | if (requiredsize < 2*outsize) |
| 5284 | requiredsize = 2*outsize; |
| 5285 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5286 | return -1; |
| 5287 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5290 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5291 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5292 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5293 | /* 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] | 5294 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5295 | space is available. Return a new reference to the object that |
| 5296 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5297 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5298 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5299 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5300 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5301 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5302 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5303 | PyObject *rep; |
| 5304 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5305 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5306 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5307 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5308 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5309 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5310 | if (res == -1) |
| 5311 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5312 | if (outsize<requiredsize) |
| 5313 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5314 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5315 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5316 | outstart[(*outpos)++] = (char)res; |
| 5317 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
| 5320 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5321 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5322 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5323 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5324 | Py_DECREF(rep); |
| 5325 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5326 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5327 | if (PyLong_Check(rep)) { |
| 5328 | Py_ssize_t requiredsize = *outpos+1; |
| 5329 | if (outsize<requiredsize) |
| 5330 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5331 | Py_DECREF(rep); |
| 5332 | return enc_EXCEPTION; |
| 5333 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5334 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5335 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5336 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5337 | else { |
| 5338 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5339 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5340 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5341 | if (outsize<requiredsize) |
| 5342 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5343 | Py_DECREF(rep); |
| 5344 | return enc_EXCEPTION; |
| 5345 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5346 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5347 | memcpy(outstart + *outpos, repchars, repsize); |
| 5348 | *outpos += repsize; |
| 5349 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5350 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5351 | Py_DECREF(rep); |
| 5352 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | /* handle an error in PyUnicode_EncodeCharmap |
| 5356 | Return 0 on success, -1 on error */ |
| 5357 | static |
| 5358 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5359 | 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] | 5360 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5361 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5362 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5363 | { |
| 5364 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5365 | Py_ssize_t repsize; |
| 5366 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5367 | Py_UNICODE *uni2; |
| 5368 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5369 | Py_ssize_t collstartpos = *inpos; |
| 5370 | Py_ssize_t collendpos = *inpos+1; |
| 5371 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5372 | char *encoding = "charmap"; |
| 5373 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5374 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5375 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5376 | /* find all unencodable characters */ |
| 5377 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5378 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5379 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5380 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5381 | if (res != -1) |
| 5382 | break; |
| 5383 | ++collendpos; |
| 5384 | continue; |
| 5385 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5386 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5387 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5388 | if (rep==NULL) |
| 5389 | return -1; |
| 5390 | else if (rep!=Py_None) { |
| 5391 | Py_DECREF(rep); |
| 5392 | break; |
| 5393 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5394 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5395 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5396 | } |
| 5397 | /* cache callback name lookup |
| 5398 | * (if not done yet, i.e. it's the first error) */ |
| 5399 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5400 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5401 | *known_errorHandler = 1; |
| 5402 | else if (!strcmp(errors, "replace")) |
| 5403 | *known_errorHandler = 2; |
| 5404 | else if (!strcmp(errors, "ignore")) |
| 5405 | *known_errorHandler = 3; |
| 5406 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5407 | *known_errorHandler = 4; |
| 5408 | else |
| 5409 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5410 | } |
| 5411 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5412 | case 1: /* strict */ |
| 5413 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5414 | return -1; |
| 5415 | case 2: /* replace */ |
| 5416 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5417 | x = charmapencode_output('?', mapping, res, respos); |
| 5418 | if (x==enc_EXCEPTION) { |
| 5419 | return -1; |
| 5420 | } |
| 5421 | else if (x==enc_FAILED) { |
| 5422 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5423 | return -1; |
| 5424 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5425 | } |
| 5426 | /* fall through */ |
| 5427 | case 3: /* ignore */ |
| 5428 | *inpos = collendpos; |
| 5429 | break; |
| 5430 | case 4: /* xmlcharrefreplace */ |
| 5431 | /* generate replacement (temporarily (mis)uses p) */ |
| 5432 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5433 | char buffer[2+29+1+1]; |
| 5434 | char *cp; |
| 5435 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5436 | for (cp = buffer; *cp; ++cp) { |
| 5437 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5438 | if (x==enc_EXCEPTION) |
| 5439 | return -1; |
| 5440 | else if (x==enc_FAILED) { |
| 5441 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5442 | return -1; |
| 5443 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5444 | } |
| 5445 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5446 | *inpos = collendpos; |
| 5447 | break; |
| 5448 | default: |
| 5449 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5450 | encoding, reason, p, size, exceptionObject, |
| 5451 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5452 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5453 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5454 | if (PyBytes_Check(repunicode)) { |
| 5455 | /* Directly copy bytes result to output. */ |
| 5456 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5457 | Py_ssize_t requiredsize; |
| 5458 | repsize = PyBytes_Size(repunicode); |
| 5459 | requiredsize = *respos + repsize; |
| 5460 | if (requiredsize > outsize) |
| 5461 | /* Make room for all additional bytes. */ |
| 5462 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5463 | Py_DECREF(repunicode); |
| 5464 | return -1; |
| 5465 | } |
| 5466 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5467 | PyBytes_AsString(repunicode), repsize); |
| 5468 | *respos += repsize; |
| 5469 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5470 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5471 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5472 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5473 | /* generate replacement */ |
| 5474 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5475 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5476 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5477 | if (x==enc_EXCEPTION) { |
| 5478 | return -1; |
| 5479 | } |
| 5480 | else if (x==enc_FAILED) { |
| 5481 | Py_DECREF(repunicode); |
| 5482 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5483 | return -1; |
| 5484 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5485 | } |
| 5486 | *inpos = newpos; |
| 5487 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5488 | } |
| 5489 | return 0; |
| 5490 | } |
| 5491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5493 | Py_ssize_t size, |
| 5494 | PyObject *mapping, |
| 5495 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5497 | /* output object */ |
| 5498 | PyObject *res = NULL; |
| 5499 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5500 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5501 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5502 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5503 | PyObject *errorHandler = NULL; |
| 5504 | PyObject *exc = NULL; |
| 5505 | /* the following variable is used for caching string comparisons |
| 5506 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5507 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5508 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5509 | |
| 5510 | /* Default to Latin-1 */ |
| 5511 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5512 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5514 | /* allocate enough for a simple encoding without |
| 5515 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5516 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5517 | if (res == NULL) |
| 5518 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5519 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5520 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5521 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5522 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5523 | /* try to encode it */ |
| 5524 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5525 | if (x==enc_EXCEPTION) /* error */ |
| 5526 | goto onError; |
| 5527 | if (x==enc_FAILED) { /* unencodable character */ |
| 5528 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5529 | &exc, |
| 5530 | &known_errorHandler, &errorHandler, errors, |
| 5531 | &res, &respos)) { |
| 5532 | goto onError; |
| 5533 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5534 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5535 | else |
| 5536 | /* done with this character => adjust input position */ |
| 5537 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5539 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5540 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5541 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5542 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5543 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5544 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5545 | Py_XDECREF(exc); |
| 5546 | Py_XDECREF(errorHandler); |
| 5547 | return res; |
| 5548 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5549 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5550 | Py_XDECREF(res); |
| 5551 | Py_XDECREF(exc); |
| 5552 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | return NULL; |
| 5554 | } |
| 5555 | |
| 5556 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5557 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5558 | { |
| 5559 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5560 | PyErr_BadArgument(); |
| 5561 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 | } |
| 5563 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5564 | PyUnicode_GET_SIZE(unicode), |
| 5565 | mapping, |
| 5566 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5567 | } |
| 5568 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5569 | /* create or adjust a UnicodeTranslateError */ |
| 5570 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5571 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5572 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5573 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5575 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5576 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5577 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | } |
| 5579 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5580 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5581 | goto onError; |
| 5582 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5583 | goto onError; |
| 5584 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5585 | goto onError; |
| 5586 | return; |
| 5587 | onError: |
| 5588 | Py_DECREF(*exceptionObject); |
| 5589 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5590 | } |
| 5591 | } |
| 5592 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5593 | /* raises a UnicodeTranslateError */ |
| 5594 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5595 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5596 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5597 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5598 | { |
| 5599 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5600 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5601 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5602 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | /* error handling callback helper: |
| 5606 | build arguments, call the callback and check the arguments, |
| 5607 | put the result into newpos and return the replacement string, which |
| 5608 | has to be freed by the caller */ |
| 5609 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5610 | PyObject **errorHandler, |
| 5611 | const char *reason, |
| 5612 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5613 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5614 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5615 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5616 | 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] | 5617 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5618 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5619 | PyObject *restuple; |
| 5620 | PyObject *resunicode; |
| 5621 | |
| 5622 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5623 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5624 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5625 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
| 5628 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5629 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5630 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5631 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5632 | |
| 5633 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5634 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5635 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5636 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5637 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5638 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5639 | Py_DECREF(restuple); |
| 5640 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5641 | } |
| 5642 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5643 | &resunicode, &i_newpos)) { |
| 5644 | Py_DECREF(restuple); |
| 5645 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5646 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5647 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5648 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5649 | else |
| 5650 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5651 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5652 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5653 | Py_DECREF(restuple); |
| 5654 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5655 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5656 | Py_INCREF(resunicode); |
| 5657 | Py_DECREF(restuple); |
| 5658 | return resunicode; |
| 5659 | } |
| 5660 | |
| 5661 | /* Lookup the character ch in the mapping and put the result in result, |
| 5662 | which must be decrefed by the caller. |
| 5663 | Return 0 on success, -1 on error */ |
| 5664 | static |
| 5665 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5666 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5667 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5668 | PyObject *x; |
| 5669 | |
| 5670 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5671 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5672 | x = PyObject_GetItem(mapping, w); |
| 5673 | Py_DECREF(w); |
| 5674 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5675 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5676 | /* No mapping found means: use 1:1 mapping. */ |
| 5677 | PyErr_Clear(); |
| 5678 | *result = NULL; |
| 5679 | return 0; |
| 5680 | } else |
| 5681 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5682 | } |
| 5683 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5684 | *result = x; |
| 5685 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5686 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5687 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5688 | long value = PyLong_AS_LONG(x); |
| 5689 | long max = PyUnicode_GetMax(); |
| 5690 | if (value < 0 || value > max) { |
| 5691 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5692 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5693 | Py_DECREF(x); |
| 5694 | return -1; |
| 5695 | } |
| 5696 | *result = x; |
| 5697 | return 0; |
| 5698 | } |
| 5699 | else if (PyUnicode_Check(x)) { |
| 5700 | *result = x; |
| 5701 | return 0; |
| 5702 | } |
| 5703 | else { |
| 5704 | /* wrong return value */ |
| 5705 | PyErr_SetString(PyExc_TypeError, |
| 5706 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5707 | Py_DECREF(x); |
| 5708 | return -1; |
| 5709 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5710 | } |
| 5711 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5712 | if not reallocate and adjust various state variables. |
| 5713 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5714 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5715 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5716 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5717 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5718 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5719 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5720 | /* remember old output position */ |
| 5721 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5722 | /* exponentially overallocate to minimize reallocations */ |
| 5723 | if (requiredsize < 2 * oldsize) |
| 5724 | requiredsize = 2 * oldsize; |
| 5725 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5726 | return -1; |
| 5727 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5728 | } |
| 5729 | return 0; |
| 5730 | } |
| 5731 | /* lookup the character, put the result in the output string and adjust |
| 5732 | various state variables. Return a new reference to the object that |
| 5733 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5734 | undefined (in which case no character was written). |
| 5735 | The called must decref result. |
| 5736 | Return 0 on success, -1 on error. */ |
| 5737 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5738 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5739 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5740 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5741 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5742 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5743 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5744 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5745 | /* not found => default to 1:1 mapping */ |
| 5746 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5747 | } |
| 5748 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5749 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5750 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5751 | /* no overflow check, because we know that the space is enough */ |
| 5752 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5753 | } |
| 5754 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5756 | if (repsize==1) { |
| 5757 | /* no overflow check, because we know that the space is enough */ |
| 5758 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5759 | } |
| 5760 | else if (repsize!=0) { |
| 5761 | /* more than one character */ |
| 5762 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5763 | (insize - (curinp-startinp)) + |
| 5764 | repsize - 1; |
| 5765 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5766 | return -1; |
| 5767 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5768 | *outp += repsize; |
| 5769 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5770 | } |
| 5771 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5772 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5773 | return 0; |
| 5774 | } |
| 5775 | |
| 5776 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5777 | Py_ssize_t size, |
| 5778 | PyObject *mapping, |
| 5779 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5781 | /* output object */ |
| 5782 | PyObject *res = NULL; |
| 5783 | /* pointers to the beginning and end+1 of input */ |
| 5784 | const Py_UNICODE *startp = p; |
| 5785 | const Py_UNICODE *endp = p + size; |
| 5786 | /* pointer into the output */ |
| 5787 | Py_UNICODE *str; |
| 5788 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5789 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5790 | char *reason = "character maps to <undefined>"; |
| 5791 | PyObject *errorHandler = NULL; |
| 5792 | PyObject *exc = NULL; |
| 5793 | /* the following variable is used for caching string comparisons |
| 5794 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5795 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5796 | int known_errorHandler = -1; |
| 5797 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5798 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5799 | PyErr_BadArgument(); |
| 5800 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5801 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5802 | |
| 5803 | /* allocate enough for a simple 1:1 translation without |
| 5804 | replacements, if we need more, we'll resize */ |
| 5805 | res = PyUnicode_FromUnicode(NULL, size); |
| 5806 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5807 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5808 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5809 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5810 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5812 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5813 | /* try to encode it */ |
| 5814 | PyObject *x = NULL; |
| 5815 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5816 | Py_XDECREF(x); |
| 5817 | goto onError; |
| 5818 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5819 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5820 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5821 | ++p; |
| 5822 | else { /* untranslatable character */ |
| 5823 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5824 | Py_ssize_t repsize; |
| 5825 | Py_ssize_t newpos; |
| 5826 | Py_UNICODE *uni2; |
| 5827 | /* startpos for collecting untranslatable chars */ |
| 5828 | const Py_UNICODE *collstart = p; |
| 5829 | const Py_UNICODE *collend = p+1; |
| 5830 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | /* find all untranslatable characters */ |
| 5833 | while (collend < endp) { |
| 5834 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5835 | goto onError; |
| 5836 | Py_XDECREF(x); |
| 5837 | if (x!=Py_None) |
| 5838 | break; |
| 5839 | ++collend; |
| 5840 | } |
| 5841 | /* cache callback name lookup |
| 5842 | * (if not done yet, i.e. it's the first error) */ |
| 5843 | if (known_errorHandler==-1) { |
| 5844 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5845 | known_errorHandler = 1; |
| 5846 | else if (!strcmp(errors, "replace")) |
| 5847 | known_errorHandler = 2; |
| 5848 | else if (!strcmp(errors, "ignore")) |
| 5849 | known_errorHandler = 3; |
| 5850 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5851 | known_errorHandler = 4; |
| 5852 | else |
| 5853 | known_errorHandler = 0; |
| 5854 | } |
| 5855 | switch (known_errorHandler) { |
| 5856 | case 1: /* strict */ |
| 5857 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5858 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5859 | case 2: /* replace */ |
| 5860 | /* No need to check for space, this is a 1:1 replacement */ |
| 5861 | for (coll = collstart; coll<collend; ++coll) |
| 5862 | *str++ = '?'; |
| 5863 | /* fall through */ |
| 5864 | case 3: /* ignore */ |
| 5865 | p = collend; |
| 5866 | break; |
| 5867 | case 4: /* xmlcharrefreplace */ |
| 5868 | /* generate replacement (temporarily (mis)uses p) */ |
| 5869 | for (p = collstart; p < collend; ++p) { |
| 5870 | char buffer[2+29+1+1]; |
| 5871 | char *cp; |
| 5872 | sprintf(buffer, "&#%d;", (int)*p); |
| 5873 | if (charmaptranslate_makespace(&res, &str, |
| 5874 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5875 | goto onError; |
| 5876 | for (cp = buffer; *cp; ++cp) |
| 5877 | *str++ = *cp; |
| 5878 | } |
| 5879 | p = collend; |
| 5880 | break; |
| 5881 | default: |
| 5882 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5883 | reason, startp, size, &exc, |
| 5884 | collstart-startp, collend-startp, &newpos); |
| 5885 | if (repunicode == NULL) |
| 5886 | goto onError; |
| 5887 | /* generate replacement */ |
| 5888 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5889 | if (charmaptranslate_makespace(&res, &str, |
| 5890 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5891 | Py_DECREF(repunicode); |
| 5892 | goto onError; |
| 5893 | } |
| 5894 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5895 | *str++ = *uni2; |
| 5896 | p = startp + newpos; |
| 5897 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5898 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5899 | } |
| 5900 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5901 | /* Resize if we allocated to much */ |
| 5902 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5903 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5904 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5905 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5906 | } |
| 5907 | Py_XDECREF(exc); |
| 5908 | Py_XDECREF(errorHandler); |
| 5909 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5911 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5912 | Py_XDECREF(res); |
| 5913 | Py_XDECREF(exc); |
| 5914 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5915 | return NULL; |
| 5916 | } |
| 5917 | |
| 5918 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5919 | PyObject *mapping, |
| 5920 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 | { |
| 5922 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5923 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | str = PyUnicode_FromObject(str); |
| 5925 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5926 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5927 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5928 | PyUnicode_GET_SIZE(str), |
| 5929 | mapping, |
| 5930 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5931 | Py_DECREF(str); |
| 5932 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5933 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5934 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | Py_XDECREF(str); |
| 5936 | return NULL; |
| 5937 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5938 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5939 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5940 | |
| 5941 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5942 | Py_ssize_t length, |
| 5943 | char *output, |
| 5944 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5945 | { |
| 5946 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5947 | PyObject *errorHandler = NULL; |
| 5948 | PyObject *exc = NULL; |
| 5949 | const char *encoding = "decimal"; |
| 5950 | const char *reason = "invalid decimal Unicode string"; |
| 5951 | /* the following variable is used for caching string comparisons |
| 5952 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5953 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5954 | |
| 5955 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5956 | PyErr_BadArgument(); |
| 5957 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5958 | } |
| 5959 | |
| 5960 | p = s; |
| 5961 | end = s + length; |
| 5962 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5963 | register Py_UNICODE ch = *p; |
| 5964 | int decimal; |
| 5965 | PyObject *repunicode; |
| 5966 | Py_ssize_t repsize; |
| 5967 | Py_ssize_t newpos; |
| 5968 | Py_UNICODE *uni2; |
| 5969 | Py_UNICODE *collstart; |
| 5970 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5971 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5972 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5973 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5974 | ++p; |
| 5975 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5976 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5977 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5978 | if (decimal >= 0) { |
| 5979 | *output++ = '0' + decimal; |
| 5980 | ++p; |
| 5981 | continue; |
| 5982 | } |
| 5983 | if (0 < ch && ch < 256) { |
| 5984 | *output++ = (char)ch; |
| 5985 | ++p; |
| 5986 | continue; |
| 5987 | } |
| 5988 | /* All other characters are considered unencodable */ |
| 5989 | collstart = p; |
| 5990 | collend = p+1; |
| 5991 | while (collend < end) { |
| 5992 | if ((0 < *collend && *collend < 256) || |
| 5993 | !Py_UNICODE_ISSPACE(*collend) || |
| 5994 | Py_UNICODE_TODECIMAL(*collend)) |
| 5995 | break; |
| 5996 | } |
| 5997 | /* cache callback name lookup |
| 5998 | * (if not done yet, i.e. it's the first error) */ |
| 5999 | if (known_errorHandler==-1) { |
| 6000 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6001 | known_errorHandler = 1; |
| 6002 | else if (!strcmp(errors, "replace")) |
| 6003 | known_errorHandler = 2; |
| 6004 | else if (!strcmp(errors, "ignore")) |
| 6005 | known_errorHandler = 3; |
| 6006 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6007 | known_errorHandler = 4; |
| 6008 | else |
| 6009 | known_errorHandler = 0; |
| 6010 | } |
| 6011 | switch (known_errorHandler) { |
| 6012 | case 1: /* strict */ |
| 6013 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 6014 | goto onError; |
| 6015 | case 2: /* replace */ |
| 6016 | for (p = collstart; p < collend; ++p) |
| 6017 | *output++ = '?'; |
| 6018 | /* fall through */ |
| 6019 | case 3: /* ignore */ |
| 6020 | p = collend; |
| 6021 | break; |
| 6022 | case 4: /* xmlcharrefreplace */ |
| 6023 | /* generate replacement (temporarily (mis)uses p) */ |
| 6024 | for (p = collstart; p < collend; ++p) |
| 6025 | output += sprintf(output, "&#%d;", (int)*p); |
| 6026 | p = collend; |
| 6027 | break; |
| 6028 | default: |
| 6029 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6030 | encoding, reason, s, length, &exc, |
| 6031 | collstart-s, collend-s, &newpos); |
| 6032 | if (repunicode == NULL) |
| 6033 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6034 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6035 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6036 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 6037 | Py_DECREF(repunicode); |
| 6038 | goto onError; |
| 6039 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6040 | /* generate replacement */ |
| 6041 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6042 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 6043 | Py_UNICODE ch = *uni2; |
| 6044 | if (Py_UNICODE_ISSPACE(ch)) |
| 6045 | *output++ = ' '; |
| 6046 | else { |
| 6047 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 6048 | if (decimal >= 0) |
| 6049 | *output++ = '0' + decimal; |
| 6050 | else if (0 < ch && ch < 256) |
| 6051 | *output++ = (char)ch; |
| 6052 | else { |
| 6053 | Py_DECREF(repunicode); |
| 6054 | raise_encode_exception(&exc, encoding, |
| 6055 | s, length, collstart-s, collend-s, reason); |
| 6056 | goto onError; |
| 6057 | } |
| 6058 | } |
| 6059 | } |
| 6060 | p = s + newpos; |
| 6061 | Py_DECREF(repunicode); |
| 6062 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6063 | } |
| 6064 | /* 0-terminate the output string */ |
| 6065 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6066 | Py_XDECREF(exc); |
| 6067 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6068 | return 0; |
| 6069 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6070 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6071 | Py_XDECREF(exc); |
| 6072 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 6073 | return -1; |
| 6074 | } |
| 6075 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | /* --- Helpers ------------------------------------------------------------ */ |
| 6077 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 6078 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6079 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6080 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6081 | #include "stringlib/count.h" |
| 6082 | #include "stringlib/find.h" |
| 6083 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6084 | #include "stringlib/split.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6085 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6086 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 6087 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 6088 | #include "stringlib/localeutil.h" |
| 6089 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6090 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6091 | #define ADJUST_INDICES(start, end, len) \ |
| 6092 | if (end > len) \ |
| 6093 | end = len; \ |
| 6094 | else if (end < 0) { \ |
| 6095 | end += len; \ |
| 6096 | if (end < 0) \ |
| 6097 | end = 0; \ |
| 6098 | } \ |
| 6099 | if (start < 0) { \ |
| 6100 | start += len; \ |
| 6101 | if (start < 0) \ |
| 6102 | start = 0; \ |
| 6103 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6104 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6105 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6106 | PyObject *substr, |
| 6107 | Py_ssize_t start, |
| 6108 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6110 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6111 | PyUnicodeObject* str_obj; |
| 6112 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6113 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6114 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 6115 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6116 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6117 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 6118 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6119 | Py_DECREF(str_obj); |
| 6120 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6122 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6123 | ADJUST_INDICES(start, end, str_obj->length); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6124 | result = stringlib_count( |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6125 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
| 6126 | PY_SSIZE_T_MAX |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6127 | ); |
| 6128 | |
| 6129 | Py_DECREF(sub_obj); |
| 6130 | Py_DECREF(str_obj); |
| 6131 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6132 | return result; |
| 6133 | } |
| 6134 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6135 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6136 | PyObject *sub, |
| 6137 | Py_ssize_t start, |
| 6138 | Py_ssize_t end, |
| 6139 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6140 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6141 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6142 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6144 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6145 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6146 | sub = PyUnicode_FromObject(sub); |
| 6147 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6148 | Py_DECREF(str); |
| 6149 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6150 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6151 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6152 | if (direction > 0) |
| 6153 | result = stringlib_find_slice( |
| 6154 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6155 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6156 | start, end |
| 6157 | ); |
| 6158 | else |
| 6159 | result = stringlib_rfind_slice( |
| 6160 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 6161 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 6162 | start, end |
| 6163 | ); |
| 6164 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6166 | Py_DECREF(sub); |
| 6167 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | return result; |
| 6169 | } |
| 6170 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6171 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6172 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6173 | PyUnicodeObject *substring, |
| 6174 | Py_ssize_t start, |
| 6175 | Py_ssize_t end, |
| 6176 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6177 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6178 | if (substring->length == 0) |
| 6179 | return 1; |
| 6180 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6181 | ADJUST_INDICES(start, end, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6182 | end -= substring->length; |
| 6183 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6184 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6185 | |
| 6186 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6187 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 6188 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6189 | } else { |
| 6190 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6191 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6192 | } |
| 6193 | |
| 6194 | return 0; |
| 6195 | } |
| 6196 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6197 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6198 | PyObject *substr, |
| 6199 | Py_ssize_t start, |
| 6200 | Py_ssize_t end, |
| 6201 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6202 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6203 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6205 | str = PyUnicode_FromObject(str); |
| 6206 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6207 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6208 | substr = PyUnicode_FromObject(substr); |
| 6209 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6210 | Py_DECREF(str); |
| 6211 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6212 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6214 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6215 | (PyUnicodeObject *)substr, |
| 6216 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6217 | Py_DECREF(str); |
| 6218 | Py_DECREF(substr); |
| 6219 | return result; |
| 6220 | } |
| 6221 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6222 | /* Apply fixfct filter to the Unicode object self and return a |
| 6223 | reference to the modified object */ |
| 6224 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6225 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6227 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6228 | { |
| 6229 | |
| 6230 | PyUnicodeObject *u; |
| 6231 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6232 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6233 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6234 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6235 | |
| 6236 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6237 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6238 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6239 | /* fixfct should return TRUE if it modified the buffer. If |
| 6240 | FALSE, return a reference to the original buffer instead |
| 6241 | (to save space, not time) */ |
| 6242 | Py_INCREF(self); |
| 6243 | Py_DECREF(u); |
| 6244 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | } |
| 6246 | return (PyObject*) u; |
| 6247 | } |
| 6248 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6249 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6250 | int fixupper(PyUnicodeObject *self) |
| 6251 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6252 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6253 | Py_UNICODE *s = self->str; |
| 6254 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6255 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6256 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6257 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6258 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6259 | ch = Py_UNICODE_TOUPPER(*s); |
| 6260 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6261 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6262 | *s = ch; |
| 6263 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6264 | s++; |
| 6265 | } |
| 6266 | |
| 6267 | return status; |
| 6268 | } |
| 6269 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6270 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | int fixlower(PyUnicodeObject *self) |
| 6272 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6273 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6274 | Py_UNICODE *s = self->str; |
| 6275 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6277 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6278 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6279 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6280 | ch = Py_UNICODE_TOLOWER(*s); |
| 6281 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6282 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6283 | *s = ch; |
| 6284 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | s++; |
| 6286 | } |
| 6287 | |
| 6288 | return status; |
| 6289 | } |
| 6290 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6291 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6292 | int fixswapcase(PyUnicodeObject *self) |
| 6293 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6294 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6295 | Py_UNICODE *s = self->str; |
| 6296 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6297 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6298 | while (len-- > 0) { |
| 6299 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6300 | *s = Py_UNICODE_TOLOWER(*s); |
| 6301 | status = 1; |
| 6302 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6303 | *s = Py_UNICODE_TOUPPER(*s); |
| 6304 | status = 1; |
| 6305 | } |
| 6306 | s++; |
| 6307 | } |
| 6308 | |
| 6309 | return status; |
| 6310 | } |
| 6311 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6312 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6313 | int fixcapitalize(PyUnicodeObject *self) |
| 6314 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6315 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6316 | Py_UNICODE *s = self->str; |
| 6317 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6318 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6319 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6320 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6321 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6322 | *s = Py_UNICODE_TOUPPER(*s); |
| 6323 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6324 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6325 | s++; |
| 6326 | while (--len > 0) { |
| 6327 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6328 | *s = Py_UNICODE_TOLOWER(*s); |
| 6329 | status = 1; |
| 6330 | } |
| 6331 | s++; |
| 6332 | } |
| 6333 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6334 | } |
| 6335 | |
| 6336 | static |
| 6337 | int fixtitle(PyUnicodeObject *self) |
| 6338 | { |
| 6339 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6340 | register Py_UNICODE *e; |
| 6341 | int previous_is_cased; |
| 6342 | |
| 6343 | /* Shortcut for single character strings */ |
| 6344 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6345 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6346 | if (*p != ch) { |
| 6347 | *p = ch; |
| 6348 | return 1; |
| 6349 | } |
| 6350 | else |
| 6351 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6352 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | e = p + PyUnicode_GET_SIZE(self); |
| 6355 | previous_is_cased = 0; |
| 6356 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6357 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6358 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6359 | if (previous_is_cased) |
| 6360 | *p = Py_UNICODE_TOLOWER(ch); |
| 6361 | else |
| 6362 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6363 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6364 | if (Py_UNICODE_ISLOWER(ch) || |
| 6365 | Py_UNICODE_ISUPPER(ch) || |
| 6366 | Py_UNICODE_ISTITLE(ch)) |
| 6367 | previous_is_cased = 1; |
| 6368 | else |
| 6369 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6370 | } |
| 6371 | return 1; |
| 6372 | } |
| 6373 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6374 | PyObject * |
| 6375 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6376 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6377 | const Py_UNICODE blank = ' '; |
| 6378 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6379 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6380 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6381 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6382 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6383 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6384 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6385 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6386 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6387 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6388 | fseq = PySequence_Fast(seq, ""); |
| 6389 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6390 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6391 | } |
| 6392 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6393 | /* NOTE: the following code can't call back into Python code, |
| 6394 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6395 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6396 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6397 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6398 | /* If empty sequence, return u"". */ |
| 6399 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6400 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6401 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6402 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6403 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6404 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6405 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6406 | item = items[0]; |
| 6407 | if (PyUnicode_CheckExact(item)) { |
| 6408 | Py_INCREF(item); |
| 6409 | res = (PyUnicodeObject *)item; |
| 6410 | goto Done; |
| 6411 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6412 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6413 | else { |
| 6414 | /* Set up sep and seplen */ |
| 6415 | if (separator == NULL) { |
| 6416 | sep = ␣ |
| 6417 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6418 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6419 | else { |
| 6420 | if (!PyUnicode_Check(separator)) { |
| 6421 | PyErr_Format(PyExc_TypeError, |
| 6422 | "separator: expected str instance," |
| 6423 | " %.80s found", |
| 6424 | Py_TYPE(separator)->tp_name); |
| 6425 | goto onError; |
| 6426 | } |
| 6427 | sep = PyUnicode_AS_UNICODE(separator); |
| 6428 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6429 | } |
| 6430 | } |
| 6431 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6432 | /* There are at least two things to join, or else we have a subclass |
| 6433 | * of str in the sequence. |
| 6434 | * Do a pre-pass to figure out the total amount of space we'll |
| 6435 | * need (sz), and see whether all argument are strings. |
| 6436 | */ |
| 6437 | sz = 0; |
| 6438 | for (i = 0; i < seqlen; i++) { |
| 6439 | const Py_ssize_t old_sz = sz; |
| 6440 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6441 | if (!PyUnicode_Check(item)) { |
| 6442 | PyErr_Format(PyExc_TypeError, |
| 6443 | "sequence item %zd: expected str instance," |
| 6444 | " %.80s found", |
| 6445 | i, Py_TYPE(item)->tp_name); |
| 6446 | goto onError; |
| 6447 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6448 | sz += PyUnicode_GET_SIZE(item); |
| 6449 | if (i != 0) |
| 6450 | sz += seplen; |
| 6451 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6452 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6453 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6454 | goto onError; |
| 6455 | } |
| 6456 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6457 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6458 | res = _PyUnicode_New(sz); |
| 6459 | if (res == NULL) |
| 6460 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6461 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6462 | /* Catenate everything. */ |
| 6463 | res_p = PyUnicode_AS_UNICODE(res); |
| 6464 | for (i = 0; i < seqlen; ++i) { |
| 6465 | Py_ssize_t itemlen; |
| 6466 | item = items[i]; |
| 6467 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6468 | /* Copy item, and maybe the separator. */ |
| 6469 | if (i) { |
| 6470 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6471 | res_p += seplen; |
| 6472 | } |
| 6473 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6474 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6475 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6476 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6477 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6478 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | return (PyObject *)res; |
| 6480 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6482 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6483 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | return NULL; |
| 6485 | } |
| 6486 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6487 | static |
| 6488 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6489 | Py_ssize_t left, |
| 6490 | Py_ssize_t right, |
| 6491 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | { |
| 6493 | PyUnicodeObject *u; |
| 6494 | |
| 6495 | if (left < 0) |
| 6496 | left = 0; |
| 6497 | if (right < 0) |
| 6498 | right = 0; |
| 6499 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6500 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6501 | Py_INCREF(self); |
| 6502 | return self; |
| 6503 | } |
| 6504 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6505 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6506 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6507 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6508 | return NULL; |
| 6509 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6510 | u = _PyUnicode_New(left + self->length + right); |
| 6511 | if (u) { |
| 6512 | if (left) |
| 6513 | Py_UNICODE_FILL(u->str, fill, left); |
| 6514 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6515 | if (right) |
| 6516 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6517 | } |
| 6518 | |
| 6519 | return u; |
| 6520 | } |
| 6521 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6522 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6523 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6524 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6525 | |
| 6526 | string = PyUnicode_FromObject(string); |
| 6527 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6528 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6529 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6530 | list = stringlib_splitlines( |
| 6531 | (PyObject*) string, PyUnicode_AS_UNICODE(string), |
| 6532 | PyUnicode_GET_SIZE(string), keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6533 | |
| 6534 | Py_DECREF(string); |
| 6535 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6536 | } |
| 6537 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6538 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6540 | PyUnicodeObject *substring, |
| 6541 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6542 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6544 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6545 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6546 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6547 | return stringlib_split_whitespace( |
| 6548 | (PyObject*) self, self->str, self->length, maxcount |
| 6549 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6550 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6551 | return stringlib_split( |
| 6552 | (PyObject*) self, self->str, self->length, |
| 6553 | substring->str, substring->length, |
| 6554 | maxcount |
| 6555 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6558 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6559 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6560 | PyUnicodeObject *substring, |
| 6561 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6562 | { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6563 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6564 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6565 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6566 | if (substring == NULL) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6567 | return stringlib_rsplit_whitespace( |
| 6568 | (PyObject*) self, self->str, self->length, maxcount |
| 6569 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6570 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6571 | return stringlib_rsplit( |
| 6572 | (PyObject*) self, self->str, self->length, |
| 6573 | substring->str, substring->length, |
| 6574 | maxcount |
| 6575 | ); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6576 | } |
| 6577 | |
| 6578 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6580 | PyUnicodeObject *str1, |
| 6581 | PyUnicodeObject *str2, |
| 6582 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | { |
| 6584 | PyUnicodeObject *u; |
| 6585 | |
| 6586 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6587 | maxcount = PY_SSIZE_T_MAX; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6588 | else if (maxcount == 0 || self->length == 0) |
| 6589 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6590 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6591 | if (str1->length == str2->length) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 6592 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6593 | /* same length */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6594 | if (str1->length == 0) |
| 6595 | goto nothing; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6596 | if (str1->length == 1) { |
| 6597 | /* replace characters */ |
| 6598 | Py_UNICODE u1, u2; |
| 6599 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6600 | goto nothing; |
| 6601 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6602 | if (!u) |
| 6603 | return NULL; |
| 6604 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6605 | u1 = str1->str[0]; |
| 6606 | u2 = str2->str[0]; |
| 6607 | for (i = 0; i < u->length; i++) |
| 6608 | if (u->str[i] == u1) { |
| 6609 | if (--maxcount < 0) |
| 6610 | break; |
| 6611 | u->str[i] = u2; |
| 6612 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | } else { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6614 | i = stringlib_find( |
| 6615 | self->str, self->length, str1->str, str1->length, 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6616 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6617 | if (i < 0) |
| 6618 | goto nothing; |
| 6619 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6620 | if (!u) |
| 6621 | return NULL; |
| 6622 | Py_UNICODE_COPY(u->str, self->str, self->length); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6623 | |
| 6624 | /* change everything in-place, starting with this one */ |
| 6625 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6626 | i += str1->length; |
| 6627 | |
| 6628 | while ( --maxcount > 0) { |
| 6629 | i = stringlib_find(self->str+i, self->length-i, |
| 6630 | str1->str, str1->length, |
| 6631 | i); |
| 6632 | if (i == -1) |
| 6633 | break; |
| 6634 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6635 | i += str1->length; |
| 6636 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6637 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6638 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6639 | |
| 6640 | Py_ssize_t n, i, j, e; |
| 6641 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6642 | Py_UNICODE *p; |
| 6643 | |
| 6644 | /* replace strings */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6645 | n = stringlib_count(self->str, self->length, str1->str, str1->length, |
| 6646 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6647 | if (n == 0) |
| 6648 | goto nothing; |
| 6649 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6650 | delta = (str2->length - str1->length); |
| 6651 | if (delta == 0) { |
| 6652 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6654 | product = n * (str2->length - str1->length); |
| 6655 | if ((product / (str2->length - str1->length)) != n) { |
| 6656 | PyErr_SetString(PyExc_OverflowError, |
| 6657 | "replace string is too long"); |
| 6658 | return NULL; |
| 6659 | } |
| 6660 | new_size = self->length + product; |
| 6661 | if (new_size < 0) { |
| 6662 | PyErr_SetString(PyExc_OverflowError, |
| 6663 | "replace string is too long"); |
| 6664 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6665 | } |
| 6666 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6667 | u = _PyUnicode_New(new_size); |
| 6668 | if (!u) |
| 6669 | return NULL; |
| 6670 | i = 0; |
| 6671 | p = u->str; |
| 6672 | e = self->length - str1->length; |
| 6673 | if (str1->length > 0) { |
| 6674 | while (n-- > 0) { |
| 6675 | /* look for next match */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 6676 | j = stringlib_find(self->str+i, self->length-i, |
| 6677 | str1->str, str1->length, |
| 6678 | i); |
| 6679 | if (j == -1) |
| 6680 | break; |
| 6681 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6682 | /* copy unchanged part [i:j] */ |
| 6683 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6684 | p += j - i; |
| 6685 | } |
| 6686 | /* copy substitution string */ |
| 6687 | if (str2->length > 0) { |
| 6688 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6689 | p += str2->length; |
| 6690 | } |
| 6691 | i = j + str1->length; |
| 6692 | } |
| 6693 | if (i < self->length) |
| 6694 | /* copy tail [i:] */ |
| 6695 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6696 | } else { |
| 6697 | /* interleave */ |
| 6698 | while (n > 0) { |
| 6699 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6700 | p += str2->length; |
| 6701 | if (--n <= 0) |
| 6702 | break; |
| 6703 | *p++ = self->str[i++]; |
| 6704 | } |
| 6705 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6706 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6708 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6709 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6710 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6711 | /* nothing to replace; return original string (when possible) */ |
| 6712 | if (PyUnicode_CheckExact(self)) { |
| 6713 | Py_INCREF(self); |
| 6714 | return (PyObject *) self; |
| 6715 | } |
| 6716 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | } |
| 6718 | |
| 6719 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6720 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6721 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6722 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | \n\ |
| 6724 | 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] | 6725 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6726 | |
| 6727 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6728 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6730 | return fixup(self, fixtitle); |
| 6731 | } |
| 6732 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6733 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6734 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | \n\ |
| 6736 | 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] | 6737 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | |
| 6739 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6740 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6741 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6742 | return fixup(self, fixcapitalize); |
| 6743 | } |
| 6744 | |
| 6745 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6746 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6747 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6748 | \n\ |
| 6749 | 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] | 6750 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | |
| 6752 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6753 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | { |
| 6755 | PyObject *list; |
| 6756 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6757 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 | /* Split into words */ |
| 6760 | list = split(self, NULL, -1); |
| 6761 | if (!list) |
| 6762 | return NULL; |
| 6763 | |
| 6764 | /* Capitalize each word */ |
| 6765 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6766 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6768 | if (item == NULL) |
| 6769 | goto onError; |
| 6770 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6771 | PyList_SET_ITEM(list, i, item); |
| 6772 | } |
| 6773 | |
| 6774 | /* Join the words to form a new string */ |
| 6775 | item = PyUnicode_Join(NULL, list); |
| 6776 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6777 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6778 | Py_DECREF(list); |
| 6779 | return (PyObject *)item; |
| 6780 | } |
| 6781 | #endif |
| 6782 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6783 | /* Argument converter. Coerces to a single unicode character */ |
| 6784 | |
| 6785 | static int |
| 6786 | convert_uc(PyObject *obj, void *addr) |
| 6787 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6788 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6789 | PyObject *uniobj; |
| 6790 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6791 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6792 | uniobj = PyUnicode_FromObject(obj); |
| 6793 | if (uniobj == NULL) { |
| 6794 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6795 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6796 | return 0; |
| 6797 | } |
| 6798 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6799 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6800 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6801 | Py_DECREF(uniobj); |
| 6802 | return 0; |
| 6803 | } |
| 6804 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6805 | *fillcharloc = unistr[0]; |
| 6806 | Py_DECREF(uniobj); |
| 6807 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6810 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6811 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6813 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6814 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6815 | |
| 6816 | static PyObject * |
| 6817 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6818 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6819 | Py_ssize_t marg, left; |
| 6820 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6821 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6822 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6823 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6824 | return NULL; |
| 6825 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6826 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6827 | Py_INCREF(self); |
| 6828 | return (PyObject*) self; |
| 6829 | } |
| 6830 | |
| 6831 | marg = width - self->length; |
| 6832 | left = marg / 2 + (marg & width & 1); |
| 6833 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6834 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6837 | #if 0 |
| 6838 | |
| 6839 | /* This code should go into some future Unicode collation support |
| 6840 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6841 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6842 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6843 | /* speedy UTF-16 code point order comparison */ |
| 6844 | /* gleaned from: */ |
| 6845 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6846 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6847 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6848 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6849 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6850 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6851 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6852 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6853 | }; |
| 6854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | static int |
| 6856 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6857 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6858 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6860 | Py_UNICODE *s1 = str1->str; |
| 6861 | Py_UNICODE *s2 = str2->str; |
| 6862 | |
| 6863 | len1 = str1->length; |
| 6864 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6865 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6866 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6867 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6868 | |
| 6869 | c1 = *s1++; |
| 6870 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6871 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6872 | if (c1 > (1<<11) * 26) |
| 6873 | c1 += utf16Fixup[c1>>11]; |
| 6874 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6875 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6876 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6877 | |
| 6878 | if (c1 != c2) |
| 6879 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6880 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6881 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6885 | } |
| 6886 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6887 | #else |
| 6888 | |
| 6889 | static int |
| 6890 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6891 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6892 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6893 | |
| 6894 | Py_UNICODE *s1 = str1->str; |
| 6895 | Py_UNICODE *s2 = str2->str; |
| 6896 | |
| 6897 | len1 = str1->length; |
| 6898 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6899 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6900 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6901 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6902 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6903 | c1 = *s1++; |
| 6904 | c2 = *s2++; |
| 6905 | |
| 6906 | if (c1 != c2) |
| 6907 | return (c1 < c2) ? -1 : 1; |
| 6908 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6909 | len1--; len2--; |
| 6910 | } |
| 6911 | |
| 6912 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6913 | } |
| 6914 | |
| 6915 | #endif |
| 6916 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6917 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6918 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6919 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6920 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6921 | return unicode_compare((PyUnicodeObject *)left, |
| 6922 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6923 | PyErr_Format(PyExc_TypeError, |
| 6924 | "Can't compare %.100s and %.100s", |
| 6925 | left->ob_type->tp_name, |
| 6926 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6927 | return -1; |
| 6928 | } |
| 6929 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6930 | int |
| 6931 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6932 | { |
| 6933 | int i; |
| 6934 | Py_UNICODE *id; |
| 6935 | assert(PyUnicode_Check(uni)); |
| 6936 | id = PyUnicode_AS_UNICODE(uni); |
| 6937 | /* Compare Unicode string and source character set string */ |
| 6938 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6939 | if (id[i] != str[i]) |
| 6940 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 6941 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 6942 | to C strings identical up to that point. */ |
Benjamin Peterson | a23831f | 2010-04-25 21:54:00 +0000 | [diff] [blame] | 6943 | if (PyUnicode_GET_SIZE(uni) != i || id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6944 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6945 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6946 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6947 | return 0; |
| 6948 | } |
| 6949 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6950 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6951 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6952 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6953 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6954 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6955 | PyObject *right, |
| 6956 | int op) |
| 6957 | { |
| 6958 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6959 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6960 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6961 | PyObject *v; |
| 6962 | if (((PyUnicodeObject *) left)->length != |
| 6963 | ((PyUnicodeObject *) right)->length) { |
| 6964 | if (op == Py_EQ) { |
| 6965 | Py_INCREF(Py_False); |
| 6966 | return Py_False; |
| 6967 | } |
| 6968 | if (op == Py_NE) { |
| 6969 | Py_INCREF(Py_True); |
| 6970 | return Py_True; |
| 6971 | } |
| 6972 | } |
| 6973 | if (left == right) |
| 6974 | result = 0; |
| 6975 | else |
| 6976 | result = unicode_compare((PyUnicodeObject *)left, |
| 6977 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6978 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6979 | /* Convert the return value to a Boolean */ |
| 6980 | switch (op) { |
| 6981 | case Py_EQ: |
| 6982 | v = TEST_COND(result == 0); |
| 6983 | break; |
| 6984 | case Py_NE: |
| 6985 | v = TEST_COND(result != 0); |
| 6986 | break; |
| 6987 | case Py_LE: |
| 6988 | v = TEST_COND(result <= 0); |
| 6989 | break; |
| 6990 | case Py_GE: |
| 6991 | v = TEST_COND(result >= 0); |
| 6992 | break; |
| 6993 | case Py_LT: |
| 6994 | v = TEST_COND(result == -1); |
| 6995 | break; |
| 6996 | case Py_GT: |
| 6997 | v = TEST_COND(result == 1); |
| 6998 | break; |
| 6999 | default: |
| 7000 | PyErr_BadArgument(); |
| 7001 | return NULL; |
| 7002 | } |
| 7003 | Py_INCREF(v); |
| 7004 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7005 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7006 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7007 | Py_INCREF(Py_NotImplemented); |
| 7008 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7009 | } |
| 7010 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7011 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7012 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7013 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7014 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7015 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7016 | |
| 7017 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7018 | sub = PyUnicode_FromObject(element); |
| 7019 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7020 | PyErr_Format(PyExc_TypeError, |
| 7021 | "'in <string>' requires string as left operand, not %s", |
| 7022 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7023 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7024 | } |
| 7025 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7026 | str = PyUnicode_FromObject(container); |
| 7027 | if (!str) { |
| 7028 | Py_DECREF(sub); |
| 7029 | return -1; |
| 7030 | } |
| 7031 | |
| 7032 | result = stringlib_contains_obj(str, sub); |
| 7033 | |
| 7034 | Py_DECREF(str); |
| 7035 | Py_DECREF(sub); |
| 7036 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7037 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7038 | } |
| 7039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7040 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7041 | |
| 7042 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7043 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7044 | { |
| 7045 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7046 | |
| 7047 | /* Coerce the two arguments */ |
| 7048 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7049 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7050 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7052 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7053 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7054 | |
| 7055 | /* Shortcuts */ |
| 7056 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7057 | Py_DECREF(v); |
| 7058 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7059 | } |
| 7060 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7061 | Py_DECREF(u); |
| 7062 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7063 | } |
| 7064 | |
| 7065 | /* Concat the two Unicode strings */ |
| 7066 | w = _PyUnicode_New(u->length + v->length); |
| 7067 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7068 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7069 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7070 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7071 | |
| 7072 | Py_DECREF(u); |
| 7073 | Py_DECREF(v); |
| 7074 | return (PyObject *)w; |
| 7075 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7076 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7077 | Py_XDECREF(u); |
| 7078 | Py_XDECREF(v); |
| 7079 | return NULL; |
| 7080 | } |
| 7081 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7082 | void |
| 7083 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7084 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7085 | PyObject *new; |
| 7086 | if (*pleft == NULL) |
| 7087 | return; |
| 7088 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7089 | Py_DECREF(*pleft); |
| 7090 | *pleft = NULL; |
| 7091 | return; |
| 7092 | } |
| 7093 | new = PyUnicode_Concat(*pleft, right); |
| 7094 | Py_DECREF(*pleft); |
| 7095 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7096 | } |
| 7097 | |
| 7098 | void |
| 7099 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7100 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7101 | PyUnicode_Append(pleft, right); |
| 7102 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7105 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7106 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7107 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7108 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7109 | 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] | 7110 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7111 | |
| 7112 | static PyObject * |
| 7113 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7114 | { |
| 7115 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7116 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7117 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7118 | PyObject *result; |
| 7119 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7120 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7121 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | return NULL; |
| 7123 | |
| 7124 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7125 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7126 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7127 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7128 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7129 | ADJUST_INDICES(start, end, self->length); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7130 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7131 | stringlib_count(self->str + start, end - start, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 7132 | substring->str, substring->length, |
| 7133 | PY_SSIZE_T_MAX) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7134 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7135 | |
| 7136 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7137 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7138 | return result; |
| 7139 | } |
| 7140 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7141 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7142 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7144 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7145 | 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] | 7146 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7147 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7148 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7149 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7150 | |
| 7151 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7152 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7154 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7155 | char *encoding = NULL; |
| 7156 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7157 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7158 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 7159 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 7160 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7161 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7162 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7163 | if (v == NULL) |
| 7164 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7165 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7166 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7167 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7168 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7169 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7170 | Py_DECREF(v); |
| 7171 | return NULL; |
| 7172 | } |
| 7173 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7174 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7175 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7176 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7177 | } |
| 7178 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7179 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7180 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7181 | \n\ |
| 7182 | 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] | 7183 | 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] | 7184 | |
| 7185 | static PyObject* |
| 7186 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7187 | { |
| 7188 | Py_UNICODE *e; |
| 7189 | Py_UNICODE *p; |
| 7190 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7191 | Py_UNICODE *qe; |
| 7192 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7193 | PyUnicodeObject *u; |
| 7194 | int tabsize = 8; |
| 7195 | |
| 7196 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7197 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7198 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7199 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7200 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7201 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7202 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7203 | for (p = self->str; p < e; p++) |
| 7204 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7205 | if (tabsize > 0) { |
| 7206 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7207 | if (j > PY_SSIZE_T_MAX - incr) |
| 7208 | goto overflow1; |
| 7209 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7210 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7211 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7212 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7213 | if (j > PY_SSIZE_T_MAX - 1) |
| 7214 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7215 | j++; |
| 7216 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7217 | if (i > PY_SSIZE_T_MAX - j) |
| 7218 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7219 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7220 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7221 | } |
| 7222 | } |
| 7223 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7224 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7225 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7226 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7227 | /* Second pass: create output string and fill it */ |
| 7228 | u = _PyUnicode_New(i + j); |
| 7229 | if (!u) |
| 7230 | return NULL; |
| 7231 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7232 | j = 0; /* same as in first pass */ |
| 7233 | q = u->str; /* next output char */ |
| 7234 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7235 | |
| 7236 | for (p = self->str; p < e; p++) |
| 7237 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7238 | if (tabsize > 0) { |
| 7239 | i = tabsize - (j % tabsize); |
| 7240 | j += i; |
| 7241 | while (i--) { |
| 7242 | if (q >= qe) |
| 7243 | goto overflow2; |
| 7244 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7245 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7246 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7247 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7248 | else { |
| 7249 | if (q >= qe) |
| 7250 | goto overflow2; |
| 7251 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7252 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7253 | if (*p == '\n' || *p == '\r') |
| 7254 | j = 0; |
| 7255 | } |
| 7256 | |
| 7257 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7258 | |
| 7259 | overflow2: |
| 7260 | Py_DECREF(u); |
| 7261 | overflow1: |
| 7262 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7263 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7264 | } |
| 7265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7266 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7267 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7268 | \n\ |
| 7269 | 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] | 7270 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7271 | arguments start and end are interpreted as in slice notation.\n\ |
| 7272 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7273 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7274 | |
| 7275 | static PyObject * |
| 7276 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7277 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7278 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7279 | Py_ssize_t start; |
| 7280 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7281 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7282 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7283 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7284 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7285 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7286 | result = stringlib_find_slice( |
| 7287 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7288 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7289 | start, end |
| 7290 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7291 | |
| 7292 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7293 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7294 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7295 | } |
| 7296 | |
| 7297 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7298 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7299 | { |
| 7300 | if (index < 0 || index >= self->length) { |
| 7301 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7302 | return NULL; |
| 7303 | } |
| 7304 | |
| 7305 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7306 | } |
| 7307 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7308 | /* Believe it or not, this produces the same value for ASCII strings |
| 7309 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7310 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7311 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7312 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7313 | Py_ssize_t len; |
| 7314 | Py_UNICODE *p; |
| 7315 | long x; |
| 7316 | |
| 7317 | if (self->hash != -1) |
| 7318 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7319 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7320 | p = self->str; |
| 7321 | x = *p << 7; |
| 7322 | while (--len >= 0) |
| 7323 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7324 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7325 | if (x == -1) |
| 7326 | x = -2; |
| 7327 | self->hash = x; |
| 7328 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7329 | } |
| 7330 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7331 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7332 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7333 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7334 | 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] | 7335 | |
| 7336 | static PyObject * |
| 7337 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7338 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7339 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7340 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7341 | Py_ssize_t start; |
| 7342 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7343 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7344 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7345 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7346 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7347 | result = stringlib_find_slice( |
| 7348 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7349 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7350 | start, end |
| 7351 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7352 | |
| 7353 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7354 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7355 | if (result < 0) { |
| 7356 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7357 | return NULL; |
| 7358 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7359 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7360 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7361 | } |
| 7362 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7363 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7364 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7365 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7366 | 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] | 7367 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7368 | |
| 7369 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7370 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7371 | { |
| 7372 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7373 | register const Py_UNICODE *e; |
| 7374 | int cased; |
| 7375 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7376 | /* Shortcut for single character strings */ |
| 7377 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7378 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7379 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7380 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7381 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7382 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7383 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7384 | e = p + PyUnicode_GET_SIZE(self); |
| 7385 | cased = 0; |
| 7386 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7387 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7388 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7389 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7390 | return PyBool_FromLong(0); |
| 7391 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7392 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7393 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7394 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7395 | } |
| 7396 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7397 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7398 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7399 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7400 | 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] | 7401 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7402 | |
| 7403 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7404 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7405 | { |
| 7406 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7407 | register const Py_UNICODE *e; |
| 7408 | int cased; |
| 7409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7410 | /* Shortcut for single character strings */ |
| 7411 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7412 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7413 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7414 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7415 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7416 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7418 | e = p + PyUnicode_GET_SIZE(self); |
| 7419 | cased = 0; |
| 7420 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7421 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7422 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7423 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7424 | return PyBool_FromLong(0); |
| 7425 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7426 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7427 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7428 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7429 | } |
| 7430 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7431 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7432 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7433 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7434 | Return True if S is a titlecased string and there is at least one\n\ |
| 7435 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7436 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7437 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7438 | |
| 7439 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7440 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7441 | { |
| 7442 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7443 | register const Py_UNICODE *e; |
| 7444 | int cased, previous_is_cased; |
| 7445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7446 | /* Shortcut for single character strings */ |
| 7447 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7448 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7449 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7450 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7451 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7452 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7453 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7455 | e = p + PyUnicode_GET_SIZE(self); |
| 7456 | cased = 0; |
| 7457 | previous_is_cased = 0; |
| 7458 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7459 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7460 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7461 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7462 | if (previous_is_cased) |
| 7463 | return PyBool_FromLong(0); |
| 7464 | previous_is_cased = 1; |
| 7465 | cased = 1; |
| 7466 | } |
| 7467 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7468 | if (!previous_is_cased) |
| 7469 | return PyBool_FromLong(0); |
| 7470 | previous_is_cased = 1; |
| 7471 | cased = 1; |
| 7472 | } |
| 7473 | else |
| 7474 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7475 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7476 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7477 | } |
| 7478 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7479 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7480 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7481 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7482 | Return True if all characters in S are whitespace\n\ |
| 7483 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7484 | |
| 7485 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7486 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7487 | { |
| 7488 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7489 | register const Py_UNICODE *e; |
| 7490 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7491 | /* Shortcut for single character strings */ |
| 7492 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7493 | Py_UNICODE_ISSPACE(*p)) |
| 7494 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7495 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7496 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7497 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7498 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7499 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7500 | e = p + PyUnicode_GET_SIZE(self); |
| 7501 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7502 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7503 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7505 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | } |
| 7507 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7508 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7509 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7510 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7511 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7512 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7513 | |
| 7514 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7515 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7516 | { |
| 7517 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7518 | register const Py_UNICODE *e; |
| 7519 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7520 | /* Shortcut for single character strings */ |
| 7521 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7522 | Py_UNICODE_ISALPHA(*p)) |
| 7523 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7524 | |
| 7525 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7526 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7527 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7528 | |
| 7529 | e = p + PyUnicode_GET_SIZE(self); |
| 7530 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7531 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7532 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7533 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7534 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7535 | } |
| 7536 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7537 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7538 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7539 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7540 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7541 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7542 | |
| 7543 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7544 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7545 | { |
| 7546 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7547 | register const Py_UNICODE *e; |
| 7548 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7549 | /* Shortcut for single character strings */ |
| 7550 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7551 | Py_UNICODE_ISALNUM(*p)) |
| 7552 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7553 | |
| 7554 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7555 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7556 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7557 | |
| 7558 | e = p + PyUnicode_GET_SIZE(self); |
| 7559 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7560 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7561 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7562 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7563 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7564 | } |
| 7565 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7566 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7567 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7568 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7569 | 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] | 7570 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7571 | |
| 7572 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7573 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7574 | { |
| 7575 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7576 | register const Py_UNICODE *e; |
| 7577 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7578 | /* Shortcut for single character strings */ |
| 7579 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7580 | Py_UNICODE_ISDECIMAL(*p)) |
| 7581 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7582 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7583 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7584 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7585 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7586 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7587 | e = p + PyUnicode_GET_SIZE(self); |
| 7588 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7589 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7590 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7592 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7593 | } |
| 7594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7595 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7597 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7598 | Return True if all characters in S are digits\n\ |
| 7599 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7600 | |
| 7601 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7602 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7603 | { |
| 7604 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7605 | register const Py_UNICODE *e; |
| 7606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7607 | /* Shortcut for single character strings */ |
| 7608 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7609 | Py_UNICODE_ISDIGIT(*p)) |
| 7610 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7611 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7612 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7613 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7614 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7616 | e = p + PyUnicode_GET_SIZE(self); |
| 7617 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7618 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7619 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7620 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7621 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7622 | } |
| 7623 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7624 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7625 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7626 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7627 | 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] | 7628 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7629 | |
| 7630 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7631 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7632 | { |
| 7633 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7634 | register const Py_UNICODE *e; |
| 7635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7636 | /* Shortcut for single character strings */ |
| 7637 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7638 | Py_UNICODE_ISNUMERIC(*p)) |
| 7639 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7640 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7641 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7642 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7643 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7644 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7645 | e = p + PyUnicode_GET_SIZE(self); |
| 7646 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7647 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7648 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7649 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7650 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7651 | } |
| 7652 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7653 | int |
| 7654 | PyUnicode_IsIdentifier(PyObject *self) |
| 7655 | { |
| 7656 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7657 | register const Py_UNICODE *e; |
| 7658 | |
| 7659 | /* Special case for empty strings */ |
| 7660 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7661 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7662 | |
| 7663 | /* PEP 3131 says that the first character must be in |
| 7664 | XID_Start and subsequent characters in XID_Continue, |
| 7665 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7666 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7667 | letters, digits, underscore). However, given the current |
| 7668 | definition of XID_Start and XID_Continue, it is sufficient |
| 7669 | to check just for these, except that _ must be allowed |
| 7670 | as starting an identifier. */ |
| 7671 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7672 | return 0; |
| 7673 | |
| 7674 | e = p + PyUnicode_GET_SIZE(self); |
| 7675 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7676 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7677 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7678 | } |
| 7679 | return 1; |
| 7680 | } |
| 7681 | |
| 7682 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7683 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7684 | \n\ |
| 7685 | Return True if S is a valid identifier according\n\ |
| 7686 | to the language definition."); |
| 7687 | |
| 7688 | static PyObject* |
| 7689 | unicode_isidentifier(PyObject *self) |
| 7690 | { |
| 7691 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7692 | } |
| 7693 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7694 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7695 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7696 | \n\ |
| 7697 | Return True if all characters in S are considered\n\ |
| 7698 | printable in repr() or S is empty, False otherwise."); |
| 7699 | |
| 7700 | static PyObject* |
| 7701 | unicode_isprintable(PyObject *self) |
| 7702 | { |
| 7703 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7704 | register const Py_UNICODE *e; |
| 7705 | |
| 7706 | /* Shortcut for single character strings */ |
| 7707 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7708 | Py_RETURN_TRUE; |
| 7709 | } |
| 7710 | |
| 7711 | e = p + PyUnicode_GET_SIZE(self); |
| 7712 | for (; p < e; p++) { |
| 7713 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7714 | Py_RETURN_FALSE; |
| 7715 | } |
| 7716 | } |
| 7717 | Py_RETURN_TRUE; |
| 7718 | } |
| 7719 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7720 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 7721 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 | \n\ |
| 7723 | 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] | 7724 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7725 | |
| 7726 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7727 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7729 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7730 | } |
| 7731 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7732 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7733 | unicode_length(PyUnicodeObject *self) |
| 7734 | { |
| 7735 | return self->length; |
| 7736 | } |
| 7737 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7738 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7739 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7740 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7741 | 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] | 7742 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7743 | |
| 7744 | static PyObject * |
| 7745 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7746 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7747 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7748 | Py_UNICODE fillchar = ' '; |
| 7749 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7750 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7751 | return NULL; |
| 7752 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7753 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7754 | Py_INCREF(self); |
| 7755 | return (PyObject*) self; |
| 7756 | } |
| 7757 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7758 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | } |
| 7760 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7761 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7762 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7764 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7765 | |
| 7766 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7767 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7768 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 | return fixup(self, fixlower); |
| 7770 | } |
| 7771 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7772 | #define LEFTSTRIP 0 |
| 7773 | #define RIGHTSTRIP 1 |
| 7774 | #define BOTHSTRIP 2 |
| 7775 | |
| 7776 | /* Arrays indexed by above */ |
| 7777 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7778 | |
| 7779 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7780 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7781 | /* externally visible for str.strip(unicode) */ |
| 7782 | PyObject * |
| 7783 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7784 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7785 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7786 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7787 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7788 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7789 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7790 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7792 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7793 | i = 0; |
| 7794 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7795 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7796 | i++; |
| 7797 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7798 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7799 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7800 | j = len; |
| 7801 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7802 | do { |
| 7803 | j--; |
| 7804 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7805 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7806 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7807 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7808 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7809 | Py_INCREF(self); |
| 7810 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7811 | } |
| 7812 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7813 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7814 | } |
| 7815 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7816 | |
| 7817 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7818 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7819 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7820 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7821 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7822 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7823 | i = 0; |
| 7824 | if (striptype != RIGHTSTRIP) { |
| 7825 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7826 | i++; |
| 7827 | } |
| 7828 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7829 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7830 | j = len; |
| 7831 | if (striptype != LEFTSTRIP) { |
| 7832 | do { |
| 7833 | j--; |
| 7834 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7835 | j++; |
| 7836 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7837 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7838 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7839 | Py_INCREF(self); |
| 7840 | return (PyObject*)self; |
| 7841 | } |
| 7842 | else |
| 7843 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | } |
| 7845 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7846 | |
| 7847 | static PyObject * |
| 7848 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7849 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7850 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7851 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7852 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7853 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7854 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7855 | if (sep != NULL && sep != Py_None) { |
| 7856 | if (PyUnicode_Check(sep)) |
| 7857 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7858 | else { |
| 7859 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7860 | "%s arg must be None or str", |
| 7861 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7862 | return NULL; |
| 7863 | } |
| 7864 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7865 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7866 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7867 | } |
| 7868 | |
| 7869 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7870 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7871 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7872 | \n\ |
| 7873 | Return a copy of the string S with leading and trailing\n\ |
| 7874 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7875 | 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] | 7876 | |
| 7877 | static PyObject * |
| 7878 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7879 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7880 | if (PyTuple_GET_SIZE(args) == 0) |
| 7881 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7882 | else |
| 7883 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7884 | } |
| 7885 | |
| 7886 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7887 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7888 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7889 | \n\ |
| 7890 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7891 | 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] | 7892 | |
| 7893 | static PyObject * |
| 7894 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7895 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7896 | if (PyTuple_GET_SIZE(args) == 0) |
| 7897 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7898 | else |
| 7899 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7900 | } |
| 7901 | |
| 7902 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7903 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7904 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7905 | \n\ |
| 7906 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7907 | 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] | 7908 | |
| 7909 | static PyObject * |
| 7910 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7911 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7912 | if (PyTuple_GET_SIZE(args) == 0) |
| 7913 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7914 | else |
| 7915 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7916 | } |
| 7917 | |
| 7918 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7919 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7920 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7921 | { |
| 7922 | PyUnicodeObject *u; |
| 7923 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7924 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7925 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7926 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7927 | if (len < 1) { |
| 7928 | Py_INCREF(unicode_empty); |
| 7929 | return (PyObject *)unicode_empty; |
| 7930 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7931 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7932 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7933 | /* no repeat, return original string */ |
| 7934 | Py_INCREF(str); |
| 7935 | return (PyObject*) str; |
| 7936 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7937 | |
| 7938 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7939 | * needed doesn't overflow size_t |
| 7940 | */ |
| 7941 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7942 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7943 | PyErr_SetString(PyExc_OverflowError, |
| 7944 | "repeated string is too long"); |
| 7945 | return NULL; |
| 7946 | } |
| 7947 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7948 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7949 | PyErr_SetString(PyExc_OverflowError, |
| 7950 | "repeated string is too long"); |
| 7951 | return NULL; |
| 7952 | } |
| 7953 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7954 | if (!u) |
| 7955 | return NULL; |
| 7956 | |
| 7957 | p = u->str; |
| 7958 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7959 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7960 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7961 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7962 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7963 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7964 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7965 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7966 | Py_UNICODE_COPY(p+done, p, n); |
| 7967 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7968 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7969 | } |
| 7970 | |
| 7971 | return (PyObject*) u; |
| 7972 | } |
| 7973 | |
| 7974 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7975 | PyObject *subobj, |
| 7976 | PyObject *replobj, |
| 7977 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7978 | { |
| 7979 | PyObject *self; |
| 7980 | PyObject *str1; |
| 7981 | PyObject *str2; |
| 7982 | PyObject *result; |
| 7983 | |
| 7984 | self = PyUnicode_FromObject(obj); |
| 7985 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7986 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7987 | str1 = PyUnicode_FromObject(subobj); |
| 7988 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7989 | Py_DECREF(self); |
| 7990 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7991 | } |
| 7992 | str2 = PyUnicode_FromObject(replobj); |
| 7993 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7994 | Py_DECREF(self); |
| 7995 | Py_DECREF(str1); |
| 7996 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7997 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7998 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7999 | (PyUnicodeObject *)str1, |
| 8000 | (PyUnicodeObject *)str2, |
| 8001 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8002 | Py_DECREF(self); |
| 8003 | Py_DECREF(str1); |
| 8004 | Py_DECREF(str2); |
| 8005 | return result; |
| 8006 | } |
| 8007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8008 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 8009 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8010 | \n\ |
| 8011 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8012 | old replaced by new. If the optional argument count is\n\ |
| 8013 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8014 | |
| 8015 | static PyObject* |
| 8016 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8017 | { |
| 8018 | PyUnicodeObject *str1; |
| 8019 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8020 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8021 | PyObject *result; |
| 8022 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8023 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8024 | return NULL; |
| 8025 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8026 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8027 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8028 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8029 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8030 | Py_DECREF(str1); |
| 8031 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8032 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | |
| 8034 | result = replace(self, str1, str2, maxcount); |
| 8035 | |
| 8036 | Py_DECREF(str1); |
| 8037 | Py_DECREF(str2); |
| 8038 | return result; |
| 8039 | } |
| 8040 | |
| 8041 | static |
| 8042 | PyObject *unicode_repr(PyObject *unicode) |
| 8043 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8044 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8045 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8046 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8047 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8048 | |
| 8049 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8050 | better to choose a different scheme. Perhaps scan the |
| 8051 | first N-chars of the string and allocate based on that size. |
| 8052 | */ |
| 8053 | /* Initial allocation is based on the longest-possible unichr |
| 8054 | escape. |
| 8055 | |
| 8056 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8057 | unichr, so in this case it's the longest unichr escape. In |
| 8058 | narrow (UTF-16) builds this is five chars per source unichr |
| 8059 | since there are two unichrs in the surrogate pair, so in narrow |
| 8060 | (UTF-16) builds it's not the longest unichr escape. |
| 8061 | |
| 8062 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8063 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8064 | escape. |
| 8065 | */ |
| 8066 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8067 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8068 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8069 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8070 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8071 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8072 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8073 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8074 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8075 | if (repr == NULL) |
| 8076 | return NULL; |
| 8077 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8078 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8079 | |
| 8080 | /* Add quote */ |
| 8081 | *p++ = (findchar(s, size, '\'') && |
| 8082 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8083 | while (size-- > 0) { |
| 8084 | Py_UNICODE ch = *s++; |
| 8085 | |
| 8086 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8087 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8088 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8089 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8090 | continue; |
| 8091 | } |
| 8092 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8093 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8094 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8095 | *p++ = '\\'; |
| 8096 | *p++ = 't'; |
| 8097 | } |
| 8098 | else if (ch == '\n') { |
| 8099 | *p++ = '\\'; |
| 8100 | *p++ = 'n'; |
| 8101 | } |
| 8102 | else if (ch == '\r') { |
| 8103 | *p++ = '\\'; |
| 8104 | *p++ = 'r'; |
| 8105 | } |
| 8106 | |
| 8107 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8108 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8109 | *p++ = '\\'; |
| 8110 | *p++ = 'x'; |
| 8111 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8112 | *p++ = hexdigits[ch & 0x000F]; |
| 8113 | } |
| 8114 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8115 | /* Copy ASCII characters as-is */ |
| 8116 | else if (ch < 0x7F) { |
| 8117 | *p++ = ch; |
| 8118 | } |
| 8119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8121 | else { |
| 8122 | Py_UCS4 ucs = ch; |
| 8123 | |
| 8124 | #ifndef Py_UNICODE_WIDE |
| 8125 | Py_UNICODE ch2 = 0; |
| 8126 | /* Get code point from surrogate pair */ |
| 8127 | if (size > 0) { |
| 8128 | ch2 = *s; |
| 8129 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8130 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8131 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8133 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8134 | size--; |
| 8135 | } |
| 8136 | } |
| 8137 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8138 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8139 | (categories Z* and C* except ASCII space) |
| 8140 | */ |
| 8141 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8142 | /* Map 8-bit characters to '\xhh' */ |
| 8143 | if (ucs <= 0xff) { |
| 8144 | *p++ = '\\'; |
| 8145 | *p++ = 'x'; |
| 8146 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8147 | *p++ = hexdigits[ch & 0x000F]; |
| 8148 | } |
| 8149 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8150 | else if (ucs >= 0x10000) { |
| 8151 | *p++ = '\\'; |
| 8152 | *p++ = 'U'; |
| 8153 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8154 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8155 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8156 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8157 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8158 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8159 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8160 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8161 | } |
| 8162 | /* Map 16-bit characters to '\uxxxx' */ |
| 8163 | else { |
| 8164 | *p++ = '\\'; |
| 8165 | *p++ = 'u'; |
| 8166 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8167 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8168 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8169 | *p++ = hexdigits[ucs & 0x000F]; |
| 8170 | } |
| 8171 | } |
| 8172 | /* Copy characters as-is */ |
| 8173 | else { |
| 8174 | *p++ = ch; |
| 8175 | #ifndef Py_UNICODE_WIDE |
| 8176 | if (ucs >= 0x10000) |
| 8177 | *p++ = ch2; |
| 8178 | #endif |
| 8179 | } |
| 8180 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8181 | } |
| 8182 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8183 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8184 | |
| 8185 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8186 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8187 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8188 | } |
| 8189 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8190 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8191 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8192 | \n\ |
| 8193 | 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] | 8194 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8195 | arguments start and end are interpreted as in slice notation.\n\ |
| 8196 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8197 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8198 | |
| 8199 | static PyObject * |
| 8200 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8201 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8202 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8203 | Py_ssize_t start; |
| 8204 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8205 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8206 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8207 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8208 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8209 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8210 | result = stringlib_rfind_slice( |
| 8211 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8212 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8213 | start, end |
| 8214 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8215 | |
| 8216 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8217 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8218 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8219 | } |
| 8220 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8221 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8222 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8223 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8224 | 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] | 8225 | |
| 8226 | static PyObject * |
| 8227 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8228 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8229 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8230 | Py_ssize_t start; |
| 8231 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8232 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8233 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8234 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8235 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8236 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8237 | result = stringlib_rfind_slice( |
| 8238 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8239 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8240 | start, end |
| 8241 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8242 | |
| 8243 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8245 | if (result < 0) { |
| 8246 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8247 | return NULL; |
| 8248 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8249 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8250 | } |
| 8251 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8252 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8253 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8254 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8255 | 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] | 8256 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8257 | |
| 8258 | static PyObject * |
| 8259 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8260 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8261 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8262 | Py_UNICODE fillchar = ' '; |
| 8263 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8264 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8265 | return NULL; |
| 8266 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8267 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8268 | Py_INCREF(self); |
| 8269 | return (PyObject*) self; |
| 8270 | } |
| 8271 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8272 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8273 | } |
| 8274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | PyObject *sep, |
| 8277 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8278 | { |
| 8279 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8280 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8281 | s = PyUnicode_FromObject(s); |
| 8282 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8283 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8284 | if (sep != NULL) { |
| 8285 | sep = PyUnicode_FromObject(sep); |
| 8286 | if (sep == NULL) { |
| 8287 | Py_DECREF(s); |
| 8288 | return NULL; |
| 8289 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8290 | } |
| 8291 | |
| 8292 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8293 | |
| 8294 | Py_DECREF(s); |
| 8295 | Py_XDECREF(sep); |
| 8296 | return result; |
| 8297 | } |
| 8298 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8299 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8300 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8301 | \n\ |
| 8302 | Return a list of the words in S, using sep as the\n\ |
| 8303 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8304 | 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] | 8305 | whitespace string is a separator and empty strings are\n\ |
| 8306 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8307 | |
| 8308 | static PyObject* |
| 8309 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8310 | { |
| 8311 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8312 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8313 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8314 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8315 | return NULL; |
| 8316 | |
| 8317 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8318 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8319 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8320 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8321 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8322 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8323 | } |
| 8324 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8325 | PyObject * |
| 8326 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8327 | { |
| 8328 | PyObject* str_obj; |
| 8329 | PyObject* sep_obj; |
| 8330 | PyObject* out; |
| 8331 | |
| 8332 | str_obj = PyUnicode_FromObject(str_in); |
| 8333 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8334 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8335 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8336 | if (!sep_obj) { |
| 8337 | Py_DECREF(str_obj); |
| 8338 | return NULL; |
| 8339 | } |
| 8340 | |
| 8341 | out = stringlib_partition( |
| 8342 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8343 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8344 | ); |
| 8345 | |
| 8346 | Py_DECREF(sep_obj); |
| 8347 | Py_DECREF(str_obj); |
| 8348 | |
| 8349 | return out; |
| 8350 | } |
| 8351 | |
| 8352 | |
| 8353 | PyObject * |
| 8354 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8355 | { |
| 8356 | PyObject* str_obj; |
| 8357 | PyObject* sep_obj; |
| 8358 | PyObject* out; |
| 8359 | |
| 8360 | str_obj = PyUnicode_FromObject(str_in); |
| 8361 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8362 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8363 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8364 | if (!sep_obj) { |
| 8365 | Py_DECREF(str_obj); |
| 8366 | return NULL; |
| 8367 | } |
| 8368 | |
| 8369 | out = stringlib_rpartition( |
| 8370 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8371 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8372 | ); |
| 8373 | |
| 8374 | Py_DECREF(sep_obj); |
| 8375 | Py_DECREF(str_obj); |
| 8376 | |
| 8377 | return out; |
| 8378 | } |
| 8379 | |
| 8380 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8381 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8382 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8383 | 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] | 8384 | 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] | 8385 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8386 | |
| 8387 | static PyObject* |
| 8388 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8389 | { |
| 8390 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8391 | } |
| 8392 | |
| 8393 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 8394 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8395 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8396 | 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] | 8397 | 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] | 8398 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8399 | |
| 8400 | static PyObject* |
| 8401 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8402 | { |
| 8403 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8404 | } |
| 8405 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8406 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8407 | PyObject *sep, |
| 8408 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8409 | { |
| 8410 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8411 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8412 | s = PyUnicode_FromObject(s); |
| 8413 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8414 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8415 | if (sep != NULL) { |
| 8416 | sep = PyUnicode_FromObject(sep); |
| 8417 | if (sep == NULL) { |
| 8418 | Py_DECREF(s); |
| 8419 | return NULL; |
| 8420 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8421 | } |
| 8422 | |
| 8423 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8424 | |
| 8425 | Py_DECREF(s); |
| 8426 | Py_XDECREF(sep); |
| 8427 | return result; |
| 8428 | } |
| 8429 | |
| 8430 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8431 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8432 | \n\ |
| 8433 | Return a list of the words in S, using sep as the\n\ |
| 8434 | delimiter string, starting at the end of the string and\n\ |
| 8435 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8436 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8437 | is a separator."); |
| 8438 | |
| 8439 | static PyObject* |
| 8440 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8441 | { |
| 8442 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8443 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8444 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8445 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8446 | return NULL; |
| 8447 | |
| 8448 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8450 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8451 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8452 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8453 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8454 | } |
| 8455 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8456 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8457 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8458 | \n\ |
| 8459 | 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] | 8460 | 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] | 8461 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8462 | |
| 8463 | static PyObject* |
| 8464 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8465 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8466 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8468 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8469 | return NULL; |
| 8470 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8471 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8472 | } |
| 8473 | |
| 8474 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8475 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8476 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8477 | if (PyUnicode_CheckExact(self)) { |
| 8478 | Py_INCREF(self); |
| 8479 | return self; |
| 8480 | } else |
| 8481 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8482 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8483 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8484 | } |
| 8485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8486 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8487 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8488 | \n\ |
| 8489 | 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] | 8490 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8491 | |
| 8492 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8493 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8494 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8495 | return fixup(self, fixswapcase); |
| 8496 | } |
| 8497 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8498 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8499 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8500 | \n\ |
| 8501 | Return a translation table usable for str.translate().\n\ |
| 8502 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8503 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8504 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8505 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8506 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8507 | character at the same position in y. If there is a third argument, it\n\ |
| 8508 | must be a string, whose characters will be mapped to None in the result."); |
| 8509 | |
| 8510 | static PyObject* |
| 8511 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8512 | { |
| 8513 | PyObject *x, *y = NULL, *z = NULL; |
| 8514 | PyObject *new = NULL, *key, *value; |
| 8515 | Py_ssize_t i = 0; |
| 8516 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8517 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8518 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8519 | return NULL; |
| 8520 | new = PyDict_New(); |
| 8521 | if (!new) |
| 8522 | return NULL; |
| 8523 | if (y != NULL) { |
| 8524 | /* x must be a string too, of equal length */ |
| 8525 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8526 | if (!PyUnicode_Check(x)) { |
| 8527 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8528 | "be a string if there is a second argument"); |
| 8529 | goto err; |
| 8530 | } |
| 8531 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8532 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8533 | "arguments must have equal length"); |
| 8534 | goto err; |
| 8535 | } |
| 8536 | /* create entries for translating chars in x to those in y */ |
| 8537 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8538 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8539 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8540 | if (!key || !value) |
| 8541 | goto err; |
| 8542 | res = PyDict_SetItem(new, key, value); |
| 8543 | Py_DECREF(key); |
| 8544 | Py_DECREF(value); |
| 8545 | if (res < 0) |
| 8546 | goto err; |
| 8547 | } |
| 8548 | /* create entries for deleting chars in z */ |
| 8549 | if (z != NULL) { |
| 8550 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8551 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8552 | if (!key) |
| 8553 | goto err; |
| 8554 | res = PyDict_SetItem(new, key, Py_None); |
| 8555 | Py_DECREF(key); |
| 8556 | if (res < 0) |
| 8557 | goto err; |
| 8558 | } |
| 8559 | } |
| 8560 | } else { |
| 8561 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8562 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8563 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8564 | "to maketrans it must be a dict"); |
| 8565 | goto err; |
| 8566 | } |
| 8567 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8568 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8569 | if (PyUnicode_Check(key)) { |
| 8570 | /* convert string keys to integer keys */ |
| 8571 | PyObject *newkey; |
| 8572 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8573 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8574 | "table must be of length 1"); |
| 8575 | goto err; |
| 8576 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8577 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8578 | if (!newkey) |
| 8579 | goto err; |
| 8580 | res = PyDict_SetItem(new, newkey, value); |
| 8581 | Py_DECREF(newkey); |
| 8582 | if (res < 0) |
| 8583 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8584 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8585 | /* just keep integer keys */ |
| 8586 | if (PyDict_SetItem(new, key, value) < 0) |
| 8587 | goto err; |
| 8588 | } else { |
| 8589 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8590 | "be strings or integers"); |
| 8591 | goto err; |
| 8592 | } |
| 8593 | } |
| 8594 | } |
| 8595 | return new; |
| 8596 | err: |
| 8597 | Py_DECREF(new); |
| 8598 | return NULL; |
| 8599 | } |
| 8600 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8601 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8602 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8603 | \n\ |
| 8604 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8605 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8606 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8607 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8608 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8609 | |
| 8610 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8611 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8612 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8613 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8614 | } |
| 8615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8616 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8617 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8618 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8619 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8620 | |
| 8621 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8622 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8623 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8624 | return fixup(self, fixupper); |
| 8625 | } |
| 8626 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8627 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8628 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8629 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8630 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8631 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8632 | |
| 8633 | static PyObject * |
| 8634 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8635 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8636 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8637 | PyUnicodeObject *u; |
| 8638 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8639 | Py_ssize_t width; |
| 8640 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8641 | return NULL; |
| 8642 | |
| 8643 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8644 | if (PyUnicode_CheckExact(self)) { |
| 8645 | Py_INCREF(self); |
| 8646 | return (PyObject*) self; |
| 8647 | } |
| 8648 | else |
| 8649 | return PyUnicode_FromUnicode( |
| 8650 | PyUnicode_AS_UNICODE(self), |
| 8651 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8652 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8653 | } |
| 8654 | |
| 8655 | fill = width - self->length; |
| 8656 | |
| 8657 | u = pad(self, fill, 0, '0'); |
| 8658 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8659 | if (u == NULL) |
| 8660 | return NULL; |
| 8661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8662 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8663 | /* move sign to beginning of string */ |
| 8664 | u->str[0] = u->str[fill]; |
| 8665 | u->str[fill] = '0'; |
| 8666 | } |
| 8667 | |
| 8668 | return (PyObject*) u; |
| 8669 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8670 | |
| 8671 | #if 0 |
| 8672 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8673 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8674 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8675 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8676 | } |
| 8677 | #endif |
| 8678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8679 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8680 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8681 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8682 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8683 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8684 | With optional end, stop comparing S at that position.\n\ |
| 8685 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8686 | |
| 8687 | static PyObject * |
| 8688 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8689 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8690 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8691 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8692 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8693 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8694 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8695 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8696 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8697 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8698 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8699 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8700 | if (PyTuple_Check(subobj)) { |
| 8701 | Py_ssize_t i; |
| 8702 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8703 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8704 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8705 | if (substring == NULL) |
| 8706 | return NULL; |
| 8707 | result = tailmatch(self, substring, start, end, -1); |
| 8708 | Py_DECREF(substring); |
| 8709 | if (result) { |
| 8710 | Py_RETURN_TRUE; |
| 8711 | } |
| 8712 | } |
| 8713 | /* nothing matched */ |
| 8714 | Py_RETURN_FALSE; |
| 8715 | } |
| 8716 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8718 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8719 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8720 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8721 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8722 | } |
| 8723 | |
| 8724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8725 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8726 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8727 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8728 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8729 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8730 | With optional end, stop comparing S at that position.\n\ |
| 8731 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8732 | |
| 8733 | static PyObject * |
| 8734 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8735 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8736 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8737 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8738 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8739 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8740 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8741 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8743 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8744 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8745 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8746 | if (PyTuple_Check(subobj)) { |
| 8747 | Py_ssize_t i; |
| 8748 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8749 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8750 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8751 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8752 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8753 | result = tailmatch(self, substring, start, end, +1); |
| 8754 | Py_DECREF(substring); |
| 8755 | if (result) { |
| 8756 | Py_RETURN_TRUE; |
| 8757 | } |
| 8758 | } |
| 8759 | Py_RETURN_FALSE; |
| 8760 | } |
| 8761 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8762 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8763 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8764 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8765 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8766 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8767 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8768 | } |
| 8769 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8770 | #include "stringlib/string_format.h" |
| 8771 | |
| 8772 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8773 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8774 | \n\ |
| 8775 | "); |
| 8776 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8777 | static PyObject * |
| 8778 | unicode__format__(PyObject* self, PyObject* args) |
| 8779 | { |
| 8780 | PyObject *format_spec; |
| 8781 | |
| 8782 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8783 | return NULL; |
| 8784 | |
| 8785 | return _PyUnicode_FormatAdvanced(self, |
| 8786 | PyUnicode_AS_UNICODE(format_spec), |
| 8787 | PyUnicode_GET_SIZE(format_spec)); |
| 8788 | } |
| 8789 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8790 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8791 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8792 | \n\ |
| 8793 | "); |
| 8794 | |
| 8795 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8796 | unicode__sizeof__(PyUnicodeObject *v) |
| 8797 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8798 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8799 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8800 | } |
| 8801 | |
| 8802 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8803 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8804 | |
| 8805 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8806 | unicode_getnewargs(PyUnicodeObject *v) |
| 8807 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8808 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8809 | } |
| 8810 | |
| 8811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8812 | static PyMethodDef unicode_methods[] = { |
| 8813 | |
| 8814 | /* Order is according to common usage: often used methods should |
| 8815 | appear first, since lookup is done sequentially. */ |
| 8816 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 8817 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8818 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8819 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8820 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8821 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8822 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8823 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8824 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8825 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8826 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8827 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8828 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8829 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8830 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8831 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8832 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8833 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8834 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8835 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8836 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8837 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8838 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8839 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8840 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8841 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8842 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8843 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8844 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8845 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8846 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8847 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8848 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8849 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8850 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8851 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8852 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8853 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8854 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8855 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8856 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8857 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8858 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8859 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8860 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8861 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8862 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8863 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8864 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8865 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8866 | #endif |
| 8867 | |
| 8868 | #if 0 |
| 8869 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8870 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8871 | #endif |
| 8872 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8873 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8874 | {NULL, NULL} |
| 8875 | }; |
| 8876 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8877 | static PyObject * |
| 8878 | unicode_mod(PyObject *v, PyObject *w) |
| 8879 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8880 | if (!PyUnicode_Check(v)) { |
| 8881 | Py_INCREF(Py_NotImplemented); |
| 8882 | return Py_NotImplemented; |
| 8883 | } |
| 8884 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8885 | } |
| 8886 | |
| 8887 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8888 | 0, /*nb_add*/ |
| 8889 | 0, /*nb_subtract*/ |
| 8890 | 0, /*nb_multiply*/ |
| 8891 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8892 | }; |
| 8893 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8894 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8895 | (lenfunc) unicode_length, /* sq_length */ |
| 8896 | PyUnicode_Concat, /* sq_concat */ |
| 8897 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8898 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8899 | 0, /* sq_slice */ |
| 8900 | 0, /* sq_ass_item */ |
| 8901 | 0, /* sq_ass_slice */ |
| 8902 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8903 | }; |
| 8904 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8905 | static PyObject* |
| 8906 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8907 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8908 | if (PyIndex_Check(item)) { |
| 8909 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8910 | if (i == -1 && PyErr_Occurred()) |
| 8911 | return NULL; |
| 8912 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8913 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8914 | return unicode_getitem(self, i); |
| 8915 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8916 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8917 | Py_UNICODE* source_buf; |
| 8918 | Py_UNICODE* result_buf; |
| 8919 | PyObject* result; |
| 8920 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8921 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8922 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8923 | return NULL; |
| 8924 | } |
| 8925 | |
| 8926 | if (slicelength <= 0) { |
| 8927 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8928 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8929 | PyUnicode_CheckExact(self)) { |
| 8930 | Py_INCREF(self); |
| 8931 | return (PyObject *)self; |
| 8932 | } else if (step == 1) { |
| 8933 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8934 | } else { |
| 8935 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8936 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8937 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8938 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8939 | if (result_buf == NULL) |
| 8940 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8941 | |
| 8942 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8943 | result_buf[i] = source_buf[cur]; |
| 8944 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8945 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8946 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8947 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8948 | return result; |
| 8949 | } |
| 8950 | } else { |
| 8951 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8952 | return NULL; |
| 8953 | } |
| 8954 | } |
| 8955 | |
| 8956 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8957 | (lenfunc)unicode_length, /* mp_length */ |
| 8958 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8959 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8960 | }; |
| 8961 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8962 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8963 | /* Helpers for PyUnicode_Format() */ |
| 8964 | |
| 8965 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8966 | 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] | 8967 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8968 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8969 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8970 | (*p_argidx)++; |
| 8971 | if (arglen < 0) |
| 8972 | return args; |
| 8973 | else |
| 8974 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8975 | } |
| 8976 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8977 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8978 | return NULL; |
| 8979 | } |
| 8980 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8981 | /* 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] | 8982 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8983 | static PyObject * |
| 8984 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8985 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8986 | char *p; |
| 8987 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8988 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8990 | x = PyFloat_AsDouble(v); |
| 8991 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8992 | return NULL; |
| 8993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8994 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8995 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8996 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 8997 | p = PyOS_double_to_string(x, type, prec, |
| 8998 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 8999 | if (p == NULL) |
| 9000 | return NULL; |
| 9001 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9002 | PyMem_Free(p); |
| 9003 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9004 | } |
| 9005 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9006 | static PyObject* |
| 9007 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9008 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9009 | char *buf; |
| 9010 | int len; |
| 9011 | PyObject *str; /* temporary string object. */ |
| 9012 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9013 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9014 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9015 | if (!str) |
| 9016 | return NULL; |
| 9017 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9018 | Py_DECREF(str); |
| 9019 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9020 | } |
| 9021 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9022 | static int |
| 9023 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9024 | size_t buflen, |
| 9025 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9026 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9027 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9028 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9029 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9030 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9031 | buf[1] = '\0'; |
| 9032 | return 1; |
| 9033 | } |
| 9034 | #ifndef Py_UNICODE_WIDE |
| 9035 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9036 | /* Decode a valid surrogate pair */ |
| 9037 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9038 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9039 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9040 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9041 | buf[0] = c0; |
| 9042 | buf[1] = c1; |
| 9043 | buf[2] = '\0'; |
| 9044 | return 2; |
| 9045 | } |
| 9046 | } |
| 9047 | #endif |
| 9048 | goto onError; |
| 9049 | } |
| 9050 | else { |
| 9051 | /* Integer input truncated to a character */ |
| 9052 | long x; |
| 9053 | x = PyLong_AsLong(v); |
| 9054 | if (x == -1 && PyErr_Occurred()) |
| 9055 | goto onError; |
| 9056 | |
| 9057 | if (x < 0 || x > 0x10ffff) { |
| 9058 | PyErr_SetString(PyExc_OverflowError, |
| 9059 | "%c arg not in range(0x110000)"); |
| 9060 | return -1; |
| 9061 | } |
| 9062 | |
| 9063 | #ifndef Py_UNICODE_WIDE |
| 9064 | if (x > 0xffff) { |
| 9065 | x -= 0x10000; |
| 9066 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9067 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9068 | return 2; |
| 9069 | } |
| 9070 | #endif |
| 9071 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9072 | buf[1] = '\0'; |
| 9073 | return 1; |
| 9074 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9075 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9076 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9077 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9078 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9079 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9080 | } |
| 9081 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9082 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9083 | 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] | 9084 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9085 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9086 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9087 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9088 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9089 | { |
| 9090 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9091 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9092 | int args_owned = 0; |
| 9093 | PyUnicodeObject *result = NULL; |
| 9094 | PyObject *dict = NULL; |
| 9095 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9096 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9097 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9098 | PyErr_BadInternalCall(); |
| 9099 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9100 | } |
| 9101 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9102 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9103 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9104 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9105 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9106 | |
| 9107 | reslen = rescnt = fmtcnt + 100; |
| 9108 | result = _PyUnicode_New(reslen); |
| 9109 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9110 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | res = PyUnicode_AS_UNICODE(result); |
| 9112 | |
| 9113 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9114 | arglen = PyTuple_Size(args); |
| 9115 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9116 | } |
| 9117 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9118 | arglen = -1; |
| 9119 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9120 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9121 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9122 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9123 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9124 | |
| 9125 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9126 | if (*fmt != '%') { |
| 9127 | if (--rescnt < 0) { |
| 9128 | rescnt = fmtcnt + 100; |
| 9129 | reslen += rescnt; |
| 9130 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9131 | goto onError; |
| 9132 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9133 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9134 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9135 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9136 | } |
| 9137 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9138 | /* Got a format specifier */ |
| 9139 | int flags = 0; |
| 9140 | Py_ssize_t width = -1; |
| 9141 | int prec = -1; |
| 9142 | Py_UNICODE c = '\0'; |
| 9143 | Py_UNICODE fill; |
| 9144 | int isnumok; |
| 9145 | PyObject *v = NULL; |
| 9146 | PyObject *temp = NULL; |
| 9147 | Py_UNICODE *pbuf; |
| 9148 | Py_UNICODE sign; |
| 9149 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9150 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9151 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9152 | fmt++; |
| 9153 | if (*fmt == '(') { |
| 9154 | Py_UNICODE *keystart; |
| 9155 | Py_ssize_t keylen; |
| 9156 | PyObject *key; |
| 9157 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9158 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9159 | if (dict == NULL) { |
| 9160 | PyErr_SetString(PyExc_TypeError, |
| 9161 | "format requires a mapping"); |
| 9162 | goto onError; |
| 9163 | } |
| 9164 | ++fmt; |
| 9165 | --fmtcnt; |
| 9166 | keystart = fmt; |
| 9167 | /* Skip over balanced parentheses */ |
| 9168 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9169 | if (*fmt == ')') |
| 9170 | --pcount; |
| 9171 | else if (*fmt == '(') |
| 9172 | ++pcount; |
| 9173 | fmt++; |
| 9174 | } |
| 9175 | keylen = fmt - keystart - 1; |
| 9176 | if (fmtcnt < 0 || pcount > 0) { |
| 9177 | PyErr_SetString(PyExc_ValueError, |
| 9178 | "incomplete format key"); |
| 9179 | goto onError; |
| 9180 | } |
| 9181 | #if 0 |
| 9182 | /* keys are converted to strings using UTF-8 and |
| 9183 | then looked up since Python uses strings to hold |
| 9184 | variables names etc. in its namespaces and we |
| 9185 | wouldn't want to break common idioms. */ |
| 9186 | key = PyUnicode_EncodeUTF8(keystart, |
| 9187 | keylen, |
| 9188 | NULL); |
| 9189 | #else |
| 9190 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9191 | #endif |
| 9192 | if (key == NULL) |
| 9193 | goto onError; |
| 9194 | if (args_owned) { |
| 9195 | Py_DECREF(args); |
| 9196 | args_owned = 0; |
| 9197 | } |
| 9198 | args = PyObject_GetItem(dict, key); |
| 9199 | Py_DECREF(key); |
| 9200 | if (args == NULL) { |
| 9201 | goto onError; |
| 9202 | } |
| 9203 | args_owned = 1; |
| 9204 | arglen = -1; |
| 9205 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9206 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9207 | while (--fmtcnt >= 0) { |
| 9208 | switch (c = *fmt++) { |
| 9209 | case '-': flags |= F_LJUST; continue; |
| 9210 | case '+': flags |= F_SIGN; continue; |
| 9211 | case ' ': flags |= F_BLANK; continue; |
| 9212 | case '#': flags |= F_ALT; continue; |
| 9213 | case '0': flags |= F_ZERO; continue; |
| 9214 | } |
| 9215 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9216 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9217 | if (c == '*') { |
| 9218 | v = getnextarg(args, arglen, &argidx); |
| 9219 | if (v == NULL) |
| 9220 | goto onError; |
| 9221 | if (!PyLong_Check(v)) { |
| 9222 | PyErr_SetString(PyExc_TypeError, |
| 9223 | "* wants int"); |
| 9224 | goto onError; |
| 9225 | } |
| 9226 | width = PyLong_AsLong(v); |
| 9227 | if (width == -1 && PyErr_Occurred()) |
| 9228 | goto onError; |
| 9229 | if (width < 0) { |
| 9230 | flags |= F_LJUST; |
| 9231 | width = -width; |
| 9232 | } |
| 9233 | if (--fmtcnt >= 0) |
| 9234 | c = *fmt++; |
| 9235 | } |
| 9236 | else if (c >= '0' && c <= '9') { |
| 9237 | width = c - '0'; |
| 9238 | while (--fmtcnt >= 0) { |
| 9239 | c = *fmt++; |
| 9240 | if (c < '0' || c > '9') |
| 9241 | break; |
| 9242 | if ((width*10) / 10 != width) { |
| 9243 | PyErr_SetString(PyExc_ValueError, |
| 9244 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9245 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9246 | } |
| 9247 | width = width*10 + (c - '0'); |
| 9248 | } |
| 9249 | } |
| 9250 | if (c == '.') { |
| 9251 | prec = 0; |
| 9252 | if (--fmtcnt >= 0) |
| 9253 | c = *fmt++; |
| 9254 | if (c == '*') { |
| 9255 | v = getnextarg(args, arglen, &argidx); |
| 9256 | if (v == NULL) |
| 9257 | goto onError; |
| 9258 | if (!PyLong_Check(v)) { |
| 9259 | PyErr_SetString(PyExc_TypeError, |
| 9260 | "* wants int"); |
| 9261 | goto onError; |
| 9262 | } |
| 9263 | prec = PyLong_AsLong(v); |
| 9264 | if (prec == -1 && PyErr_Occurred()) |
| 9265 | goto onError; |
| 9266 | if (prec < 0) |
| 9267 | prec = 0; |
| 9268 | if (--fmtcnt >= 0) |
| 9269 | c = *fmt++; |
| 9270 | } |
| 9271 | else if (c >= '0' && c <= '9') { |
| 9272 | prec = c - '0'; |
| 9273 | while (--fmtcnt >= 0) { |
Stefan Krah | 99212f6 | 2010-07-19 17:58:26 +0000 | [diff] [blame] | 9274 | c = *fmt++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9275 | if (c < '0' || c > '9') |
| 9276 | break; |
| 9277 | if ((prec*10) / 10 != prec) { |
| 9278 | PyErr_SetString(PyExc_ValueError, |
| 9279 | "prec too big"); |
| 9280 | goto onError; |
| 9281 | } |
| 9282 | prec = prec*10 + (c - '0'); |
| 9283 | } |
| 9284 | } |
| 9285 | } /* prec */ |
| 9286 | if (fmtcnt >= 0) { |
| 9287 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9288 | if (--fmtcnt >= 0) |
| 9289 | c = *fmt++; |
| 9290 | } |
| 9291 | } |
| 9292 | if (fmtcnt < 0) { |
| 9293 | PyErr_SetString(PyExc_ValueError, |
| 9294 | "incomplete format"); |
| 9295 | goto onError; |
| 9296 | } |
| 9297 | if (c != '%') { |
| 9298 | v = getnextarg(args, arglen, &argidx); |
| 9299 | if (v == NULL) |
| 9300 | goto onError; |
| 9301 | } |
| 9302 | sign = 0; |
| 9303 | fill = ' '; |
| 9304 | switch (c) { |
| 9305 | |
| 9306 | case '%': |
| 9307 | pbuf = formatbuf; |
| 9308 | /* presume that buffer length is at least 1 */ |
| 9309 | pbuf[0] = '%'; |
| 9310 | len = 1; |
| 9311 | break; |
| 9312 | |
| 9313 | case 's': |
| 9314 | case 'r': |
| 9315 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 9316 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9317 | temp = v; |
| 9318 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9319 | } |
| 9320 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9321 | if (c == 's') |
| 9322 | temp = PyObject_Str(v); |
| 9323 | else if (c == 'r') |
| 9324 | temp = PyObject_Repr(v); |
| 9325 | else |
| 9326 | temp = PyObject_ASCII(v); |
| 9327 | if (temp == NULL) |
| 9328 | goto onError; |
| 9329 | if (PyUnicode_Check(temp)) |
| 9330 | /* nothing to do */; |
| 9331 | else { |
| 9332 | Py_DECREF(temp); |
| 9333 | PyErr_SetString(PyExc_TypeError, |
| 9334 | "%s argument has non-string str()"); |
| 9335 | goto onError; |
| 9336 | } |
| 9337 | } |
| 9338 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9339 | len = PyUnicode_GET_SIZE(temp); |
| 9340 | if (prec >= 0 && len > prec) |
| 9341 | len = prec; |
| 9342 | break; |
| 9343 | |
| 9344 | case 'i': |
| 9345 | case 'd': |
| 9346 | case 'u': |
| 9347 | case 'o': |
| 9348 | case 'x': |
| 9349 | case 'X': |
| 9350 | if (c == 'i') |
| 9351 | c = 'd'; |
| 9352 | isnumok = 0; |
| 9353 | if (PyNumber_Check(v)) { |
| 9354 | PyObject *iobj=NULL; |
| 9355 | |
| 9356 | if (PyLong_Check(v)) { |
| 9357 | iobj = v; |
| 9358 | Py_INCREF(iobj); |
| 9359 | } |
| 9360 | else { |
| 9361 | iobj = PyNumber_Long(v); |
| 9362 | } |
| 9363 | if (iobj!=NULL) { |
| 9364 | if (PyLong_Check(iobj)) { |
| 9365 | isnumok = 1; |
| 9366 | temp = formatlong(iobj, flags, prec, c); |
| 9367 | Py_DECREF(iobj); |
| 9368 | if (!temp) |
| 9369 | goto onError; |
| 9370 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9371 | len = PyUnicode_GET_SIZE(temp); |
| 9372 | sign = 1; |
| 9373 | } |
| 9374 | else { |
| 9375 | Py_DECREF(iobj); |
| 9376 | } |
| 9377 | } |
| 9378 | } |
| 9379 | if (!isnumok) { |
| 9380 | PyErr_Format(PyExc_TypeError, |
| 9381 | "%%%c format: a number is required, " |
| 9382 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9383 | goto onError; |
| 9384 | } |
| 9385 | if (flags & F_ZERO) |
| 9386 | fill = '0'; |
| 9387 | break; |
| 9388 | |
| 9389 | case 'e': |
| 9390 | case 'E': |
| 9391 | case 'f': |
| 9392 | case 'F': |
| 9393 | case 'g': |
| 9394 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9395 | temp = formatfloat(v, flags, prec, c); |
| 9396 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9397 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9398 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9399 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9400 | sign = 1; |
| 9401 | if (flags & F_ZERO) |
| 9402 | fill = '0'; |
| 9403 | break; |
| 9404 | |
| 9405 | case 'c': |
| 9406 | pbuf = formatbuf; |
| 9407 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9408 | if (len < 0) |
| 9409 | goto onError; |
| 9410 | break; |
| 9411 | |
| 9412 | default: |
| 9413 | PyErr_Format(PyExc_ValueError, |
| 9414 | "unsupported format character '%c' (0x%x) " |
| 9415 | "at index %zd", |
| 9416 | (31<=c && c<=126) ? (char)c : '?', |
| 9417 | (int)c, |
| 9418 | (Py_ssize_t)(fmt - 1 - |
| 9419 | PyUnicode_AS_UNICODE(uformat))); |
| 9420 | goto onError; |
| 9421 | } |
| 9422 | if (sign) { |
| 9423 | if (*pbuf == '-' || *pbuf == '+') { |
| 9424 | sign = *pbuf++; |
| 9425 | len--; |
| 9426 | } |
| 9427 | else if (flags & F_SIGN) |
| 9428 | sign = '+'; |
| 9429 | else if (flags & F_BLANK) |
| 9430 | sign = ' '; |
| 9431 | else |
| 9432 | sign = 0; |
| 9433 | } |
| 9434 | if (width < len) |
| 9435 | width = len; |
| 9436 | if (rescnt - (sign != 0) < width) { |
| 9437 | reslen -= rescnt; |
| 9438 | rescnt = width + fmtcnt + 100; |
| 9439 | reslen += rescnt; |
| 9440 | if (reslen < 0) { |
| 9441 | Py_XDECREF(temp); |
| 9442 | PyErr_NoMemory(); |
| 9443 | goto onError; |
| 9444 | } |
| 9445 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9446 | Py_XDECREF(temp); |
| 9447 | goto onError; |
| 9448 | } |
| 9449 | res = PyUnicode_AS_UNICODE(result) |
| 9450 | + reslen - rescnt; |
| 9451 | } |
| 9452 | if (sign) { |
| 9453 | if (fill != ' ') |
| 9454 | *res++ = sign; |
| 9455 | rescnt--; |
| 9456 | if (width > len) |
| 9457 | width--; |
| 9458 | } |
| 9459 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9460 | assert(pbuf[0] == '0'); |
| 9461 | assert(pbuf[1] == c); |
| 9462 | if (fill != ' ') { |
| 9463 | *res++ = *pbuf++; |
| 9464 | *res++ = *pbuf++; |
| 9465 | } |
| 9466 | rescnt -= 2; |
| 9467 | width -= 2; |
| 9468 | if (width < 0) |
| 9469 | width = 0; |
| 9470 | len -= 2; |
| 9471 | } |
| 9472 | if (width > len && !(flags & F_LJUST)) { |
| 9473 | do { |
| 9474 | --rescnt; |
| 9475 | *res++ = fill; |
| 9476 | } while (--width > len); |
| 9477 | } |
| 9478 | if (fill == ' ') { |
| 9479 | if (sign) |
| 9480 | *res++ = sign; |
| 9481 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9482 | assert(pbuf[0] == '0'); |
| 9483 | assert(pbuf[1] == c); |
| 9484 | *res++ = *pbuf++; |
| 9485 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9486 | } |
| 9487 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9488 | Py_UNICODE_COPY(res, pbuf, len); |
| 9489 | res += len; |
| 9490 | rescnt -= len; |
| 9491 | while (--width >= len) { |
| 9492 | --rescnt; |
| 9493 | *res++ = ' '; |
| 9494 | } |
| 9495 | if (dict && (argidx < arglen) && c != '%') { |
| 9496 | PyErr_SetString(PyExc_TypeError, |
| 9497 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9498 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9499 | goto onError; |
| 9500 | } |
| 9501 | Py_XDECREF(temp); |
| 9502 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9503 | } /* until end */ |
| 9504 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9505 | PyErr_SetString(PyExc_TypeError, |
| 9506 | "not all arguments converted during string formatting"); |
| 9507 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9508 | } |
| 9509 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9510 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9511 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9512 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9513 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9514 | } |
| 9515 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9516 | return (PyObject *)result; |
| 9517 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9518 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9519 | Py_XDECREF(result); |
| 9520 | Py_DECREF(uformat); |
| 9521 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9522 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9523 | } |
| 9524 | return NULL; |
| 9525 | } |
| 9526 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9527 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9528 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9529 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9530 | static PyObject * |
| 9531 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9532 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9533 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9534 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9535 | char *encoding = NULL; |
| 9536 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9537 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9538 | if (type != &PyUnicode_Type) |
| 9539 | return unicode_subtype_new(type, args, kwds); |
| 9540 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9541 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9542 | return NULL; |
| 9543 | if (x == NULL) |
| 9544 | return (PyObject *)_PyUnicode_New(0); |
| 9545 | if (encoding == NULL && errors == NULL) |
| 9546 | return PyObject_Str(x); |
| 9547 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9548 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9549 | } |
| 9550 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9551 | static PyObject * |
| 9552 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9553 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9554 | PyUnicodeObject *tmp, *pnew; |
| 9555 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9556 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9557 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9558 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9559 | if (tmp == NULL) |
| 9560 | return NULL; |
| 9561 | assert(PyUnicode_Check(tmp)); |
| 9562 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9563 | if (pnew == NULL) { |
| 9564 | Py_DECREF(tmp); |
| 9565 | return NULL; |
| 9566 | } |
| 9567 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9568 | if (pnew->str == NULL) { |
| 9569 | _Py_ForgetReference((PyObject *)pnew); |
| 9570 | PyObject_Del(pnew); |
| 9571 | Py_DECREF(tmp); |
| 9572 | return PyErr_NoMemory(); |
| 9573 | } |
| 9574 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9575 | pnew->length = n; |
| 9576 | pnew->hash = tmp->hash; |
| 9577 | Py_DECREF(tmp); |
| 9578 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9579 | } |
| 9580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9581 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9582 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9583 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9584 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9585 | encoding defaults to the current default string encoding.\n\ |
| 9586 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9587 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9588 | static PyObject *unicode_iter(PyObject *seq); |
| 9589 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9590 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9591 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9592 | "str", /* tp_name */ |
| 9593 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9594 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9595 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9596 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9597 | 0, /* tp_print */ |
| 9598 | 0, /* tp_getattr */ |
| 9599 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9600 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9601 | unicode_repr, /* tp_repr */ |
| 9602 | &unicode_as_number, /* tp_as_number */ |
| 9603 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9604 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9605 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9606 | 0, /* tp_call*/ |
| 9607 | (reprfunc) unicode_str, /* tp_str */ |
| 9608 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9609 | 0, /* tp_setattro */ |
| 9610 | 0, /* tp_as_buffer */ |
| 9611 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9612 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9613 | unicode_doc, /* tp_doc */ |
| 9614 | 0, /* tp_traverse */ |
| 9615 | 0, /* tp_clear */ |
| 9616 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9617 | 0, /* tp_weaklistoffset */ |
| 9618 | unicode_iter, /* tp_iter */ |
| 9619 | 0, /* tp_iternext */ |
| 9620 | unicode_methods, /* tp_methods */ |
| 9621 | 0, /* tp_members */ |
| 9622 | 0, /* tp_getset */ |
| 9623 | &PyBaseObject_Type, /* tp_base */ |
| 9624 | 0, /* tp_dict */ |
| 9625 | 0, /* tp_descr_get */ |
| 9626 | 0, /* tp_descr_set */ |
| 9627 | 0, /* tp_dictoffset */ |
| 9628 | 0, /* tp_init */ |
| 9629 | 0, /* tp_alloc */ |
| 9630 | unicode_new, /* tp_new */ |
| 9631 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9632 | }; |
| 9633 | |
| 9634 | /* Initialize the Unicode implementation */ |
| 9635 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9636 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9637 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9638 | int i; |
| 9639 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9640 | /* XXX - move this array to unicodectype.c ? */ |
| 9641 | Py_UNICODE linebreak[] = { |
| 9642 | 0x000A, /* LINE FEED */ |
| 9643 | 0x000D, /* CARRIAGE RETURN */ |
| 9644 | 0x001C, /* FILE SEPARATOR */ |
| 9645 | 0x001D, /* GROUP SEPARATOR */ |
| 9646 | 0x001E, /* RECORD SEPARATOR */ |
| 9647 | 0x0085, /* NEXT LINE */ |
| 9648 | 0x2028, /* LINE SEPARATOR */ |
| 9649 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9650 | }; |
| 9651 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9652 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9653 | free_list = NULL; |
| 9654 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9655 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9656 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9657 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9658 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9659 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9660 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9661 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9662 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9663 | |
| 9664 | /* initialize the linebreak bloom filter */ |
| 9665 | bloom_linebreak = make_bloom_mask( |
| 9666 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9667 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9668 | |
| 9669 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9670 | } |
| 9671 | |
| 9672 | /* Finalize the Unicode implementation */ |
| 9673 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9674 | int |
| 9675 | PyUnicode_ClearFreeList(void) |
| 9676 | { |
| 9677 | int freelist_size = numfree; |
| 9678 | PyUnicodeObject *u; |
| 9679 | |
| 9680 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9681 | PyUnicodeObject *v = u; |
| 9682 | u = *(PyUnicodeObject **)u; |
| 9683 | if (v->str) |
| 9684 | PyObject_DEL(v->str); |
| 9685 | Py_XDECREF(v->defenc); |
| 9686 | PyObject_Del(v); |
| 9687 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9688 | } |
| 9689 | free_list = NULL; |
| 9690 | assert(numfree == 0); |
| 9691 | return freelist_size; |
| 9692 | } |
| 9693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9694 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9695 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9696 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9697 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9698 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9699 | Py_XDECREF(unicode_empty); |
| 9700 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9701 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9702 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9703 | if (unicode_latin1[i]) { |
| 9704 | Py_DECREF(unicode_latin1[i]); |
| 9705 | unicode_latin1[i] = NULL; |
| 9706 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9707 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9708 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9709 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9710 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9711 | void |
| 9712 | PyUnicode_InternInPlace(PyObject **p) |
| 9713 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9714 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9715 | PyObject *t; |
| 9716 | if (s == NULL || !PyUnicode_Check(s)) |
| 9717 | Py_FatalError( |
| 9718 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9719 | /* If it's a subclass, we don't really know what putting |
| 9720 | it in the interned dict might do. */ |
| 9721 | if (!PyUnicode_CheckExact(s)) |
| 9722 | return; |
| 9723 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9724 | return; |
| 9725 | if (interned == NULL) { |
| 9726 | interned = PyDict_New(); |
| 9727 | if (interned == NULL) { |
| 9728 | PyErr_Clear(); /* Don't leave an exception */ |
| 9729 | return; |
| 9730 | } |
| 9731 | } |
| 9732 | /* It might be that the GetItem call fails even |
| 9733 | though the key is present in the dictionary, |
| 9734 | namely when this happens during a stack overflow. */ |
| 9735 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9736 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9737 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9738 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9739 | if (t) { |
| 9740 | Py_INCREF(t); |
| 9741 | Py_DECREF(*p); |
| 9742 | *p = t; |
| 9743 | return; |
| 9744 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9745 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9746 | PyThreadState_GET()->recursion_critical = 1; |
| 9747 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9748 | PyErr_Clear(); |
| 9749 | PyThreadState_GET()->recursion_critical = 0; |
| 9750 | return; |
| 9751 | } |
| 9752 | PyThreadState_GET()->recursion_critical = 0; |
| 9753 | /* The two references in interned are not counted by refcnt. |
| 9754 | The deallocator will take care of this */ |
| 9755 | Py_REFCNT(s) -= 2; |
| 9756 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9757 | } |
| 9758 | |
| 9759 | void |
| 9760 | PyUnicode_InternImmortal(PyObject **p) |
| 9761 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9762 | PyUnicode_InternInPlace(p); |
| 9763 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9764 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9765 | Py_INCREF(*p); |
| 9766 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9767 | } |
| 9768 | |
| 9769 | PyObject * |
| 9770 | PyUnicode_InternFromString(const char *cp) |
| 9771 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9772 | PyObject *s = PyUnicode_FromString(cp); |
| 9773 | if (s == NULL) |
| 9774 | return NULL; |
| 9775 | PyUnicode_InternInPlace(&s); |
| 9776 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9777 | } |
| 9778 | |
| 9779 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9780 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9781 | PyObject *keys; |
| 9782 | PyUnicodeObject *s; |
| 9783 | Py_ssize_t i, n; |
| 9784 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9785 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9786 | if (interned == NULL || !PyDict_Check(interned)) |
| 9787 | return; |
| 9788 | keys = PyDict_Keys(interned); |
| 9789 | if (keys == NULL || !PyList_Check(keys)) { |
| 9790 | PyErr_Clear(); |
| 9791 | return; |
| 9792 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9793 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9794 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9795 | detector, interned unicode strings are not forcibly deallocated; |
| 9796 | rather, we give them their stolen references back, and then clear |
| 9797 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9798 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9799 | n = PyList_GET_SIZE(keys); |
| 9800 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9801 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9802 | for (i = 0; i < n; i++) { |
| 9803 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9804 | switch (s->state) { |
| 9805 | case SSTATE_NOT_INTERNED: |
| 9806 | /* XXX Shouldn't happen */ |
| 9807 | break; |
| 9808 | case SSTATE_INTERNED_IMMORTAL: |
| 9809 | Py_REFCNT(s) += 1; |
| 9810 | immortal_size += s->length; |
| 9811 | break; |
| 9812 | case SSTATE_INTERNED_MORTAL: |
| 9813 | Py_REFCNT(s) += 2; |
| 9814 | mortal_size += s->length; |
| 9815 | break; |
| 9816 | default: |
| 9817 | Py_FatalError("Inconsistent interned string state."); |
| 9818 | } |
| 9819 | s->state = SSTATE_NOT_INTERNED; |
| 9820 | } |
| 9821 | fprintf(stderr, "total size of all interned strings: " |
| 9822 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9823 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9824 | Py_DECREF(keys); |
| 9825 | PyDict_Clear(interned); |
| 9826 | Py_DECREF(interned); |
| 9827 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9828 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9829 | |
| 9830 | |
| 9831 | /********************* Unicode Iterator **************************/ |
| 9832 | |
| 9833 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9834 | PyObject_HEAD |
| 9835 | Py_ssize_t it_index; |
| 9836 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9837 | } unicodeiterobject; |
| 9838 | |
| 9839 | static void |
| 9840 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9841 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9842 | _PyObject_GC_UNTRACK(it); |
| 9843 | Py_XDECREF(it->it_seq); |
| 9844 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9845 | } |
| 9846 | |
| 9847 | static int |
| 9848 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9849 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9850 | Py_VISIT(it->it_seq); |
| 9851 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9852 | } |
| 9853 | |
| 9854 | static PyObject * |
| 9855 | unicodeiter_next(unicodeiterobject *it) |
| 9856 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9857 | PyUnicodeObject *seq; |
| 9858 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9859 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9860 | assert(it != NULL); |
| 9861 | seq = it->it_seq; |
| 9862 | if (seq == NULL) |
| 9863 | return NULL; |
| 9864 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9865 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9866 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9867 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9868 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9869 | if (item != NULL) |
| 9870 | ++it->it_index; |
| 9871 | return item; |
| 9872 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9873 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9874 | Py_DECREF(seq); |
| 9875 | it->it_seq = NULL; |
| 9876 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9877 | } |
| 9878 | |
| 9879 | static PyObject * |
| 9880 | unicodeiter_len(unicodeiterobject *it) |
| 9881 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9882 | Py_ssize_t len = 0; |
| 9883 | if (it->it_seq) |
| 9884 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9885 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9886 | } |
| 9887 | |
| 9888 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9889 | |
| 9890 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9891 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9892 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9893 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9894 | }; |
| 9895 | |
| 9896 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9897 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9898 | "str_iterator", /* tp_name */ |
| 9899 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9900 | 0, /* tp_itemsize */ |
| 9901 | /* methods */ |
| 9902 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9903 | 0, /* tp_print */ |
| 9904 | 0, /* tp_getattr */ |
| 9905 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9906 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9907 | 0, /* tp_repr */ |
| 9908 | 0, /* tp_as_number */ |
| 9909 | 0, /* tp_as_sequence */ |
| 9910 | 0, /* tp_as_mapping */ |
| 9911 | 0, /* tp_hash */ |
| 9912 | 0, /* tp_call */ |
| 9913 | 0, /* tp_str */ |
| 9914 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9915 | 0, /* tp_setattro */ |
| 9916 | 0, /* tp_as_buffer */ |
| 9917 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9918 | 0, /* tp_doc */ |
| 9919 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9920 | 0, /* tp_clear */ |
| 9921 | 0, /* tp_richcompare */ |
| 9922 | 0, /* tp_weaklistoffset */ |
| 9923 | PyObject_SelfIter, /* tp_iter */ |
| 9924 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9925 | unicodeiter_methods, /* tp_methods */ |
| 9926 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9927 | }; |
| 9928 | |
| 9929 | static PyObject * |
| 9930 | unicode_iter(PyObject *seq) |
| 9931 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9932 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9933 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9934 | if (!PyUnicode_Check(seq)) { |
| 9935 | PyErr_BadInternalCall(); |
| 9936 | return NULL; |
| 9937 | } |
| 9938 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9939 | if (it == NULL) |
| 9940 | return NULL; |
| 9941 | it->it_index = 0; |
| 9942 | Py_INCREF(seq); |
| 9943 | it->it_seq = (PyUnicodeObject *)seq; |
| 9944 | _PyObject_GC_TRACK(it); |
| 9945 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9946 | } |
| 9947 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9948 | size_t |
| 9949 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9950 | { |
| 9951 | int res = 0; |
| 9952 | while(*u++) |
| 9953 | res++; |
| 9954 | return res; |
| 9955 | } |
| 9956 | |
| 9957 | Py_UNICODE* |
| 9958 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9959 | { |
| 9960 | Py_UNICODE *u = s1; |
| 9961 | while ((*u++ = *s2++)); |
| 9962 | return s1; |
| 9963 | } |
| 9964 | |
| 9965 | Py_UNICODE* |
| 9966 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9967 | { |
| 9968 | Py_UNICODE *u = s1; |
| 9969 | while ((*u++ = *s2++)) |
| 9970 | if (n-- == 0) |
| 9971 | break; |
| 9972 | return s1; |
| 9973 | } |
| 9974 | |
| 9975 | int |
| 9976 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9977 | { |
| 9978 | while (*s1 && *s2 && *s1 == *s2) |
| 9979 | s1++, s2++; |
| 9980 | if (*s1 && *s2) |
| 9981 | return (*s1 < *s2) ? -1 : +1; |
| 9982 | if (*s1) |
| 9983 | return 1; |
| 9984 | if (*s2) |
| 9985 | return -1; |
| 9986 | return 0; |
| 9987 | } |
| 9988 | |
Victor Stinner | ef8d95c | 2010-08-16 22:03:11 +0000 | [diff] [blame] | 9989 | int |
| 9990 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9991 | { |
| 9992 | register Py_UNICODE u1, u2; |
| 9993 | for (; n != 0; n--) { |
| 9994 | u1 = *s1; |
| 9995 | u2 = *s2; |
| 9996 | if (u1 != u2) |
| 9997 | return (u1 < u2) ? -1 : +1; |
| 9998 | if (u1 == '\0') |
| 9999 | return 0; |
| 10000 | s1++; |
| 10001 | s2++; |
| 10002 | } |
| 10003 | return 0; |
| 10004 | } |
| 10005 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10006 | Py_UNICODE* |
| 10007 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10008 | { |
| 10009 | const Py_UNICODE *p; |
| 10010 | for (p = s; *p; p++) |
| 10011 | if (*p == c) |
| 10012 | return (Py_UNICODE*)p; |
| 10013 | return NULL; |
| 10014 | } |
| 10015 | |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 10016 | Py_UNICODE* |
| 10017 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10018 | { |
| 10019 | const Py_UNICODE *p; |
| 10020 | p = s + Py_UNICODE_strlen(s); |
| 10021 | while (p != s) { |
| 10022 | p--; |
| 10023 | if (*p == c) |
| 10024 | return (Py_UNICODE*)p; |
| 10025 | } |
| 10026 | return NULL; |
| 10027 | } |
| 10028 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10029 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10030 | #ifdef __cplusplus |
| 10031 | } |
| 10032 | #endif |