| 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 |  | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +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 | 1c5d21d | 2009-01-31 22:33:02 +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 |  | 
| Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +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" | 
 | 44 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | #include "unicodeobject.h" | 
| Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 46 | #include "ucnhash.h" | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> | 
 | 50 | #endif | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ | 
 | 53 |  | 
| Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 54 | #define PyUnicode_MAXFREELIST       1024 | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 55 |  | 
 | 56 | /* Limit for the Unicode object free list stay alive optimization. | 
 | 57 |  | 
 | 58 |    The implementation will keep allocated Unicode memory intact for | 
 | 59 |    all objects on the free list having a size less than this | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 |    limit. This reduces malloc() overhead for small Unicode objects. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 |  | 
| Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 62 |    At worst this will result in PyUnicode_MAXFREELIST * | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 |    (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 |    malloc()-overhead) bytes of unused garbage. | 
 | 65 |  | 
 | 66 |    Setting the limit to 0 effectively turns the feature off. | 
 | 67 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 |    Note: This is an experimental feature ! If you get core dumps when | 
 | 69 |    using Unicode objects, turn this feature off. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 |  | 
 | 71 | */ | 
 | 72 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT       9 | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 |  | 
 | 75 | /* Endianness switches; defaults to little endian */ | 
 | 76 |  | 
 | 77 | #ifdef WORDS_BIGENDIAN | 
 | 78 | # define BYTEORDER_IS_BIG_ENDIAN | 
 | 79 | #else | 
 | 80 | # define BYTEORDER_IS_LITTLE_ENDIAN | 
 | 81 | #endif | 
 | 82 |  | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ | 
 | 84 |  | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 85 | NOTE: In the interpreter's initialization phase, some globals are currently | 
 | 86 |       initialized dynamically as needed. In the process Unicode objects may | 
 | 87 |       be created before the Unicode type is ready. | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 88 |  | 
 | 89 | */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 |  | 
| Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 91 |  | 
 | 92 | #ifdef __cplusplus | 
 | 93 | extern "C" { | 
 | 94 | #endif | 
 | 95 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 96 | /* Free list for Unicode objects */ | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 97 | static PyUnicodeObject *free_list = NULL; | 
 | 98 | static int numfree = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 99 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 100 | /* The empty Unicode object is shared to improve performance. */ | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 101 | static PyUnicodeObject *unicode_empty = NULL; | 
 | 102 |  | 
 | 103 | #define _Py_RETURN_UNICODE_EMPTY()                      \ | 
 | 104 |     do {                                                \ | 
 | 105 |         if (unicode_empty != NULL)                      \ | 
 | 106 |             Py_INCREF(unicode_empty);                   \ | 
 | 107 |         else {                                          \ | 
 | 108 |             unicode_empty = _PyUnicode_New(0);          \ | 
 | 109 |             if (unicode_empty != NULL)                  \ | 
 | 110 |                 Py_INCREF(unicode_empty);               \ | 
 | 111 |         }                                               \ | 
 | 112 |         return (PyObject *)unicode_empty;               \ | 
 | 113 |     } while (0) | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 114 |  | 
 | 115 | /* Single character Unicode strings in the Latin-1 range are being | 
 | 116 |    shared as well. */ | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 117 | static PyUnicodeObject *unicode_latin1[256] = {NULL}; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 118 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 119 | /* Default encoding to use and assume when NULL is passed as encoding | 
 | 120 |    parameter; it is initialized by _PyUnicode_Init(). | 
 | 121 |  | 
 | 122 |    Always use the PyUnicode_SetDefaultEncoding() and | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 123 |    PyUnicode_GetDefaultEncoding() APIs to access this global. | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 124 |  | 
 | 125 | */ | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 126 | static char unicode_default_encoding[100 + 1] = "ascii"; | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 127 |  | 
| Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 128 | /* Fast detection of the most frequent whitespace characters */ | 
 | 129 | const unsigned char _Py_ascii_whitespace[] = { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 130 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Florent Xicluna | 22b2438 | 2010-03-30 08:24:06 +0000 | [diff] [blame] | 131 | /*     case 0x0009: * CHARACTER TABULATION */ | 
| Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 132 | /*     case 0x000A: * LINE FEED */ | 
| Florent Xicluna | 22b2438 | 2010-03-30 08:24:06 +0000 | [diff] [blame] | 133 | /*     case 0x000B: * LINE TABULATION */ | 
| Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 134 | /*     case 0x000C: * FORM FEED */ | 
 | 135 | /*     case 0x000D: * CARRIAGE RETURN */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 136 |     0, 1, 1, 1, 1, 1, 0, 0, | 
 | 137 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 138 | /*     case 0x001C: * FILE SEPARATOR */ | 
 | 139 | /*     case 0x001D: * GROUP SEPARATOR */ | 
 | 140 | /*     case 0x001E: * RECORD SEPARATOR */ | 
 | 141 | /*     case 0x001F: * UNIT SEPARATOR */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 142 |     0, 0, 0, 0, 1, 1, 1, 1, | 
| Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 143 | /*     case 0x0020: * SPACE */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 144 |     1, 0, 0, 0, 0, 0, 0, 0, | 
 | 145 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 146 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 147 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 148 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 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, | 
 | 155 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 156 |     0, 0, 0, 0, 0, 0, 0, 0 | 
| Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 157 | }; | 
 | 158 |  | 
 | 159 | /* Same for linebreaks */ | 
 | 160 | static unsigned char ascii_linebreak[] = { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 161 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 162 | /*         0x000A, * LINE FEED */ | 
| Florent Xicluna | 22b2438 | 2010-03-30 08:24:06 +0000 | [diff] [blame] | 163 | /*         0x000B, * LINE TABULATION */ | 
 | 164 | /*         0x000C, * FORM FEED */ | 
| Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 165 | /*         0x000D, * CARRIAGE RETURN */ | 
| Florent Xicluna | 22b2438 | 2010-03-30 08:24:06 +0000 | [diff] [blame] | 166 |     0, 0, 1, 1, 1, 1, 0, 0, | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 167 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 168 | /*         0x001C, * FILE SEPARATOR */ | 
 | 169 | /*         0x001D, * GROUP SEPARATOR */ | 
 | 170 | /*         0x001E, * RECORD SEPARATOR */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 171 |     0, 0, 0, 0, 1, 1, 1, 0, | 
 | 172 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 173 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 174 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 175 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 176 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 177 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 178 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 179 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 180 |     0, 0, 0, 0, 0, 0, 0, 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 | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 185 | }; | 
 | 186 |  | 
 | 187 |  | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 188 | Py_UNICODE | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 189 | PyUnicode_GetMax(void) | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 190 | { | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 191 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 192 |     return 0x10FFFF; | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 193 | #else | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 194 |     /* This is actually an illegal character, so it should | 
 | 195 |        not be passed to unichr. */ | 
 | 196 |     return 0xFFFF; | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 197 | #endif | 
 | 198 | } | 
 | 199 |  | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 200 | /* --- Bloom Filters ----------------------------------------------------- */ | 
 | 201 |  | 
 | 202 | /* stuff to implement simple "bloom filters" for Unicode characters. | 
 | 203 |    to keep things simple, we use a single bitmask, using the least 5 | 
 | 204 |    bits from each unicode characters as the bit index. */ | 
 | 205 |  | 
 | 206 | /* the linebreak mask is set up by Unicode_Init below */ | 
 | 207 |  | 
| Antoine Pitrou | 1004292 | 2010-01-13 14:01:26 +0000 | [diff] [blame] | 208 | #if LONG_BIT >= 128 | 
 | 209 | #define BLOOM_WIDTH 128 | 
 | 210 | #elif LONG_BIT >= 64 | 
 | 211 | #define BLOOM_WIDTH 64 | 
 | 212 | #elif LONG_BIT >= 32 | 
 | 213 | #define BLOOM_WIDTH 32 | 
 | 214 | #else | 
 | 215 | #error "LONG_BIT is smaller than 32" | 
 | 216 | #endif | 
 | 217 |  | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 218 | #define BLOOM_MASK unsigned long | 
 | 219 |  | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 220 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 221 |  | 
| Antoine Pitrou | 1004292 | 2010-01-13 14:01:26 +0000 | [diff] [blame] | 222 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) | 
 | 223 | #define BLOOM(mask, ch)     ((mask &  (1UL << ((ch) & (BLOOM_WIDTH - 1))))) | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 224 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 225 | #define BLOOM_LINEBREAK(ch)                                             \ | 
 | 226 |     ((ch) < 128U ? ascii_linebreak[(ch)] :                              \ | 
 | 227 |      (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 228 |  | 
| Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 229 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 230 | { | 
 | 231 |     /* calculate simple bloom-style bitmask for a given unicode string */ | 
 | 232 |  | 
| Antoine Pitrou | 1004292 | 2010-01-13 14:01:26 +0000 | [diff] [blame] | 233 |     BLOOM_MASK mask; | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 234 |     Py_ssize_t i; | 
 | 235 |  | 
 | 236 |     mask = 0; | 
 | 237 |     for (i = 0; i < len; i++) | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 238 |         BLOOM_ADD(mask, ptr[i]); | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 239 |  | 
 | 240 |     return mask; | 
 | 241 | } | 
 | 242 |  | 
| Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 243 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 244 | { | 
 | 245 |     Py_ssize_t i; | 
 | 246 |  | 
 | 247 |     for (i = 0; i < setlen; i++) | 
 | 248 |         if (set[i] == chr) | 
 | 249 |             return 1; | 
 | 250 |  | 
| Fredrik Lundh | 7763351 | 2006-05-23 19:47:35 +0000 | [diff] [blame] | 251 |     return 0; | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 252 | } | 
 | 253 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 254 | #define BLOOM_MEMBER(mask, chr, set, setlen)                    \ | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 255 |     BLOOM(mask, chr) && unicode_member(chr, set, setlen) | 
 | 256 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 257 | /* --- Unicode Object ----------------------------------------------------- */ | 
 | 258 |  | 
 | 259 | static | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 260 | int unicode_resize(register PyUnicodeObject *unicode, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 261 |                    Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | { | 
 | 263 |     void *oldstr; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 264 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 265 |     /* Shortcut if there's nothing much to do. */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 |     if (unicode->length == length) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 267 |         goto reset; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 268 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 269 |     /* Resizing shared object (unicode_empty or single character | 
 | 270 |        objects) in-place is not allowed. Use PyUnicode_Resize() | 
 | 271 |        instead ! */ | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 272 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 273 |     if (unicode == unicode_empty || | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 274 |         (unicode->length == 1 && | 
 | 275 |          unicode->str[0] < 256U && | 
 | 276 |          unicode_latin1[unicode->str[0]] == unicode)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 |         PyErr_SetString(PyExc_SystemError, | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 278 |                         "can't resize shared unicode objects"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 279 |         return -1; | 
 | 280 |     } | 
 | 281 |  | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 282 |     /* We allocate one more byte to make sure the string is Ux0000 terminated. | 
 | 283 |        The overallocation is also used by fastsearch, which assumes that it's | 
| Andrew M. Kuchling | 07bbfc6 | 2006-05-26 19:51:10 +0000 | [diff] [blame] | 284 |        safe to look at str[length] (without making any assumptions about what | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 285 |        it contains). */ | 
 | 286 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 287 |     oldstr = unicode->str; | 
| Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 288 |     unicode->str = PyObject_REALLOC(unicode->str, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 289 |                                     sizeof(Py_UNICODE) * (length + 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 290 |     if (!unicode->str) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 291 |         unicode->str = (Py_UNICODE *)oldstr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 292 |         PyErr_NoMemory(); | 
 | 293 |         return -1; | 
 | 294 |     } | 
 | 295 |     unicode->str[length] = 0; | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 296 |     unicode->length = length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 298 |   reset: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 |     /* Reset the object caches */ | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 300 |     if (unicode->defenc) { | 
| Georg Brandl | d070cc5 | 2010-08-01 21:06:46 +0000 | [diff] [blame] | 301 |         Py_CLEAR(unicode->defenc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 302 |     } | 
 | 303 |     unicode->hash = -1; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 304 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 |     return 0; | 
 | 306 | } | 
 | 307 |  | 
 | 308 | /* We allocate one more byte to make sure the string is | 
| Georg Brandl | e27d044 | 2010-08-01 20:54:30 +0000 | [diff] [blame] | 309 |    Ux0000 terminated; some code relies on that. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 310 |  | 
 | 311 |    XXX This allocator could further be enhanced by assuring that the | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 312 |    free list never reduces its size below 1. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 313 |  | 
 | 314 | */ | 
 | 315 |  | 
 | 316 | static | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 317 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 318 | { | 
 | 319 |     register PyUnicodeObject *unicode; | 
 | 320 |  | 
| Andrew Dalke | e0df762 | 2006-05-27 11:04:36 +0000 | [diff] [blame] | 321 |     /* Optimization for empty strings */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 322 |     if (length == 0 && unicode_empty != NULL) { | 
 | 323 |         Py_INCREF(unicode_empty); | 
 | 324 |         return unicode_empty; | 
 | 325 |     } | 
 | 326 |  | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 327 |     /* Ensure we won't overflow the size. */ | 
 | 328 |     if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { | 
 | 329 |         return (PyUnicodeObject *)PyErr_NoMemory(); | 
 | 330 |     } | 
 | 331 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 332 |     /* Unicode freelist & memory allocation */ | 
| Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 333 |     if (free_list) { | 
 | 334 |         unicode = free_list; | 
 | 335 |         free_list = *(PyUnicodeObject **)unicode; | 
 | 336 |         numfree--; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 337 |         if (unicode->str) { | 
 | 338 |             /* Keep-Alive optimization: we only upsize the buffer, | 
 | 339 |                never downsize it. */ | 
 | 340 |             if ((unicode->length < length) && | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 341 |                 unicode_resize(unicode, length) < 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 342 |                 PyObject_DEL(unicode->str); | 
 | 343 |                 unicode->str = NULL; | 
 | 344 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 345 |         } | 
| Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 346 |         else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 347 |             size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); | 
 | 348 |             unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); | 
| Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 349 |         } | 
| Martin Panter | 646b528 | 2016-06-21 23:58:05 +0000 | [diff] [blame] | 350 |         (void)PyObject_INIT(unicode, &PyUnicode_Type); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 351 |     } | 
 | 352 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 353 |         size_t new_size; | 
| Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 354 |         unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 355 |         if (unicode == NULL) | 
 | 356 |             return NULL; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 357 |         new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); | 
 | 358 |         unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 359 |     } | 
 | 360 |  | 
| Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 361 |     if (!unicode->str) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 362 |         PyErr_NoMemory(); | 
 | 363 |         goto onError; | 
| Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 364 |     } | 
| Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 365 |     /* Initialize the first element to guard against cases where | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 366 |      * the caller fails before initializing str -- unicode_resize() | 
 | 367 |      * reads str[0], and the Keep-Alive optimization can keep memory | 
 | 368 |      * allocated for str alive across a call to unicode_dealloc(unicode). | 
 | 369 |      * We don't want unicode_resize to read uninitialized memory in | 
 | 370 |      * that case. | 
 | 371 |      */ | 
| Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 372 |     unicode->str[0] = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 373 |     unicode->str[length] = 0; | 
| Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 374 |     unicode->length = length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 375 |     unicode->hash = -1; | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 376 |     unicode->defenc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 377 |     return unicode; | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 378 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 379 |   onError: | 
| Amaury Forgeot d'Arc | 06847b1 | 2008-07-31 23:39:05 +0000 | [diff] [blame] | 380 |     /* XXX UNREF/NEWREF interface should be more symmetrical */ | 
 | 381 |     _Py_DEC_REFTOTAL; | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 382 |     _Py_ForgetReference((PyObject *)unicode); | 
| Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 383 |     PyObject_Del(unicode); | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 384 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 385 | } | 
 | 386 |  | 
 | 387 | static | 
| Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 388 | void unicode_dealloc(register PyUnicodeObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 389 | { | 
| Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 390 |     if (PyUnicode_CheckExact(unicode) && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 391 |         numfree < PyUnicode_MAXFREELIST) { | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 392 |         /* Keep-Alive optimization */ | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 393 |         if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { | 
 | 394 |             PyObject_DEL(unicode->str); | 
 | 395 |             unicode->str = NULL; | 
 | 396 |             unicode->length = 0; | 
 | 397 |         } | 
 | 398 |         if (unicode->defenc) { | 
| Georg Brandl | d070cc5 | 2010-08-01 21:06:46 +0000 | [diff] [blame] | 399 |             Py_CLEAR(unicode->defenc); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 400 |         } | 
 | 401 |         /* Add to free list */ | 
| Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 402 |         *(PyUnicodeObject **)unicode = free_list; | 
 | 403 |         free_list = unicode; | 
 | 404 |         numfree++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 405 |     } | 
 | 406 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 407 |         PyObject_DEL(unicode->str); | 
 | 408 |         Py_XDECREF(unicode->defenc); | 
 | 409 |         Py_TYPE(unicode)->tp_free((PyObject *)unicode); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 410 |     } | 
 | 411 | } | 
 | 412 |  | 
| Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 413 | static | 
 | 414 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 415 | { | 
 | 416 |     register PyUnicodeObject *v; | 
 | 417 |  | 
 | 418 |     /* Argument checks */ | 
 | 419 |     if (unicode == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 420 |         PyErr_BadInternalCall(); | 
 | 421 |         return -1; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 422 |     } | 
| Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 423 |     v = *unicode; | 
| Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 424 |     if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 425 |         PyErr_BadInternalCall(); | 
 | 426 |         return -1; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 427 |     } | 
 | 428 |  | 
 | 429 |     /* Resizing unicode_empty and single character objects is not | 
 | 430 |        possible since these are being shared. We simply return a fresh | 
 | 431 |        copy with the same Unicode content. */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 432 |     if (v->length != length && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 433 |         (v == unicode_empty || v->length == 1)) { | 
 | 434 |         PyUnicodeObject *w = _PyUnicode_New(length); | 
 | 435 |         if (w == NULL) | 
 | 436 |             return -1; | 
 | 437 |         Py_UNICODE_COPY(w->str, v->str, | 
 | 438 |                         length < v->length ? length : v->length); | 
| Serhiy Storchaka | 763a61c | 2016-04-10 18:05:12 +0300 | [diff] [blame] | 439 |         Py_SETREF(*unicode, w); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 440 |         return 0; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 441 |     } | 
 | 442 |  | 
 | 443 |     /* Note that we don't have to modify *unicode for unshared Unicode | 
 | 444 |        objects, since we can modify them in-place. */ | 
 | 445 |     return unicode_resize(v, length); | 
 | 446 | } | 
 | 447 |  | 
| Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 448 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) | 
 | 449 | { | 
 | 450 |     return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); | 
 | 451 | } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 452 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 453 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 454 |                                 Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 455 | { | 
 | 456 |     PyUnicodeObject *unicode; | 
 | 457 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 458 |     /* If the Unicode data is known at construction time, we can apply | 
 | 459 |        some optimizations which share commonly used objects. */ | 
 | 460 |     if (u != NULL) { | 
 | 461 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 462 |         /* Optimization for empty strings */ | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 463 |         if (size == 0) | 
 | 464 |             _Py_RETURN_UNICODE_EMPTY(); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 465 |  | 
 | 466 |         /* Single character Unicode objects in the Latin-1 range are | 
 | 467 |            shared when using this constructor */ | 
 | 468 |         if (size == 1 && *u < 256) { | 
 | 469 |             unicode = unicode_latin1[*u]; | 
 | 470 |             if (!unicode) { | 
 | 471 |                 unicode = _PyUnicode_New(1); | 
 | 472 |                 if (!unicode) | 
 | 473 |                     return NULL; | 
 | 474 |                 unicode->str[0] = *u; | 
 | 475 |                 unicode_latin1[*u] = unicode; | 
 | 476 |             } | 
 | 477 |             Py_INCREF(unicode); | 
 | 478 |             return (PyObject *)unicode; | 
 | 479 |         } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 480 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 481 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 482 |     unicode = _PyUnicode_New(size); | 
 | 483 |     if (!unicode) | 
 | 484 |         return NULL; | 
 | 485 |  | 
 | 486 |     /* Copy the Unicode data into the new object */ | 
 | 487 |     if (u != NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 488 |         Py_UNICODE_COPY(unicode->str, u, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 489 |  | 
 | 490 |     return (PyObject *)unicode; | 
 | 491 | } | 
 | 492 |  | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 493 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) | 
 | 494 | { | 
 | 495 |     PyUnicodeObject *unicode; | 
| Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 496 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 497 |     if (size < 0) { | 
 | 498 |         PyErr_SetString(PyExc_SystemError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 499 |                         "Negative size passed to PyUnicode_FromStringAndSize"); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 500 |         return NULL; | 
 | 501 |     } | 
| Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 502 |  | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 503 |     /* If the Unicode data is known at construction time, we can apply | 
 | 504 |        some optimizations which share commonly used objects. | 
 | 505 |        Also, this means the input must be UTF-8, so fall back to the | 
 | 506 |        UTF-8 decoder at the end. */ | 
 | 507 |     if (u != NULL) { | 
 | 508 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 509 |         /* Optimization for empty strings */ | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 510 |         if (size == 0) | 
 | 511 |             _Py_RETURN_UNICODE_EMPTY(); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 512 |  | 
 | 513 |         /* Single characters are shared when using this constructor. | 
 | 514 |            Restrict to ASCII, since the input must be UTF-8. */ | 
 | 515 |         if (size == 1 && Py_CHARMASK(*u) < 128) { | 
 | 516 |             unicode = unicode_latin1[Py_CHARMASK(*u)]; | 
 | 517 |             if (!unicode) { | 
 | 518 |                 unicode = _PyUnicode_New(1); | 
 | 519 |                 if (!unicode) | 
 | 520 |                     return NULL; | 
 | 521 |                 unicode->str[0] = Py_CHARMASK(*u); | 
 | 522 |                 unicode_latin1[Py_CHARMASK(*u)] = unicode; | 
 | 523 |             } | 
 | 524 |             Py_INCREF(unicode); | 
 | 525 |             return (PyObject *)unicode; | 
 | 526 |         } | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 527 |  | 
 | 528 |         return PyUnicode_DecodeUTF8(u, size, NULL); | 
 | 529 |     } | 
 | 530 |  | 
 | 531 |     unicode = _PyUnicode_New(size); | 
 | 532 |     if (!unicode) | 
 | 533 |         return NULL; | 
 | 534 |  | 
 | 535 |     return (PyObject *)unicode; | 
 | 536 | } | 
 | 537 |  | 
 | 538 | PyObject *PyUnicode_FromString(const char *u) | 
 | 539 | { | 
 | 540 |     size_t size = strlen(u); | 
 | 541 |     if (size > PY_SSIZE_T_MAX) { | 
 | 542 |         PyErr_SetString(PyExc_OverflowError, "input too long"); | 
 | 543 |         return NULL; | 
 | 544 |     } | 
 | 545 |  | 
 | 546 |     return PyUnicode_FromStringAndSize(u, size); | 
 | 547 | } | 
 | 548 |  | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 549 | /* _Py_UNICODE_NEXT is a private macro used to retrieve the character pointed | 
 | 550 |  * by 'ptr', possibly combining surrogate pairs on narrow builds. | 
 | 551 |  * 'ptr' and 'end' must be Py_UNICODE*, with 'ptr' pointing at the character | 
 | 552 |  * that should be returned and 'end' pointing to the end of the buffer. | 
 | 553 |  * ('end' is used on narrow builds to detect a lone surrogate at the | 
 | 554 |  * end of the buffer that should be returned unchanged.) | 
 | 555 |  * The ptr and end arguments should be side-effect free and ptr must an lvalue. | 
 | 556 |  * The type of the returned char is always Py_UCS4. | 
 | 557 |  * | 
 | 558 |  * Note: the macro advances ptr to next char, so it might have side-effects | 
 | 559 |  *       (especially if used with other macros). | 
 | 560 |  */ | 
 | 561 |  | 
 | 562 | /* helper macros used by _Py_UNICODE_NEXT */ | 
 | 563 | #define _Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= ch && ch <= 0xDBFF) | 
 | 564 | #define _Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= ch && ch <= 0xDFFF) | 
 | 565 | /* Join two surrogate characters and return a single Py_UCS4 value. */ | 
 | 566 | #define _Py_UNICODE_JOIN_SURROGATES(high, low)  \ | 
 | 567 |     (((((Py_UCS4)(high) & 0x03FF) << 10) |      \ | 
 | 568 |       ((Py_UCS4)(low) & 0x03FF)) + 0x10000) | 
 | 569 |  | 
 | 570 | #ifdef Py_UNICODE_WIDE | 
 | 571 | #define _Py_UNICODE_NEXT(ptr, end) *(ptr)++ | 
 | 572 | #else | 
 | 573 | #define _Py_UNICODE_NEXT(ptr, end)                                      \ | 
 | 574 |      (((_Py_UNICODE_IS_HIGH_SURROGATE(*(ptr)) && (ptr) < (end)) &&      \ | 
 | 575 |         _Py_UNICODE_IS_LOW_SURROGATE((ptr)[1])) ?                       \ | 
 | 576 |        ((ptr) += 2,_Py_UNICODE_JOIN_SURROGATES((ptr)[-2], (ptr)[-1])) : \ | 
 | 577 |        (Py_UCS4)*(ptr)++) | 
 | 578 | #endif | 
 | 579 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 580 | #ifdef HAVE_WCHAR_H | 
 | 581 |  | 
| Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 582 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) | 
 | 583 | # define CONVERT_WCHAR_TO_SURROGATES | 
 | 584 | #endif | 
 | 585 |  | 
 | 586 | #ifdef CONVERT_WCHAR_TO_SURROGATES | 
 | 587 |  | 
 | 588 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need | 
 | 589 |    to convert from UTF32 to UTF16. */ | 
 | 590 |  | 
 | 591 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, | 
 | 592 |                                  Py_ssize_t size) | 
 | 593 | { | 
 | 594 |     PyUnicodeObject *unicode; | 
 | 595 |     register Py_ssize_t i; | 
 | 596 |     Py_ssize_t alloc; | 
 | 597 |     const wchar_t *orig_w; | 
 | 598 |  | 
 | 599 |     if (w == NULL) { | 
 | 600 |         PyErr_BadInternalCall(); | 
 | 601 |         return NULL; | 
 | 602 |     } | 
 | 603 |  | 
 | 604 |     alloc = size; | 
 | 605 |     orig_w = w; | 
 | 606 |     for (i = size; i > 0; i--) { | 
 | 607 |         if (*w > 0xFFFF) | 
 | 608 |             alloc++; | 
 | 609 |         w++; | 
 | 610 |     } | 
 | 611 |     w = orig_w; | 
 | 612 |     unicode = _PyUnicode_New(alloc); | 
 | 613 |     if (!unicode) | 
 | 614 |         return NULL; | 
 | 615 |  | 
 | 616 |     /* Copy the wchar_t data into the new object */ | 
 | 617 |     { | 
 | 618 |         register Py_UNICODE *u; | 
 | 619 |         u = PyUnicode_AS_UNICODE(unicode); | 
 | 620 |         for (i = size; i > 0; i--) { | 
 | 621 |             if (*w > 0xFFFF) { | 
 | 622 |                 wchar_t ordinal = *w++; | 
 | 623 |                 ordinal -= 0x10000; | 
 | 624 |                 *u++ = 0xD800 | (ordinal >> 10); | 
 | 625 |                 *u++ = 0xDC00 | (ordinal & 0x3FF); | 
 | 626 |             } | 
 | 627 |             else | 
 | 628 |                 *u++ = *w++; | 
 | 629 |         } | 
 | 630 |     } | 
 | 631 |     return (PyObject *)unicode; | 
 | 632 | } | 
 | 633 |  | 
 | 634 | #else | 
 | 635 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 636 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 637 |                                  Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 638 | { | 
 | 639 |     PyUnicodeObject *unicode; | 
 | 640 |  | 
 | 641 |     if (w == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 642 |         PyErr_BadInternalCall(); | 
 | 643 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 644 |     } | 
 | 645 |  | 
 | 646 |     unicode = _PyUnicode_New(size); | 
 | 647 |     if (!unicode) | 
 | 648 |         return NULL; | 
 | 649 |  | 
 | 650 |     /* Copy the wchar_t data into the new object */ | 
 | 651 | #ifdef HAVE_USABLE_WCHAR_T | 
 | 652 |     memcpy(unicode->str, w, size * sizeof(wchar_t)); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 653 | #else | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 654 |     { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 655 |         register Py_UNICODE *u; | 
 | 656 |         register Py_ssize_t i; | 
 | 657 |         u = PyUnicode_AS_UNICODE(unicode); | 
 | 658 |         for (i = size; i > 0; i--) | 
 | 659 |             *u++ = *w++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 660 |     } | 
 | 661 | #endif | 
 | 662 |  | 
 | 663 |     return (PyObject *)unicode; | 
 | 664 | } | 
 | 665 |  | 
| Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 666 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ | 
 | 667 |  | 
 | 668 | #undef CONVERT_WCHAR_TO_SURROGATES | 
 | 669 |  | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 670 | static void | 
 | 671 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) | 
 | 672 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 673 |     *fmt++ = '%'; | 
 | 674 |     if (width) { | 
 | 675 |         if (zeropad) | 
 | 676 |             *fmt++ = '0'; | 
 | 677 |         fmt += sprintf(fmt, "%d", width); | 
 | 678 |     } | 
 | 679 |     if (precision) | 
 | 680 |         fmt += sprintf(fmt, ".%d", precision); | 
 | 681 |     if (longflag) | 
 | 682 |         *fmt++ = 'l'; | 
 | 683 |     else if (size_tflag) { | 
 | 684 |         char *f = PY_FORMAT_SIZE_T; | 
 | 685 |         while (*f) | 
 | 686 |             *fmt++ = *f++; | 
 | 687 |     } | 
 | 688 |     *fmt++ = c; | 
 | 689 |     *fmt = '\0'; | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 690 | } | 
 | 691 |  | 
| Victor Stinner | 2af8d2f | 2014-07-30 00:39:05 +0200 | [diff] [blame] | 692 | #define appendstring(string) \ | 
 | 693 |     do { \ | 
 | 694 |         for (copy = string;*copy; copy++) { \ | 
 | 695 |             *s++ = (unsigned char)*copy; \ | 
 | 696 |         } \ | 
 | 697 |     } while (0) | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 698 |  | 
 | 699 | PyObject * | 
 | 700 | PyUnicode_FromFormatV(const char *format, va_list vargs) | 
 | 701 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 702 |     va_list count; | 
 | 703 |     Py_ssize_t callcount = 0; | 
 | 704 |     PyObject **callresults = NULL; | 
 | 705 |     PyObject **callresult = NULL; | 
 | 706 |     Py_ssize_t n = 0; | 
 | 707 |     int width = 0; | 
 | 708 |     int precision = 0; | 
 | 709 |     int zeropad; | 
 | 710 |     const char* f; | 
 | 711 |     Py_UNICODE *s; | 
 | 712 |     PyObject *string; | 
 | 713 |     /* used by sprintf */ | 
 | 714 |     char buffer[21]; | 
 | 715 |     /* use abuffer instead of buffer, if we need more space | 
 | 716 |      * (which can happen if there's a format specifier with width). */ | 
 | 717 |     char *abuffer = NULL; | 
 | 718 |     char *realbuffer; | 
 | 719 |     Py_ssize_t abuffersize = 0; | 
 | 720 |     char fmt[60]; /* should be enough for %0width.precisionld */ | 
 | 721 |     const char *copy; | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 722 |  | 
 | 723 | #ifdef VA_LIST_IS_ARRAY | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 724 |     Py_MEMCPY(count, vargs, sizeof(va_list)); | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 725 | #else | 
 | 726 | #ifdef  __va_copy | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 727 |     __va_copy(count, vargs); | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 728 | #else | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 729 |     count = vargs; | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 730 | #endif | 
 | 731 | #endif | 
| Walter Dörwald | ed960ac | 2009-05-03 22:36:33 +0000 | [diff] [blame] | 732 |      /* step 1: count the number of %S/%R/%s format specifications | 
 | 733 |       * (we call PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() for these | 
 | 734 |       * objects once during step 3 and put the result in an array) */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 735 |     for (f = format; *f; f++) { | 
| Walter Dörwald | ed960ac | 2009-05-03 22:36:33 +0000 | [diff] [blame] | 736 |          if (*f == '%') { | 
| Serhiy Storchaka | 0e0282e | 2015-01-27 22:17:56 +0200 | [diff] [blame] | 737 |              f++; | 
 | 738 |              while (*f && *f != '%' && !isalpha((unsigned)*f)) | 
 | 739 |                  f++; | 
| Serhiy Storchaka | 227526d | 2015-01-31 01:15:29 +0200 | [diff] [blame] | 740 |              if (!*f) | 
 | 741 |                  break; | 
| Serhiy Storchaka | 0e0282e | 2015-01-27 22:17:56 +0200 | [diff] [blame] | 742 |              if (*f == 's' || *f=='S' || *f=='R') | 
| Walter Dörwald | ed960ac | 2009-05-03 22:36:33 +0000 | [diff] [blame] | 743 |                  ++callcount; | 
 | 744 |          } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 745 |     } | 
 | 746 |     /* step 2: allocate memory for the results of | 
| Walter Dörwald | ed960ac | 2009-05-03 22:36:33 +0000 | [diff] [blame] | 747 |      * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 748 |     if (callcount) { | 
 | 749 |         callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); | 
 | 750 |         if (!callresults) { | 
 | 751 |             PyErr_NoMemory(); | 
 | 752 |             return NULL; | 
 | 753 |         } | 
 | 754 |         callresult = callresults; | 
 | 755 |     } | 
 | 756 |     /* step 3: figure out how large a buffer we need */ | 
 | 757 |     for (f = format; *f; f++) { | 
 | 758 |         if (*f == '%') { | 
| Serhiy Storchaka | 0e0282e | 2015-01-27 22:17:56 +0200 | [diff] [blame] | 759 |             const char* p = f++; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 760 |             width = 0; | 
 | 761 |             while (isdigit((unsigned)*f)) | 
 | 762 |                 width = (width*10) + *f++ - '0'; | 
| Serhiy Storchaka | 0e0282e | 2015-01-27 22:17:56 +0200 | [diff] [blame] | 763 |             precision = 0; | 
 | 764 |             if (*f == '.') { | 
 | 765 |                 f++; | 
 | 766 |                 while (isdigit((unsigned)*f)) | 
 | 767 |                     precision = (precision*10) + *f++ - '0'; | 
 | 768 |             } | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 769 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 770 |             /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since | 
 | 771 |              * they don't affect the amount of space we reserve. | 
 | 772 |              */ | 
 | 773 |             if ((*f == 'l' || *f == 'z') && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 774 |                 (f[1] == 'd' || f[1] == 'u')) | 
 | 775 |                 ++f; | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 776 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 777 |             switch (*f) { | 
 | 778 |             case 'c': | 
| Serhiy Storchaka | ba908c7 | 2013-06-23 20:22:09 +0300 | [diff] [blame] | 779 |             { | 
 | 780 |                 int ordinal = va_arg(count, int); | 
 | 781 | #ifdef Py_UNICODE_WIDE | 
 | 782 |                 if (ordinal < 0 || ordinal > 0x10ffff) { | 
 | 783 |                     PyErr_SetString(PyExc_OverflowError, | 
 | 784 |                                     "%c arg not in range(0x110000) " | 
 | 785 |                                     "(wide Python build)"); | 
 | 786 |                     goto fail; | 
 | 787 |                 } | 
 | 788 | #else | 
 | 789 |                 if (ordinal < 0 || ordinal > 0xffff) { | 
 | 790 |                     PyErr_SetString(PyExc_OverflowError, | 
 | 791 |                                     "%c arg not in range(0x10000) " | 
 | 792 |                                     "(narrow Python build)"); | 
 | 793 |                     goto fail; | 
 | 794 |                 } | 
 | 795 | #endif | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 796 |                 /* fall through... */ | 
| Serhiy Storchaka | ba908c7 | 2013-06-23 20:22:09 +0300 | [diff] [blame] | 797 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 798 |             case '%': | 
 | 799 |                 n++; | 
 | 800 |                 break; | 
 | 801 |             case 'd': case 'u': case 'i': case 'x': | 
 | 802 |                 (void) va_arg(count, int); | 
| Serhiy Storchaka | 0e0282e | 2015-01-27 22:17:56 +0200 | [diff] [blame] | 803 |                 if (width < precision) | 
 | 804 |                     width = precision; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 805 |                 /* 20 bytes is enough to hold a 64-bit | 
 | 806 |                    integer.  Decimal takes the most space. | 
 | 807 |                    This isn't enough for octal. | 
 | 808 |                    If a width is specified we need more | 
 | 809 |                    (which we allocate later). */ | 
 | 810 |                 if (width < 20) | 
 | 811 |                     width = 20; | 
 | 812 |                 n += width; | 
 | 813 |                 if (abuffersize < width) | 
 | 814 |                     abuffersize = width; | 
 | 815 |                 break; | 
 | 816 |             case 's': | 
 | 817 |             { | 
 | 818 |                 /* UTF-8 */ | 
| Georg Brandl | ba68a99 | 2009-05-05 09:19:43 +0000 | [diff] [blame] | 819 |                 const char *s = va_arg(count, const char*); | 
| Walter Dörwald | ed960ac | 2009-05-03 22:36:33 +0000 | [diff] [blame] | 820 |                 PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); | 
 | 821 |                 if (!str) | 
 | 822 |                     goto fail; | 
 | 823 |                 n += PyUnicode_GET_SIZE(str); | 
 | 824 |                 /* Remember the str and switch to the next slot */ | 
 | 825 |                 *callresult++ = str; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 826 |                 break; | 
 | 827 |             } | 
 | 828 |             case 'U': | 
 | 829 |             { | 
 | 830 |                 PyObject *obj = va_arg(count, PyObject *); | 
 | 831 |                 assert(obj && PyUnicode_Check(obj)); | 
 | 832 |                 n += PyUnicode_GET_SIZE(obj); | 
 | 833 |                 break; | 
 | 834 |             } | 
 | 835 |             case 'V': | 
 | 836 |             { | 
 | 837 |                 PyObject *obj = va_arg(count, PyObject *); | 
 | 838 |                 const char *str = va_arg(count, const char *); | 
 | 839 |                 assert(obj || str); | 
 | 840 |                 assert(!obj || PyUnicode_Check(obj)); | 
 | 841 |                 if (obj) | 
 | 842 |                     n += PyUnicode_GET_SIZE(obj); | 
 | 843 |                 else | 
 | 844 |                     n += strlen(str); | 
 | 845 |                 break; | 
 | 846 |             } | 
 | 847 |             case 'S': | 
 | 848 |             { | 
 | 849 |                 PyObject *obj = va_arg(count, PyObject *); | 
 | 850 |                 PyObject *str; | 
 | 851 |                 assert(obj); | 
 | 852 |                 str = PyObject_Str(obj); | 
 | 853 |                 if (!str) | 
 | 854 |                     goto fail; | 
| Victor Stinner | 2af8d2f | 2014-07-30 00:39:05 +0200 | [diff] [blame] | 855 |                 n += PyString_GET_SIZE(str); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 856 |                 /* Remember the str and switch to the next slot */ | 
 | 857 |                 *callresult++ = str; | 
 | 858 |                 break; | 
 | 859 |             } | 
 | 860 |             case 'R': | 
 | 861 |             { | 
 | 862 |                 PyObject *obj = va_arg(count, PyObject *); | 
 | 863 |                 PyObject *repr; | 
 | 864 |                 assert(obj); | 
 | 865 |                 repr = PyObject_Repr(obj); | 
 | 866 |                 if (!repr) | 
 | 867 |                     goto fail; | 
 | 868 |                 n += PyUnicode_GET_SIZE(repr); | 
 | 869 |                 /* Remember the repr and switch to the next slot */ | 
 | 870 |                 *callresult++ = repr; | 
 | 871 |                 break; | 
 | 872 |             } | 
 | 873 |             case 'p': | 
 | 874 |                 (void) va_arg(count, int); | 
 | 875 |                 /* maximum 64-bit pointer representation: | 
 | 876 |                  * 0xffffffffffffffff | 
 | 877 |                  * so 19 characters is enough. | 
 | 878 |                  * XXX I count 18 -- what's the extra for? | 
 | 879 |                  */ | 
 | 880 |                 n += 19; | 
 | 881 |                 break; | 
 | 882 |             default: | 
 | 883 |                 /* if we stumble upon an unknown | 
 | 884 |                    formatting code, copy the rest of | 
 | 885 |                    the format string to the output | 
 | 886 |                    string. (we cannot just skip the | 
 | 887 |                    code, since there's no way to know | 
 | 888 |                    what's in the argument list) */ | 
 | 889 |                 n += strlen(p); | 
 | 890 |                 goto expand; | 
 | 891 |             } | 
 | 892 |         } else | 
 | 893 |             n++; | 
 | 894 |     } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 895 |   expand: | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 896 |     if (abuffersize > 20) { | 
| Serhiy Storchaka | 5ec0bbf | 2015-01-30 23:35:03 +0200 | [diff] [blame] | 897 |         /* add 1 for sprintf's trailing null byte */ | 
 | 898 |         abuffer = PyObject_Malloc(abuffersize + 1); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 899 |         if (!abuffer) { | 
 | 900 |             PyErr_NoMemory(); | 
 | 901 |             goto fail; | 
 | 902 |         } | 
 | 903 |         realbuffer = abuffer; | 
 | 904 |     } | 
 | 905 |     else | 
 | 906 |         realbuffer = buffer; | 
 | 907 |     /* step 4: fill the buffer */ | 
 | 908 |     /* Since we've analyzed how much space we need for the worst case, | 
 | 909 |        we don't have to resize the string. | 
 | 910 |        There can be no errors beyond this point. */ | 
 | 911 |     string = PyUnicode_FromUnicode(NULL, n); | 
 | 912 |     if (!string) | 
 | 913 |         goto fail; | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 914 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 915 |     s = PyUnicode_AS_UNICODE(string); | 
 | 916 |     callresult = callresults; | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 917 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 918 |     for (f = format; *f; f++) { | 
 | 919 |         if (*f == '%') { | 
 | 920 |             const char* p = f++; | 
 | 921 |             int longflag = 0; | 
 | 922 |             int size_tflag = 0; | 
 | 923 |             zeropad = (*f == '0'); | 
 | 924 |             /* parse the width.precision part */ | 
 | 925 |             width = 0; | 
 | 926 |             while (isdigit((unsigned)*f)) | 
 | 927 |                 width = (width*10) + *f++ - '0'; | 
 | 928 |             precision = 0; | 
 | 929 |             if (*f == '.') { | 
 | 930 |                 f++; | 
 | 931 |                 while (isdigit((unsigned)*f)) | 
 | 932 |                     precision = (precision*10) + *f++ - '0'; | 
 | 933 |             } | 
 | 934 |             /* handle the long flag, but only for %ld and %lu. | 
 | 935 |                others can be added when necessary. */ | 
 | 936 |             if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { | 
 | 937 |                 longflag = 1; | 
 | 938 |                 ++f; | 
 | 939 |             } | 
 | 940 |             /* handle the size_t flag. */ | 
 | 941 |             if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { | 
 | 942 |                 size_tflag = 1; | 
 | 943 |                 ++f; | 
 | 944 |             } | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 945 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 946 |             switch (*f) { | 
 | 947 |             case 'c': | 
 | 948 |                 *s++ = va_arg(vargs, int); | 
 | 949 |                 break; | 
 | 950 |             case 'd': | 
 | 951 |                 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); | 
 | 952 |                 if (longflag) | 
 | 953 |                     sprintf(realbuffer, fmt, va_arg(vargs, long)); | 
 | 954 |                 else if (size_tflag) | 
 | 955 |                     sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); | 
 | 956 |                 else | 
 | 957 |                     sprintf(realbuffer, fmt, va_arg(vargs, int)); | 
 | 958 |                 appendstring(realbuffer); | 
 | 959 |                 break; | 
 | 960 |             case 'u': | 
 | 961 |                 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); | 
 | 962 |                 if (longflag) | 
 | 963 |                     sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); | 
 | 964 |                 else if (size_tflag) | 
 | 965 |                     sprintf(realbuffer, fmt, va_arg(vargs, size_t)); | 
 | 966 |                 else | 
 | 967 |                     sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); | 
 | 968 |                 appendstring(realbuffer); | 
 | 969 |                 break; | 
 | 970 |             case 'i': | 
 | 971 |                 makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); | 
 | 972 |                 sprintf(realbuffer, fmt, va_arg(vargs, int)); | 
 | 973 |                 appendstring(realbuffer); | 
 | 974 |                 break; | 
 | 975 |             case 'x': | 
 | 976 |                 makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); | 
 | 977 |                 sprintf(realbuffer, fmt, va_arg(vargs, int)); | 
 | 978 |                 appendstring(realbuffer); | 
 | 979 |                 break; | 
 | 980 |             case 's': | 
 | 981 |             { | 
| Walter Dörwald | ed960ac | 2009-05-03 22:36:33 +0000 | [diff] [blame] | 982 |                 /* unused, since we already have the result */ | 
 | 983 |                 (void) va_arg(vargs, char *); | 
 | 984 |                 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), | 
 | 985 |                                 PyUnicode_GET_SIZE(*callresult)); | 
 | 986 |                 s += PyUnicode_GET_SIZE(*callresult); | 
 | 987 |                 /* We're done with the unicode()/repr() => forget it */ | 
 | 988 |                 Py_DECREF(*callresult); | 
 | 989 |                 /* switch to next unicode()/repr() result */ | 
 | 990 |                 ++callresult; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 991 |                 break; | 
 | 992 |             } | 
 | 993 |             case 'U': | 
 | 994 |             { | 
 | 995 |                 PyObject *obj = va_arg(vargs, PyObject *); | 
 | 996 |                 Py_ssize_t size = PyUnicode_GET_SIZE(obj); | 
 | 997 |                 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); | 
 | 998 |                 s += size; | 
 | 999 |                 break; | 
 | 1000 |             } | 
 | 1001 |             case 'V': | 
 | 1002 |             { | 
 | 1003 |                 PyObject *obj = va_arg(vargs, PyObject *); | 
 | 1004 |                 const char *str = va_arg(vargs, const char *); | 
 | 1005 |                 if (obj) { | 
 | 1006 |                     Py_ssize_t size = PyUnicode_GET_SIZE(obj); | 
 | 1007 |                     Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); | 
 | 1008 |                     s += size; | 
 | 1009 |                 } else { | 
 | 1010 |                     appendstring(str); | 
 | 1011 |                 } | 
 | 1012 |                 break; | 
 | 1013 |             } | 
 | 1014 |             case 'S': | 
 | 1015 |             case 'R': | 
 | 1016 |             { | 
| Victor Stinner | 2af8d2f | 2014-07-30 00:39:05 +0200 | [diff] [blame] | 1017 |                 const char *str = PyString_AS_STRING(*callresult); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1018 |                 /* unused, since we already have the result */ | 
 | 1019 |                 (void) va_arg(vargs, PyObject *); | 
| Victor Stinner | 2af8d2f | 2014-07-30 00:39:05 +0200 | [diff] [blame] | 1020 |                 appendstring(str); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1021 |                 /* We're done with the unicode()/repr() => forget it */ | 
 | 1022 |                 Py_DECREF(*callresult); | 
 | 1023 |                 /* switch to next unicode()/repr() result */ | 
 | 1024 |                 ++callresult; | 
 | 1025 |                 break; | 
 | 1026 |             } | 
 | 1027 |             case 'p': | 
 | 1028 |                 sprintf(buffer, "%p", va_arg(vargs, void*)); | 
 | 1029 |                 /* %p is ill-defined:  ensure leading 0x. */ | 
 | 1030 |                 if (buffer[1] == 'X') | 
 | 1031 |                     buffer[1] = 'x'; | 
 | 1032 |                 else if (buffer[1] != 'x') { | 
 | 1033 |                     memmove(buffer+2, buffer, strlen(buffer)+1); | 
 | 1034 |                     buffer[0] = '0'; | 
 | 1035 |                     buffer[1] = 'x'; | 
 | 1036 |                 } | 
 | 1037 |                 appendstring(buffer); | 
 | 1038 |                 break; | 
 | 1039 |             case '%': | 
 | 1040 |                 *s++ = '%'; | 
 | 1041 |                 break; | 
 | 1042 |             default: | 
 | 1043 |                 appendstring(p); | 
 | 1044 |                 goto end; | 
 | 1045 |             } | 
 | 1046 |         } else | 
 | 1047 |             *s++ = *f; | 
 | 1048 |     } | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1049 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1050 |   end: | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1051 |     if (callresults) | 
 | 1052 |         PyObject_Free(callresults); | 
 | 1053 |     if (abuffer) | 
 | 1054 |         PyObject_Free(abuffer); | 
 | 1055 |     PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); | 
 | 1056 |     return string; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1057 |   fail: | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1058 |     if (callresults) { | 
 | 1059 |         PyObject **callresult2 = callresults; | 
 | 1060 |         while (callresult2 < callresult) { | 
 | 1061 |             Py_DECREF(*callresult2); | 
 | 1062 |             ++callresult2; | 
 | 1063 |         } | 
 | 1064 |         PyObject_Free(callresults); | 
 | 1065 |     } | 
 | 1066 |     if (abuffer) | 
 | 1067 |         PyObject_Free(abuffer); | 
 | 1068 |     return NULL; | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1069 | } | 
 | 1070 |  | 
 | 1071 | #undef appendstring | 
 | 1072 |  | 
 | 1073 | PyObject * | 
 | 1074 | PyUnicode_FromFormat(const char *format, ...) | 
 | 1075 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1076 |     PyObject* ret; | 
 | 1077 |     va_list vargs; | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1078 |  | 
 | 1079 | #ifdef HAVE_STDARG_PROTOTYPES | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1080 |     va_start(vargs, format); | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1081 | #else | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1082 |     va_start(vargs); | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1083 | #endif | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1084 |     ret = PyUnicode_FromFormatV(format, vargs); | 
 | 1085 |     va_end(vargs); | 
 | 1086 |     return ret; | 
| Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 1087 | } | 
 | 1088 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1089 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1090 |                                 wchar_t *w, | 
 | 1091 |                                 Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1092 | { | 
 | 1093 |     if (unicode == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1094 |         PyErr_BadInternalCall(); | 
 | 1095 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1096 |     } | 
| Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1097 |  | 
 | 1098 |     /* If possible, try to copy the 0-termination as well */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1099 |     if (size > PyUnicode_GET_SIZE(unicode)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1100 |         size = PyUnicode_GET_SIZE(unicode) + 1; | 
| Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1101 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1102 | #ifdef HAVE_USABLE_WCHAR_T | 
 | 1103 |     memcpy(w, unicode->str, size * sizeof(wchar_t)); | 
 | 1104 | #else | 
 | 1105 |     { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1106 |         register Py_UNICODE *u; | 
 | 1107 |         register Py_ssize_t i; | 
 | 1108 |         u = PyUnicode_AS_UNICODE(unicode); | 
 | 1109 |         for (i = size; i > 0; i--) | 
 | 1110 |             *w++ = *u++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1111 |     } | 
 | 1112 | #endif | 
 | 1113 |  | 
| Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1114 |     if (size > PyUnicode_GET_SIZE(unicode)) | 
 | 1115 |         return PyUnicode_GET_SIZE(unicode); | 
 | 1116 |     else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1117 |         return size; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1118 | } | 
 | 1119 |  | 
 | 1120 | #endif | 
 | 1121 |  | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1122 | PyObject *PyUnicode_FromOrdinal(int ordinal) | 
 | 1123 | { | 
| Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1124 |     Py_UNICODE s[1]; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1125 |  | 
 | 1126 | #ifdef Py_UNICODE_WIDE | 
 | 1127 |     if (ordinal < 0 || ordinal > 0x10ffff) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1128 |         PyErr_SetString(PyExc_ValueError, | 
 | 1129 |                         "unichr() arg not in range(0x110000) " | 
 | 1130 |                         "(wide Python build)"); | 
 | 1131 |         return NULL; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1132 |     } | 
 | 1133 | #else | 
 | 1134 |     if (ordinal < 0 || ordinal > 0xffff) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1135 |         PyErr_SetString(PyExc_ValueError, | 
 | 1136 |                         "unichr() arg not in range(0x10000) " | 
 | 1137 |                         "(narrow Python build)"); | 
 | 1138 |         return NULL; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1139 |     } | 
 | 1140 | #endif | 
 | 1141 |  | 
| Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1142 |     s[0] = (Py_UNICODE)ordinal; | 
 | 1143 |     return PyUnicode_FromUnicode(s, 1); | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1144 | } | 
 | 1145 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1146 | PyObject *PyUnicode_FromObject(register PyObject *obj) | 
 | 1147 | { | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1148 |     /* XXX Perhaps we should make this API an alias of | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1149 |        PyObject_Unicode() instead ?! */ | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1150 |     if (PyUnicode_CheckExact(obj)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1151 |         Py_INCREF(obj); | 
 | 1152 |         return obj; | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1153 |     } | 
 | 1154 |     if (PyUnicode_Check(obj)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1155 |         /* For a Unicode subtype that's not a Unicode object, | 
 | 1156 |            return a true Unicode object with the same data. */ | 
 | 1157 |         return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), | 
 | 1158 |                                      PyUnicode_GET_SIZE(obj)); | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1159 |     } | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1160 |     return PyUnicode_FromEncodedObject(obj, NULL, "strict"); | 
 | 1161 | } | 
 | 1162 |  | 
 | 1163 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1164 |                                       const char *encoding, | 
 | 1165 |                                       const char *errors) | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1166 | { | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1167 |     const char *s = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1168 |     Py_ssize_t len; | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1169 |     PyObject *v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1170 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1171 |     if (obj == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1172 |         PyErr_BadInternalCall(); | 
 | 1173 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1174 |     } | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1175 |  | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1176 | #if 0 | 
 | 1177 |     /* For b/w compatibility we also accept Unicode objects provided | 
| Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1178 |        that no encodings is given and then redirect to | 
 | 1179 |        PyObject_Unicode() which then applies the additional logic for | 
 | 1180 |        Unicode subclasses. | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1181 |  | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1182 |        NOTE: This API should really only be used for object which | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1183 |        represent *encoded* Unicode ! | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1184 |  | 
 | 1185 |     */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1186 |     if (PyUnicode_Check(obj)) { | 
 | 1187 |         if (encoding) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1188 |             PyErr_SetString(PyExc_TypeError, | 
 | 1189 |                             "decoding Unicode is not supported"); | 
 | 1190 |             return NULL; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1191 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1192 |         return PyObject_Unicode(obj); | 
 | 1193 |     } | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1194 | #else | 
 | 1195 |     if (PyUnicode_Check(obj)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1196 |         PyErr_SetString(PyExc_TypeError, | 
 | 1197 |                         "decoding Unicode is not supported"); | 
 | 1198 |         return NULL; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1199 |     } | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1200 | #endif | 
 | 1201 |  | 
 | 1202 |     /* Coerce object */ | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1203 |     if (PyString_Check(obj)) { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1204 |         s = PyString_AS_STRING(obj); | 
 | 1205 |         len = PyString_GET_SIZE(obj); | 
| Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1206 |     } | 
| Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 1207 |     else if (PyByteArray_Check(obj)) { | 
| Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1208 |         /* Python 2.x specific */ | 
 | 1209 |         PyErr_Format(PyExc_TypeError, | 
 | 1210 |                      "decoding bytearray is not supported"); | 
 | 1211 |         return NULL; | 
 | 1212 |     } | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1213 |     else if (PyObject_AsCharBuffer(obj, &s, &len)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1214 |         /* Overwrite the error message with something more useful in | 
 | 1215 |            case of a TypeError. */ | 
 | 1216 |         if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 1217 |             PyErr_Format(PyExc_TypeError, | 
 | 1218 |                          "coercing to Unicode: need string or buffer, " | 
 | 1219 |                          "%.80s found", | 
 | 1220 |                          Py_TYPE(obj)->tp_name); | 
 | 1221 |         goto onError; | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1222 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1223 |  | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1224 |     /* Convert to Unicode */ | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 1225 |     if (len == 0) | 
 | 1226 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1227 |  | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 1228 |     v = PyUnicode_Decode(s, len, encoding, errors); | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1229 |     return v; | 
 | 1230 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1231 |   onError: | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1232 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1233 | } | 
 | 1234 |  | 
 | 1235 | PyObject *PyUnicode_Decode(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1236 |                            Py_ssize_t size, | 
 | 1237 |                            const char *encoding, | 
 | 1238 |                            const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1239 | { | 
 | 1240 |     PyObject *buffer = NULL, *unicode; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1241 |  | 
 | 1242 |     if (encoding == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1243 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1244 |  | 
 | 1245 |     /* Shortcuts for common default encodings */ | 
 | 1246 |     if (strcmp(encoding, "utf-8") == 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1247 |         return PyUnicode_DecodeUTF8(s, size, errors); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1248 |     else if (strcmp(encoding, "latin-1") == 0) | 
 | 1249 |         return PyUnicode_DecodeLatin1(s, size, errors); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1250 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) | 
 | 1251 |     else if (strcmp(encoding, "mbcs") == 0) | 
 | 1252 |         return PyUnicode_DecodeMBCS(s, size, errors); | 
 | 1253 | #endif | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1254 |     else if (strcmp(encoding, "ascii") == 0) | 
 | 1255 |         return PyUnicode_DecodeASCII(s, size, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1256 |  | 
 | 1257 |     /* Decode via the codec registry */ | 
 | 1258 |     buffer = PyBuffer_FromMemory((void *)s, size); | 
 | 1259 |     if (buffer == NULL) | 
 | 1260 |         goto onError; | 
| Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 1261 |     unicode = _PyCodec_DecodeText(buffer, encoding, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1262 |     if (unicode == NULL) | 
 | 1263 |         goto onError; | 
 | 1264 |     if (!PyUnicode_Check(unicode)) { | 
 | 1265 |         PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1266 |                      "decoder did not return an unicode object (type=%.400s)", | 
| Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1267 |                      Py_TYPE(unicode)->tp_name); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1268 |         Py_DECREF(unicode); | 
 | 1269 |         goto onError; | 
 | 1270 |     } | 
 | 1271 |     Py_DECREF(buffer); | 
 | 1272 |     return unicode; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1273 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1274 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1275 |     Py_XDECREF(buffer); | 
 | 1276 |     return NULL; | 
 | 1277 | } | 
 | 1278 |  | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1279 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, | 
 | 1280 |                                     const char *encoding, | 
 | 1281 |                                     const char *errors) | 
 | 1282 | { | 
 | 1283 |     PyObject *v; | 
 | 1284 |  | 
 | 1285 |     if (!PyUnicode_Check(unicode)) { | 
 | 1286 |         PyErr_BadArgument(); | 
 | 1287 |         goto onError; | 
 | 1288 |     } | 
 | 1289 |  | 
| Serhiy Storchaka | e37003e | 2015-12-03 20:47:48 +0200 | [diff] [blame] | 1290 |     if (PyErr_WarnPy3k("decoding Unicode is not supported in 3.x", 1) < 0) | 
 | 1291 |         goto onError; | 
 | 1292 |  | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1293 |     if (encoding == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1294 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1295 |  | 
 | 1296 |     /* Decode via the codec registry */ | 
| Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 1297 |     v = _PyCodec_DecodeText(unicode, encoding, errors); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1298 |     if (v == NULL) | 
 | 1299 |         goto onError; | 
 | 1300 |     return v; | 
 | 1301 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1302 |   onError: | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1303 |     return NULL; | 
 | 1304 | } | 
 | 1305 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1306 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1307 |                            Py_ssize_t size, | 
 | 1308 |                            const char *encoding, | 
 | 1309 |                            const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1310 | { | 
 | 1311 |     PyObject *v, *unicode; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1312 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1313 |     unicode = PyUnicode_FromUnicode(s, size); | 
 | 1314 |     if (unicode == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1315 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1316 |     v = PyUnicode_AsEncodedString(unicode, encoding, errors); | 
 | 1317 |     Py_DECREF(unicode); | 
 | 1318 |     return v; | 
 | 1319 | } | 
 | 1320 |  | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1321 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, | 
 | 1322 |                                     const char *encoding, | 
 | 1323 |                                     const char *errors) | 
 | 1324 | { | 
 | 1325 |     PyObject *v; | 
 | 1326 |  | 
 | 1327 |     if (!PyUnicode_Check(unicode)) { | 
 | 1328 |         PyErr_BadArgument(); | 
 | 1329 |         goto onError; | 
 | 1330 |     } | 
 | 1331 |  | 
 | 1332 |     if (encoding == NULL) | 
| Benjamin Peterson | d17fec7 | 2009-01-31 21:47:42 +0000 | [diff] [blame] | 1333 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1334 |  | 
 | 1335 |     /* Encode via the codec registry */ | 
| Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 1336 |     v = _PyCodec_EncodeText(unicode, encoding, errors); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1337 |     if (v == NULL) | 
 | 1338 |         goto onError; | 
 | 1339 |     return v; | 
 | 1340 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1341 |   onError: | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1342 |     return NULL; | 
 | 1343 | } | 
 | 1344 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1345 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, | 
 | 1346 |                                     const char *encoding, | 
 | 1347 |                                     const char *errors) | 
 | 1348 | { | 
 | 1349 |     PyObject *v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1350 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1351 |     if (!PyUnicode_Check(unicode)) { | 
 | 1352 |         PyErr_BadArgument(); | 
 | 1353 |         goto onError; | 
 | 1354 |     } | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1355 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1356 |     if (encoding == NULL) | 
| Benjamin Peterson | d17fec7 | 2009-01-31 21:47:42 +0000 | [diff] [blame] | 1357 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1358 |  | 
 | 1359 |     /* Shortcuts for common default encodings */ | 
 | 1360 |     if (errors == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1361 |         if (strcmp(encoding, "utf-8") == 0) | 
 | 1362 |             return PyUnicode_AsUTF8String(unicode); | 
 | 1363 |         else if (strcmp(encoding, "latin-1") == 0) | 
 | 1364 |             return PyUnicode_AsLatin1String(unicode); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1365 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1366 |         else if (strcmp(encoding, "mbcs") == 0) | 
 | 1367 |             return PyUnicode_AsMBCSString(unicode); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1368 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1369 |         else if (strcmp(encoding, "ascii") == 0) | 
 | 1370 |             return PyUnicode_AsASCIIString(unicode); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1371 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1372 |  | 
 | 1373 |     /* Encode via the codec registry */ | 
| Serhiy Storchaka | c7797dc | 2015-05-31 20:21:00 +0300 | [diff] [blame] | 1374 |     v = _PyCodec_EncodeText(unicode, encoding, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1375 |     if (v == NULL) | 
 | 1376 |         goto onError; | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1377 |     if (!PyString_Check(v)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1378 |         PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1379 |                      "encoder did not return a string object (type=%.400s)", | 
| Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1380 |                      Py_TYPE(v)->tp_name); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1381 |         Py_DECREF(v); | 
 | 1382 |         goto onError; | 
 | 1383 |     } | 
 | 1384 |     return v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1385 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1386 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 |     return NULL; | 
 | 1388 | } | 
 | 1389 |  | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1390 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1391 |                                             const char *errors) | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1392 | { | 
 | 1393 |     PyObject *v = ((PyUnicodeObject *)unicode)->defenc; | 
 | 1394 |  | 
 | 1395 |     if (v) | 
 | 1396 |         return v; | 
 | 1397 |     v = PyUnicode_AsEncodedString(unicode, NULL, errors); | 
 | 1398 |     if (v && errors == NULL) | 
 | 1399 |         ((PyUnicodeObject *)unicode)->defenc = v; | 
 | 1400 |     return v; | 
 | 1401 | } | 
 | 1402 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1403 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) | 
 | 1404 | { | 
 | 1405 |     if (!PyUnicode_Check(unicode)) { | 
 | 1406 |         PyErr_BadArgument(); | 
 | 1407 |         goto onError; | 
 | 1408 |     } | 
 | 1409 |     return PyUnicode_AS_UNICODE(unicode); | 
 | 1410 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1411 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1412 |     return NULL; | 
 | 1413 | } | 
 | 1414 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1415 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1416 | { | 
 | 1417 |     if (!PyUnicode_Check(unicode)) { | 
 | 1418 |         PyErr_BadArgument(); | 
 | 1419 |         goto onError; | 
 | 1420 |     } | 
 | 1421 |     return PyUnicode_GET_SIZE(unicode); | 
 | 1422 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1423 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1424 |     return -1; | 
 | 1425 | } | 
 | 1426 |  | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1427 | const char *PyUnicode_GetDefaultEncoding(void) | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1428 | { | 
 | 1429 |     return unicode_default_encoding; | 
 | 1430 | } | 
 | 1431 |  | 
 | 1432 | int PyUnicode_SetDefaultEncoding(const char *encoding) | 
 | 1433 | { | 
 | 1434 |     PyObject *v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1435 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1436 |     /* Make sure the encoding is valid. As side effect, this also | 
 | 1437 |        loads the encoding into the codec registry cache. */ | 
 | 1438 |     v = _PyCodec_Lookup(encoding); | 
 | 1439 |     if (v == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1440 |         goto onError; | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1441 |     Py_DECREF(v); | 
 | 1442 |     strncpy(unicode_default_encoding, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1443 |             encoding, | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 1444 |             sizeof(unicode_default_encoding) - 1); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1445 |     return 0; | 
 | 1446 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1447 |   onError: | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1448 |     return -1; | 
 | 1449 | } | 
 | 1450 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1451 | /* error handling callback helper: | 
 | 1452 |    build arguments, call the callback and check the arguments, | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1453 |    if no exception occurred, copy the replacement to the output | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1454 |    and adjust various state variables. | 
 | 1455 |    return 0 on success, -1 on error | 
 | 1456 | */ | 
 | 1457 |  | 
 | 1458 | static | 
 | 1459 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1460 |                                      const char *encoding, const char *reason, | 
 | 1461 |                                      const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, | 
 | 1462 |                                      Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | 
 | 1463 |                                      PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1464 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1465 |     static char *argparse = "O!n;decoding error handler must return (unicode, int) tuple"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1466 |  | 
 | 1467 |     PyObject *restuple = NULL; | 
 | 1468 |     PyObject *repunicode = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1469 |     Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); | 
 | 1470 |     Py_ssize_t requiredsize; | 
 | 1471 |     Py_ssize_t newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1472 |     Py_UNICODE *repptr; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1473 |     Py_ssize_t repsize; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1474 |     int res = -1; | 
 | 1475 |  | 
 | 1476 |     if (*errorHandler == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1477 |         *errorHandler = PyCodec_LookupError(errors); | 
 | 1478 |         if (*errorHandler == NULL) | 
 | 1479 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1480 |     } | 
 | 1481 |  | 
 | 1482 |     if (*exceptionObject == NULL) { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1483 |         *exceptionObject = PyUnicodeDecodeError_Create( | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1484 |             encoding, input, insize, *startinpos, *endinpos, reason); | 
 | 1485 |         if (*exceptionObject == NULL) | 
 | 1486 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1487 |     } | 
 | 1488 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1489 |         if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) | 
 | 1490 |             goto onError; | 
 | 1491 |         if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) | 
 | 1492 |             goto onError; | 
 | 1493 |         if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) | 
 | 1494 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1495 |     } | 
 | 1496 |  | 
 | 1497 |     restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); | 
 | 1498 |     if (restuple == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1499 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1500 |     if (!PyTuple_Check(restuple)) { | 
| Georg Brandl | cbb4958 | 2009-02-13 11:06:59 +0000 | [diff] [blame] | 1501 |         PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1502 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1503 |     } | 
 | 1504 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1505 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1506 |     if (newpos<0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1507 |         newpos = insize+newpos; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1508 |     if (newpos<0 || newpos>insize) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1509 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); | 
 | 1510 |         goto onError; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1511 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1512 |  | 
 | 1513 |     /* need more space? (at least enough for what we | 
 | 1514 |        have+the replacement+the rest of the string (starting | 
 | 1515 |        at the new input position), so we won't have to check space | 
 | 1516 |        when there are no errors in the rest of the string) */ | 
 | 1517 |     repptr = PyUnicode_AS_UNICODE(repunicode); | 
 | 1518 |     repsize = PyUnicode_GET_SIZE(repunicode); | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 1519 |     requiredsize = *outpos; | 
 | 1520 |     if (requiredsize > PY_SSIZE_T_MAX - repsize) | 
 | 1521 |         goto overflow; | 
 | 1522 |     requiredsize += repsize; | 
 | 1523 |     if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos)) | 
 | 1524 |         goto overflow; | 
 | 1525 |     requiredsize += insize - newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1526 |     if (requiredsize > outsize) { | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 1527 |         if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1528 |             requiredsize = 2*outsize; | 
 | 1529 |         if (_PyUnicode_Resize(output, requiredsize) < 0) | 
 | 1530 |             goto onError; | 
 | 1531 |         *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1532 |     } | 
 | 1533 |     *endinpos = newpos; | 
 | 1534 |     *inptr = input + newpos; | 
 | 1535 |     Py_UNICODE_COPY(*outptr, repptr, repsize); | 
 | 1536 |     *outptr += repsize; | 
 | 1537 |     *outpos += repsize; | 
 | 1538 |     /* we made it! */ | 
 | 1539 |     res = 0; | 
 | 1540 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1541 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1542 |     Py_XDECREF(restuple); | 
 | 1543 |     return res; | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 1544 |  | 
 | 1545 |   overflow: | 
 | 1546 |     PyErr_SetString(PyExc_OverflowError, | 
 | 1547 |                     "decoded result is too long for a Python string"); | 
 | 1548 |     goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1549 | } | 
 | 1550 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1551 | /* --- UTF-7 Codec -------------------------------------------------------- */ | 
 | 1552 |  | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1553 | /* See RFC2152 for details.  We encode conservatively and decode liberally. */ | 
 | 1554 |  | 
 | 1555 | /* Three simple macros defining base-64. */ | 
 | 1556 |  | 
 | 1557 | /* Is c a base-64 character? */ | 
 | 1558 |  | 
 | 1559 | #define IS_BASE64(c) \ | 
| Serhiy Storchaka | 462502b | 2015-10-10 09:33:11 +0300 | [diff] [blame] | 1560 |     (((c) >= 'A' && (c) <= 'Z') ||     \ | 
 | 1561 |      ((c) >= 'a' && (c) <= 'z') ||     \ | 
 | 1562 |      ((c) >= '0' && (c) <= '9') ||     \ | 
 | 1563 |      (c) == '+' || (c) == '/') | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1564 |  | 
 | 1565 | /* given that c is a base-64 character, what is its base-64 value? */ | 
 | 1566 |  | 
 | 1567 | #define FROM_BASE64(c)                                                  \ | 
 | 1568 |     (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' :                           \ | 
 | 1569 |      ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 :                      \ | 
 | 1570 |      ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 :                      \ | 
 | 1571 |      (c) == '+' ? 62 : 63) | 
 | 1572 |  | 
 | 1573 | /* What is the base-64 character of the bottom 6 bits of n? */ | 
 | 1574 |  | 
 | 1575 | #define TO_BASE64(n)  \ | 
 | 1576 |     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) | 
 | 1577 |  | 
 | 1578 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be | 
 | 1579 |  * decoded as itself.  We are permissive on decoding; the only ASCII | 
 | 1580 |  * byte not decoding to itself is the + which begins a base64 | 
 | 1581 |  * string. */ | 
 | 1582 |  | 
 | 1583 | #define DECODE_DIRECT(c)                                \ | 
 | 1584 |     ((c) <= 127 && (c) != '+') | 
 | 1585 |  | 
 | 1586 | /* The UTF-7 encoder treats ASCII characters differently according to | 
 | 1587 |  * whether they are Set D, Set O, Whitespace, or special (i.e. none of | 
 | 1588 |  * the above).  See RFC2152.  This array identifies these different | 
 | 1589 |  * sets: | 
 | 1590 |  * 0 : "Set D" | 
 | 1591 |  *     alphanumeric and '(),-./:? | 
 | 1592 |  * 1 : "Set O" | 
 | 1593 |  *     !"#$%&*;<=>@[]^_`{|} | 
 | 1594 |  * 2 : "whitespace" | 
 | 1595 |  *     ht nl cr sp | 
 | 1596 |  * 3 : special (must be base64 encoded) | 
 | 1597 |  *     everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) | 
 | 1598 |  */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1599 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1600 | static | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1601 | char utf7_category[128] = { | 
 | 1602 | /* nul soh stx etx eot enq ack bel bs  ht  nl  vt  np  cr  so  si  */ | 
 | 1603 |     3,  3,  3,  3,  3,  3,  3,  3,  3,  2,  2,  3,  3,  2,  3,  3, | 
 | 1604 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em  sub esc fs  gs  rs  us  */ | 
 | 1605 |     3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3, | 
 | 1606 | /* sp   !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /  */ | 
 | 1607 |     2,  1,  1,  1,  1,  1,  1,  0,  0,  0,  1,  3,  0,  0,  0,  0, | 
 | 1608 | /*  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?  */ | 
 | 1609 |     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  0, | 
 | 1610 | /*  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O  */ | 
 | 1611 |     1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, | 
 | 1612 | /*  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _  */ | 
 | 1613 |     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  3,  1,  1,  1, | 
 | 1614 | /*  `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o  */ | 
 | 1615 |     1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, | 
 | 1616 | /*  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~  del */ | 
 | 1617 |     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] | 1618 | }; | 
 | 1619 |  | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1620 | /* ENCODE_DIRECT: this character should be encoded as itself.  The | 
 | 1621 |  * answer depends on whether we are encoding set O as itself, and also | 
 | 1622 |  * on whether we are encoding whitespace as itself.  RFC2152 makes it | 
 | 1623 |  * clear that the answers to these questions vary between | 
 | 1624 |  * applications, so this code needs to be flexible.  */ | 
| Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1625 |  | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1626 | #define ENCODE_DIRECT(c, directO, directWS)             \ | 
 | 1627 |     ((c) < 128 && (c) > 0 &&                            \ | 
 | 1628 |      ((utf7_category[(c)] == 0) ||                      \ | 
 | 1629 |       (directWS && (utf7_category[(c)] == 2)) ||        \ | 
 | 1630 |       (directO && (utf7_category[(c)] == 1)))) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1631 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1632 | PyObject *PyUnicode_DecodeUTF7(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1633 |                                Py_ssize_t size, | 
 | 1634 |                                const char *errors) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1635 | { | 
| Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1636 |     return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); | 
 | 1637 | } | 
 | 1638 |  | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1639 | /* The decoder.  The only state we preserve is our read position, | 
 | 1640 |  * i.e. how many characters we have consumed.  So if we end in the | 
 | 1641 |  * middle of a shift sequence we have to back off the read position | 
 | 1642 |  * and the output to the beginning of the sequence, otherwise we lose | 
 | 1643 |  * all the shift state (seen bits, number of bits seen, high | 
 | 1644 |  * surrogate). */ | 
 | 1645 |  | 
| Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1646 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1647 |                                        Py_ssize_t size, | 
 | 1648 |                                        const char *errors, | 
 | 1649 |                                        Py_ssize_t *consumed) | 
| Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1650 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1651 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1652 |     Py_ssize_t startinpos; | 
 | 1653 |     Py_ssize_t endinpos; | 
 | 1654 |     Py_ssize_t outpos; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1655 |     const char *e; | 
 | 1656 |     PyUnicodeObject *unicode; | 
 | 1657 |     Py_UNICODE *p; | 
 | 1658 |     const char *errmsg = ""; | 
 | 1659 |     int inShift = 0; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1660 |     Py_UNICODE *shiftOutStart; | 
 | 1661 |     unsigned int base64bits = 0; | 
 | 1662 |     unsigned long base64buffer = 0; | 
 | 1663 |     Py_UNICODE surrogate = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1664 |     PyObject *errorHandler = NULL; | 
 | 1665 |     PyObject *exc = NULL; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1666 |  | 
 | 1667 |     unicode = _PyUnicode_New(size); | 
 | 1668 |     if (!unicode) | 
 | 1669 |         return NULL; | 
| Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1670 |     if (size == 0) { | 
 | 1671 |         if (consumed) | 
 | 1672 |             *consumed = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1673 |         return (PyObject *)unicode; | 
| Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1674 |     } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1675 |  | 
 | 1676 |     p = unicode->str; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1677 |     shiftOutStart = p; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1678 |     e = s + size; | 
 | 1679 |  | 
 | 1680 |     while (s < e) { | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1681 |         Py_UNICODE ch = (unsigned char) *s; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1682 |  | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1683 |         if (inShift) { /* in a base-64 section */ | 
 | 1684 |             if (IS_BASE64(ch)) { /* consume a base-64 character */ | 
 | 1685 |                 base64buffer = (base64buffer << 6) | FROM_BASE64(ch); | 
 | 1686 |                 base64bits += 6; | 
 | 1687 |                 s++; | 
 | 1688 |                 if (base64bits >= 16) { | 
 | 1689 |                     /* we have enough bits for a UTF-16 value */ | 
 | 1690 |                     Py_UNICODE outCh = (Py_UNICODE) | 
 | 1691 |                                        (base64buffer >> (base64bits-16)); | 
 | 1692 |                     base64bits -= 16; | 
 | 1693 |                     base64buffer &= (1 << base64bits) - 1; /* clear high bits */ | 
| Serhiy Storchaka | f105672 | 2013-10-19 20:37:49 +0300 | [diff] [blame] | 1694 |                     assert(outCh <= 0xffff); | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1695 |                     if (surrogate) { | 
 | 1696 |                         /* expecting a second surrogate */ | 
 | 1697 |                         if (outCh >= 0xDC00 && outCh <= 0xDFFF) { | 
 | 1698 | #ifdef Py_UNICODE_WIDE | 
 | 1699 |                             *p++ = (((surrogate & 0x3FF)<<10) | 
 | 1700 |                                     | (outCh & 0x3FF)) + 0x10000; | 
 | 1701 | #else | 
 | 1702 |                             *p++ = surrogate; | 
 | 1703 |                             *p++ = outCh; | 
 | 1704 | #endif | 
 | 1705 |                             surrogate = 0; | 
| Antoine Pitrou | 3040254 | 2011-11-15 01:49:40 +0100 | [diff] [blame] | 1706 |                             continue; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1707 |                         } | 
 | 1708 |                         else { | 
| Antoine Pitrou | 3040254 | 2011-11-15 01:49:40 +0100 | [diff] [blame] | 1709 |                             *p++ = surrogate; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1710 |                             surrogate = 0; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1711 |                         } | 
 | 1712 |                     } | 
| Antoine Pitrou | 3040254 | 2011-11-15 01:49:40 +0100 | [diff] [blame] | 1713 |                     if (outCh >= 0xD800 && outCh <= 0xDBFF) { | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1714 |                         /* first surrogate */ | 
 | 1715 |                         surrogate = outCh; | 
 | 1716 |                     } | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1717 |                     else { | 
 | 1718 |                         *p++ = outCh; | 
 | 1719 |                     } | 
 | 1720 |                 } | 
 | 1721 |             } | 
 | 1722 |             else { /* now leaving a base-64 section */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1723 |                 inShift = 0; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1724 |                 if (base64bits > 0) { /* left-over bits */ | 
 | 1725 |                     if (base64bits >= 6) { | 
 | 1726 |                         /* We've seen at least one base-64 character */ | 
| Serhiy Storchaka | e12f632 | 2015-10-02 13:14:53 +0300 | [diff] [blame] | 1727 |                         s++; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1728 |                         errmsg = "partial character in shift sequence"; | 
 | 1729 |                         goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1730 |                     } | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1731 |                     else { | 
 | 1732 |                         /* Some bits remain; they should be zero */ | 
 | 1733 |                         if (base64buffer != 0) { | 
| Serhiy Storchaka | e12f632 | 2015-10-02 13:14:53 +0300 | [diff] [blame] | 1734 |                             s++; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1735 |                             errmsg = "non-zero padding bits in shift sequence"; | 
 | 1736 |                             goto utf7Error; | 
 | 1737 |                         } | 
 | 1738 |                     } | 
 | 1739 |                 } | 
| Serhiy Storchaka | e12f632 | 2015-10-02 13:14:53 +0300 | [diff] [blame] | 1740 |                 if (surrogate && DECODE_DIRECT(ch)) | 
 | 1741 |                     *p++ = surrogate; | 
 | 1742 |                 surrogate = 0; | 
 | 1743 |                 if (ch == '-') { | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1744 |                     /* '-' is absorbed; other terminating | 
 | 1745 |                        characters are preserved */ | 
| Serhiy Storchaka | e12f632 | 2015-10-02 13:14:53 +0300 | [diff] [blame] | 1746 |                     s++; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1747 |                 } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1748 |             } | 
 | 1749 |         } | 
 | 1750 |         else if ( ch == '+' ) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1751 |             startinpos = s-starts; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1752 |             s++; /* consume '+' */ | 
 | 1753 |             if (s < e && *s == '-') { /* '+-' encodes '+' */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1754 |                 s++; | 
 | 1755 |                 *p++ = '+'; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1756 |             } | 
 | 1757 |             else { /* begin base64-encoded section */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1758 |                 inShift = 1; | 
| Serhiy Storchaka | e12f632 | 2015-10-02 13:14:53 +0300 | [diff] [blame] | 1759 |                 surrogate = 0; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1760 |                 shiftOutStart = p; | 
 | 1761 |                 base64bits = 0; | 
| Serhiy Storchaka | f105672 | 2013-10-19 20:37:49 +0300 | [diff] [blame] | 1762 |                 base64buffer = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1763 |             } | 
 | 1764 |         } | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1765 |         else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1766 |             *p++ = ch; | 
 | 1767 |             s++; | 
 | 1768 |         } | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1769 |         else { | 
 | 1770 |             startinpos = s-starts; | 
 | 1771 |             s++; | 
 | 1772 |             errmsg = "unexpected special character"; | 
 | 1773 |             goto utf7Error; | 
 | 1774 |         } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1775 |         continue; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1776 | utf7Error: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1777 |         outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 1778 |         endinpos = s-starts; | 
 | 1779 |         if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1780 |                 errors, &errorHandler, | 
 | 1781 |                 "utf7", errmsg, | 
 | 1782 |                 starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 1783 |                 &unicode, &outpos, &p)) | 
 | 1784 |             goto onError; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1785 |     } | 
 | 1786 |  | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1787 |     /* end of string */ | 
 | 1788 |  | 
 | 1789 |     if (inShift && !consumed) { /* in shift sequence, no more to follow */ | 
 | 1790 |         /* if we're in an inconsistent state, that's an error */ | 
| Serhiy Storchaka | e12f632 | 2015-10-02 13:14:53 +0300 | [diff] [blame] | 1791 |         inShift = 0; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1792 |         if (surrogate || | 
 | 1793 |                 (base64bits >= 6) || | 
 | 1794 |                 (base64bits > 0 && base64buffer != 0)) { | 
 | 1795 |             outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 1796 |             endinpos = size; | 
 | 1797 |             if (unicode_decode_call_errorhandler( | 
 | 1798 |                     errors, &errorHandler, | 
 | 1799 |                     "utf7", "unterminated shift sequence", | 
 | 1800 |                     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 1801 |                     &unicode, &outpos, &p)) | 
 | 1802 |                 goto onError; | 
 | 1803 |         } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1804 |     } | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1805 |  | 
 | 1806 |     /* return state */ | 
| Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1807 |     if (consumed) { | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1808 |         if (inShift) { | 
 | 1809 |             p = shiftOutStart; /* back off output */ | 
| Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1810 |             *consumed = startinpos; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1811 |         } | 
 | 1812 |         else { | 
| Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1813 |             *consumed = s-starts; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1814 |         } | 
| Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1815 |     } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1816 |  | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1817 |     if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1818 |         goto onError; | 
 | 1819 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1820 |     Py_XDECREF(errorHandler); | 
 | 1821 |     Py_XDECREF(exc); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1822 |     return (PyObject *)unicode; | 
 | 1823 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1824 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1825 |     Py_XDECREF(errorHandler); | 
 | 1826 |     Py_XDECREF(exc); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1827 |     Py_DECREF(unicode); | 
 | 1828 |     return NULL; | 
 | 1829 | } | 
 | 1830 |  | 
 | 1831 |  | 
 | 1832 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1833 |                                Py_ssize_t size, | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1834 |                                int base64SetO, | 
 | 1835 |                                int base64WhiteSpace, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1836 |                                const char *errors) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1837 | { | 
 | 1838 |     PyObject *v; | 
 | 1839 |     /* It might be possible to tighten this worst case */ | 
| Alexandre Vassalotti | fd00916 | 2009-07-07 02:17:30 +0000 | [diff] [blame] | 1840 |     Py_ssize_t allocated = 8 * size; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1841 |     int inShift = 0; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1842 |     Py_ssize_t i = 0; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1843 |     unsigned int base64bits = 0; | 
 | 1844 |     unsigned long base64buffer = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1845 |     char * out; | 
 | 1846 |     char * start; | 
 | 1847 |  | 
| Alexandre Vassalotti | fd00916 | 2009-07-07 02:17:30 +0000 | [diff] [blame] | 1848 |     if (allocated / 8 != size) | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 1849 |         return PyErr_NoMemory(); | 
 | 1850 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1851 |     if (size == 0) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 1852 |         return PyString_FromStringAndSize(NULL, 0); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1853 |  | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1854 |     v = PyString_FromStringAndSize(NULL, allocated); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1855 |     if (v == NULL) | 
 | 1856 |         return NULL; | 
 | 1857 |  | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1858 |     start = out = PyString_AS_STRING(v); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1859 |     for (;i < size; ++i) { | 
 | 1860 |         Py_UNICODE ch = s[i]; | 
 | 1861 |  | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1862 |         if (inShift) { | 
 | 1863 |             if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { | 
 | 1864 |                 /* shifting out */ | 
 | 1865 |                 if (base64bits) { /* output remaining bits */ | 
 | 1866 |                     *out++ = TO_BASE64(base64buffer << (6-base64bits)); | 
 | 1867 |                     base64buffer = 0; | 
 | 1868 |                     base64bits = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1869 |                 } | 
 | 1870 |                 inShift = 0; | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1871 |                 /* Characters not in the BASE64 set implicitly unshift the sequence | 
 | 1872 |                    so no '-' is required, except if the character is itself a '-' */ | 
 | 1873 |                 if (IS_BASE64(ch) || ch == '-') { | 
 | 1874 |                     *out++ = '-'; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1875 |                 } | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1876 |                 *out++ = (char) ch; | 
 | 1877 |             } | 
 | 1878 |             else { | 
 | 1879 |                 goto encode_char; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1880 |             } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1881 |         } | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1882 |         else { /* not in a shift sequence */ | 
 | 1883 |             if (ch == '+') { | 
 | 1884 |                 *out++ = '+'; | 
 | 1885 |                         *out++ = '-'; | 
 | 1886 |             } | 
 | 1887 |             else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { | 
 | 1888 |                 *out++ = (char) ch; | 
 | 1889 |             } | 
 | 1890 |             else { | 
 | 1891 |                 *out++ = '+'; | 
 | 1892 |                 inShift = 1; | 
 | 1893 |                 goto encode_char; | 
 | 1894 |             } | 
 | 1895 |         } | 
 | 1896 |         continue; | 
 | 1897 | encode_char: | 
 | 1898 | #ifdef Py_UNICODE_WIDE | 
 | 1899 |         if (ch >= 0x10000) { | 
 | 1900 |             /* code first surrogate */ | 
 | 1901 |             base64bits += 16; | 
 | 1902 |             base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); | 
 | 1903 |             while (base64bits >= 6) { | 
 | 1904 |                 *out++ = TO_BASE64(base64buffer >> (base64bits-6)); | 
 | 1905 |                 base64bits -= 6; | 
 | 1906 |             } | 
 | 1907 |             /* prepare second surrogate */ | 
 | 1908 |             ch =  0xDC00 | ((ch-0x10000) & 0x3FF); | 
 | 1909 |         } | 
 | 1910 | #endif | 
 | 1911 |         base64bits += 16; | 
 | 1912 |         base64buffer = (base64buffer << 16) | ch; | 
 | 1913 |         while (base64bits >= 6) { | 
 | 1914 |             *out++ = TO_BASE64(base64buffer >> (base64bits-6)); | 
 | 1915 |             base64bits -= 6; | 
 | 1916 |         } | 
| Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1917 |     } | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1918 |     if (base64bits) | 
 | 1919 |         *out++= TO_BASE64(base64buffer << (6-base64bits) ); | 
 | 1920 |     if (inShift) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1921 |         *out++ = '-'; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1922 |  | 
| Benjamin Peterson | bea424a | 2010-04-03 00:57:33 +0000 | [diff] [blame] | 1923 |     if (_PyString_Resize(&v, out - start)) | 
 | 1924 |         return NULL; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1925 |     return v; | 
 | 1926 | } | 
 | 1927 |  | 
| Antoine Pitrou | 653dece | 2009-05-04 18:32:32 +0000 | [diff] [blame] | 1928 | #undef IS_BASE64 | 
 | 1929 | #undef FROM_BASE64 | 
 | 1930 | #undef TO_BASE64 | 
 | 1931 | #undef DECODE_DIRECT | 
 | 1932 | #undef ENCODE_DIRECT | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1933 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1934 | /* --- UTF-8 Codec -------------------------------------------------------- */ | 
 | 1935 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1936 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1937 | char utf8_code_length[256] = { | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 1938 |     /* Map UTF-8 encoded prefix byte to sequence length.  Zero means | 
 | 1939 |        illegal prefix.  See RFC 3629 for details */ | 
 | 1940 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ | 
 | 1941 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
| Chris Jerdonek | ad4b000 | 2012-10-07 20:37:54 -0700 | [diff] [blame] | 1942 |     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] | 1943 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1944 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1945 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 1946 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 1947 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ | 
 | 1948 |     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] | 1949 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
 | 1950 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 1951 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ | 
 | 1952 |     0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ | 
 | 1953 |     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ | 
 | 1954 |     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ | 
 | 1955 |     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] | 1956 | }; | 
 | 1957 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1958 | PyObject *PyUnicode_DecodeUTF8(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1959 |                                Py_ssize_t size, | 
 | 1960 |                                const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1961 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1962 |     return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); | 
 | 1963 | } | 
 | 1964 |  | 
 | 1965 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 1966 |                                        Py_ssize_t size, | 
 | 1967 |                                        const char *errors, | 
 | 1968 |                                        Py_ssize_t *consumed) | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1969 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1970 |     const char *starts = s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1971 |     int n; | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 1972 |     int k; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1973 |     Py_ssize_t startinpos; | 
 | 1974 |     Py_ssize_t endinpos; | 
 | 1975 |     Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1976 |     const char *e; | 
 | 1977 |     PyUnicodeObject *unicode; | 
 | 1978 |     Py_UNICODE *p; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1979 |     const char *errmsg = ""; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1980 |     PyObject *errorHandler = NULL; | 
 | 1981 |     PyObject *exc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1982 |  | 
 | 1983 |     /* Note: size will always be longer than the resulting Unicode | 
 | 1984 |        character count */ | 
 | 1985 |     unicode = _PyUnicode_New(size); | 
 | 1986 |     if (!unicode) | 
 | 1987 |         return NULL; | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1988 |     if (size == 0) { | 
 | 1989 |         if (consumed) | 
 | 1990 |             *consumed = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1991 |         return (PyObject *)unicode; | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1992 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1993 |  | 
 | 1994 |     /* Unpack UTF-8 encoded data */ | 
 | 1995 |     p = unicode->str; | 
 | 1996 |     e = s + size; | 
 | 1997 |  | 
 | 1998 |     while (s < e) { | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1999 |         Py_UCS4 ch = (unsigned char)*s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2000 |  | 
 | 2001 |         if (ch < 0x80) { | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2002 |             *p++ = (Py_UNICODE)ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2003 |             s++; | 
 | 2004 |             continue; | 
 | 2005 |         } | 
 | 2006 |  | 
 | 2007 |         n = utf8_code_length[ch]; | 
 | 2008 |  | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2009 |         if (s + n > e) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2010 |             if (consumed) | 
 | 2011 |                 break; | 
 | 2012 |             else { | 
 | 2013 |                 errmsg = "unexpected end of data"; | 
 | 2014 |                 startinpos = s-starts; | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2015 |                 endinpos = startinpos+1; | 
 | 2016 |                 for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) | 
 | 2017 |                     endinpos++; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2018 |                 goto utf8Error; | 
 | 2019 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2020 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2021 |  | 
 | 2022 |         switch (n) { | 
 | 2023 |  | 
 | 2024 |         case 0: | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2025 |             errmsg = "invalid start byte"; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2026 |             startinpos = s-starts; | 
 | 2027 |             endinpos = startinpos+1; | 
 | 2028 |             goto utf8Error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2029 |  | 
 | 2030 |         case 1: | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2031 |             errmsg = "internal error"; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2032 |             startinpos = s-starts; | 
 | 2033 |             endinpos = startinpos+1; | 
 | 2034 |             goto utf8Error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2035 |  | 
 | 2036 |         case 2: | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2037 |             if ((s[1] & 0xc0) != 0x80) { | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2038 |                 errmsg = "invalid continuation byte"; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2039 |                 startinpos = s-starts; | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2040 |                 endinpos = startinpos + 1; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2041 |                 goto utf8Error; | 
 | 2042 |             } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2043 |             ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2044 |             assert ((ch > 0x007F) && (ch <= 0x07FF)); | 
 | 2045 |             *p++ = (Py_UNICODE)ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2046 |             break; | 
 | 2047 |  | 
 | 2048 |         case 3: | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2049 |             /* XXX: surrogates shouldn't be valid UTF-8! | 
 | 2050 |                see http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf | 
 | 2051 |                (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt | 
 | 2052 |                Uncomment the 2 lines below to make them invalid, | 
| Serhiy Storchaka | e8c9e14 | 2015-01-18 11:42:50 +0200 | [diff] [blame] | 2053 |                code points: d800-dfff; UTF-8: \xed\xa0\x80-\xed\xbf\xbf. */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2054 |             if ((s[1] & 0xc0) != 0x80 || | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2055 |                 (s[2] & 0xc0) != 0x80 || | 
 | 2056 |                 ((unsigned char)s[0] == 0xE0 && | 
 | 2057 |                  (unsigned char)s[1] < 0xA0)/* || | 
 | 2058 |                 ((unsigned char)s[0] == 0xED && | 
 | 2059 |                  (unsigned char)s[1] > 0x9F)*/) { | 
 | 2060 |                 errmsg = "invalid continuation byte"; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2061 |                 startinpos = s-starts; | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2062 |                 endinpos = startinpos + 1; | 
 | 2063 |  | 
 | 2064 |                 /* if s[1] first two bits are 1 and 0, then the invalid | 
 | 2065 |                    continuation byte is s[2], so increment endinpos by 1, | 
 | 2066 |                    if not, s[1] is invalid and endinpos doesn't need to | 
 | 2067 |                    be incremented. */ | 
 | 2068 |                 if ((s[1] & 0xC0) == 0x80) | 
 | 2069 |                     endinpos++; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2070 |                 goto utf8Error; | 
 | 2071 |             } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2072 |             ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2073 |             assert ((ch > 0x07FF) && (ch <= 0xFFFF)); | 
 | 2074 |             *p++ = (Py_UNICODE)ch; | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2075 |             break; | 
 | 2076 |  | 
 | 2077 |         case 4: | 
 | 2078 |             if ((s[1] & 0xc0) != 0x80 || | 
 | 2079 |                 (s[2] & 0xc0) != 0x80 || | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2080 |                 (s[3] & 0xc0) != 0x80 || | 
 | 2081 |                 ((unsigned char)s[0] == 0xF0 && | 
 | 2082 |                  (unsigned char)s[1] < 0x90) || | 
 | 2083 |                 ((unsigned char)s[0] == 0xF4 && | 
 | 2084 |                  (unsigned char)s[1] > 0x8F)) { | 
 | 2085 |                 errmsg = "invalid continuation byte"; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2086 |                 startinpos = s-starts; | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2087 |                 endinpos = startinpos + 1; | 
 | 2088 |                 if ((s[1] & 0xC0) == 0x80) { | 
 | 2089 |                     endinpos++; | 
 | 2090 |                     if ((s[2] & 0xC0) == 0x80) | 
 | 2091 |                         endinpos++; | 
 | 2092 |                 } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2093 |                 goto utf8Error; | 
 | 2094 |             } | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2095 |             ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + | 
| Ezio Melotti | e57e50c | 2010-06-05 17:51:07 +0000 | [diff] [blame] | 2096 |                  ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); | 
 | 2097 |             assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); | 
 | 2098 |  | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2099 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2100 |             *p++ = (Py_UNICODE)ch; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2101 | #else | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2102 |             /*  compute and append the two surrogates: */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2103 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2104 |             /*  translate from 10000..10FFFF to 0..FFFF */ | 
 | 2105 |             ch -= 0x10000; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2106 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2107 |             /*  high surrogate = top 10 bits added to D800 */ | 
 | 2108 |             *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2109 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2110 |             /*  low surrogate = bottom 10 bits added to DC00 */ | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2111 |             *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2112 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2113 |             break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2114 |         } | 
 | 2115 |         s += n; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2116 |         continue; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2117 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2118 |       utf8Error: | 
 | 2119 |         outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 2120 |         if (unicode_decode_call_errorhandler( | 
 | 2121 |                 errors, &errorHandler, | 
 | 2122 |                 "utf8", errmsg, | 
 | 2123 |                 starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2124 |                 &unicode, &outpos, &p)) | 
 | 2125 |             goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2126 |     } | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2127 |     if (consumed) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2128 |         *consumed = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2129 |  | 
 | 2130 |     /* Adjust length */ | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2131 |     if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2132 |         goto onError; | 
 | 2133 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2134 |     Py_XDECREF(errorHandler); | 
 | 2135 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2136 |     return (PyObject *)unicode; | 
 | 2137 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2138 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2139 |     Py_XDECREF(errorHandler); | 
 | 2140 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2141 |     Py_DECREF(unicode); | 
 | 2142 |     return NULL; | 
 | 2143 | } | 
 | 2144 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2145 | /* Allocation strategy:  if the string is short, convert into a stack buffer | 
 | 2146 |    and allocate exactly as much space needed at the end.  Else allocate the | 
 | 2147 |    maximum possible needed (4 result bytes per Unicode character), and return | 
 | 2148 |    the excess memory at the end. | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2149 | */ | 
| Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2150 | PyObject * | 
 | 2151 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2152 |                      Py_ssize_t size, | 
 | 2153 |                      const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2154 | { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2155 | #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] | 2156 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2157 |     Py_ssize_t i;           /* index into s of next input byte */ | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2158 |     PyObject *v;        /* result string object */ | 
 | 2159 |     char *p;            /* next free byte in output buffer */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2160 |     Py_ssize_t nallocated;  /* number of result bytes allocated */ | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2161 |     Py_ssize_t nneeded;        /* number of result bytes needed */ | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2162 |     char stackbuf[MAX_SHORT_UNICHARS * 4]; | 
| Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2163 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2164 |     assert(s != NULL); | 
 | 2165 |     assert(size >= 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2166 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2167 |     if (size <= MAX_SHORT_UNICHARS) { | 
 | 2168 |         /* Write into the stack buffer; nallocated can't overflow. | 
 | 2169 |          * At the end, we'll allocate exactly as much heap space as it | 
 | 2170 |          * turns out we need. | 
 | 2171 |          */ | 
 | 2172 |         nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); | 
 | 2173 |         v = NULL;   /* will allocate after we're done */ | 
 | 2174 |         p = stackbuf; | 
 | 2175 |     } | 
 | 2176 |     else { | 
 | 2177 |         /* Overallocate on the heap, and give the excess back at the end. */ | 
 | 2178 |         nallocated = size * 4; | 
 | 2179 |         if (nallocated / 4 != size)  /* overflow! */ | 
 | 2180 |             return PyErr_NoMemory(); | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2181 |         v = PyString_FromStringAndSize(NULL, nallocated); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2182 |         if (v == NULL) | 
 | 2183 |             return NULL; | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2184 |         p = PyString_AS_STRING(v); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2185 |     } | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2186 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2187 |     for (i = 0; i < size;) { | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2188 |         Py_UCS4 ch = s[i++]; | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2189 |  | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2190 |         if (ch < 0x80) | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2191 |             /* Encode ASCII */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2192 |             *p++ = (char) ch; | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2193 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2194 |         else if (ch < 0x0800) { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2195 |             /* Encode Latin-1 */ | 
| Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2196 |             *p++ = (char)(0xc0 | (ch >> 6)); | 
 | 2197 |             *p++ = (char)(0x80 | (ch & 0x3f)); | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2198 |         } | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2199 |         else { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2200 |             /* Encode UCS2 Unicode ordinals */ | 
 | 2201 |             if (ch < 0x10000) { | 
 | 2202 |                 /* Special case: check for high surrogate */ | 
 | 2203 |                 if (0xD800 <= ch && ch <= 0xDBFF && i != size) { | 
 | 2204 |                     Py_UCS4 ch2 = s[i]; | 
 | 2205 |                     /* Check for low surrogate and combine the two to | 
 | 2206 |                        form a UCS4 value */ | 
 | 2207 |                     if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2208 |                         ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2209 |                         i++; | 
 | 2210 |                         goto encodeUCS4; | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2211 |                     } | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2212 |                     /* Fall through: handles isolated high surrogates */ | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2213 |                 } | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2214 |                 *p++ = (char)(0xe0 | (ch >> 12)); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2215 |                 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); | 
 | 2216 |                 *p++ = (char)(0x80 | (ch & 0x3f)); | 
 | 2217 |                 continue; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2218 |             } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2219 |           encodeUCS4: | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2220 |             /* Encode UCS4 Unicode ordinals */ | 
 | 2221 |             *p++ = (char)(0xf0 | (ch >> 18)); | 
 | 2222 |             *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); | 
 | 2223 |             *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); | 
 | 2224 |             *p++ = (char)(0x80 | (ch & 0x3f)); | 
 | 2225 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2226 |     } | 
| Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2227 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2228 |     if (v == NULL) { | 
 | 2229 |         /* This was stack allocated. */ | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2230 |         nneeded = p - stackbuf; | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2231 |         assert(nneeded <= nallocated); | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2232 |         v = PyString_FromStringAndSize(stackbuf, nneeded); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2233 |     } | 
 | 2234 |     else { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2235 |         /* Cut back to size actually needed. */ | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2236 |         nneeded = p - PyString_AS_STRING(v); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2237 |         assert(nneeded <= nallocated); | 
| Benjamin Peterson | bea424a | 2010-04-03 00:57:33 +0000 | [diff] [blame] | 2238 |         if (_PyString_Resize(&v, nneeded)) | 
 | 2239 |             return NULL; | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2240 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2241 |     return v; | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2242 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2243 | #undef MAX_SHORT_UNICHARS | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2244 | } | 
 | 2245 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2246 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) | 
 | 2247 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2248 |     if (!PyUnicode_Check(unicode)) { | 
 | 2249 |         PyErr_BadArgument(); | 
 | 2250 |         return NULL; | 
 | 2251 |     } | 
| Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2252 |     return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2253 |                                 PyUnicode_GET_SIZE(unicode), | 
 | 2254 |                                 NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2255 | } | 
 | 2256 |  | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2257 | /* --- UTF-32 Codec ------------------------------------------------------- */ | 
 | 2258 |  | 
 | 2259 | PyObject * | 
 | 2260 | PyUnicode_DecodeUTF32(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2261 |                       Py_ssize_t size, | 
 | 2262 |                       const char *errors, | 
 | 2263 |                       int *byteorder) | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2264 | { | 
 | 2265 |     return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); | 
 | 2266 | } | 
 | 2267 |  | 
 | 2268 | PyObject * | 
 | 2269 | PyUnicode_DecodeUTF32Stateful(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2270 |                               Py_ssize_t size, | 
 | 2271 |                               const char *errors, | 
 | 2272 |                               int *byteorder, | 
 | 2273 |                               Py_ssize_t *consumed) | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2274 | { | 
 | 2275 |     const char *starts = s; | 
 | 2276 |     Py_ssize_t startinpos; | 
 | 2277 |     Py_ssize_t endinpos; | 
 | 2278 |     Py_ssize_t outpos; | 
 | 2279 |     PyUnicodeObject *unicode; | 
 | 2280 |     Py_UNICODE *p; | 
 | 2281 | #ifndef Py_UNICODE_WIDE | 
| Antoine Pitrou | cca3a3f | 2010-06-11 21:42:26 +0000 | [diff] [blame] | 2282 |     int pairs = 0; | 
| Benjamin Peterson | 8e5effa | 2010-06-12 17:47:06 +0000 | [diff] [blame] | 2283 |     const unsigned char *qq; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2284 | #else | 
 | 2285 |     const int pairs = 0; | 
 | 2286 | #endif | 
| Benjamin Peterson | 8e5effa | 2010-06-12 17:47:06 +0000 | [diff] [blame] | 2287 |     const unsigned char *q, *e; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2288 |     int bo = 0;       /* assume native ordering by default */ | 
 | 2289 |     const char *errmsg = ""; | 
| Walter Dörwald | 20b40d3 | 2007-08-17 16:52:50 +0000 | [diff] [blame] | 2290 |     /* Offsets from q for retrieving bytes in the right order. */ | 
 | 2291 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 2292 |     int iorder[] = {0, 1, 2, 3}; | 
 | 2293 | #else | 
 | 2294 |     int iorder[] = {3, 2, 1, 0}; | 
 | 2295 | #endif | 
| Walter Dörwald | 9ab80a9 | 2007-08-17 16:58:43 +0000 | [diff] [blame] | 2296 |     PyObject *errorHandler = NULL; | 
 | 2297 |     PyObject *exc = NULL; | 
| Chris Jerdonek | ad4b000 | 2012-10-07 20:37:54 -0700 | [diff] [blame] | 2298 |  | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2299 |     q = (unsigned char *)s; | 
 | 2300 |     e = q + size; | 
 | 2301 |  | 
 | 2302 |     if (byteorder) | 
 | 2303 |         bo = *byteorder; | 
 | 2304 |  | 
 | 2305 |     /* Check for BOM marks (U+FEFF) in the input and adjust current | 
 | 2306 |        byte order setting accordingly. In native mode, the leading BOM | 
 | 2307 |        mark is skipped, in all other modes, it is copied to the output | 
 | 2308 |        stream as-is (giving a ZWNBSP character). */ | 
 | 2309 |     if (bo == 0) { | 
 | 2310 |         if (size >= 4) { | 
| Benjamin Peterson | 89f676f | 2016-09-06 20:40:04 -0700 | [diff] [blame] | 2311 |             const Py_UCS4 bom = ((unsigned int)q[iorder[3]] << 24) | (q[iorder[2]] << 16) | | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2312 |                 (q[iorder[1]] << 8) | q[iorder[0]]; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2313 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2314 |             if (bom == 0x0000FEFF) { | 
 | 2315 |                 q += 4; | 
 | 2316 |                 bo = -1; | 
 | 2317 |             } | 
 | 2318 |             else if (bom == 0xFFFE0000) { | 
 | 2319 |                 q += 4; | 
 | 2320 |                 bo = 1; | 
 | 2321 |             } | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2322 | #else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2323 |             if (bom == 0x0000FEFF) { | 
 | 2324 |                 q += 4; | 
 | 2325 |                 bo = 1; | 
 | 2326 |             } | 
 | 2327 |             else if (bom == 0xFFFE0000) { | 
 | 2328 |                 q += 4; | 
 | 2329 |                 bo = -1; | 
 | 2330 |             } | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2331 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2332 |         } | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2333 |     } | 
 | 2334 |  | 
 | 2335 |     if (bo == -1) { | 
 | 2336 |         /* force LE */ | 
 | 2337 |         iorder[0] = 0; | 
 | 2338 |         iorder[1] = 1; | 
 | 2339 |         iorder[2] = 2; | 
 | 2340 |         iorder[3] = 3; | 
 | 2341 |     } | 
 | 2342 |     else if (bo == 1) { | 
 | 2343 |         /* force BE */ | 
 | 2344 |         iorder[0] = 3; | 
 | 2345 |         iorder[1] = 2; | 
 | 2346 |         iorder[2] = 1; | 
 | 2347 |         iorder[3] = 0; | 
 | 2348 |     } | 
 | 2349 |  | 
| Antoine Pitrou | cca3a3f | 2010-06-11 21:42:26 +0000 | [diff] [blame] | 2350 |     /* On narrow builds we split characters outside the BMP into two | 
| Serhiy Storchaka | e8c9e14 | 2015-01-18 11:42:50 +0200 | [diff] [blame] | 2351 |        code points => count how much extra space we need. */ | 
| Antoine Pitrou | cca3a3f | 2010-06-11 21:42:26 +0000 | [diff] [blame] | 2352 | #ifndef Py_UNICODE_WIDE | 
| Serhiy Storchaka | c9631a1 | 2013-01-08 22:43:18 +0200 | [diff] [blame] | 2353 |     for (qq = q; e - qq >= 4; qq += 4) | 
| Antoine Pitrou | cca3a3f | 2010-06-11 21:42:26 +0000 | [diff] [blame] | 2354 |         if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) | 
 | 2355 |             pairs++; | 
 | 2356 | #endif | 
 | 2357 |  | 
 | 2358 |     /* This might be one to much, because of a BOM */ | 
 | 2359 |     unicode = _PyUnicode_New((size+3)/4+pairs); | 
 | 2360 |     if (!unicode) | 
 | 2361 |         return NULL; | 
 | 2362 |     if (size == 0) | 
 | 2363 |         return (PyObject *)unicode; | 
 | 2364 |  | 
 | 2365 |     /* Unpack UTF-32 encoded data */ | 
 | 2366 |     p = unicode->str; | 
 | 2367 |  | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2368 |     while (q < e) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2369 |         Py_UCS4 ch; | 
 | 2370 |         /* remaining bytes at the end? (size should be divisible by 4) */ | 
 | 2371 |         if (e-q<4) { | 
 | 2372 |             if (consumed) | 
 | 2373 |                 break; | 
 | 2374 |             errmsg = "truncated data"; | 
 | 2375 |             startinpos = ((const char *)q)-starts; | 
 | 2376 |             endinpos = ((const char *)e)-starts; | 
 | 2377 |             goto utf32Error; | 
 | 2378 |             /* The remaining input chars are ignored if the callback | 
 | 2379 |                chooses to skip the input */ | 
 | 2380 |         } | 
| Benjamin Peterson | 89f676f | 2016-09-06 20:40:04 -0700 | [diff] [blame] | 2381 |         ch = ((unsigned int)q[iorder[3]] << 24) | (q[iorder[2]] << 16) | | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2382 |             (q[iorder[1]] << 8) | q[iorder[0]]; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2383 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2384 |         if (ch >= 0x110000) | 
 | 2385 |         { | 
| Serhiy Storchaka | e8c9e14 | 2015-01-18 11:42:50 +0200 | [diff] [blame] | 2386 |             errmsg = "code point not in range(0x110000)"; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2387 |             startinpos = ((const char *)q)-starts; | 
 | 2388 |             endinpos = startinpos+4; | 
 | 2389 |             goto utf32Error; | 
 | 2390 |         } | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2391 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2392 |         if (ch >= 0x10000) | 
 | 2393 |         { | 
 | 2394 |             *p++ = 0xD800 | ((ch-0x10000) >> 10); | 
 | 2395 |             *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); | 
 | 2396 |         } | 
 | 2397 |         else | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2398 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2399 |             *p++ = ch; | 
 | 2400 |         q += 4; | 
 | 2401 |         continue; | 
 | 2402 |       utf32Error: | 
 | 2403 |         outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 2404 |         if (unicode_decode_call_errorhandler( | 
 | 2405 |                 errors, &errorHandler, | 
 | 2406 |                 "utf32", errmsg, | 
| Georg Brandl | e9741f3 | 2009-09-17 11:28:09 +0000 | [diff] [blame] | 2407 |                 starts, size, &startinpos, &endinpos, &exc, (const char **)&q, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2408 |                 &unicode, &outpos, &p)) | 
 | 2409 |             goto onError; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2410 |     } | 
 | 2411 |  | 
 | 2412 |     if (byteorder) | 
 | 2413 |         *byteorder = bo; | 
 | 2414 |  | 
 | 2415 |     if (consumed) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2416 |         *consumed = (const char *)q-starts; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2417 |  | 
 | 2418 |     /* Adjust length */ | 
 | 2419 |     if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) | 
 | 2420 |         goto onError; | 
 | 2421 |  | 
 | 2422 |     Py_XDECREF(errorHandler); | 
 | 2423 |     Py_XDECREF(exc); | 
 | 2424 |     return (PyObject *)unicode; | 
 | 2425 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2426 |   onError: | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2427 |     Py_DECREF(unicode); | 
 | 2428 |     Py_XDECREF(errorHandler); | 
 | 2429 |     Py_XDECREF(exc); | 
 | 2430 |     return NULL; | 
 | 2431 | } | 
 | 2432 |  | 
 | 2433 | PyObject * | 
 | 2434 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2435 |                       Py_ssize_t size, | 
 | 2436 |                       const char *errors, | 
 | 2437 |                       int byteorder) | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2438 | { | 
 | 2439 |     PyObject *v; | 
 | 2440 |     unsigned char *p; | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2441 |     Py_ssize_t nsize, bytesize; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2442 | #ifndef Py_UNICODE_WIDE | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2443 |     Py_ssize_t i, pairs; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2444 | #else | 
 | 2445 |     const int pairs = 0; | 
 | 2446 | #endif | 
 | 2447 |     /* Offsets from p for storing byte pairs in the right order. */ | 
 | 2448 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 2449 |     int iorder[] = {0, 1, 2, 3}; | 
 | 2450 | #else | 
 | 2451 |     int iorder[] = {3, 2, 1, 0}; | 
 | 2452 | #endif | 
 | 2453 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2454 | #define STORECHAR(CH)                           \ | 
 | 2455 |     do {                                        \ | 
 | 2456 |         p[iorder[3]] = ((CH) >> 24) & 0xff;     \ | 
 | 2457 |         p[iorder[2]] = ((CH) >> 16) & 0xff;     \ | 
 | 2458 |         p[iorder[1]] = ((CH) >> 8) & 0xff;      \ | 
 | 2459 |         p[iorder[0]] = (CH) & 0xff;             \ | 
 | 2460 |         p += 4;                                 \ | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2461 |     } while(0) | 
 | 2462 |  | 
| Serhiy Storchaka | e8c9e14 | 2015-01-18 11:42:50 +0200 | [diff] [blame] | 2463 |     /* In narrow builds we can output surrogate pairs as one code point, | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2464 |        so we need less space. */ | 
 | 2465 | #ifndef Py_UNICODE_WIDE | 
 | 2466 |     for (i = pairs = 0; i < size-1; i++) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2467 |         if (0xD800 <= s[i] && s[i] <= 0xDBFF && | 
 | 2468 |             0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) | 
 | 2469 |             pairs++; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2470 | #endif | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2471 |     nsize = (size - pairs + (byteorder == 0)); | 
 | 2472 |     bytesize = nsize * 4; | 
 | 2473 |     if (bytesize / 4 != nsize) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2474 |         return PyErr_NoMemory(); | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2475 |     v = PyString_FromStringAndSize(NULL, bytesize); | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2476 |     if (v == NULL) | 
 | 2477 |         return NULL; | 
 | 2478 |  | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2479 |     p = (unsigned char *)PyString_AS_STRING(v); | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2480 |     if (byteorder == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2481 |         STORECHAR(0xFEFF); | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2482 |     if (size == 0) | 
 | 2483 |         return v; | 
 | 2484 |  | 
 | 2485 |     if (byteorder == -1) { | 
 | 2486 |         /* force LE */ | 
 | 2487 |         iorder[0] = 0; | 
 | 2488 |         iorder[1] = 1; | 
 | 2489 |         iorder[2] = 2; | 
 | 2490 |         iorder[3] = 3; | 
 | 2491 |     } | 
 | 2492 |     else if (byteorder == 1) { | 
 | 2493 |         /* force BE */ | 
 | 2494 |         iorder[0] = 3; | 
 | 2495 |         iorder[1] = 2; | 
 | 2496 |         iorder[2] = 1; | 
 | 2497 |         iorder[3] = 0; | 
 | 2498 |     } | 
 | 2499 |  | 
 | 2500 |     while (size-- > 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2501 |         Py_UCS4 ch = *s++; | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2502 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2503 |         if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { | 
 | 2504 |             Py_UCS4 ch2 = *s; | 
 | 2505 |             if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | 
 | 2506 |                 ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | 
 | 2507 |                 s++; | 
 | 2508 |                 size--; | 
 | 2509 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2510 |         } | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2511 | #endif | 
 | 2512 |         STORECHAR(ch); | 
 | 2513 |     } | 
 | 2514 |     return v; | 
 | 2515 | #undef STORECHAR | 
 | 2516 | } | 
 | 2517 |  | 
 | 2518 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) | 
 | 2519 | { | 
 | 2520 |     if (!PyUnicode_Check(unicode)) { | 
 | 2521 |         PyErr_BadArgument(); | 
 | 2522 |         return NULL; | 
 | 2523 |     } | 
 | 2524 |     return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2525 |                                  PyUnicode_GET_SIZE(unicode), | 
 | 2526 |                                  NULL, | 
 | 2527 |                                  0); | 
| Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2528 | } | 
 | 2529 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2530 | /* --- UTF-16 Codec ------------------------------------------------------- */ | 
 | 2531 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2532 | PyObject * | 
 | 2533 | PyUnicode_DecodeUTF16(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2534 |                       Py_ssize_t size, | 
 | 2535 |                       const char *errors, | 
 | 2536 |                       int *byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2537 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2538 |     return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); | 
 | 2539 | } | 
 | 2540 |  | 
 | 2541 | PyObject * | 
 | 2542 | PyUnicode_DecodeUTF16Stateful(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2543 |                               Py_ssize_t size, | 
 | 2544 |                               const char *errors, | 
 | 2545 |                               int *byteorder, | 
 | 2546 |                               Py_ssize_t *consumed) | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2547 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2548 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2549 |     Py_ssize_t startinpos; | 
 | 2550 |     Py_ssize_t endinpos; | 
 | 2551 |     Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2552 |     PyUnicodeObject *unicode; | 
 | 2553 |     Py_UNICODE *p; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2554 |     const unsigned char *q, *e; | 
 | 2555 |     int bo = 0;       /* assume native ordering by default */ | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2556 |     const char *errmsg = ""; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2557 |     /* Offsets from q for retrieving byte pairs in the right order. */ | 
 | 2558 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 2559 |     int ihi = 1, ilo = 0; | 
 | 2560 | #else | 
 | 2561 |     int ihi = 0, ilo = 1; | 
 | 2562 | #endif | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2563 |     PyObject *errorHandler = NULL; | 
 | 2564 |     PyObject *exc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2565 |  | 
 | 2566 |     /* Note: size will always be longer than the resulting Unicode | 
 | 2567 |        character count */ | 
 | 2568 |     unicode = _PyUnicode_New(size); | 
 | 2569 |     if (!unicode) | 
 | 2570 |         return NULL; | 
 | 2571 |     if (size == 0) | 
 | 2572 |         return (PyObject *)unicode; | 
 | 2573 |  | 
 | 2574 |     /* Unpack UTF-16 encoded data */ | 
 | 2575 |     p = unicode->str; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2576 |     q = (unsigned char *)s; | 
 | 2577 |     e = q + size; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2578 |  | 
 | 2579 |     if (byteorder) | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2580 |         bo = *byteorder; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2581 |  | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2582 |     /* Check for BOM marks (U+FEFF) in the input and adjust current | 
 | 2583 |        byte order setting accordingly. In native mode, the leading BOM | 
 | 2584 |        mark is skipped, in all other modes, it is copied to the output | 
 | 2585 |        stream as-is (giving a ZWNBSP character). */ | 
 | 2586 |     if (bo == 0) { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2587 |         if (size >= 2) { | 
 | 2588 |             const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2589 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2590 |             if (bom == 0xFEFF) { | 
 | 2591 |                 q += 2; | 
 | 2592 |                 bo = -1; | 
 | 2593 |             } | 
 | 2594 |             else if (bom == 0xFFFE) { | 
 | 2595 |                 q += 2; | 
 | 2596 |                 bo = 1; | 
 | 2597 |             } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2598 | #else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2599 |             if (bom == 0xFEFF) { | 
 | 2600 |                 q += 2; | 
 | 2601 |                 bo = 1; | 
 | 2602 |             } | 
 | 2603 |             else if (bom == 0xFFFE) { | 
 | 2604 |                 q += 2; | 
 | 2605 |                 bo = -1; | 
 | 2606 |             } | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2607 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2608 |         } | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2609 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2610 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2611 |     if (bo == -1) { | 
 | 2612 |         /* force LE */ | 
 | 2613 |         ihi = 1; | 
 | 2614 |         ilo = 0; | 
 | 2615 |     } | 
 | 2616 |     else if (bo == 1) { | 
 | 2617 |         /* force BE */ | 
 | 2618 |         ihi = 0; | 
 | 2619 |         ilo = 1; | 
 | 2620 |     } | 
 | 2621 |  | 
 | 2622 |     while (q < e) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2623 |         Py_UNICODE ch; | 
 | 2624 |         /* remaining bytes at the end? (size should be even) */ | 
 | 2625 |         if (e-q<2) { | 
 | 2626 |             if (consumed) | 
 | 2627 |                 break; | 
 | 2628 |             errmsg = "truncated data"; | 
 | 2629 |             startinpos = ((const char *)q)-starts; | 
 | 2630 |             endinpos = ((const char *)e)-starts; | 
 | 2631 |             goto utf16Error; | 
 | 2632 |             /* The remaining input chars are ignored if the callback | 
 | 2633 |                chooses to skip the input */ | 
 | 2634 |         } | 
 | 2635 |         ch = (q[ihi] << 8) | q[ilo]; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2636 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2637 |         q += 2; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2638 |  | 
 | 2639 |         if (ch < 0xD800 || ch > 0xDFFF) { | 
 | 2640 |             *p++ = ch; | 
 | 2641 |             continue; | 
 | 2642 |         } | 
 | 2643 |  | 
 | 2644 |         /* UTF-16 code pair: */ | 
| Antoine Pitrou | 715a63b | 2012-07-21 00:52:06 +0200 | [diff] [blame] | 2645 |         if (e - q < 2) { | 
| Serhiy Storchaka | c4b82c0 | 2013-01-08 23:12:00 +0200 | [diff] [blame] | 2646 |             q -= 2; | 
 | 2647 |             if (consumed) | 
 | 2648 |                 break; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2649 |             errmsg = "unexpected end of data"; | 
| Serhiy Storchaka | c4b82c0 | 2013-01-08 23:12:00 +0200 | [diff] [blame] | 2650 |             startinpos = ((const char *)q)-starts; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2651 |             endinpos = ((const char *)e)-starts; | 
 | 2652 |             goto utf16Error; | 
 | 2653 |         } | 
 | 2654 |         if (0xD800 <= ch && ch <= 0xDBFF) { | 
 | 2655 |             Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; | 
 | 2656 |             q += 2; | 
 | 2657 |             if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2658 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2659 |                 *p++ = ch; | 
 | 2660 |                 *p++ = ch2; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2661 | #else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2662 |                 *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2663 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2664 |                 continue; | 
 | 2665 |             } | 
 | 2666 |             else { | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2667 |                 errmsg = "illegal UTF-16 surrogate"; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2668 |                 startinpos = (((const char *)q)-4)-starts; | 
 | 2669 |                 endinpos = startinpos+2; | 
 | 2670 |                 goto utf16Error; | 
 | 2671 |             } | 
 | 2672 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 2673 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2674 |         errmsg = "illegal encoding"; | 
 | 2675 |         startinpos = (((const char *)q)-2)-starts; | 
 | 2676 |         endinpos = startinpos+2; | 
 | 2677 |         /* Fall through to report the error */ | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2678 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2679 |       utf16Error: | 
 | 2680 |         outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 2681 |         if (unicode_decode_call_errorhandler( | 
 | 2682 |                 errors, &errorHandler, | 
 | 2683 |                 "utf16", errmsg, | 
 | 2684 |                 starts, size, &startinpos, &endinpos, &exc, (const char **)&q, | 
 | 2685 |                 &unicode, &outpos, &p)) | 
 | 2686 |             goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2687 |     } | 
 | 2688 |  | 
 | 2689 |     if (byteorder) | 
 | 2690 |         *byteorder = bo; | 
 | 2691 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2692 |     if (consumed) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2693 |         *consumed = (const char *)q-starts; | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2694 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2695 |     /* Adjust length */ | 
| Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2696 |     if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2697 |         goto onError; | 
 | 2698 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2699 |     Py_XDECREF(errorHandler); | 
 | 2700 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2701 |     return (PyObject *)unicode; | 
 | 2702 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2703 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2704 |     Py_DECREF(unicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2705 |     Py_XDECREF(errorHandler); | 
 | 2706 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2707 |     return NULL; | 
 | 2708 | } | 
 | 2709 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2710 | PyObject * | 
 | 2711 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2712 |                       Py_ssize_t size, | 
 | 2713 |                       const char *errors, | 
 | 2714 |                       int byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2715 | { | 
 | 2716 |     PyObject *v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2717 |     unsigned char *p; | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2718 |     Py_ssize_t nsize, bytesize; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2719 | #ifdef Py_UNICODE_WIDE | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2720 |     Py_ssize_t i, pairs; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2721 | #else | 
 | 2722 |     const int pairs = 0; | 
 | 2723 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2724 |     /* Offsets from p for storing byte pairs in the right order. */ | 
 | 2725 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 2726 |     int ihi = 1, ilo = 0; | 
 | 2727 | #else | 
 | 2728 |     int ihi = 0, ilo = 1; | 
 | 2729 | #endif | 
 | 2730 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2731 | #define STORECHAR(CH)                           \ | 
 | 2732 |     do {                                        \ | 
 | 2733 |         p[ihi] = ((CH) >> 8) & 0xff;            \ | 
 | 2734 |         p[ilo] = (CH) & 0xff;                   \ | 
 | 2735 |         p += 2;                                 \ | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2736 |     } while(0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2737 |  | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2738 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2739 |     for (i = pairs = 0; i < size; i++) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2740 |         if (s[i] >= 0x10000) | 
 | 2741 |             pairs++; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2742 | #endif | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2743 |     /* 2 * (size + pairs + (byteorder == 0)) */ | 
 | 2744 |     if (size > PY_SSIZE_T_MAX || | 
 | 2745 |         size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2746 |         return PyErr_NoMemory(); | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2747 |     nsize = size + pairs + (byteorder == 0); | 
 | 2748 |     bytesize = nsize * 2; | 
 | 2749 |     if (bytesize / 2 != nsize) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2750 |         return PyErr_NoMemory(); | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2751 |     v = PyString_FromStringAndSize(NULL, bytesize); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2752 |     if (v == NULL) | 
 | 2753 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2754 |  | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2755 |     p = (unsigned char *)PyString_AS_STRING(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2756 |     if (byteorder == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2757 |         STORECHAR(0xFEFF); | 
| Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2758 |     if (size == 0) | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2759 |         return v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2760 |  | 
 | 2761 |     if (byteorder == -1) { | 
 | 2762 |         /* force LE */ | 
 | 2763 |         ihi = 1; | 
 | 2764 |         ilo = 0; | 
 | 2765 |     } | 
 | 2766 |     else if (byteorder == 1) { | 
 | 2767 |         /* force BE */ | 
 | 2768 |         ihi = 0; | 
 | 2769 |         ilo = 1; | 
 | 2770 |     } | 
 | 2771 |  | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2772 |     while (size-- > 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2773 |         Py_UNICODE ch = *s++; | 
 | 2774 |         Py_UNICODE ch2 = 0; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2775 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2776 |         if (ch >= 0x10000) { | 
 | 2777 |             ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); | 
 | 2778 |             ch  = 0xD800 | ((ch-0x10000) >> 10); | 
 | 2779 |         } | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2780 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2781 |         STORECHAR(ch); | 
 | 2782 |         if (ch2) | 
 | 2783 |             STORECHAR(ch2); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2784 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2785 |     return v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2786 | #undef STORECHAR | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2787 | } | 
 | 2788 |  | 
 | 2789 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) | 
 | 2790 | { | 
 | 2791 |     if (!PyUnicode_Check(unicode)) { | 
 | 2792 |         PyErr_BadArgument(); | 
 | 2793 |         return NULL; | 
 | 2794 |     } | 
 | 2795 |     return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2796 |                                  PyUnicode_GET_SIZE(unicode), | 
 | 2797 |                                  NULL, | 
 | 2798 |                                  0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2799 | } | 
 | 2800 |  | 
 | 2801 | /* --- Unicode Escape Codec ----------------------------------------------- */ | 
 | 2802 |  | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2803 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; | 
| Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2804 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2805 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2806 |                                         Py_ssize_t size, | 
 | 2807 |                                         const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2808 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2809 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2810 |     Py_ssize_t startinpos; | 
 | 2811 |     Py_ssize_t endinpos; | 
 | 2812 |     Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2813 |     PyUnicodeObject *v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2814 |     Py_UNICODE *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 |     const char *end; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2816 |     char* message; | 
 | 2817 |     Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2818 |     PyObject *errorHandler = NULL; | 
 | 2819 |     PyObject *exc = NULL; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2820 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2821 |     /* Escaped strings will always be longer than the resulting | 
 | 2822 |        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] | 2823 |        length after conversion to the true value. | 
 | 2824 |        (but if the error callback returns a long replacement string | 
 | 2825 |        we'll have to allocate more space) */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2826 |     v = _PyUnicode_New(size); | 
 | 2827 |     if (v == NULL) | 
 | 2828 |         goto onError; | 
 | 2829 |     if (size == 0) | 
 | 2830 |         return (PyObject *)v; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2831 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2832 |     p = PyUnicode_AS_UNICODE(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2833 |     end = s + size; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2834 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2835 |     while (s < end) { | 
 | 2836 |         unsigned char c; | 
| Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2837 |         Py_UNICODE x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2838 |         int digits; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 |  | 
 | 2840 |         /* Non-escape characters are interpreted as Unicode ordinals */ | 
 | 2841 |         if (*s != '\\') { | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2842 |             *p++ = (unsigned char) *s++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2843 |             continue; | 
 | 2844 |         } | 
 | 2845 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2846 |         startinpos = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2847 |         /* \ - Escapes */ | 
 | 2848 |         s++; | 
| Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2849 |         c = *s++; | 
 | 2850 |         if (s > end) | 
 | 2851 |             c = '\0'; /* Invalid after \ */ | 
 | 2852 |         switch (c) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2853 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2854 |             /* \x escapes */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2855 |         case '\n': break; | 
 | 2856 |         case '\\': *p++ = '\\'; break; | 
 | 2857 |         case '\'': *p++ = '\''; break; | 
 | 2858 |         case '\"': *p++ = '\"'; break; | 
 | 2859 |         case 'b': *p++ = '\b'; break; | 
 | 2860 |         case 'f': *p++ = '\014'; break; /* FF */ | 
 | 2861 |         case 't': *p++ = '\t'; break; | 
 | 2862 |         case 'n': *p++ = '\n'; break; | 
 | 2863 |         case 'r': *p++ = '\r'; break; | 
 | 2864 |         case 'v': *p++ = '\013'; break; /* VT */ | 
 | 2865 |         case 'a': *p++ = '\007'; break; /* BEL, not classic C */ | 
 | 2866 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2867 |             /* \OOO (octal) escapes */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2868 |         case '0': case '1': case '2': case '3': | 
 | 2869 |         case '4': case '5': case '6': case '7': | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2870 |             x = s[-1] - '0'; | 
| Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2871 |             if (s < end && '0' <= *s && *s <= '7') { | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2872 |                 x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2873 |                 if (s < end && '0' <= *s && *s <= '7') | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2874 |                     x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 |             } | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2876 |             *p++ = x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2877 |             break; | 
 | 2878 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2879 |             /* hex escapes */ | 
 | 2880 |             /* \xXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2881 |         case 'x': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2882 |             digits = 2; | 
 | 2883 |             message = "truncated \\xXX escape"; | 
 | 2884 |             goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2885 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2886 |             /* \uXXXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2887 |         case 'u': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2888 |             digits = 4; | 
 | 2889 |             message = "truncated \\uXXXX escape"; | 
 | 2890 |             goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2891 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2892 |             /* \UXXXXXXXX */ | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2893 |         case 'U': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2894 |             digits = 8; | 
 | 2895 |             message = "truncated \\UXXXXXXXX escape"; | 
 | 2896 |         hexescape: | 
 | 2897 |             chr = 0; | 
| Serhiy Storchaka | c8e5812 | 2013-01-29 10:20:34 +0200 | [diff] [blame] | 2898 |             if (end - s < digits) { | 
 | 2899 |                 /* count only hex digits */ | 
 | 2900 |                 for (; s < end; ++s) { | 
 | 2901 |                     c = (unsigned char)*s; | 
 | 2902 |                     if (!Py_ISXDIGIT(c)) | 
 | 2903 |                         goto error; | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2904 |                 } | 
| Serhiy Storchaka | c8e5812 | 2013-01-29 10:20:34 +0200 | [diff] [blame] | 2905 |                 goto error; | 
 | 2906 |             } | 
 | 2907 |             for (; digits--; ++s) { | 
 | 2908 |                 c = (unsigned char)*s; | 
 | 2909 |                 if (!Py_ISXDIGIT(c)) | 
 | 2910 |                     goto error; | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2911 |                 chr = (chr<<4) & ~0xF; | 
 | 2912 |                 if (c >= '0' && c <= '9') | 
 | 2913 |                     chr += c - '0'; | 
 | 2914 |                 else if (c >= 'a' && c <= 'f') | 
 | 2915 |                     chr += 10 + c - 'a'; | 
 | 2916 |                 else | 
 | 2917 |                     chr += 10 + c - 'A'; | 
 | 2918 |             } | 
| Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2919 |             if (chr == 0xffffffff && PyErr_Occurred()) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2920 |                 /* _decoding_error will have already written into the | 
 | 2921 |                    target buffer. */ | 
 | 2922 |                 break; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2923 |         store: | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2924 |             /* when we get here, chr is a 32-bit unicode character */ | 
 | 2925 |             if (chr <= 0xffff) | 
 | 2926 |                 /* UCS-2 character */ | 
 | 2927 |                 *p++ = (Py_UNICODE) chr; | 
 | 2928 |             else if (chr <= 0x10ffff) { | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2929 |                 /* UCS-4 character. Either store directly, or as | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2930 |                    surrogate pair. */ | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2931 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2932 |                 *p++ = chr; | 
 | 2933 | #else | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2934 |                 chr -= 0x10000L; | 
 | 2935 |                 *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2936 |                 *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2937 | #endif | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2938 |             } else { | 
| Serhiy Storchaka | c8e5812 | 2013-01-29 10:20:34 +0200 | [diff] [blame] | 2939 |                 message = "illegal Unicode character"; | 
 | 2940 |                 goto error; | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2941 |             } | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2942 |             break; | 
 | 2943 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 2944 |             /* \N{name} */ | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2945 |         case 'N': | 
 | 2946 |             message = "malformed \\N character escape"; | 
 | 2947 |             if (ucnhash_CAPI == NULL) { | 
 | 2948 |                 /* load the unicode data module */ | 
| Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2949 |                 ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2950 |                 if (ucnhash_CAPI == NULL) | 
 | 2951 |                     goto ucnhashError; | 
 | 2952 |             } | 
 | 2953 |             if (*s == '{') { | 
 | 2954 |                 const char *start = s+1; | 
 | 2955 |                 /* look for the closing brace */ | 
 | 2956 |                 while (*s != '}' && s < end) | 
 | 2957 |                     s++; | 
 | 2958 |                 if (s > start && s < end && *s == '}') { | 
 | 2959 |                     /* found a name.  look it up in the unicode database */ | 
 | 2960 |                     message = "unknown Unicode character name"; | 
 | 2961 |                     s++; | 
| Serhiy Storchaka | 1d3acd4 | 2013-01-21 11:48:24 +0200 | [diff] [blame] | 2962 |                     if (s - start - 1 <= INT_MAX && | 
 | 2963 |                         ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2964 |                         goto store; | 
 | 2965 |                 } | 
 | 2966 |             } | 
| Serhiy Storchaka | c8e5812 | 2013-01-29 10:20:34 +0200 | [diff] [blame] | 2967 |             goto error; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2968 |  | 
 | 2969 |         default: | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2970 |             if (s > end) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2971 |                 message = "\\ at end of string"; | 
 | 2972 |                 s--; | 
| Serhiy Storchaka | c8e5812 | 2013-01-29 10:20:34 +0200 | [diff] [blame] | 2973 |                 goto error; | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2974 |             } | 
 | 2975 |             else { | 
 | 2976 |                 *p++ = '\\'; | 
 | 2977 |                 *p++ = (unsigned char)s[-1]; | 
 | 2978 |             } | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2979 |             break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2980 |         } | 
| Serhiy Storchaka | c8e5812 | 2013-01-29 10:20:34 +0200 | [diff] [blame] | 2981 |         continue; | 
 | 2982 |  | 
 | 2983 |       error: | 
 | 2984 |         endinpos = s-starts; | 
 | 2985 |         outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 2986 |         if (unicode_decode_call_errorhandler( | 
 | 2987 |                 errors, &errorHandler, | 
 | 2988 |                 "unicodeescape", message, | 
 | 2989 |                 starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 2990 |                 &v, &outpos, &p)) | 
 | 2991 |             goto onError; | 
 | 2992 |         continue; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2993 |     } | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2994 |     if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2995 |         goto onError; | 
| Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2996 |     Py_XDECREF(errorHandler); | 
 | 2997 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 |     return (PyObject *)v; | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2999 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3000 |   ucnhashError: | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3001 |     PyErr_SetString( | 
 | 3002 |         PyExc_UnicodeError, | 
 | 3003 |         "\\N escapes not supported (can't load unicodedata module)" | 
 | 3004 |         ); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3005 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3006 |     Py_XDECREF(errorHandler); | 
 | 3007 |     Py_XDECREF(exc); | 
| Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3008 |     return NULL; | 
 | 3009 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3010 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3011 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3012 |     Py_XDECREF(errorHandler); | 
 | 3013 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3014 |     return NULL; | 
 | 3015 | } | 
 | 3016 |  | 
 | 3017 | /* Return a Unicode-Escape string version of the Unicode object. | 
 | 3018 |  | 
 | 3019 |    If quotes is true, the string is enclosed in u"" or u'' quotes as | 
 | 3020 |    appropriate. | 
 | 3021 |  | 
 | 3022 | */ | 
 | 3023 |  | 
| Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 3024 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3025 |                                              Py_ssize_t size, | 
 | 3026 |                                              Py_UNICODE ch) | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 3027 | { | 
 | 3028 |     /* like wcschr, but doesn't stop at NULL characters */ | 
 | 3029 |  | 
 | 3030 |     while (size-- > 0) { | 
 | 3031 |         if (*s == ch) | 
 | 3032 |             return s; | 
 | 3033 |         s++; | 
 | 3034 |     } | 
 | 3035 |  | 
 | 3036 |     return NULL; | 
 | 3037 | } | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3038 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | static | 
 | 3040 | PyObject *unicodeescape_string(const Py_UNICODE *s, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3041 |                                Py_ssize_t size, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3042 |                                int quotes) | 
 | 3043 | { | 
 | 3044 |     PyObject *repr; | 
 | 3045 |     char *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3046 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3047 |     static const char *hexdigit = "0123456789abcdef"; | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3048 | #ifdef Py_UNICODE_WIDE | 
 | 3049 |     const Py_ssize_t expandsize = 10; | 
 | 3050 | #else | 
 | 3051 |     const Py_ssize_t expandsize = 6; | 
 | 3052 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3053 |  | 
| Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 3054 |     /* XXX(nnorwitz): rather than over-allocating, it would be | 
 | 3055 |        better to choose a different scheme.  Perhaps scan the | 
 | 3056 |        first N-chars of the string and allocate based on that size. | 
 | 3057 |     */ | 
 | 3058 |     /* Initial allocation is based on the longest-possible unichr | 
 | 3059 |        escape. | 
 | 3060 |  | 
 | 3061 |        In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source | 
 | 3062 |        unichr, so in this case it's the longest unichr escape. In | 
 | 3063 |        narrow (UTF-16) builds this is five chars per source unichr | 
 | 3064 |        since there are two unichrs in the surrogate pair, so in narrow | 
 | 3065 |        (UTF-16) builds it's not the longest unichr escape. | 
 | 3066 |  | 
 | 3067 |        In wide or narrow builds '\uxxxx' is 6 chars per source unichr, | 
 | 3068 |        so in the narrow (UTF-16) build case it's the longest unichr | 
 | 3069 |        escape. | 
 | 3070 |     */ | 
 | 3071 |  | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3072 |     if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3073 |         return PyErr_NoMemory(); | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3074 |  | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3075 |     repr = PyString_FromStringAndSize(NULL, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3076 |                                       2 | 
 | 3077 |                                       + expandsize*size | 
 | 3078 |                                       + 1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3079 |     if (repr == NULL) | 
 | 3080 |         return NULL; | 
 | 3081 |  | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3082 |     p = PyString_AS_STRING(repr); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3083 |  | 
 | 3084 |     if (quotes) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3085 |         *p++ = 'u'; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3086 |         *p++ = (findchar(s, size, '\'') && | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3087 |                 !findchar(s, size, '"')) ? '"' : '\''; | 
 | 3088 |     } | 
 | 3089 |     while (size-- > 0) { | 
 | 3090 |         Py_UNICODE ch = *s++; | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3091 |  | 
| Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 3092 |         /* Escape quotes and backslashes */ | 
 | 3093 |         if ((quotes && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3094 |              ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3095 |             *p++ = '\\'; | 
 | 3096 |             *p++ = (char) ch; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3097 |             continue; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3098 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3099 |  | 
| Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3100 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3101 |         /* Map 21-bit characters to '\U00xxxxxx' */ | 
 | 3102 |         else if (ch >= 0x10000) { | 
 | 3103 |             *p++ = '\\'; | 
 | 3104 |             *p++ = 'U'; | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3105 |             *p++ = hexdigit[(ch >> 28) & 0x0000000F]; | 
 | 3106 |             *p++ = hexdigit[(ch >> 24) & 0x0000000F]; | 
 | 3107 |             *p++ = hexdigit[(ch >> 20) & 0x0000000F]; | 
 | 3108 |             *p++ = hexdigit[(ch >> 16) & 0x0000000F]; | 
 | 3109 |             *p++ = hexdigit[(ch >> 12) & 0x0000000F]; | 
 | 3110 |             *p++ = hexdigit[(ch >> 8) & 0x0000000F]; | 
 | 3111 |             *p++ = hexdigit[(ch >> 4) & 0x0000000F]; | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3112 |             *p++ = hexdigit[ch & 0x0000000F]; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3113 |             continue; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3114 |         } | 
| Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 3115 | #else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3116 |         /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ | 
 | 3117 |         else if (ch >= 0xD800 && ch < 0xDC00) { | 
 | 3118 |             Py_UNICODE ch2; | 
 | 3119 |             Py_UCS4 ucs; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3120 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3121 |             ch2 = *s++; | 
 | 3122 |             size--; | 
| Georg Brandl | e27d044 | 2010-08-01 20:54:30 +0000 | [diff] [blame] | 3123 |             if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3124 |                 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | 
 | 3125 |                 *p++ = '\\'; | 
 | 3126 |                 *p++ = 'U'; | 
 | 3127 |                 *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; | 
 | 3128 |                 *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; | 
 | 3129 |                 *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; | 
 | 3130 |                 *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; | 
 | 3131 |                 *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; | 
 | 3132 |                 *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; | 
 | 3133 |                 *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; | 
 | 3134 |                 *p++ = hexdigit[ucs & 0x0000000F]; | 
 | 3135 |                 continue; | 
 | 3136 |             } | 
 | 3137 |             /* Fall through: isolated surrogates are copied as-is */ | 
 | 3138 |             s--; | 
 | 3139 |             size++; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3140 |         } | 
| Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 3141 | #endif | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3142 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3143 |         /* Map 16-bit characters to '\uxxxx' */ | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3144 |         if (ch >= 256) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3145 |             *p++ = '\\'; | 
 | 3146 |             *p++ = 'u'; | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3147 |             *p++ = hexdigit[(ch >> 12) & 0x000F]; | 
 | 3148 |             *p++ = hexdigit[(ch >> 8) & 0x000F]; | 
 | 3149 |             *p++ = hexdigit[(ch >> 4) & 0x000F]; | 
 | 3150 |             *p++ = hexdigit[ch & 0x000F]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3151 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3152 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3153 |         /* Map special whitespace to '\t', \n', '\r' */ | 
 | 3154 |         else if (ch == '\t') { | 
 | 3155 |             *p++ = '\\'; | 
 | 3156 |             *p++ = 't'; | 
 | 3157 |         } | 
 | 3158 |         else if (ch == '\n') { | 
 | 3159 |             *p++ = '\\'; | 
 | 3160 |             *p++ = 'n'; | 
 | 3161 |         } | 
 | 3162 |         else if (ch == '\r') { | 
 | 3163 |             *p++ = '\\'; | 
 | 3164 |             *p++ = 'r'; | 
 | 3165 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3166 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3167 |         /* Map non-printable US ASCII to '\xhh' */ | 
| Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3168 |         else if (ch < ' ' || ch >= 0x7F) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3169 |             *p++ = '\\'; | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3170 |             *p++ = 'x'; | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3171 |             *p++ = hexdigit[(ch >> 4) & 0x000F]; | 
 | 3172 |             *p++ = hexdigit[ch & 0x000F]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3173 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3174 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3175 |         /* Copy everything else as-is */ | 
 | 3176 |         else | 
 | 3177 |             *p++ = (char) ch; | 
 | 3178 |     } | 
 | 3179 |     if (quotes) | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3180 |         *p++ = PyString_AS_STRING(repr)[1]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3181 |  | 
 | 3182 |     *p = '\0'; | 
| Benjamin Peterson | bea424a | 2010-04-03 00:57:33 +0000 | [diff] [blame] | 3183 |     if (_PyString_Resize(&repr, p - PyString_AS_STRING(repr))) | 
 | 3184 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3185 |     return repr; | 
 | 3186 | } | 
 | 3187 |  | 
 | 3188 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3189 |                                         Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3190 | { | 
 | 3191 |     return unicodeescape_string(s, size, 0); | 
 | 3192 | } | 
 | 3193 |  | 
 | 3194 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) | 
 | 3195 | { | 
 | 3196 |     if (!PyUnicode_Check(unicode)) { | 
 | 3197 |         PyErr_BadArgument(); | 
 | 3198 |         return NULL; | 
 | 3199 |     } | 
 | 3200 |     return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3201 |                                          PyUnicode_GET_SIZE(unicode)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3202 | } | 
 | 3203 |  | 
 | 3204 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ | 
 | 3205 |  | 
 | 3206 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3207 |                                            Py_ssize_t size, | 
 | 3208 |                                            const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3209 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3210 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3211 |     Py_ssize_t startinpos; | 
 | 3212 |     Py_ssize_t endinpos; | 
 | 3213 |     Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3214 |     PyUnicodeObject *v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3215 |     Py_UNICODE *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3216 |     const char *end; | 
 | 3217 |     const char *bs; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3218 |     PyObject *errorHandler = NULL; | 
 | 3219 |     PyObject *exc = NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3220 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3221 |     /* Escaped strings will always be longer than the resulting | 
 | 3222 |        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] | 3223 |        length after conversion to the true value. (But decoding error | 
 | 3224 |        handler might have to resize the string) */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3225 |     v = _PyUnicode_New(size); | 
 | 3226 |     if (v == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3227 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3228 |     if (size == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3229 |         return (PyObject *)v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3230 |     p = PyUnicode_AS_UNICODE(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3231 |     end = s + size; | 
 | 3232 |     while (s < end) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3233 |         unsigned char c; | 
 | 3234 |         Py_UCS4 x; | 
 | 3235 |         int i; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3236 |         int count; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3237 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3238 |         /* Non-escape characters are interpreted as Unicode ordinals */ | 
 | 3239 |         if (*s != '\\') { | 
 | 3240 |             *p++ = (unsigned char)*s++; | 
 | 3241 |             continue; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3242 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3243 |         startinpos = s-starts; | 
 | 3244 |  | 
 | 3245 |         /* \u-escapes are only interpreted iff the number of leading | 
 | 3246 |            backslashes if odd */ | 
 | 3247 |         bs = s; | 
 | 3248 |         for (;s < end;) { | 
 | 3249 |             if (*s != '\\') | 
 | 3250 |                 break; | 
 | 3251 |             *p++ = (unsigned char)*s++; | 
 | 3252 |         } | 
 | 3253 |         if (((s - bs) & 1) == 0 || | 
 | 3254 |             s >= end || | 
 | 3255 |             (*s != 'u' && *s != 'U')) { | 
 | 3256 |             continue; | 
 | 3257 |         } | 
 | 3258 |         p--; | 
 | 3259 |         count = *s=='u' ? 4 : 8; | 
 | 3260 |         s++; | 
 | 3261 |  | 
 | 3262 |         /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ | 
 | 3263 |         outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 3264 |         for (x = 0, i = 0; i < count; ++i, ++s) { | 
 | 3265 |             c = (unsigned char)*s; | 
 | 3266 |             if (!isxdigit(c)) { | 
 | 3267 |                 endinpos = s-starts; | 
 | 3268 |                 if (unicode_decode_call_errorhandler( | 
 | 3269 |                         errors, &errorHandler, | 
 | 3270 |                         "rawunicodeescape", "truncated \\uXXXX", | 
 | 3271 |                         starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 3272 |                         &v, &outpos, &p)) | 
 | 3273 |                     goto onError; | 
 | 3274 |                 goto nextByte; | 
 | 3275 |             } | 
 | 3276 |             x = (x<<4) & ~0xF; | 
 | 3277 |             if (c >= '0' && c <= '9') | 
 | 3278 |                 x += c - '0'; | 
 | 3279 |             else if (c >= 'a' && c <= 'f') | 
 | 3280 |                 x += 10 + c - 'a'; | 
 | 3281 |             else | 
 | 3282 |                 x += 10 + c - 'A'; | 
 | 3283 |         } | 
| Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3284 |         if (x <= 0xffff) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3285 |             /* UCS-2 character */ | 
 | 3286 |             *p++ = (Py_UNICODE) x; | 
| Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3287 |         else if (x <= 0x10ffff) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3288 |             /* UCS-4 character. Either store directly, or as | 
 | 3289 |                surrogate pair. */ | 
| Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3290 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3291 |             *p++ = (Py_UNICODE) x; | 
| Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3292 | #else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3293 |             x -= 0x10000L; | 
 | 3294 |             *p++ = 0xD800 + (Py_UNICODE) (x >> 10); | 
 | 3295 |             *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); | 
| Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3296 | #endif | 
 | 3297 |         } else { | 
 | 3298 |             endinpos = s-starts; | 
 | 3299 |             outpos = p-PyUnicode_AS_UNICODE(v); | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3300 |             if (unicode_decode_call_errorhandler( | 
 | 3301 |                     errors, &errorHandler, | 
 | 3302 |                     "rawunicodeescape", "\\Uxxxxxxxx out of range", | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3303 |                     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 3304 |                     &v, &outpos, &p)) | 
 | 3305 |                 goto onError; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3306 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3307 |       nextByte: | 
 | 3308 |         ; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3309 |     } | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3310 |     if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3311 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3312 |     Py_XDECREF(errorHandler); | 
 | 3313 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3314 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3315 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3316 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3318 |     Py_XDECREF(errorHandler); | 
 | 3319 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3320 |     return NULL; | 
 | 3321 | } | 
 | 3322 |  | 
 | 3323 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3324 |                                            Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3325 | { | 
 | 3326 |     PyObject *repr; | 
 | 3327 |     char *p; | 
 | 3328 |     char *q; | 
 | 3329 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3330 |     static const char *hexdigit = "0123456789abcdef"; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3331 | #ifdef Py_UNICODE_WIDE | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3332 |     const Py_ssize_t expandsize = 10; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3333 | #else | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3334 |     const Py_ssize_t expandsize = 6; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3335 | #endif | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3336 |  | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3337 |     if (size > PY_SSIZE_T_MAX / expandsize) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3338 |         return PyErr_NoMemory(); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3339 |  | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3340 |     repr = PyString_FromStringAndSize(NULL, expandsize * size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3341 |     if (repr == NULL) | 
 | 3342 |         return NULL; | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3343 |     if (size == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3344 |         return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3345 |  | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3346 |     p = q = PyString_AS_STRING(repr); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3347 |     while (size-- > 0) { | 
 | 3348 |         Py_UNICODE ch = *s++; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3349 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3350 |         /* Map 32-bit characters to '\Uxxxxxxxx' */ | 
 | 3351 |         if (ch >= 0x10000) { | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3352 |             *p++ = '\\'; | 
 | 3353 |             *p++ = 'U'; | 
 | 3354 |             *p++ = hexdigit[(ch >> 28) & 0xf]; | 
 | 3355 |             *p++ = hexdigit[(ch >> 24) & 0xf]; | 
 | 3356 |             *p++ = hexdigit[(ch >> 20) & 0xf]; | 
 | 3357 |             *p++ = hexdigit[(ch >> 16) & 0xf]; | 
 | 3358 |             *p++ = hexdigit[(ch >> 12) & 0xf]; | 
 | 3359 |             *p++ = hexdigit[(ch >> 8) & 0xf]; | 
 | 3360 |             *p++ = hexdigit[(ch >> 4) & 0xf]; | 
 | 3361 |             *p++ = hexdigit[ch & 15]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3362 |         } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3363 |         else | 
| Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3364 | #else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3365 |             /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ | 
 | 3366 |             if (ch >= 0xD800 && ch < 0xDC00) { | 
 | 3367 |                 Py_UNICODE ch2; | 
 | 3368 |                 Py_UCS4 ucs; | 
| Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3369 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3370 |                 ch2 = *s++; | 
 | 3371 |                 size--; | 
| Georg Brandl | e27d044 | 2010-08-01 20:54:30 +0000 | [diff] [blame] | 3372 |                 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3373 |                     ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | 
 | 3374 |                     *p++ = '\\'; | 
 | 3375 |                     *p++ = 'U'; | 
 | 3376 |                     *p++ = hexdigit[(ucs >> 28) & 0xf]; | 
 | 3377 |                     *p++ = hexdigit[(ucs >> 24) & 0xf]; | 
 | 3378 |                     *p++ = hexdigit[(ucs >> 20) & 0xf]; | 
 | 3379 |                     *p++ = hexdigit[(ucs >> 16) & 0xf]; | 
 | 3380 |                     *p++ = hexdigit[(ucs >> 12) & 0xf]; | 
 | 3381 |                     *p++ = hexdigit[(ucs >> 8) & 0xf]; | 
 | 3382 |                     *p++ = hexdigit[(ucs >> 4) & 0xf]; | 
 | 3383 |                     *p++ = hexdigit[ucs & 0xf]; | 
 | 3384 |                     continue; | 
 | 3385 |                 } | 
 | 3386 |                 /* Fall through: isolated surrogates are copied as-is */ | 
 | 3387 |                 s--; | 
 | 3388 |                 size++; | 
 | 3389 |             } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3390 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3391 |         /* Map 16-bit characters to '\uxxxx' */ | 
 | 3392 |         if (ch >= 256) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3393 |             *p++ = '\\'; | 
 | 3394 |             *p++ = 'u'; | 
 | 3395 |             *p++ = hexdigit[(ch >> 12) & 0xf]; | 
 | 3396 |             *p++ = hexdigit[(ch >> 8) & 0xf]; | 
 | 3397 |             *p++ = hexdigit[(ch >> 4) & 0xf]; | 
 | 3398 |             *p++ = hexdigit[ch & 15]; | 
 | 3399 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3400 |         /* Copy everything else as-is */ | 
 | 3401 |         else | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3402 |             *p++ = (char) ch; | 
 | 3403 |     } | 
 | 3404 |     *p = '\0'; | 
| Benjamin Peterson | bea424a | 2010-04-03 00:57:33 +0000 | [diff] [blame] | 3405 |     if (_PyString_Resize(&repr, p - q)) | 
 | 3406 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3407 |     return repr; | 
 | 3408 | } | 
 | 3409 |  | 
 | 3410 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) | 
 | 3411 | { | 
 | 3412 |     if (!PyUnicode_Check(unicode)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3413 |         PyErr_BadArgument(); | 
 | 3414 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3415 |     } | 
 | 3416 |     return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3417 |                                             PyUnicode_GET_SIZE(unicode)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3418 | } | 
 | 3419 |  | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3420 | /* --- Unicode Internal Codec ------------------------------------------- */ | 
 | 3421 |  | 
 | 3422 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3423 |                                            Py_ssize_t size, | 
 | 3424 |                                            const char *errors) | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3425 | { | 
 | 3426 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3427 |     Py_ssize_t startinpos; | 
 | 3428 |     Py_ssize_t endinpos; | 
 | 3429 |     Py_ssize_t outpos; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3430 |     PyUnicodeObject *v; | 
 | 3431 |     Py_UNICODE *p; | 
 | 3432 |     const char *end; | 
 | 3433 |     const char *reason; | 
 | 3434 |     PyObject *errorHandler = NULL; | 
 | 3435 |     PyObject *exc = NULL; | 
 | 3436 |  | 
| Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3437 | #ifdef Py_UNICODE_WIDE | 
 | 3438 |     Py_UNICODE unimax = PyUnicode_GetMax(); | 
 | 3439 | #endif | 
 | 3440 |  | 
| Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 3441 |     /* XXX overflow detection missing */ | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3442 |     v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); | 
 | 3443 |     if (v == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3444 |         goto onError; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3445 |     if (PyUnicode_GetSize((PyObject *)v) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3446 |         return (PyObject *)v; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3447 |     p = PyUnicode_AS_UNICODE(v); | 
 | 3448 |     end = s + size; | 
 | 3449 |  | 
 | 3450 |     while (s < end) { | 
| Serhiy Storchaka | d5327d9 | 2013-02-07 16:23:11 +0200 | [diff] [blame] | 3451 |         if (end-s < Py_UNICODE_SIZE) { | 
 | 3452 |             endinpos = end-starts; | 
 | 3453 |             reason = "truncated input"; | 
 | 3454 |             goto error; | 
 | 3455 |         } | 
| Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 3456 |         memcpy(p, s, sizeof(Py_UNICODE)); | 
| Serhiy Storchaka | d5327d9 | 2013-02-07 16:23:11 +0200 | [diff] [blame] | 3457 | #ifdef Py_UNICODE_WIDE | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3458 |         /* We have to sanity check the raw data, otherwise doom looms for | 
 | 3459 |            some malformed UCS-4 data. */ | 
| Serhiy Storchaka | d5327d9 | 2013-02-07 16:23:11 +0200 | [diff] [blame] | 3460 |         if (*p > unimax || *p < 0) { | 
 | 3461 |             endinpos = s - starts + Py_UNICODE_SIZE; | 
 | 3462 |             reason = "illegal code point (> 0x10FFFF)"; | 
 | 3463 |             goto error; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3464 |         } | 
| Serhiy Storchaka | d5327d9 | 2013-02-07 16:23:11 +0200 | [diff] [blame] | 3465 | #endif | 
 | 3466 |         p++; | 
 | 3467 |         s += Py_UNICODE_SIZE; | 
 | 3468 |         continue; | 
 | 3469 |  | 
 | 3470 |   error: | 
 | 3471 |         startinpos = s - starts; | 
 | 3472 |         outpos = p - PyUnicode_AS_UNICODE(v); | 
 | 3473 |         if (unicode_decode_call_errorhandler( | 
 | 3474 |                 errors, &errorHandler, | 
 | 3475 |                 "unicode_internal", reason, | 
 | 3476 |                 starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 3477 |                 &v, &outpos, &p)) { | 
 | 3478 |             goto onError; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3479 |         } | 
 | 3480 |     } | 
 | 3481 |  | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3482 |     if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3483 |         goto onError; | 
 | 3484 |     Py_XDECREF(errorHandler); | 
 | 3485 |     Py_XDECREF(exc); | 
 | 3486 |     return (PyObject *)v; | 
 | 3487 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3488 |   onError: | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3489 |     Py_XDECREF(v); | 
 | 3490 |     Py_XDECREF(errorHandler); | 
 | 3491 |     Py_XDECREF(exc); | 
 | 3492 |     return NULL; | 
 | 3493 | } | 
 | 3494 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3495 | /* --- Latin-1 Codec ------------------------------------------------------ */ | 
 | 3496 |  | 
 | 3497 | PyObject *PyUnicode_DecodeLatin1(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3498 |                                  Py_ssize_t size, | 
 | 3499 |                                  const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3500 | { | 
 | 3501 |     PyUnicodeObject *v; | 
 | 3502 |     Py_UNICODE *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3503 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3504 |     /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3505 |     if (size == 1) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3506 |         Py_UNICODE r = *(unsigned char*)s; | 
 | 3507 |         return PyUnicode_FromUnicode(&r, 1); | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3508 |     } | 
 | 3509 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3510 |     v = _PyUnicode_New(size); | 
 | 3511 |     if (v == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3512 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3513 |     if (size == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3514 |         return (PyObject *)v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3515 |     p = PyUnicode_AS_UNICODE(v); | 
 | 3516 |     while (size-- > 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3517 |         *p++ = (unsigned char)*s++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3518 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3519 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3520 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3521 |     Py_XDECREF(v); | 
 | 3522 |     return NULL; | 
 | 3523 | } | 
 | 3524 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3525 | /* create or adjust a UnicodeEncodeError */ | 
 | 3526 | static void make_encode_exception(PyObject **exceptionObject, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3527 |                                   const char *encoding, | 
 | 3528 |                                   const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 3529 |                                   Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 3530 |                                   const char *reason) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3531 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3532 |     if (*exceptionObject == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3533 |         *exceptionObject = PyUnicodeEncodeError_Create( | 
 | 3534 |             encoding, unicode, size, startpos, endpos, reason); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3535 |     } | 
 | 3536 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3537 |         if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) | 
 | 3538 |             goto onError; | 
 | 3539 |         if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) | 
 | 3540 |             goto onError; | 
 | 3541 |         if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) | 
 | 3542 |             goto onError; | 
 | 3543 |         return; | 
 | 3544 |       onError: | 
| Serhiy Storchaka | 98a9722 | 2014-02-09 13:14:04 +0200 | [diff] [blame] | 3545 |         Py_CLEAR(*exceptionObject); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3546 |     } | 
 | 3547 | } | 
 | 3548 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3549 | /* raises a UnicodeEncodeError */ | 
 | 3550 | static void raise_encode_exception(PyObject **exceptionObject, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3551 |                                    const char *encoding, | 
 | 3552 |                                    const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 3553 |                                    Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 3554 |                                    const char *reason) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3555 | { | 
 | 3556 |     make_encode_exception(exceptionObject, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3557 |                           encoding, unicode, size, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3558 |     if (*exceptionObject != NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3559 |         PyCodec_StrictErrors(*exceptionObject); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3560 | } | 
 | 3561 |  | 
 | 3562 | /* error handling callback helper: | 
 | 3563 |    build arguments, call the callback and check the arguments, | 
 | 3564 |    put the result into newpos and return the replacement string, which | 
 | 3565 |    has to be freed by the caller */ | 
 | 3566 | static PyObject *unicode_encode_call_errorhandler(const char *errors, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3567 |                                                   PyObject **errorHandler, | 
 | 3568 |                                                   const char *encoding, const char *reason, | 
 | 3569 |                                                   const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | 
 | 3570 |                                                   Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 3571 |                                                   Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3572 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3573 |     static char *argparse = "O!n;encoding error handler must return (unicode, int) tuple"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3574 |  | 
 | 3575 |     PyObject *restuple; | 
 | 3576 |     PyObject *resunicode; | 
 | 3577 |  | 
 | 3578 |     if (*errorHandler == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3579 |         *errorHandler = PyCodec_LookupError(errors); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3580 |         if (*errorHandler == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3581 |             return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3582 |     } | 
 | 3583 |  | 
 | 3584 |     make_encode_exception(exceptionObject, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3585 |                           encoding, unicode, size, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3586 |     if (*exceptionObject == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3587 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3588 |  | 
 | 3589 |     restuple = PyObject_CallFunctionObjArgs( | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3590 |         *errorHandler, *exceptionObject, NULL); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3591 |     if (restuple == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3592 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3593 |     if (!PyTuple_Check(restuple)) { | 
| Georg Brandl | cbb4958 | 2009-02-13 11:06:59 +0000 | [diff] [blame] | 3594 |         PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3595 |         Py_DECREF(restuple); | 
 | 3596 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3597 |     } | 
 | 3598 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3599 |                           &resunicode, newpos)) { | 
 | 3600 |         Py_DECREF(restuple); | 
 | 3601 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3602 |     } | 
 | 3603 |     if (*newpos<0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3604 |         *newpos = size+*newpos; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3605 |     if (*newpos<0 || *newpos>size) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3606 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); | 
 | 3607 |         Py_DECREF(restuple); | 
 | 3608 |         return NULL; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3609 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3610 |     Py_INCREF(resunicode); | 
 | 3611 |     Py_DECREF(restuple); | 
 | 3612 |     return resunicode; | 
 | 3613 | } | 
 | 3614 |  | 
 | 3615 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3616 |                                      Py_ssize_t size, | 
 | 3617 |                                      const char *errors, | 
 | 3618 |                                      int limit) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3619 | { | 
 | 3620 |     /* output object */ | 
 | 3621 |     PyObject *res; | 
 | 3622 |     /* pointers to the beginning and end+1 of input */ | 
 | 3623 |     const Py_UNICODE *startp = p; | 
 | 3624 |     const Py_UNICODE *endp = p + size; | 
 | 3625 |     /* pointer to the beginning of the unencodable characters */ | 
 | 3626 |     /* const Py_UNICODE *badp = NULL; */ | 
 | 3627 |     /* pointer into the output */ | 
 | 3628 |     char *str; | 
 | 3629 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3630 |     Py_ssize_t respos = 0; | 
 | 3631 |     Py_ssize_t ressize; | 
| Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3632 |     const char *encoding = (limit == 256) ? "latin-1" : "ascii"; | 
 | 3633 |     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] | 3634 |     PyObject *errorHandler = NULL; | 
 | 3635 |     PyObject *exc = NULL; | 
 | 3636 |     /* the following variable is used for caching string comparisons | 
 | 3637 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ | 
 | 3638 |     int known_errorHandler = -1; | 
 | 3639 |  | 
 | 3640 |     /* allocate enough for a simple encoding without | 
 | 3641 |        replacements, if we need more, we'll resize */ | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3642 |     res = PyString_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3643 |     if (res == NULL) | 
 | 3644 |         goto onError; | 
 | 3645 |     if (size == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3646 |         return res; | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3647 |     str = PyString_AS_STRING(res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3648 |     ressize = size; | 
 | 3649 |  | 
 | 3650 |     while (p<endp) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3651 |         Py_UNICODE c = *p; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3652 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3653 |         /* can we encode this? */ | 
 | 3654 |         if (c<limit) { | 
 | 3655 |             /* no overflow check, because we know that the space is enough */ | 
 | 3656 |             *str++ = (char)c; | 
 | 3657 |             ++p; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3658 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3659 |         else { | 
 | 3660 |             Py_ssize_t unicodepos = p-startp; | 
 | 3661 |             Py_ssize_t requiredsize; | 
 | 3662 |             PyObject *repunicode; | 
 | 3663 |             Py_ssize_t repsize; | 
 | 3664 |             Py_ssize_t newpos; | 
 | 3665 |             Py_ssize_t respos; | 
 | 3666 |             Py_UNICODE *uni2; | 
 | 3667 |             /* startpos for collecting unencodable chars */ | 
 | 3668 |             const Py_UNICODE *collstart = p; | 
 | 3669 |             const Py_UNICODE *collend = p; | 
 | 3670 |             /* find all unecodable characters */ | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3671 |             while ((collend < endp) && ((*collend) >= limit)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3672 |                 ++collend; | 
 | 3673 |             /* cache callback name lookup (if not done yet, i.e. it's the first error) */ | 
 | 3674 |             if (known_errorHandler==-1) { | 
 | 3675 |                 if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 3676 |                     known_errorHandler = 1; | 
 | 3677 |                 else if (!strcmp(errors, "replace")) | 
 | 3678 |                     known_errorHandler = 2; | 
 | 3679 |                 else if (!strcmp(errors, "ignore")) | 
 | 3680 |                     known_errorHandler = 3; | 
 | 3681 |                 else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 3682 |                     known_errorHandler = 4; | 
 | 3683 |                 else | 
 | 3684 |                     known_errorHandler = 0; | 
 | 3685 |             } | 
 | 3686 |             switch (known_errorHandler) { | 
 | 3687 |             case 1: /* strict */ | 
 | 3688 |                 raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); | 
 | 3689 |                 goto onError; | 
 | 3690 |             case 2: /* replace */ | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3691 |                 while (collstart++ < collend) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3692 |                     *str++ = '?'; /* fall through */ | 
 | 3693 |             case 3: /* ignore */ | 
 | 3694 |                 p = collend; | 
 | 3695 |                 break; | 
 | 3696 |             case 4: /* xmlcharrefreplace */ | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3697 |                 respos = str - PyString_AS_STRING(res); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3698 |                 /* determine replacement size (temporarily (mis)uses p) */ | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3699 |                 requiredsize = respos; | 
 | 3700 |                 for (p = collstart; p < collend;) { | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 3701 |                     Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend); | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3702 |                     Py_ssize_t incr; | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 3703 |                     if (ch < 10) | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3704 |                         incr = 2+1+1; | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 3705 |                     else if (ch < 100) | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3706 |                         incr = 2+2+1; | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 3707 |                     else if (ch < 1000) | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3708 |                         incr = 2+3+1; | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 3709 |                     else if (ch < 10000) | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3710 |                         incr = 2+4+1; | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 3711 |                     else if (ch < 100000) | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3712 |                         incr = 2+5+1; | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 3713 |                     else if (ch < 1000000) | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3714 |                         incr = 2+6+1; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3715 |                     else | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3716 |                         incr = 2+7+1; | 
 | 3717 |                     if (requiredsize > PY_SSIZE_T_MAX - incr) | 
 | 3718 |                         goto overflow; | 
 | 3719 |                     requiredsize += incr; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3720 |                 } | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3721 |                 if (requiredsize > PY_SSIZE_T_MAX - (endp - collend)) | 
 | 3722 |                     goto overflow; | 
 | 3723 |                 requiredsize += endp - collend; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3724 |                 if (requiredsize > ressize) { | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3725 |                     if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3726 |                         requiredsize = 2*ressize; | 
 | 3727 |                     if (_PyString_Resize(&res, requiredsize)) | 
 | 3728 |                         goto onError; | 
 | 3729 |                     str = PyString_AS_STRING(res) + respos; | 
 | 3730 |                     ressize = requiredsize; | 
 | 3731 |                 } | 
 | 3732 |                 /* generate replacement (temporarily (mis)uses p) */ | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 3733 |                 for (p = collstart; p < collend;) { | 
 | 3734 |                     Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend); | 
 | 3735 |                     str += sprintf(str, "&#%d;", (int)ch); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3736 |                 } | 
 | 3737 |                 p = collend; | 
 | 3738 |                 break; | 
 | 3739 |             default: | 
 | 3740 |                 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | 
 | 3741 |                                                               encoding, reason, startp, size, &exc, | 
 | 3742 |                                                               collstart-startp, collend-startp, &newpos); | 
 | 3743 |                 if (repunicode == NULL) | 
 | 3744 |                     goto onError; | 
 | 3745 |                 /* need more space? (at least enough for what we have+the | 
 | 3746 |                    replacement+the rest of the string, so we won't have to | 
 | 3747 |                    check space for encodable characters) */ | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3748 |                 respos = str - PyString_AS_STRING(res); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3749 |                 repsize = PyUnicode_GET_SIZE(repunicode); | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3750 |                 if (respos > PY_SSIZE_T_MAX - repsize) | 
 | 3751 |                     goto overflow; | 
 | 3752 |                 requiredsize = respos + repsize; | 
 | 3753 |                 if (requiredsize > PY_SSIZE_T_MAX - (endp - collend)) | 
 | 3754 |                     goto overflow; | 
 | 3755 |                 requiredsize += endp - collend; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3756 |                 if (requiredsize > ressize) { | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3757 |                     if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3758 |                         requiredsize = 2*ressize; | 
 | 3759 |                     if (_PyString_Resize(&res, requiredsize)) { | 
 | 3760 |                         Py_DECREF(repunicode); | 
 | 3761 |                         goto onError; | 
 | 3762 |                     } | 
 | 3763 |                     str = PyString_AS_STRING(res) + respos; | 
 | 3764 |                     ressize = requiredsize; | 
 | 3765 |                 } | 
 | 3766 |                 /* check if there is anything unencodable in the replacement | 
 | 3767 |                    and copy it to the output */ | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3768 |                 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2, ++str) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3769 |                     c = *uni2; | 
 | 3770 |                     if (c >= limit) { | 
 | 3771 |                         raise_encode_exception(&exc, encoding, startp, size, | 
 | 3772 |                                                unicodepos, unicodepos+1, reason); | 
 | 3773 |                         Py_DECREF(repunicode); | 
 | 3774 |                         goto onError; | 
 | 3775 |                     } | 
 | 3776 |                     *str = (char)c; | 
 | 3777 |                 } | 
 | 3778 |                 p = startp + newpos; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3779 |                 Py_DECREF(repunicode); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3780 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 3781 |         } | 
 | 3782 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3783 |     /* Resize if we allocated to much */ | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3784 |     respos = str - PyString_AS_STRING(res); | 
 | 3785 |     if (respos < ressize) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3786 |         /* If this falls res will be NULL */ | 
 | 3787 |         _PyString_Resize(&res, respos); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3788 |     Py_XDECREF(errorHandler); | 
 | 3789 |     Py_XDECREF(exc); | 
 | 3790 |     return res; | 
 | 3791 |  | 
| Benjamin Peterson | ed4c130 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 3792 |   overflow: | 
 | 3793 |     PyErr_SetString(PyExc_OverflowError, | 
 | 3794 |                     "encoded result is too long for a Python string"); | 
 | 3795 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3796 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3797 |     Py_XDECREF(res); | 
 | 3798 |     Py_XDECREF(errorHandler); | 
 | 3799 |     Py_XDECREF(exc); | 
 | 3800 |     return NULL; | 
 | 3801 | } | 
 | 3802 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3803 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3804 |                                  Py_ssize_t size, | 
 | 3805 |                                  const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3806 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3807 |     return unicode_encode_ucs1(p, size, errors, 256); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3808 | } | 
 | 3809 |  | 
 | 3810 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) | 
 | 3811 | { | 
 | 3812 |     if (!PyUnicode_Check(unicode)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3813 |         PyErr_BadArgument(); | 
 | 3814 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3815 |     } | 
 | 3816 |     return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3817 |                                   PyUnicode_GET_SIZE(unicode), | 
 | 3818 |                                   NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3819 | } | 
 | 3820 |  | 
 | 3821 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ | 
 | 3822 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3823 | PyObject *PyUnicode_DecodeASCII(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3824 |                                 Py_ssize_t size, | 
 | 3825 |                                 const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3826 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3827 |     const char *starts = s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3828 |     PyUnicodeObject *v; | 
 | 3829 |     Py_UNICODE *p; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3830 |     Py_ssize_t startinpos; | 
 | 3831 |     Py_ssize_t endinpos; | 
 | 3832 |     Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3833 |     const char *e; | 
 | 3834 |     PyObject *errorHandler = NULL; | 
 | 3835 |     PyObject *exc = NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3836 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3837 |     /* ASCII is equivalent to the first 128 ordinals in Unicode. */ | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3838 |     if (size == 1 && *(unsigned char*)s < 128) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3839 |         Py_UNICODE r = *(unsigned char*)s; | 
 | 3840 |         return PyUnicode_FromUnicode(&r, 1); | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3841 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3842 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3843 |     v = _PyUnicode_New(size); | 
 | 3844 |     if (v == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3845 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3846 |     if (size == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3847 |         return (PyObject *)v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3848 |     p = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3849 |     e = s + size; | 
 | 3850 |     while (s < e) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3851 |         register unsigned char c = (unsigned char)*s; | 
 | 3852 |         if (c < 128) { | 
 | 3853 |             *p++ = c; | 
 | 3854 |             ++s; | 
 | 3855 |         } | 
 | 3856 |         else { | 
 | 3857 |             startinpos = s-starts; | 
 | 3858 |             endinpos = startinpos + 1; | 
 | 3859 |             outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); | 
 | 3860 |             if (unicode_decode_call_errorhandler( | 
 | 3861 |                     errors, &errorHandler, | 
 | 3862 |                     "ascii", "ordinal not in range(128)", | 
 | 3863 |                     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 3864 |                     &v, &outpos, &p)) | 
 | 3865 |                 goto onError; | 
 | 3866 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3867 |     } | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3868 |     if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3869 |         if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
 | 3870 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3871 |     Py_XDECREF(errorHandler); | 
 | 3872 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3873 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3874 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3875 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3876 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3877 |     Py_XDECREF(errorHandler); | 
 | 3878 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3879 |     return NULL; | 
 | 3880 | } | 
 | 3881 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3882 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3883 |                                 Py_ssize_t size, | 
 | 3884 |                                 const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3885 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3886 |     return unicode_encode_ucs1(p, size, errors, 128); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3887 | } | 
 | 3888 |  | 
 | 3889 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) | 
 | 3890 | { | 
 | 3891 |     if (!PyUnicode_Check(unicode)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3892 |         PyErr_BadArgument(); | 
 | 3893 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3894 |     } | 
 | 3895 |     return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3896 |                                  PyUnicode_GET_SIZE(unicode), | 
 | 3897 |                                  NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3898 | } | 
 | 3899 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3900 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3901 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3902 | /* --- MBCS codecs for Windows -------------------------------------------- */ | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3903 |  | 
| Hirokazu Yamamoto | 52a3492 | 2009-03-21 10:32:52 +0000 | [diff] [blame] | 3904 | #if SIZEOF_INT < SIZEOF_SIZE_T | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3905 | #define NEED_RETRY | 
 | 3906 | #endif | 
 | 3907 |  | 
 | 3908 | /* XXX This code is limited to "true" double-byte encodings, as | 
 | 3909 |    a) it assumes an incomplete character consists of a single byte, and | 
 | 3910 |    b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3911 |    encodings, see IsDBCSLeadByteEx documentation. */ | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3912 |  | 
 | 3913 | static int is_dbcs_lead_byte(const char *s, int offset) | 
 | 3914 | { | 
 | 3915 |     const char *curr = s + offset; | 
 | 3916 |  | 
 | 3917 |     if (IsDBCSLeadByte(*curr)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3918 |         const char *prev = CharPrev(s, curr); | 
 | 3919 |         return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3920 |     } | 
 | 3921 |     return 0; | 
 | 3922 | } | 
 | 3923 |  | 
 | 3924 | /* | 
 | 3925 |  * Decode MBCS string into unicode object. If 'final' is set, converts | 
 | 3926 |  * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. | 
 | 3927 |  */ | 
 | 3928 | static int decode_mbcs(PyUnicodeObject **v, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3929 |                        const char *s, /* MBCS string */ | 
 | 3930 |                        int size, /* sizeof MBCS string */ | 
 | 3931 |                        int final) | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3932 | { | 
 | 3933 |     Py_UNICODE *p; | 
 | 3934 |     Py_ssize_t n = 0; | 
 | 3935 |     int usize = 0; | 
 | 3936 |  | 
 | 3937 |     assert(size >= 0); | 
 | 3938 |  | 
 | 3939 |     /* Skip trailing lead-byte unless 'final' is set */ | 
 | 3940 |     if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3941 |         --size; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3942 |  | 
 | 3943 |     /* First get the size of the result */ | 
 | 3944 |     if (size > 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3945 |         usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); | 
 | 3946 |         if (usize == 0) { | 
 | 3947 |             PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 3948 |             return -1; | 
 | 3949 |         } | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3950 |     } | 
 | 3951 |  | 
 | 3952 |     if (*v == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3953 |         /* Create unicode object */ | 
 | 3954 |         *v = _PyUnicode_New(usize); | 
 | 3955 |         if (*v == NULL) | 
 | 3956 |             return -1; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3957 |     } | 
 | 3958 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3959 |         /* Extend unicode object */ | 
 | 3960 |         n = PyUnicode_GET_SIZE(*v); | 
 | 3961 |         if (_PyUnicode_Resize(v, n + usize) < 0) | 
 | 3962 |             return -1; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3963 |     } | 
 | 3964 |  | 
 | 3965 |     /* Do the conversion */ | 
 | 3966 |     if (size > 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3967 |         p = PyUnicode_AS_UNICODE(*v) + n; | 
 | 3968 |         if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { | 
 | 3969 |             PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 3970 |             return -1; | 
 | 3971 |         } | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3972 |     } | 
 | 3973 |  | 
 | 3974 |     return size; | 
 | 3975 | } | 
 | 3976 |  | 
 | 3977 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3978 |                                        Py_ssize_t size, | 
 | 3979 |                                        const char *errors, | 
 | 3980 |                                        Py_ssize_t *consumed) | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3981 | { | 
 | 3982 |     PyUnicodeObject *v = NULL; | 
 | 3983 |     int done; | 
 | 3984 |  | 
 | 3985 |     if (consumed) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3986 |         *consumed = 0; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3987 |  | 
 | 3988 | #ifdef NEED_RETRY | 
 | 3989 |   retry: | 
 | 3990 |     if (size > INT_MAX) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3991 |         done = decode_mbcs(&v, s, INT_MAX, 0); | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3992 |     else | 
 | 3993 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3994 |         done = decode_mbcs(&v, s, (int)size, !consumed); | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3995 |  | 
 | 3996 |     if (done < 0) { | 
 | 3997 |         Py_XDECREF(v); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 3998 |         return NULL; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3999 |     } | 
 | 4000 |  | 
 | 4001 |     if (consumed) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4002 |         *consumed += done; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4003 |  | 
 | 4004 | #ifdef NEED_RETRY | 
 | 4005 |     if (size > INT_MAX) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4006 |         s += done; | 
 | 4007 |         size -= done; | 
 | 4008 |         goto retry; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4009 |     } | 
 | 4010 | #endif | 
 | 4011 |  | 
 | 4012 |     return (PyObject *)v; | 
 | 4013 | } | 
 | 4014 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4015 | PyObject *PyUnicode_DecodeMBCS(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4016 |                                Py_ssize_t size, | 
 | 4017 |                                const char *errors) | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4018 | { | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4019 |     return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); | 
 | 4020 | } | 
 | 4021 |  | 
 | 4022 | /* | 
 | 4023 |  * Convert unicode into string object (MBCS). | 
 | 4024 |  * Returns 0 if succeed, -1 otherwise. | 
 | 4025 |  */ | 
 | 4026 | static int encode_mbcs(PyObject **repr, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4027 |                        const Py_UNICODE *p, /* unicode */ | 
 | 4028 |                        int size) /* size of unicode */ | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4029 | { | 
 | 4030 |     int mbcssize = 0; | 
 | 4031 |     Py_ssize_t n = 0; | 
 | 4032 |  | 
 | 4033 |     assert(size >= 0); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4034 |  | 
 | 4035 |     /* First get the size of the result */ | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4036 |     if (size > 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4037 |         mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); | 
 | 4038 |         if (mbcssize == 0) { | 
 | 4039 |             PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 4040 |             return -1; | 
 | 4041 |         } | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4042 |     } | 
 | 4043 |  | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4044 |     if (*repr == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4045 |         /* Create string object */ | 
 | 4046 |         *repr = PyString_FromStringAndSize(NULL, mbcssize); | 
 | 4047 |         if (*repr == NULL) | 
 | 4048 |             return -1; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4049 |     } | 
 | 4050 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4051 |         /* Extend string object */ | 
 | 4052 |         n = PyString_Size(*repr); | 
 | 4053 |         if (_PyString_Resize(repr, n + mbcssize) < 0) | 
 | 4054 |             return -1; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4055 |     } | 
 | 4056 |  | 
 | 4057 |     /* Do the conversion */ | 
 | 4058 |     if (size > 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4059 |         char *s = PyString_AS_STRING(*repr) + n; | 
 | 4060 |         if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { | 
 | 4061 |             PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 4062 |             return -1; | 
 | 4063 |         } | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4064 |     } | 
 | 4065 |  | 
 | 4066 |     return 0; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4067 | } | 
 | 4068 |  | 
 | 4069 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4070 |                                Py_ssize_t size, | 
 | 4071 |                                const char *errors) | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4072 | { | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4073 |     PyObject *repr = NULL; | 
 | 4074 |     int ret; | 
| Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4075 |  | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4076 | #ifdef NEED_RETRY | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4077 |   retry: | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4078 |     if (size > INT_MAX) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4079 |         ret = encode_mbcs(&repr, p, INT_MAX); | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4080 |     else | 
 | 4081 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4082 |         ret = encode_mbcs(&repr, p, (int)size); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4083 |  | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4084 |     if (ret < 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4085 |         Py_XDECREF(repr); | 
 | 4086 |         return NULL; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4087 |     } | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4088 |  | 
 | 4089 | #ifdef NEED_RETRY | 
 | 4090 |     if (size > INT_MAX) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4091 |         p += INT_MAX; | 
 | 4092 |         size -= INT_MAX; | 
 | 4093 |         goto retry; | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4094 |     } | 
 | 4095 | #endif | 
 | 4096 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4097 |     return repr; | 
 | 4098 | } | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4099 |  | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4100 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) | 
 | 4101 | { | 
 | 4102 |     if (!PyUnicode_Check(unicode)) { | 
 | 4103 |         PyErr_BadArgument(); | 
 | 4104 |         return NULL; | 
 | 4105 |     } | 
 | 4106 |     return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4107 |                                 PyUnicode_GET_SIZE(unicode), | 
 | 4108 |                                 NULL); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4109 | } | 
 | 4110 |  | 
| Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 4111 | #undef NEED_RETRY | 
 | 4112 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4113 | #endif /* MS_WINDOWS */ | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4114 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4115 | /* --- Character Mapping Codec -------------------------------------------- */ | 
 | 4116 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4117 | PyObject *PyUnicode_DecodeCharmap(const char *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4118 |                                   Py_ssize_t size, | 
 | 4119 |                                   PyObject *mapping, | 
 | 4120 |                                   const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4121 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4122 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4123 |     Py_ssize_t startinpos; | 
 | 4124 |     Py_ssize_t endinpos; | 
 | 4125 |     Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4126 |     const char *e; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4127 |     PyUnicodeObject *v; | 
 | 4128 |     Py_UNICODE *p; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4129 |     Py_ssize_t extrachars = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4130 |     PyObject *errorHandler = NULL; | 
 | 4131 |     PyObject *exc = NULL; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4132 |     Py_UNICODE *mapstring = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4133 |     Py_ssize_t maplen = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4134 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4135 |     /* Default to Latin-1 */ | 
 | 4136 |     if (mapping == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4137 |         return PyUnicode_DecodeLatin1(s, size, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4138 |  | 
 | 4139 |     v = _PyUnicode_New(size); | 
 | 4140 |     if (v == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4141 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4142 |     if (size == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4143 |         return (PyObject *)v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4144 |     p = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4145 |     e = s + size; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4146 |     if (PyUnicode_CheckExact(mapping)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4147 |         mapstring = PyUnicode_AS_UNICODE(mapping); | 
 | 4148 |         maplen = PyUnicode_GET_SIZE(mapping); | 
 | 4149 |         while (s < e) { | 
 | 4150 |             unsigned char ch = *s; | 
 | 4151 |             Py_UNICODE x = 0xfffe; /* illegal value */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4152 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4153 |             if (ch < maplen) | 
 | 4154 |                 x = mapstring[ch]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4155 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4156 |             if (x == 0xfffe) { | 
 | 4157 |                 /* undefined mapping */ | 
 | 4158 |                 outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 4159 |                 startinpos = s-starts; | 
 | 4160 |                 endinpos = startinpos+1; | 
 | 4161 |                 if (unicode_decode_call_errorhandler( | 
 | 4162 |                         errors, &errorHandler, | 
 | 4163 |                         "charmap", "character maps to <undefined>", | 
 | 4164 |                         starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 4165 |                         &v, &outpos, &p)) { | 
 | 4166 |                     goto onError; | 
 | 4167 |                 } | 
 | 4168 |                 continue; | 
 | 4169 |             } | 
 | 4170 |             *p++ = x; | 
 | 4171 |             ++s; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4172 |         } | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4173 |     } | 
 | 4174 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4175 |         while (s < e) { | 
 | 4176 |             unsigned char ch = *s; | 
 | 4177 |             PyObject *w, *x; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4178 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4179 |             /* Get mapping (char ordinal -> integer, Unicode char or None) */ | 
 | 4180 |             w = PyInt_FromLong((long)ch); | 
 | 4181 |             if (w == NULL) | 
 | 4182 |                 goto onError; | 
 | 4183 |             x = PyObject_GetItem(mapping, w); | 
 | 4184 |             Py_DECREF(w); | 
 | 4185 |             if (x == NULL) { | 
 | 4186 |                 if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 4187 |                     /* No mapping found means: mapping is undefined. */ | 
 | 4188 |                     PyErr_Clear(); | 
| Serhiy Storchaka | 9599745 | 2013-01-15 14:42:59 +0200 | [diff] [blame] | 4189 |                     goto Undefined; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4190 |                 } else | 
 | 4191 |                     goto onError; | 
 | 4192 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4193 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4194 |             /* Apply mapping */ | 
| Serhiy Storchaka | 9599745 | 2013-01-15 14:42:59 +0200 | [diff] [blame] | 4195 |             if (x == Py_None) | 
 | 4196 |                 goto Undefined; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4197 |             if (PyInt_Check(x)) { | 
 | 4198 |                 long value = PyInt_AS_LONG(x); | 
| Serhiy Storchaka | 9599745 | 2013-01-15 14:42:59 +0200 | [diff] [blame] | 4199 |                 if (value == 0xFFFE) | 
 | 4200 |                     goto Undefined; | 
| Antoine Pitrou | e3ae321 | 2012-11-17 21:14:58 +0100 | [diff] [blame] | 4201 |                 if (value < 0 || value > 0x10FFFF) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4202 |                     PyErr_SetString(PyExc_TypeError, | 
| Antoine Pitrou | e3ae321 | 2012-11-17 21:14:58 +0100 | [diff] [blame] | 4203 |                                     "character mapping must be in range(0x110000)"); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4204 |                     Py_DECREF(x); | 
 | 4205 |                     goto onError; | 
 | 4206 |                 } | 
| Antoine Pitrou | e3ae321 | 2012-11-17 21:14:58 +0100 | [diff] [blame] | 4207 |  | 
 | 4208 | #ifndef Py_UNICODE_WIDE | 
 | 4209 |                 if (value > 0xFFFF) { | 
 | 4210 |                     /* see the code for 1-n mapping below */ | 
 | 4211 |                     if (extrachars < 2) { | 
 | 4212 |                         /* resize first */ | 
 | 4213 |                         Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); | 
 | 4214 |                         Py_ssize_t needed = 10 - extrachars; | 
 | 4215 |                         extrachars += needed; | 
 | 4216 |                         /* XXX overflow detection missing */ | 
 | 4217 |                         if (_PyUnicode_Resize(&v, | 
 | 4218 |                                               PyUnicode_GET_SIZE(v) + needed) < 0) { | 
 | 4219 |                             Py_DECREF(x); | 
 | 4220 |                             goto onError; | 
 | 4221 |                         } | 
 | 4222 |                         p = PyUnicode_AS_UNICODE(v) + oldpos; | 
 | 4223 |                     } | 
 | 4224 |                     value -= 0x10000; | 
 | 4225 |                     *p++ = 0xD800 | (value >> 10); | 
 | 4226 |                     *p++ = 0xDC00 | (value & 0x3FF); | 
 | 4227 |                     extrachars -= 2; | 
 | 4228 |                 } | 
 | 4229 |                 else | 
 | 4230 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4231 |                 *p++ = (Py_UNICODE)value; | 
 | 4232 |             } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4233 |             else if (PyUnicode_Check(x)) { | 
 | 4234 |                 Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4235 |  | 
| Serhiy Storchaka | 9599745 | 2013-01-15 14:42:59 +0200 | [diff] [blame] | 4236 |                 if (targetsize == 1) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4237 |                     /* 1-1 mapping */ | 
| Serhiy Storchaka | 9599745 | 2013-01-15 14:42:59 +0200 | [diff] [blame] | 4238 |                     Py_UNICODE value = *PyUnicode_AS_UNICODE(x); | 
 | 4239 |                     if (value == 0xFFFE) | 
 | 4240 |                         goto Undefined; | 
 | 4241 |                     *p++ = value; | 
 | 4242 |                 } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4243 |                 else if (targetsize > 1) { | 
 | 4244 |                     /* 1-n mapping */ | 
 | 4245 |                     if (targetsize > extrachars) { | 
 | 4246 |                         /* resize first */ | 
 | 4247 |                         Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); | 
 | 4248 |                         Py_ssize_t needed = (targetsize - extrachars) + \ | 
 | 4249 |                             (targetsize << 2); | 
 | 4250 |                         extrachars += needed; | 
 | 4251 |                         /* XXX overflow detection missing */ | 
 | 4252 |                         if (_PyUnicode_Resize(&v, | 
 | 4253 |                                               PyUnicode_GET_SIZE(v) + needed) < 0) { | 
 | 4254 |                             Py_DECREF(x); | 
 | 4255 |                             goto onError; | 
 | 4256 |                         } | 
 | 4257 |                         p = PyUnicode_AS_UNICODE(v) + oldpos; | 
 | 4258 |                     } | 
 | 4259 |                     Py_UNICODE_COPY(p, | 
 | 4260 |                                     PyUnicode_AS_UNICODE(x), | 
 | 4261 |                                     targetsize); | 
 | 4262 |                     p += targetsize; | 
 | 4263 |                     extrachars -= targetsize; | 
 | 4264 |                 } | 
 | 4265 |                 /* 1-0 mapping: skip the character */ | 
 | 4266 |             } | 
 | 4267 |             else { | 
 | 4268 |                 /* wrong return value */ | 
 | 4269 |                 PyErr_SetString(PyExc_TypeError, | 
 | 4270 |                                 "character mapping must return integer, None or unicode"); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4271 |                 Py_DECREF(x); | 
 | 4272 |                 goto onError; | 
 | 4273 |             } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4274 |             Py_DECREF(x); | 
 | 4275 |             ++s; | 
| Serhiy Storchaka | 9599745 | 2013-01-15 14:42:59 +0200 | [diff] [blame] | 4276 |             continue; | 
 | 4277 | Undefined: | 
 | 4278 |             /* undefined mapping */ | 
 | 4279 |             Py_XDECREF(x); | 
 | 4280 |             outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 4281 |             startinpos = s-starts; | 
 | 4282 |             endinpos = startinpos+1; | 
 | 4283 |             if (unicode_decode_call_errorhandler( | 
 | 4284 |                     errors, &errorHandler, | 
 | 4285 |                     "charmap", "character maps to <undefined>", | 
 | 4286 |                     starts, size, &startinpos, &endinpos, &exc, &s, | 
 | 4287 |                     &v, &outpos, &p)) { | 
 | 4288 |                 goto onError; | 
 | 4289 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4290 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4291 |     } | 
 | 4292 |     if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4293 |         if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
 | 4294 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4295 |     Py_XDECREF(errorHandler); | 
 | 4296 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4297 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4298 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4299 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4300 |     Py_XDECREF(errorHandler); | 
 | 4301 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4302 |     Py_XDECREF(v); | 
 | 4303 |     return NULL; | 
 | 4304 | } | 
 | 4305 |  | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4306 | /* Charmap encoding: the lookup table */ | 
 | 4307 |  | 
 | 4308 | struct encoding_map{ | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4309 |     PyObject_HEAD | 
 | 4310 |     unsigned char level1[32]; | 
 | 4311 |     int count2, count3; | 
 | 4312 |     unsigned char level23[1]; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4313 | }; | 
 | 4314 |  | 
 | 4315 | static PyObject* | 
 | 4316 | encoding_map_size(PyObject *obj, PyObject* args) | 
 | 4317 | { | 
 | 4318 |     struct encoding_map *map = (struct encoding_map*)obj; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4319 |     return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4320 |                           128*map->count3); | 
 | 4321 | } | 
 | 4322 |  | 
 | 4323 | static PyMethodDef encoding_map_methods[] = { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4324 |     {"size", encoding_map_size, METH_NOARGS, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4325 |      PyDoc_STR("Return the size (in bytes) of this object") }, | 
 | 4326 |     { 0 } | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4327 | }; | 
 | 4328 |  | 
 | 4329 | static void | 
 | 4330 | encoding_map_dealloc(PyObject* o) | 
 | 4331 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4332 |     PyObject_FREE(o); | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4333 | } | 
 | 4334 |  | 
 | 4335 | static PyTypeObject EncodingMapType = { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4336 |     PyVarObject_HEAD_INIT(NULL, 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4337 |     "EncodingMap",          /*tp_name*/ | 
 | 4338 |     sizeof(struct encoding_map),   /*tp_basicsize*/ | 
 | 4339 |     0,                      /*tp_itemsize*/ | 
 | 4340 |     /* methods */ | 
 | 4341 |     encoding_map_dealloc,   /*tp_dealloc*/ | 
 | 4342 |     0,                      /*tp_print*/ | 
 | 4343 |     0,                      /*tp_getattr*/ | 
 | 4344 |     0,                      /*tp_setattr*/ | 
 | 4345 |     0,                      /*tp_compare*/ | 
 | 4346 |     0,                      /*tp_repr*/ | 
 | 4347 |     0,                      /*tp_as_number*/ | 
 | 4348 |     0,                      /*tp_as_sequence*/ | 
 | 4349 |     0,                      /*tp_as_mapping*/ | 
 | 4350 |     0,                      /*tp_hash*/ | 
 | 4351 |     0,                      /*tp_call*/ | 
 | 4352 |     0,                      /*tp_str*/ | 
 | 4353 |     0,                      /*tp_getattro*/ | 
 | 4354 |     0,                      /*tp_setattro*/ | 
 | 4355 |     0,                      /*tp_as_buffer*/ | 
 | 4356 |     Py_TPFLAGS_DEFAULT,     /*tp_flags*/ | 
 | 4357 |     0,                      /*tp_doc*/ | 
 | 4358 |     0,                      /*tp_traverse*/ | 
 | 4359 |     0,                      /*tp_clear*/ | 
 | 4360 |     0,                      /*tp_richcompare*/ | 
 | 4361 |     0,                      /*tp_weaklistoffset*/ | 
 | 4362 |     0,                      /*tp_iter*/ | 
 | 4363 |     0,                      /*tp_iternext*/ | 
 | 4364 |     encoding_map_methods,   /*tp_methods*/ | 
 | 4365 |     0,                      /*tp_members*/ | 
 | 4366 |     0,                      /*tp_getset*/ | 
 | 4367 |     0,                      /*tp_base*/ | 
 | 4368 |     0,                      /*tp_dict*/ | 
 | 4369 |     0,                      /*tp_descr_get*/ | 
 | 4370 |     0,                      /*tp_descr_set*/ | 
 | 4371 |     0,                      /*tp_dictoffset*/ | 
 | 4372 |     0,                      /*tp_init*/ | 
 | 4373 |     0,                      /*tp_alloc*/ | 
 | 4374 |     0,                      /*tp_new*/ | 
 | 4375 |     0,                      /*tp_free*/ | 
 | 4376 |     0,                      /*tp_is_gc*/ | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4377 | }; | 
 | 4378 |  | 
 | 4379 | PyObject* | 
 | 4380 | PyUnicode_BuildEncodingMap(PyObject* string) | 
 | 4381 | { | 
 | 4382 |     Py_UNICODE *decode; | 
 | 4383 |     PyObject *result; | 
 | 4384 |     struct encoding_map *mresult; | 
 | 4385 |     int i; | 
 | 4386 |     int need_dict = 0; | 
 | 4387 |     unsigned char level1[32]; | 
 | 4388 |     unsigned char level2[512]; | 
 | 4389 |     unsigned char *mlevel1, *mlevel2, *mlevel3; | 
 | 4390 |     int count2 = 0, count3 = 0; | 
 | 4391 |  | 
 | 4392 |     if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { | 
 | 4393 |         PyErr_BadArgument(); | 
 | 4394 |         return NULL; | 
 | 4395 |     } | 
 | 4396 |     decode = PyUnicode_AS_UNICODE(string); | 
 | 4397 |     memset(level1, 0xFF, sizeof level1); | 
 | 4398 |     memset(level2, 0xFF, sizeof level2); | 
 | 4399 |  | 
 | 4400 |     /* If there isn't a one-to-one mapping of NULL to \0, | 
 | 4401 |        or if there are non-BMP characters, we need to use | 
 | 4402 |        a mapping dictionary. */ | 
 | 4403 |     if (decode[0] != 0) | 
 | 4404 |         need_dict = 1; | 
 | 4405 |     for (i = 1; i < 256; i++) { | 
 | 4406 |         int l1, l2; | 
 | 4407 |         if (decode[i] == 0 | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4408 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4409 |             || decode[i] > 0xFFFF | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4410 | #endif | 
 | 4411 |             ) { | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4412 |             need_dict = 1; | 
 | 4413 |             break; | 
 | 4414 |         } | 
 | 4415 |         if (decode[i] == 0xFFFE) | 
 | 4416 |             /* unmapped character */ | 
 | 4417 |             continue; | 
 | 4418 |         l1 = decode[i] >> 11; | 
 | 4419 |         l2 = decode[i] >> 7; | 
 | 4420 |         if (level1[l1] == 0xFF) | 
 | 4421 |             level1[l1] = count2++; | 
 | 4422 |         if (level2[l2] == 0xFF) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4423 |             level2[l2] = count3++; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4424 |     } | 
 | 4425 |  | 
 | 4426 |     if (count2 >= 0xFF || count3 >= 0xFF) | 
 | 4427 |         need_dict = 1; | 
 | 4428 |  | 
 | 4429 |     if (need_dict) { | 
 | 4430 |         PyObject *result = PyDict_New(); | 
 | 4431 |         PyObject *key, *value; | 
 | 4432 |         if (!result) | 
 | 4433 |             return NULL; | 
 | 4434 |         for (i = 0; i < 256; i++) { | 
| Brett Cannon | a7f13ee | 2010-05-04 01:16:51 +0000 | [diff] [blame] | 4435 |             value = NULL; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4436 |             key = PyInt_FromLong(decode[i]); | 
 | 4437 |             value = PyInt_FromLong(i); | 
 | 4438 |             if (!key || !value) | 
 | 4439 |                 goto failed1; | 
 | 4440 |             if (PyDict_SetItem(result, key, value) == -1) | 
 | 4441 |                 goto failed1; | 
| Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4442 |             Py_DECREF(key); | 
 | 4443 |             Py_DECREF(value); | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4444 |         } | 
 | 4445 |         return result; | 
 | 4446 |       failed1: | 
 | 4447 |         Py_XDECREF(key); | 
 | 4448 |         Py_XDECREF(value); | 
 | 4449 |         Py_DECREF(result); | 
 | 4450 |         return NULL; | 
 | 4451 |     } | 
 | 4452 |  | 
 | 4453 |     /* Create a three-level trie */ | 
 | 4454 |     result = PyObject_MALLOC(sizeof(struct encoding_map) + | 
 | 4455 |                              16*count2 + 128*count3 - 1); | 
 | 4456 |     if (!result) | 
 | 4457 |         return PyErr_NoMemory(); | 
 | 4458 |     PyObject_Init(result, &EncodingMapType); | 
 | 4459 |     mresult = (struct encoding_map*)result; | 
 | 4460 |     mresult->count2 = count2; | 
 | 4461 |     mresult->count3 = count3; | 
 | 4462 |     mlevel1 = mresult->level1; | 
 | 4463 |     mlevel2 = mresult->level23; | 
 | 4464 |     mlevel3 = mresult->level23 + 16*count2; | 
 | 4465 |     memcpy(mlevel1, level1, 32); | 
 | 4466 |     memset(mlevel2, 0xFF, 16*count2); | 
 | 4467 |     memset(mlevel3, 0, 128*count3); | 
 | 4468 |     count3 = 0; | 
 | 4469 |     for (i = 1; i < 256; i++) { | 
 | 4470 |         int o1, o2, o3, i2, i3; | 
 | 4471 |         if (decode[i] == 0xFFFE) | 
 | 4472 |             /* unmapped character */ | 
 | 4473 |             continue; | 
 | 4474 |         o1 = decode[i]>>11; | 
 | 4475 |         o2 = (decode[i]>>7) & 0xF; | 
 | 4476 |         i2 = 16*mlevel1[o1] + o2; | 
 | 4477 |         if (mlevel2[i2] == 0xFF) | 
 | 4478 |             mlevel2[i2] = count3++; | 
 | 4479 |         o3 = decode[i] & 0x7F; | 
 | 4480 |         i3 = 128*mlevel2[i2] + o3; | 
 | 4481 |         mlevel3[i3] = i; | 
 | 4482 |     } | 
 | 4483 |     return result; | 
 | 4484 | } | 
 | 4485 |  | 
 | 4486 | static int | 
 | 4487 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) | 
 | 4488 | { | 
 | 4489 |     struct encoding_map *map = (struct encoding_map*)mapping; | 
 | 4490 |     int l1 = c>>11; | 
 | 4491 |     int l2 = (c>>7) & 0xF; | 
 | 4492 |     int l3 = c & 0x7F; | 
 | 4493 |     int i; | 
 | 4494 |  | 
 | 4495 | #ifdef Py_UNICODE_WIDE | 
 | 4496 |     if (c > 0xFFFF) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4497 |         return -1; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4498 |     } | 
 | 4499 | #endif | 
 | 4500 |     if (c == 0) | 
 | 4501 |         return 0; | 
 | 4502 |     /* level 1*/ | 
 | 4503 |     i = map->level1[l1]; | 
 | 4504 |     if (i == 0xFF) { | 
 | 4505 |         return -1; | 
 | 4506 |     } | 
 | 4507 |     /* level 2*/ | 
 | 4508 |     i = map->level23[16*i+l2]; | 
 | 4509 |     if (i == 0xFF) { | 
 | 4510 |         return -1; | 
 | 4511 |     } | 
 | 4512 |     /* level 3 */ | 
 | 4513 |     i = map->level23[16*map->count2 + 128*i + l3]; | 
 | 4514 |     if (i == 0) { | 
 | 4515 |         return -1; | 
 | 4516 |     } | 
 | 4517 |     return i; | 
 | 4518 | } | 
 | 4519 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4520 | /* Lookup the character ch in the mapping. If the character | 
 | 4521 |    can't be found, Py_None is returned (or NULL, if another | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4522 |    error occurred). */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4523 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4524 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4525 |     PyObject *w = PyInt_FromLong((long)c); | 
 | 4526 |     PyObject *x; | 
 | 4527 |  | 
 | 4528 |     if (w == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4529 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4530 |     x = PyObject_GetItem(mapping, w); | 
 | 4531 |     Py_DECREF(w); | 
 | 4532 |     if (x == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4533 |         if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 4534 |             /* No mapping found means: mapping is undefined. */ | 
 | 4535 |             PyErr_Clear(); | 
 | 4536 |             x = Py_None; | 
 | 4537 |             Py_INCREF(x); | 
 | 4538 |             return x; | 
 | 4539 |         } else | 
 | 4540 |             return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4541 |     } | 
| Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4542 |     else if (x == Py_None) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4543 |         return x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4544 |     else if (PyInt_Check(x)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4545 |         long value = PyInt_AS_LONG(x); | 
 | 4546 |         if (value < 0 || value > 255) { | 
 | 4547 |             PyErr_SetString(PyExc_TypeError, | 
 | 4548 |                             "character mapping must be in range(256)"); | 
 | 4549 |             Py_DECREF(x); | 
 | 4550 |             return NULL; | 
 | 4551 |         } | 
 | 4552 |         return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4553 |     } | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4554 |     else if (PyString_Check(x)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4555 |         return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4556 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4557 |         /* wrong return value */ | 
 | 4558 |         PyErr_SetString(PyExc_TypeError, | 
 | 4559 |                         "character mapping must return integer, None or str"); | 
 | 4560 |         Py_DECREF(x); | 
 | 4561 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4562 |     } | 
 | 4563 | } | 
 | 4564 |  | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4565 | static int | 
 | 4566 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) | 
 | 4567 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4568 |     Py_ssize_t outsize = PyString_GET_SIZE(*outobj); | 
 | 4569 |     /* exponentially overallocate to minimize reallocations */ | 
 | 4570 |     if (requiredsize < 2*outsize) | 
 | 4571 |         requiredsize = 2*outsize; | 
 | 4572 |     if (_PyString_Resize(outobj, requiredsize)) { | 
 | 4573 |         return 0; | 
 | 4574 |     } | 
 | 4575 |     return 1; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4576 | } | 
 | 4577 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4578 | typedef enum charmapencode_result { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4579 |     enc_SUCCESS, enc_FAILED, enc_EXCEPTION | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4580 | }charmapencode_result; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4581 | /* lookup the character, put the result in the output string and adjust | 
 | 4582 |    various state variables. Reallocate the output string if not enough | 
 | 4583 |    space is available. Return a new reference to the object that | 
 | 4584 |    was put in the output buffer, or Py_None, if the mapping was undefined | 
 | 4585 |    (in which case no character was written) or NULL, if a | 
| Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4586 |    reallocation error occurred. The caller must decref the result */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4587 | static | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4588 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4589 |                                           PyObject **outobj, Py_ssize_t *outpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4590 | { | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4591 |     PyObject *rep; | 
 | 4592 |     char *outstart; | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4593 |     Py_ssize_t outsize = PyString_GET_SIZE(*outobj); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4594 |  | 
| Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4595 |     if (Py_TYPE(mapping) == &EncodingMapType) { | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4596 |         int res = encoding_map_lookup(c, mapping); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4597 |         Py_ssize_t requiredsize = *outpos+1; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4598 |         if (res == -1) | 
 | 4599 |             return enc_FAILED; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4600 |         if (outsize<requiredsize) | 
 | 4601 |             if (!charmapencode_resize(outobj, outpos, requiredsize)) | 
 | 4602 |                 return enc_EXCEPTION; | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4603 |         outstart = PyString_AS_STRING(*outobj); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4604 |         outstart[(*outpos)++] = (char)res; | 
 | 4605 |         return enc_SUCCESS; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4606 |     } | 
 | 4607 |  | 
 | 4608 |     rep = charmapencode_lookup(c, mapping); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4609 |     if (rep==NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4610 |         return enc_EXCEPTION; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4611 |     else if (rep==Py_None) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4612 |         Py_DECREF(rep); | 
 | 4613 |         return enc_FAILED; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4614 |     } else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4615 |         if (PyInt_Check(rep)) { | 
 | 4616 |             Py_ssize_t requiredsize = *outpos+1; | 
 | 4617 |             if (outsize<requiredsize) | 
 | 4618 |                 if (!charmapencode_resize(outobj, outpos, requiredsize)) { | 
 | 4619 |                     Py_DECREF(rep); | 
 | 4620 |                     return enc_EXCEPTION; | 
 | 4621 |                 } | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4622 |             outstart = PyString_AS_STRING(*outobj); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4623 |             outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4624 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4625 |         else { | 
 | 4626 |             const char *repchars = PyString_AS_STRING(rep); | 
 | 4627 |             Py_ssize_t repsize = PyString_GET_SIZE(rep); | 
 | 4628 |             Py_ssize_t requiredsize = *outpos+repsize; | 
 | 4629 |             if (outsize<requiredsize) | 
 | 4630 |                 if (!charmapencode_resize(outobj, outpos, requiredsize)) { | 
 | 4631 |                     Py_DECREF(rep); | 
 | 4632 |                     return enc_EXCEPTION; | 
 | 4633 |                 } | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4634 |             outstart = PyString_AS_STRING(*outobj); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4635 |             memcpy(outstart + *outpos, repchars, repsize); | 
 | 4636 |             *outpos += repsize; | 
 | 4637 |         } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4638 |     } | 
| Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4639 |     Py_DECREF(rep); | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4640 |     return enc_SUCCESS; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4641 | } | 
 | 4642 |  | 
 | 4643 | /* handle an error in PyUnicode_EncodeCharmap | 
 | 4644 |    Return 0 on success, -1 on error */ | 
 | 4645 | static | 
 | 4646 | int charmap_encoding_error( | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4647 |     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] | 4648 |     PyObject **exceptionObject, | 
| Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4649 |     int *known_errorHandler, PyObject **errorHandler, const char *errors, | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4650 |     PyObject **res, Py_ssize_t *respos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4651 | { | 
 | 4652 |     PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4653 |     Py_ssize_t repsize; | 
 | 4654 |     Py_ssize_t newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4655 |     Py_UNICODE *uni2; | 
 | 4656 |     /* startpos for collecting unencodable chars */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4657 |     Py_ssize_t collstartpos = *inpos; | 
 | 4658 |     Py_ssize_t collendpos = *inpos+1; | 
 | 4659 |     Py_ssize_t collpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4660 |     char *encoding = "charmap"; | 
 | 4661 |     char *reason = "character maps to <undefined>"; | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4662 |     charmapencode_result x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4663 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4664 |     /* find all unencodable characters */ | 
 | 4665 |     while (collendpos < size) { | 
| Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4666 |         PyObject *rep; | 
| Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4667 |         if (Py_TYPE(mapping) == &EncodingMapType) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4668 |             int res = encoding_map_lookup(p[collendpos], mapping); | 
 | 4669 |             if (res != -1) | 
 | 4670 |                 break; | 
 | 4671 |             ++collendpos; | 
 | 4672 |             continue; | 
 | 4673 |         } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4674 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4675 |         rep = charmapencode_lookup(p[collendpos], mapping); | 
 | 4676 |         if (rep==NULL) | 
 | 4677 |             return -1; | 
 | 4678 |         else if (rep!=Py_None) { | 
 | 4679 |             Py_DECREF(rep); | 
 | 4680 |             break; | 
 | 4681 |         } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4682 |         Py_DECREF(rep); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4683 |         ++collendpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4684 |     } | 
 | 4685 |     /* cache callback name lookup | 
 | 4686 |      * (if not done yet, i.e. it's the first error) */ | 
 | 4687 |     if (*known_errorHandler==-1) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4688 |         if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 4689 |             *known_errorHandler = 1; | 
 | 4690 |         else if (!strcmp(errors, "replace")) | 
 | 4691 |             *known_errorHandler = 2; | 
 | 4692 |         else if (!strcmp(errors, "ignore")) | 
 | 4693 |             *known_errorHandler = 3; | 
 | 4694 |         else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 4695 |             *known_errorHandler = 4; | 
 | 4696 |         else | 
 | 4697 |             *known_errorHandler = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4698 |     } | 
 | 4699 |     switch (*known_errorHandler) { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4700 |     case 1: /* strict */ | 
 | 4701 |         raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 4702 |         return -1; | 
 | 4703 |     case 2: /* replace */ | 
 | 4704 |         for (collpos = collstartpos; collpos<collendpos; ++collpos) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4705 |             x = charmapencode_output('?', mapping, res, respos); | 
 | 4706 |             if (x==enc_EXCEPTION) { | 
 | 4707 |                 return -1; | 
 | 4708 |             } | 
 | 4709 |             else if (x==enc_FAILED) { | 
 | 4710 |                 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 4711 |                 return -1; | 
 | 4712 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4713 |         } | 
 | 4714 |         /* fall through */ | 
 | 4715 |     case 3: /* ignore */ | 
 | 4716 |         *inpos = collendpos; | 
 | 4717 |         break; | 
 | 4718 |     case 4: /* xmlcharrefreplace */ | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 4719 |         /* generate replacement */ | 
 | 4720 |         for (collpos = collstartpos; collpos < collendpos;) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4721 |             char buffer[2+29+1+1]; | 
 | 4722 |             char *cp; | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 4723 |             Py_UCS4 ch = p[collpos++]; | 
 | 4724 | #ifndef Py_UNICODE_WIDE | 
 | 4725 |             if ((0xD800 <= ch && ch <= 0xDBFF) && | 
 | 4726 |                 (collpos < collendpos) && | 
 | 4727 |                 (0xDC00 <= p[collpos] && p[collpos] <= 0xDFFF)) { | 
 | 4728 |                 ch = ((((ch & 0x03FF) << 10) | | 
 | 4729 |                        ((Py_UCS4)p[collpos++] & 0x03FF)) + 0x10000); | 
 | 4730 |             } | 
 | 4731 | #endif | 
 | 4732 |             sprintf(buffer, "&#%d;", (int)ch); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4733 |             for (cp = buffer; *cp; ++cp) { | 
 | 4734 |                 x = charmapencode_output(*cp, mapping, res, respos); | 
 | 4735 |                 if (x==enc_EXCEPTION) | 
 | 4736 |                     return -1; | 
 | 4737 |                 else if (x==enc_FAILED) { | 
 | 4738 |                     raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 4739 |                     return -1; | 
 | 4740 |                 } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4741 |             } | 
 | 4742 |         } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4743 |         *inpos = collendpos; | 
 | 4744 |         break; | 
 | 4745 |     default: | 
 | 4746 |         repunicode = unicode_encode_call_errorhandler(errors, errorHandler, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4747 |                                                       encoding, reason, p, size, exceptionObject, | 
 | 4748 |                                                       collstartpos, collendpos, &newpos); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4749 |         if (repunicode == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4750 |             return -1; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4751 |         /* generate replacement  */ | 
 | 4752 |         repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 4753 |         for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4754 |             x = charmapencode_output(*uni2, mapping, res, respos); | 
 | 4755 |             if (x==enc_EXCEPTION) { | 
 | 4756 |                 return -1; | 
 | 4757 |             } | 
 | 4758 |             else if (x==enc_FAILED) { | 
 | 4759 |                 Py_DECREF(repunicode); | 
 | 4760 |                 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 4761 |                 return -1; | 
 | 4762 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4763 |         } | 
 | 4764 |         *inpos = newpos; | 
 | 4765 |         Py_DECREF(repunicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4766 |     } | 
 | 4767 |     return 0; | 
 | 4768 | } | 
 | 4769 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4770 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4771 |                                   Py_ssize_t size, | 
 | 4772 |                                   PyObject *mapping, | 
 | 4773 |                                   const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4774 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4775 |     /* output object */ | 
 | 4776 |     PyObject *res = NULL; | 
 | 4777 |     /* current input position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4778 |     Py_ssize_t inpos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4779 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4780 |     Py_ssize_t respos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4781 |     PyObject *errorHandler = NULL; | 
 | 4782 |     PyObject *exc = NULL; | 
 | 4783 |     /* the following variable is used for caching string comparisons | 
 | 4784 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
 | 4785 |      * 3=ignore, 4=xmlcharrefreplace */ | 
 | 4786 |     int known_errorHandler = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4787 |  | 
 | 4788 |     /* Default to Latin-1 */ | 
 | 4789 |     if (mapping == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4790 |         return PyUnicode_EncodeLatin1(p, size, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4791 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4792 |     /* allocate enough for a simple encoding without | 
 | 4793 |        replacements, if we need more, we'll resize */ | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4794 |     res = PyString_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4795 |     if (res == NULL) | 
 | 4796 |         goto onError; | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4797 |     if (size == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4798 |         return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4799 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4800 |     while (inpos<size) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4801 |         /* try to encode it */ | 
 | 4802 |         charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); | 
 | 4803 |         if (x==enc_EXCEPTION) /* error */ | 
 | 4804 |             goto onError; | 
 | 4805 |         if (x==enc_FAILED) { /* unencodable character */ | 
 | 4806 |             if (charmap_encoding_error(p, size, &inpos, mapping, | 
 | 4807 |                                        &exc, | 
 | 4808 |                                        &known_errorHandler, &errorHandler, errors, | 
 | 4809 |                                        &res, &respos)) { | 
 | 4810 |                 goto onError; | 
 | 4811 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4812 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4813 |         else | 
 | 4814 |             /* done with this character => adjust input position */ | 
 | 4815 |             ++inpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4816 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4817 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4818 |     /* Resize if we allocated to much */ | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4819 |     if (respos<PyString_GET_SIZE(res)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4820 |         if (_PyString_Resize(&res, respos)) | 
 | 4821 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4822 |     } | 
 | 4823 |     Py_XDECREF(exc); | 
 | 4824 |     Py_XDECREF(errorHandler); | 
 | 4825 |     return res; | 
 | 4826 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4827 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4828 |     Py_XDECREF(res); | 
 | 4829 |     Py_XDECREF(exc); | 
 | 4830 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4831 |     return NULL; | 
 | 4832 | } | 
 | 4833 |  | 
 | 4834 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4835 |                                     PyObject *mapping) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4836 | { | 
 | 4837 |     if (!PyUnicode_Check(unicode) || mapping == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4838 |         PyErr_BadArgument(); | 
 | 4839 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4840 |     } | 
 | 4841 |     return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4842 |                                    PyUnicode_GET_SIZE(unicode), | 
 | 4843 |                                    mapping, | 
 | 4844 |                                    NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4845 | } | 
 | 4846 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4847 | /* create or adjust a UnicodeTranslateError */ | 
 | 4848 | static void make_translate_exception(PyObject **exceptionObject, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4849 |                                      const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 4850 |                                      Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 4851 |                                      const char *reason) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4852 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4853 |     if (*exceptionObject == NULL) { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4854 |         *exceptionObject = PyUnicodeTranslateError_Create( | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4855 |             unicode, size, startpos, endpos, reason); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4856 |     } | 
 | 4857 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4858 |         if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) | 
 | 4859 |             goto onError; | 
 | 4860 |         if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) | 
 | 4861 |             goto onError; | 
 | 4862 |         if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) | 
 | 4863 |             goto onError; | 
 | 4864 |         return; | 
 | 4865 |       onError: | 
| Serhiy Storchaka | 98a9722 | 2014-02-09 13:14:04 +0200 | [diff] [blame] | 4866 |         Py_CLEAR(*exceptionObject); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4867 |     } | 
 | 4868 | } | 
 | 4869 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4870 | /* raises a UnicodeTranslateError */ | 
 | 4871 | static void raise_translate_exception(PyObject **exceptionObject, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4872 |                                       const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 4873 |                                       Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 4874 |                                       const char *reason) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4875 | { | 
 | 4876 |     make_translate_exception(exceptionObject, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4877 |                              unicode, size, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4878 |     if (*exceptionObject != NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4879 |         PyCodec_StrictErrors(*exceptionObject); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4880 | } | 
 | 4881 |  | 
 | 4882 | /* error handling callback helper: | 
 | 4883 |    build arguments, call the callback and check the arguments, | 
 | 4884 |    put the result into newpos and return the replacement string, which | 
 | 4885 |    has to be freed by the caller */ | 
 | 4886 | static PyObject *unicode_translate_call_errorhandler(const char *errors, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4887 |                                                      PyObject **errorHandler, | 
 | 4888 |                                                      const char *reason, | 
 | 4889 |                                                      const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | 
 | 4890 |                                                      Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 4891 |                                                      Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4892 | { | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4893 |     static char *argparse = "O!n;translating error handler must return (unicode, int) tuple"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4894 |  | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4895 |     Py_ssize_t i_newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4896 |     PyObject *restuple; | 
 | 4897 |     PyObject *resunicode; | 
 | 4898 |  | 
 | 4899 |     if (*errorHandler == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4900 |         *errorHandler = PyCodec_LookupError(errors); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4901 |         if (*errorHandler == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4902 |             return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4903 |     } | 
 | 4904 |  | 
 | 4905 |     make_translate_exception(exceptionObject, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4906 |                              unicode, size, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4907 |     if (*exceptionObject == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4908 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4909 |  | 
 | 4910 |     restuple = PyObject_CallFunctionObjArgs( | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4911 |         *errorHandler, *exceptionObject, NULL); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4912 |     if (restuple == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4913 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4914 |     if (!PyTuple_Check(restuple)) { | 
| Georg Brandl | cbb4958 | 2009-02-13 11:06:59 +0000 | [diff] [blame] | 4915 |         PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4916 |         Py_DECREF(restuple); | 
 | 4917 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4918 |     } | 
 | 4919 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4920 |                           &resunicode, &i_newpos)) { | 
 | 4921 |         Py_DECREF(restuple); | 
 | 4922 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4923 |     } | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4924 |     if (i_newpos<0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4925 |         *newpos = size+i_newpos; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4926 |     else | 
 | 4927 |         *newpos = i_newpos; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4928 |     if (*newpos<0 || *newpos>size) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4929 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); | 
 | 4930 |         Py_DECREF(restuple); | 
 | 4931 |         return NULL; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4932 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4933 |     Py_INCREF(resunicode); | 
 | 4934 |     Py_DECREF(restuple); | 
 | 4935 |     return resunicode; | 
 | 4936 | } | 
 | 4937 |  | 
 | 4938 | /* Lookup the character ch in the mapping and put the result in result, | 
 | 4939 |    which must be decrefed by the caller. | 
 | 4940 |    Return 0 on success, -1 on error */ | 
 | 4941 | static | 
 | 4942 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) | 
 | 4943 | { | 
 | 4944 |     PyObject *w = PyInt_FromLong((long)c); | 
 | 4945 |     PyObject *x; | 
 | 4946 |  | 
 | 4947 |     if (w == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4948 |         return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4949 |     x = PyObject_GetItem(mapping, w); | 
 | 4950 |     Py_DECREF(w); | 
 | 4951 |     if (x == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4952 |         if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 4953 |             /* No mapping found means: use 1:1 mapping. */ | 
 | 4954 |             PyErr_Clear(); | 
 | 4955 |             *result = NULL; | 
 | 4956 |             return 0; | 
 | 4957 |         } else | 
 | 4958 |             return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4959 |     } | 
 | 4960 |     else if (x == Py_None) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4961 |         *result = x; | 
 | 4962 |         return 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4963 |     } | 
 | 4964 |     else if (PyInt_Check(x)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4965 |         long value = PyInt_AS_LONG(x); | 
 | 4966 |         long max = PyUnicode_GetMax(); | 
 | 4967 |         if (value < 0 || value > max) { | 
 | 4968 |             PyErr_Format(PyExc_TypeError, | 
 | 4969 |                          "character mapping must be in range(0x%lx)", max+1); | 
 | 4970 |             Py_DECREF(x); | 
 | 4971 |             return -1; | 
 | 4972 |         } | 
 | 4973 |         *result = x; | 
 | 4974 |         return 0; | 
 | 4975 |     } | 
 | 4976 |     else if (PyUnicode_Check(x)) { | 
 | 4977 |         *result = x; | 
 | 4978 |         return 0; | 
 | 4979 |     } | 
 | 4980 |     else { | 
 | 4981 |         /* wrong return value */ | 
 | 4982 |         PyErr_SetString(PyExc_TypeError, | 
 | 4983 |                         "character mapping must return integer, None or unicode"); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 4984 |         Py_DECREF(x); | 
 | 4985 |         return -1; | 
 | 4986 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4987 | } | 
 | 4988 | /* ensure that *outobj is at least requiredsize characters long, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4989 |    if not reallocate and adjust various state variables. | 
 | 4990 |    Return 0 on success, -1 on error */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4991 | static | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4992 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4993 |                                Py_ssize_t requiredsize) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4994 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4995 |     Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4996 |     if (requiredsize > oldsize) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 4997 |         /* remember old output position */ | 
 | 4998 |         Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); | 
 | 4999 |         /* exponentially overallocate to minimize reallocations */ | 
 | 5000 |         if (requiredsize < 2 * oldsize) | 
 | 5001 |             requiredsize = 2 * oldsize; | 
 | 5002 |         if (PyUnicode_Resize(outobj, requiredsize) < 0) | 
 | 5003 |             return -1; | 
 | 5004 |         *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5005 |     } | 
 | 5006 |     return 0; | 
 | 5007 | } | 
 | 5008 | /* lookup the character, put the result in the output string and adjust | 
 | 5009 |    various state variables. Return a new reference to the object that | 
 | 5010 |    was put in the output buffer in *result, or Py_None, if the mapping was | 
 | 5011 |    undefined (in which case no character was written). | 
 | 5012 |    The called must decref result. | 
 | 5013 |    Return 0 on success, -1 on error. */ | 
 | 5014 | static | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5015 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5016 |                             Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, | 
 | 5017 |                             PyObject **res) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5018 | { | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5019 |     if (charmaptranslate_lookup(*curinp, mapping, res)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5020 |         return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5021 |     if (*res==NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5022 |         /* not found => default to 1:1 mapping */ | 
 | 5023 |         *(*outp)++ = *curinp; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5024 |     } | 
 | 5025 |     else if (*res==Py_None) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5026 |         ; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5027 |     else if (PyInt_Check(*res)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5028 |         /* no overflow check, because we know that the space is enough */ | 
 | 5029 |         *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5030 |     } | 
 | 5031 |     else if (PyUnicode_Check(*res)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5032 |         Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); | 
 | 5033 |         if (repsize==1) { | 
 | 5034 |             /* no overflow check, because we know that the space is enough */ | 
 | 5035 |             *(*outp)++ = *PyUnicode_AS_UNICODE(*res); | 
 | 5036 |         } | 
 | 5037 |         else if (repsize!=0) { | 
 | 5038 |             /* more than one character */ | 
 | 5039 |             Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + | 
 | 5040 |                 (insize - (curinp-startinp)) + | 
 | 5041 |                 repsize - 1; | 
 | 5042 |             if (charmaptranslate_makespace(outobj, outp, requiredsize)) | 
 | 5043 |                 return -1; | 
 | 5044 |             memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); | 
 | 5045 |             *outp += repsize; | 
 | 5046 |         } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5047 |     } | 
 | 5048 |     else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5049 |         return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5050 |     return 0; | 
 | 5051 | } | 
 | 5052 |  | 
 | 5053 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5054 |                                      Py_ssize_t size, | 
 | 5055 |                                      PyObject *mapping, | 
 | 5056 |                                      const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5057 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5058 |     /* output object */ | 
 | 5059 |     PyObject *res = NULL; | 
 | 5060 |     /* pointers to the beginning and end+1 of input */ | 
 | 5061 |     const Py_UNICODE *startp = p; | 
 | 5062 |     const Py_UNICODE *endp = p + size; | 
 | 5063 |     /* pointer into the output */ | 
 | 5064 |     Py_UNICODE *str; | 
 | 5065 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5066 |     Py_ssize_t respos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5067 |     char *reason = "character maps to <undefined>"; | 
 | 5068 |     PyObject *errorHandler = NULL; | 
 | 5069 |     PyObject *exc = NULL; | 
 | 5070 |     /* the following variable is used for caching string comparisons | 
 | 5071 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
 | 5072 |      * 3=ignore, 4=xmlcharrefreplace */ | 
 | 5073 |     int known_errorHandler = -1; | 
 | 5074 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5075 |     if (mapping == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5076 |         PyErr_BadArgument(); | 
 | 5077 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5078 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5079 |  | 
 | 5080 |     /* allocate enough for a simple 1:1 translation without | 
 | 5081 |        replacements, if we need more, we'll resize */ | 
 | 5082 |     res = PyUnicode_FromUnicode(NULL, size); | 
 | 5083 |     if (res == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5084 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5085 |     if (size == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5086 |         return res; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5087 |     str = PyUnicode_AS_UNICODE(res); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5088 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5089 |     while (p<endp) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5090 |         /* try to encode it */ | 
 | 5091 |         PyObject *x = NULL; | 
 | 5092 |         if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { | 
 | 5093 |             Py_XDECREF(x); | 
 | 5094 |             goto onError; | 
 | 5095 |         } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5096 |         Py_XDECREF(x); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5097 |         if (x!=Py_None) /* it worked => adjust input pointer */ | 
 | 5098 |             ++p; | 
 | 5099 |         else { /* untranslatable character */ | 
 | 5100 |             PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
 | 5101 |             Py_ssize_t repsize; | 
 | 5102 |             Py_ssize_t newpos; | 
 | 5103 |             Py_UNICODE *uni2; | 
 | 5104 |             /* startpos for collecting untranslatable chars */ | 
 | 5105 |             const Py_UNICODE *collstart = p; | 
 | 5106 |             const Py_UNICODE *collend = p+1; | 
 | 5107 |             const Py_UNICODE *coll; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5108 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5109 |             /* find all untranslatable characters */ | 
 | 5110 |             while (collend < endp) { | 
 | 5111 |                 if (charmaptranslate_lookup(*collend, mapping, &x)) | 
 | 5112 |                     goto onError; | 
 | 5113 |                 Py_XDECREF(x); | 
 | 5114 |                 if (x!=Py_None) | 
 | 5115 |                     break; | 
 | 5116 |                 ++collend; | 
 | 5117 |             } | 
 | 5118 |             /* cache callback name lookup | 
 | 5119 |              * (if not done yet, i.e. it's the first error) */ | 
 | 5120 |             if (known_errorHandler==-1) { | 
 | 5121 |                 if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 5122 |                     known_errorHandler = 1; | 
 | 5123 |                 else if (!strcmp(errors, "replace")) | 
 | 5124 |                     known_errorHandler = 2; | 
 | 5125 |                 else if (!strcmp(errors, "ignore")) | 
 | 5126 |                     known_errorHandler = 3; | 
 | 5127 |                 else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 5128 |                     known_errorHandler = 4; | 
 | 5129 |                 else | 
 | 5130 |                     known_errorHandler = 0; | 
 | 5131 |             } | 
 | 5132 |             switch (known_errorHandler) { | 
 | 5133 |             case 1: /* strict */ | 
 | 5134 |                 raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5135 |                 goto onError; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5136 |             case 2: /* replace */ | 
 | 5137 |                 /* No need to check for space, this is a 1:1 replacement */ | 
 | 5138 |                 for (coll = collstart; coll<collend; ++coll) | 
 | 5139 |                     *str++ = '?'; | 
 | 5140 |                 /* fall through */ | 
 | 5141 |             case 3: /* ignore */ | 
 | 5142 |                 p = collend; | 
 | 5143 |                 break; | 
 | 5144 |             case 4: /* xmlcharrefreplace */ | 
 | 5145 |                 /* generate replacement (temporarily (mis)uses p) */ | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 5146 |                 for (p = collstart; p < collend;) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5147 |                     char buffer[2+29+1+1]; | 
 | 5148 |                     char *cp; | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 5149 |                     Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend); | 
 | 5150 |                     sprintf(buffer, "&#%d;", (int)ch); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5151 |                     if (charmaptranslate_makespace(&res, &str, | 
 | 5152 |                                                    (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) | 
 | 5153 |                         goto onError; | 
 | 5154 |                     for (cp = buffer; *cp; ++cp) | 
 | 5155 |                         *str++ = *cp; | 
 | 5156 |                 } | 
 | 5157 |                 p = collend; | 
 | 5158 |                 break; | 
 | 5159 |             default: | 
 | 5160 |                 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, | 
 | 5161 |                                                                  reason, startp, size, &exc, | 
 | 5162 |                                                                  collstart-startp, collend-startp, &newpos); | 
 | 5163 |                 if (repunicode == NULL) | 
 | 5164 |                     goto onError; | 
 | 5165 |                 /* generate replacement  */ | 
 | 5166 |                 repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 5167 |                 if (charmaptranslate_makespace(&res, &str, | 
 | 5168 |                                                (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { | 
 | 5169 |                     Py_DECREF(repunicode); | 
 | 5170 |                     goto onError; | 
 | 5171 |                 } | 
 | 5172 |                 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) | 
 | 5173 |                     *str++ = *uni2; | 
 | 5174 |                 p = startp + newpos; | 
 | 5175 |                 Py_DECREF(repunicode); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5176 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5177 |         } | 
 | 5178 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5179 |     /* Resize if we allocated to much */ | 
 | 5180 |     respos = str-PyUnicode_AS_UNICODE(res); | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5181 |     if (respos<PyUnicode_GET_SIZE(res)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5182 |         if (PyUnicode_Resize(&res, respos) < 0) | 
 | 5183 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5184 |     } | 
 | 5185 |     Py_XDECREF(exc); | 
 | 5186 |     Py_XDECREF(errorHandler); | 
 | 5187 |     return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5188 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5189 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5190 |     Py_XDECREF(res); | 
 | 5191 |     Py_XDECREF(exc); | 
 | 5192 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5193 |     return NULL; | 
 | 5194 | } | 
 | 5195 |  | 
 | 5196 | PyObject *PyUnicode_Translate(PyObject *str, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5197 |                               PyObject *mapping, | 
 | 5198 |                               const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5199 | { | 
 | 5200 |     PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5201 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5202 |     str = PyUnicode_FromObject(str); | 
 | 5203 |     if (str == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5204 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 |     result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5206 |                                         PyUnicode_GET_SIZE(str), | 
 | 5207 |                                         mapping, | 
 | 5208 |                                         errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5209 |     Py_DECREF(str); | 
 | 5210 |     return result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5211 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5212 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 |     Py_XDECREF(str); | 
 | 5214 |     return NULL; | 
 | 5215 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5216 |  | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5217 | /* --- Decimal Encoder ---------------------------------------------------- */ | 
 | 5218 |  | 
 | 5219 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5220 |                             Py_ssize_t length, | 
 | 5221 |                             char *output, | 
 | 5222 |                             const char *errors) | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5223 | { | 
 | 5224 |     Py_UNICODE *p, *end; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5225 |     PyObject *errorHandler = NULL; | 
 | 5226 |     PyObject *exc = NULL; | 
 | 5227 |     const char *encoding = "decimal"; | 
 | 5228 |     const char *reason = "invalid decimal Unicode string"; | 
 | 5229 |     /* the following variable is used for caching string comparisons | 
 | 5230 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ | 
 | 5231 |     int known_errorHandler = -1; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5232 |  | 
 | 5233 |     if (output == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5234 |         PyErr_BadArgument(); | 
 | 5235 |         return -1; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5236 |     } | 
 | 5237 |  | 
 | 5238 |     p = s; | 
 | 5239 |     end = s + length; | 
 | 5240 |     while (p < end) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5241 |         register Py_UNICODE ch = *p; | 
 | 5242 |         int decimal; | 
 | 5243 |         PyObject *repunicode; | 
 | 5244 |         Py_ssize_t repsize; | 
 | 5245 |         Py_ssize_t newpos; | 
 | 5246 |         Py_UNICODE *uni2; | 
 | 5247 |         Py_UNICODE *collstart; | 
 | 5248 |         Py_UNICODE *collend; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5249 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5250 |         if (Py_UNICODE_ISSPACE(ch)) { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5251 |             *output++ = ' '; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5252 |             ++p; | 
 | 5253 |             continue; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5254 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5255 |         decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 5256 |         if (decimal >= 0) { | 
 | 5257 |             *output++ = '0' + decimal; | 
 | 5258 |             ++p; | 
 | 5259 |             continue; | 
 | 5260 |         } | 
 | 5261 |         if (0 < ch && ch < 256) { | 
 | 5262 |             *output++ = (char)ch; | 
 | 5263 |             ++p; | 
 | 5264 |             continue; | 
 | 5265 |         } | 
 | 5266 |         /* All other characters are considered unencodable */ | 
 | 5267 |         collstart = p; | 
| Victor Stinner | 975134e | 2011-11-22 01:54:19 +0100 | [diff] [blame] | 5268 |         for (collend = p+1; collend < end; collend++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5269 |             if ((0 < *collend && *collend < 256) || | 
| Victor Stinner | 975134e | 2011-11-22 01:54:19 +0100 | [diff] [blame] | 5270 |                 Py_UNICODE_ISSPACE(*collend) || | 
 | 5271 |                 0 <= Py_UNICODE_TODECIMAL(*collend)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5272 |                 break; | 
 | 5273 |         } | 
 | 5274 |         /* cache callback name lookup | 
 | 5275 |          * (if not done yet, i.e. it's the first error) */ | 
 | 5276 |         if (known_errorHandler==-1) { | 
 | 5277 |             if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 5278 |                 known_errorHandler = 1; | 
 | 5279 |             else if (!strcmp(errors, "replace")) | 
 | 5280 |                 known_errorHandler = 2; | 
 | 5281 |             else if (!strcmp(errors, "ignore")) | 
 | 5282 |                 known_errorHandler = 3; | 
 | 5283 |             else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 5284 |                 known_errorHandler = 4; | 
 | 5285 |             else | 
 | 5286 |                 known_errorHandler = 0; | 
 | 5287 |         } | 
 | 5288 |         switch (known_errorHandler) { | 
 | 5289 |         case 1: /* strict */ | 
 | 5290 |             raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); | 
 | 5291 |             goto onError; | 
 | 5292 |         case 2: /* replace */ | 
 | 5293 |             for (p = collstart; p < collend; ++p) | 
 | 5294 |                 *output++ = '?'; | 
 | 5295 |             /* fall through */ | 
 | 5296 |         case 3: /* ignore */ | 
 | 5297 |             p = collend; | 
 | 5298 |             break; | 
 | 5299 |         case 4: /* xmlcharrefreplace */ | 
 | 5300 |             /* generate replacement (temporarily (mis)uses p) */ | 
| Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 5301 |             for (p = collstart; p < collend;) { | 
 | 5302 |                 Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend); | 
 | 5303 |                 output += sprintf(output, "&#%d;", ch); | 
 | 5304 |             } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5305 |             p = collend; | 
 | 5306 |             break; | 
 | 5307 |         default: | 
 | 5308 |             repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | 
 | 5309 |                                                           encoding, reason, s, length, &exc, | 
 | 5310 |                                                           collstart-s, collend-s, &newpos); | 
 | 5311 |             if (repunicode == NULL) | 
 | 5312 |                 goto onError; | 
 | 5313 |             /* generate replacement  */ | 
 | 5314 |             repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 5315 |             for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | 
 | 5316 |                 Py_UNICODE ch = *uni2; | 
 | 5317 |                 if (Py_UNICODE_ISSPACE(ch)) | 
 | 5318 |                     *output++ = ' '; | 
 | 5319 |                 else { | 
 | 5320 |                     decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 5321 |                     if (decimal >= 0) | 
 | 5322 |                         *output++ = '0' + decimal; | 
 | 5323 |                     else if (0 < ch && ch < 256) | 
 | 5324 |                         *output++ = (char)ch; | 
 | 5325 |                     else { | 
 | 5326 |                         Py_DECREF(repunicode); | 
 | 5327 |                         raise_encode_exception(&exc, encoding, | 
 | 5328 |                                                s, length, collstart-s, collend-s, reason); | 
 | 5329 |                         goto onError; | 
 | 5330 |                     } | 
 | 5331 |                 } | 
 | 5332 |             } | 
 | 5333 |             p = s + newpos; | 
 | 5334 |             Py_DECREF(repunicode); | 
 | 5335 |         } | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5336 |     } | 
 | 5337 |     /* 0-terminate the output string */ | 
 | 5338 |     *output++ = '\0'; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5339 |     Py_XDECREF(exc); | 
 | 5340 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5341 |     return 0; | 
 | 5342 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5343 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5344 |     Py_XDECREF(exc); | 
 | 5345 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5346 |     return -1; | 
 | 5347 | } | 
 | 5348 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5349 | /* --- Helpers ------------------------------------------------------------ */ | 
 | 5350 |  | 
| Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 5351 | #include "stringlib/unicodedefs.h" | 
| Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 5352 | #include "stringlib/fastsearch.h" | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5353 |  | 
 | 5354 | #include "stringlib/count.h" | 
 | 5355 | #include "stringlib/find.h" | 
| Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5356 | #include "stringlib/partition.h" | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5357 | #include "stringlib/split.h" | 
| Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5358 |  | 
| Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5359 | /* helper macro to fixup start/end slice values */ | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5360 | #define ADJUST_INDICES(start, end, len)         \ | 
 | 5361 |     if (end > len)                              \ | 
 | 5362 |         end = len;                              \ | 
 | 5363 |     else if (end < 0) {                         \ | 
 | 5364 |         end += len;                             \ | 
 | 5365 |         if (end < 0)                            \ | 
 | 5366 |             end = 0;                            \ | 
 | 5367 |     }                                           \ | 
 | 5368 |     if (start < 0) {                            \ | 
 | 5369 |         start += len;                           \ | 
 | 5370 |         if (start < 0)                          \ | 
 | 5371 |             start = 0;                          \ | 
 | 5372 |     } | 
| Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5373 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5374 | Py_ssize_t PyUnicode_Count(PyObject *str, | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5375 |                            PyObject *substr, | 
 | 5376 |                            Py_ssize_t start, | 
 | 5377 |                            Py_ssize_t end) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5378 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5379 |     Py_ssize_t result; | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5380 |     PyUnicodeObject* str_obj; | 
 | 5381 |     PyUnicodeObject* sub_obj; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5382 |  | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5383 |     str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); | 
 | 5384 |     if (!str_obj) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5385 |         return -1; | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5386 |     sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); | 
 | 5387 |     if (!sub_obj) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5388 |         Py_DECREF(str_obj); | 
 | 5389 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5391 |  | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5392 |     ADJUST_INDICES(start, end, str_obj->length); | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5393 |     result = stringlib_count( | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5394 |         str_obj->str + start, end - start, sub_obj->str, sub_obj->length, | 
 | 5395 |         PY_SSIZE_T_MAX | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5396 |         ); | 
 | 5397 |  | 
 | 5398 |     Py_DECREF(sub_obj); | 
 | 5399 |     Py_DECREF(str_obj); | 
 | 5400 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5401 |     return result; | 
 | 5402 | } | 
 | 5403 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5404 | Py_ssize_t PyUnicode_Find(PyObject *str, | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5405 |                           PyObject *sub, | 
| Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5406 |                           Py_ssize_t start, | 
 | 5407 |                           Py_ssize_t end, | 
 | 5408 |                           int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5410 |     Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5411 |  | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5412 |     str = PyUnicode_FromObject(str); | 
 | 5413 |     if (!str) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5414 |         return -2; | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5415 |     sub = PyUnicode_FromObject(sub); | 
 | 5416 |     if (!sub) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5417 |         Py_DECREF(str); | 
 | 5418 |         return -2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5419 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5420 |  | 
| Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5421 |     if (direction > 0) | 
| Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5422 |         result = stringlib_find_slice( | 
 | 5423 |             PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), | 
 | 5424 |             PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), | 
 | 5425 |             start, end | 
 | 5426 |             ); | 
| Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5427 |     else | 
| Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5428 |         result = stringlib_rfind_slice( | 
 | 5429 |             PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), | 
 | 5430 |             PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), | 
 | 5431 |             start, end | 
 | 5432 |             ); | 
| Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5433 |  | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5434 |     Py_DECREF(str); | 
 | 5435 |     Py_DECREF(sub); | 
 | 5436 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 |     return result; | 
 | 5438 | } | 
 | 5439 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5440 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | int tailmatch(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5442 |               PyUnicodeObject *substring, | 
 | 5443 |               Py_ssize_t start, | 
 | 5444 |               Py_ssize_t end, | 
 | 5445 |               int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5446 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 |     if (substring->length == 0) | 
 | 5448 |         return 1; | 
 | 5449 |  | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5450 |     ADJUST_INDICES(start, end, self->length); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5451 |     end -= substring->length; | 
 | 5452 |     if (end < start) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5453 |         return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 |  | 
 | 5455 |     if (direction > 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5456 |         if (Py_UNICODE_MATCH(self, end, substring)) | 
 | 5457 |             return 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5458 |     } else { | 
 | 5459 |         if (Py_UNICODE_MATCH(self, start, substring)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5460 |             return 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5461 |     } | 
 | 5462 |  | 
 | 5463 |     return 0; | 
 | 5464 | } | 
 | 5465 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5466 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5467 |                                PyObject *substr, | 
 | 5468 |                                Py_ssize_t start, | 
 | 5469 |                                Py_ssize_t end, | 
 | 5470 |                                int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5472 |     Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5473 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5474 |     str = PyUnicode_FromObject(str); | 
 | 5475 |     if (str == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5476 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5477 |     substr = PyUnicode_FromObject(substr); | 
 | 5478 |     if (substr == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5479 |         Py_DECREF(str); | 
 | 5480 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5481 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5482 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5483 |     result = tailmatch((PyUnicodeObject *)str, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5484 |                        (PyUnicodeObject *)substr, | 
 | 5485 |                        start, end, direction); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5486 |     Py_DECREF(str); | 
 | 5487 |     Py_DECREF(substr); | 
 | 5488 |     return result; | 
 | 5489 | } | 
 | 5490 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5491 | /* Apply fixfct filter to the Unicode object self and return a | 
 | 5492 |    reference to the modified object */ | 
 | 5493 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5494 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5495 | PyObject *fixup(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5496 |                 int (*fixfct)(PyUnicodeObject *s)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5497 | { | 
 | 5498 |  | 
 | 5499 |     PyUnicodeObject *u; | 
 | 5500 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5501 |     u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 |     if (u == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5503 |         return NULL; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5504 |  | 
 | 5505 |     Py_UNICODE_COPY(u->str, self->str, self->length); | 
 | 5506 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5507 |     if (!fixfct(u) && PyUnicode_CheckExact(self)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5508 |         /* fixfct should return TRUE if it modified the buffer. If | 
 | 5509 |            FALSE, return a reference to the original buffer instead | 
 | 5510 |            (to save space, not time) */ | 
 | 5511 |         Py_INCREF(self); | 
 | 5512 |         Py_DECREF(u); | 
 | 5513 |         return (PyObject*) self; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5514 |     } | 
 | 5515 |     return (PyObject*) u; | 
 | 5516 | } | 
 | 5517 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5518 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 | int fixupper(PyUnicodeObject *self) | 
 | 5520 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5521 |     Py_ssize_t len = self->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5522 |     Py_UNICODE *s = self->str; | 
 | 5523 |     int status = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5524 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5525 |     while (len-- > 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5526 |         register Py_UNICODE ch; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5527 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5528 |         ch = Py_UNICODE_TOUPPER(*s); | 
 | 5529 |         if (ch != *s) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5530 |             status = 1; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5531 |             *s = ch; | 
 | 5532 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5533 |         s++; | 
 | 5534 |     } | 
 | 5535 |  | 
 | 5536 |     return status; | 
 | 5537 | } | 
 | 5538 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5539 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | int fixlower(PyUnicodeObject *self) | 
 | 5541 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5542 |     Py_ssize_t len = self->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5543 |     Py_UNICODE *s = self->str; | 
 | 5544 |     int status = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5545 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5546 |     while (len-- > 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5547 |         register Py_UNICODE ch; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5548 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5549 |         ch = Py_UNICODE_TOLOWER(*s); | 
 | 5550 |         if (ch != *s) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5551 |             status = 1; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5552 |             *s = ch; | 
 | 5553 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5554 |         s++; | 
 | 5555 |     } | 
 | 5556 |  | 
 | 5557 |     return status; | 
 | 5558 | } | 
 | 5559 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5560 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5561 | int fixswapcase(PyUnicodeObject *self) | 
 | 5562 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5563 |     Py_ssize_t len = self->length; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5564 |     Py_UNICODE *s = self->str; | 
 | 5565 |     int status = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5566 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5567 |     while (len-- > 0) { | 
 | 5568 |         if (Py_UNICODE_ISUPPER(*s)) { | 
 | 5569 |             *s = Py_UNICODE_TOLOWER(*s); | 
 | 5570 |             status = 1; | 
 | 5571 |         } else if (Py_UNICODE_ISLOWER(*s)) { | 
 | 5572 |             *s = Py_UNICODE_TOUPPER(*s); | 
 | 5573 |             status = 1; | 
 | 5574 |         } | 
 | 5575 |         s++; | 
 | 5576 |     } | 
 | 5577 |  | 
 | 5578 |     return status; | 
 | 5579 | } | 
 | 5580 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5581 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5582 | int fixcapitalize(PyUnicodeObject *self) | 
 | 5583 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5584 |     Py_ssize_t len = self->length; | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5585 |     Py_UNICODE *s = self->str; | 
 | 5586 |     int status = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5587 |  | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5588 |     if (len == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5589 |         return 0; | 
| Ezio Melotti | 15d6b65 | 2011-08-15 09:22:24 +0300 | [diff] [blame] | 5590 |     if (!Py_UNICODE_ISUPPER(*s)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5591 |         *s = Py_UNICODE_TOUPPER(*s); | 
 | 5592 |         status = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5593 |     } | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5594 |     s++; | 
 | 5595 |     while (--len > 0) { | 
| Ezio Melotti | 15d6b65 | 2011-08-15 09:22:24 +0300 | [diff] [blame] | 5596 |         if (!Py_UNICODE_ISLOWER(*s)) { | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5597 |             *s = Py_UNICODE_TOLOWER(*s); | 
 | 5598 |             status = 1; | 
 | 5599 |         } | 
 | 5600 |         s++; | 
 | 5601 |     } | 
 | 5602 |     return status; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | } | 
 | 5604 |  | 
 | 5605 | static | 
 | 5606 | int fixtitle(PyUnicodeObject *self) | 
 | 5607 | { | 
 | 5608 |     register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 5609 |     register Py_UNICODE *e; | 
 | 5610 |     int previous_is_cased; | 
 | 5611 |  | 
 | 5612 |     /* Shortcut for single character strings */ | 
 | 5613 |     if (PyUnicode_GET_SIZE(self) == 1) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5614 |         Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); | 
 | 5615 |         if (*p != ch) { | 
 | 5616 |             *p = ch; | 
 | 5617 |             return 1; | 
 | 5618 |         } | 
 | 5619 |         else | 
 | 5620 |             return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5621 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5622 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5623 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 5624 |     previous_is_cased = 0; | 
 | 5625 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5626 |         register const Py_UNICODE ch = *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5627 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5628 |         if (previous_is_cased) | 
 | 5629 |             *p = Py_UNICODE_TOLOWER(ch); | 
 | 5630 |         else | 
 | 5631 |             *p = Py_UNICODE_TOTITLE(ch); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5632 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5633 |         if (Py_UNICODE_ISLOWER(ch) || | 
 | 5634 |             Py_UNICODE_ISUPPER(ch) || | 
 | 5635 |             Py_UNICODE_ISTITLE(ch)) | 
 | 5636 |             previous_is_cased = 1; | 
 | 5637 |         else | 
 | 5638 |             previous_is_cased = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 |     } | 
 | 5640 |     return 1; | 
 | 5641 | } | 
 | 5642 |  | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5643 | PyObject * | 
 | 5644 | PyUnicode_Join(PyObject *separator, PyObject *seq) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5645 | { | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5646 |     PyObject *internal_separator = NULL; | 
| Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5647 |     const Py_UNICODE blank = ' '; | 
 | 5648 |     const Py_UNICODE *sep = ␣ | 
| Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5649 |     Py_ssize_t seplen = 1; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5650 |     PyUnicodeObject *res = NULL; /* the result */ | 
| Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5651 |     Py_ssize_t res_alloc = 100;  /* # allocated bytes for string in res */ | 
 | 5652 |     Py_ssize_t res_used;         /* # used bytes */ | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5653 |     Py_UNICODE *res_p;       /* pointer to free byte in res's string area */ | 
 | 5654 |     PyObject *fseq;          /* PySequence_Fast(seq) */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5655 |     Py_ssize_t seqlen;              /* len(fseq) -- number of items in sequence */ | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5656 |     PyObject *item; | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5657 |     Py_ssize_t i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 |  | 
| Benjamin Peterson | 7a91bf8 | 2014-02-15 13:02:52 -0500 | [diff] [blame] | 5659 |     fseq = PySequence_Fast(seq, "can only join an iterable"); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5660 |     if (fseq == NULL) { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5661 |         return NULL; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5662 |     } | 
 | 5663 |  | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5664 |     /* Grrrr.  A codec may be invoked to convert str objects to | 
 | 5665 |      * Unicode, and so it's possible to call back into Python code | 
 | 5666 |      * during PyUnicode_FromObject(), and so it's possible for a sick | 
 | 5667 |      * codec to change the size of fseq (if seq is a list).  Therefore | 
 | 5668 |      * we have to keep refetching the size -- can't assume seqlen | 
 | 5669 |      * is invariant. | 
 | 5670 |      */ | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5671 |     seqlen = PySequence_Fast_GET_SIZE(fseq); | 
 | 5672 |     /* If empty sequence, return u"". */ | 
 | 5673 |     if (seqlen == 0) { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5674 |         res = _PyUnicode_New(0);  /* empty sequence; return u"" */ | 
 | 5675 |         goto Done; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5676 |     } | 
 | 5677 |     /* If singleton sequence with an exact Unicode, return that. */ | 
 | 5678 |     if (seqlen == 1) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5679 |         item = PySequence_Fast_GET_ITEM(fseq, 0); | 
 | 5680 |         if (PyUnicode_CheckExact(item)) { | 
 | 5681 |             Py_INCREF(item); | 
 | 5682 |             res = (PyUnicodeObject *)item; | 
 | 5683 |             goto Done; | 
 | 5684 |         } | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5685 |     } | 
 | 5686 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5687 |     /* At least two items to join, or one that isn't exact Unicode. */ | 
 | 5688 |     if (seqlen > 1) { | 
 | 5689 |         /* Set up sep and seplen -- they're needed. */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5690 |         if (separator == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5691 |             sep = ␣ | 
 | 5692 |             seplen = 1; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5693 |         } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5694 |         else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5695 |             internal_separator = PyUnicode_FromObject(separator); | 
 | 5696 |             if (internal_separator == NULL) | 
 | 5697 |                 goto onError; | 
 | 5698 |             sep = PyUnicode_AS_UNICODE(internal_separator); | 
 | 5699 |             seplen = PyUnicode_GET_SIZE(internal_separator); | 
 | 5700 |             /* In case PyUnicode_FromObject() mutated seq. */ | 
 | 5701 |             seqlen = PySequence_Fast_GET_SIZE(fseq); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5702 |         } | 
 | 5703 |     } | 
 | 5704 |  | 
 | 5705 |     /* Get space. */ | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5706 |     res = _PyUnicode_New(res_alloc); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5707 |     if (res == NULL) | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5708 |         goto onError; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5709 |     res_p = PyUnicode_AS_UNICODE(res); | 
 | 5710 |     res_used = 0; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5711 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5712 |     for (i = 0; i < seqlen; ++i) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5713 |         Py_ssize_t itemlen; | 
 | 5714 |         Py_ssize_t new_res_used; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5715 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5716 |         item = PySequence_Fast_GET_ITEM(fseq, i); | 
 | 5717 |         /* Convert item to Unicode. */ | 
 | 5718 |         if (! PyUnicode_Check(item) && ! PyString_Check(item)) { | 
 | 5719 |             PyErr_Format(PyExc_TypeError, | 
 | 5720 |                          "sequence item %zd: expected string or Unicode," | 
 | 5721 |                          " %.80s found", | 
 | 5722 |                          i, Py_TYPE(item)->tp_name); | 
 | 5723 |             goto onError; | 
 | 5724 |         } | 
 | 5725 |         item = PyUnicode_FromObject(item); | 
 | 5726 |         if (item == NULL) | 
 | 5727 |             goto onError; | 
 | 5728 |         /* We own a reference to item from here on. */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5729 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5730 |         /* In case PyUnicode_FromObject() mutated seq. */ | 
 | 5731 |         seqlen = PySequence_Fast_GET_SIZE(fseq); | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5732 |  | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5733 |         /* Make sure we have enough space for the separator and the item. */ | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5734 |         itemlen = PyUnicode_GET_SIZE(item); | 
| Xiang Zhang | 7bdb516 | 2017-01-09 11:13:20 +0800 | [diff] [blame] | 5735 |         if (res_used > PY_SSIZE_T_MAX - itemlen) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5736 |             goto Overflow; | 
| Xiang Zhang | 7bdb516 | 2017-01-09 11:13:20 +0800 | [diff] [blame] | 5737 |         new_res_used = res_used + itemlen; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5738 |         if (i < seqlen - 1) { | 
| Xiang Zhang | 7bdb516 | 2017-01-09 11:13:20 +0800 | [diff] [blame] | 5739 |             if (new_res_used > PY_SSIZE_T_MAX - seplen) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5740 |                 goto Overflow; | 
| Xiang Zhang | 7bdb516 | 2017-01-09 11:13:20 +0800 | [diff] [blame] | 5741 |             new_res_used += seplen; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 5742 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5743 |         if (new_res_used > res_alloc) { | 
 | 5744 |             /* double allocated size until it's big enough */ | 
 | 5745 |             do { | 
| Xiang Zhang | 7bdb516 | 2017-01-09 11:13:20 +0800 | [diff] [blame] | 5746 |                 if (res_alloc > PY_SSIZE_T_MAX / 2) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5747 |                     goto Overflow; | 
| Xiang Zhang | 7bdb516 | 2017-01-09 11:13:20 +0800 | [diff] [blame] | 5748 |                 res_alloc += res_alloc; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5749 |             } while (new_res_used > res_alloc); | 
 | 5750 |             if (_PyUnicode_Resize(&res, res_alloc) < 0) { | 
 | 5751 |                 Py_DECREF(item); | 
 | 5752 |                 goto onError; | 
 | 5753 |             } | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5754 |             res_p = PyUnicode_AS_UNICODE(res) + res_used; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5755 |         } | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5756 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5757 |         /* Copy item, and maybe the separator. */ | 
 | 5758 |         Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); | 
 | 5759 |         res_p += itemlen; | 
 | 5760 |         if (i < seqlen - 1) { | 
 | 5761 |             Py_UNICODE_COPY(res_p, sep, seplen); | 
 | 5762 |             res_p += seplen; | 
 | 5763 |         } | 
 | 5764 |         Py_DECREF(item); | 
 | 5765 |         res_used = new_res_used; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5766 |     } | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5767 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5768 |     /* Shrink res to match the used area; this probably can't fail, | 
 | 5769 |      * but it's cheap to check. | 
 | 5770 |      */ | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5771 |     if (_PyUnicode_Resize(&res, res_used) < 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5772 |         goto onError; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5773 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5774 |   Done: | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5775 |     Py_XDECREF(internal_separator); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5776 |     Py_DECREF(fseq); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5777 |     return (PyObject *)res; | 
 | 5778 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5779 |   Overflow: | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5780 |     PyErr_SetString(PyExc_OverflowError, | 
| Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5781 |                     "join() result is too long for a Python string"); | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5782 |     Py_DECREF(item); | 
 | 5783 |     /* fall through */ | 
 | 5784 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5785 |   onError: | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5786 |     Py_XDECREF(internal_separator); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5787 |     Py_DECREF(fseq); | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5788 |     Py_XDECREF(res); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5789 |     return NULL; | 
 | 5790 | } | 
 | 5791 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5792 | static | 
 | 5793 | PyUnicodeObject *pad(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5794 |                      Py_ssize_t left, | 
 | 5795 |                      Py_ssize_t right, | 
 | 5796 |                      Py_UNICODE fill) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5797 | { | 
 | 5798 |     PyUnicodeObject *u; | 
 | 5799 |  | 
 | 5800 |     if (left < 0) | 
 | 5801 |         left = 0; | 
 | 5802 |     if (right < 0) | 
 | 5803 |         right = 0; | 
 | 5804 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5805 |     if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5806 |         Py_INCREF(self); | 
 | 5807 |         return self; | 
 | 5808 |     } | 
 | 5809 |  | 
| Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 5810 |     if (left > PY_SSIZE_T_MAX - self->length || | 
 | 5811 |         right > PY_SSIZE_T_MAX - (left + self->length)) { | 
 | 5812 |         PyErr_SetString(PyExc_OverflowError, "padded string is too long"); | 
 | 5813 |         return NULL; | 
 | 5814 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5815 |     u = _PyUnicode_New(left + self->length + right); | 
 | 5816 |     if (u) { | 
 | 5817 |         if (left) | 
 | 5818 |             Py_UNICODE_FILL(u->str, fill, left); | 
 | 5819 |         Py_UNICODE_COPY(u->str + left, self->str, self->length); | 
 | 5820 |         if (right) | 
 | 5821 |             Py_UNICODE_FILL(u->str + left + self->length, fill, right); | 
 | 5822 |     } | 
 | 5823 |  | 
 | 5824 |     return u; | 
 | 5825 | } | 
 | 5826 |  | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5827 | PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5828 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5829 |     PyObject *list; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5830 |  | 
 | 5831 |     string = PyUnicode_FromObject(string); | 
 | 5832 |     if (string == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5833 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5834 |  | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5835 |     list = stringlib_splitlines( | 
 | 5836 |         (PyObject*) string, PyUnicode_AS_UNICODE(string), | 
 | 5837 |         PyUnicode_GET_SIZE(string), keepends); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5838 |  | 
 | 5839 |     Py_DECREF(string); | 
 | 5840 |     return list; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5841 | } | 
 | 5842 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5843 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | PyObject *split(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5845 |                 PyUnicodeObject *substring, | 
 | 5846 |                 Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5847 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5848 |     if (maxcount < 0) | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5849 |         maxcount = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5850 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 |     if (substring == NULL) | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5852 |         return stringlib_split_whitespace( | 
 | 5853 |             (PyObject*) self,  self->str, self->length, maxcount | 
 | 5854 |             ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5855 |  | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5856 |     return stringlib_split( | 
 | 5857 |         (PyObject*) self,  self->str, self->length, | 
 | 5858 |         substring->str, substring->length, | 
 | 5859 |         maxcount | 
 | 5860 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5861 | } | 
 | 5862 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5863 | static | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5864 | PyObject *rsplit(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5865 |                  PyUnicodeObject *substring, | 
 | 5866 |                  Py_ssize_t maxcount) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5867 | { | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5868 |     if (maxcount < 0) | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5869 |         maxcount = PY_SSIZE_T_MAX; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5870 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5871 |     if (substring == NULL) | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5872 |         return stringlib_rsplit_whitespace( | 
 | 5873 |             (PyObject*) self,  self->str, self->length, maxcount | 
 | 5874 |             ); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5875 |  | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5876 |     return stringlib_rsplit( | 
 | 5877 |         (PyObject*) self,  self->str, self->length, | 
 | 5878 |         substring->str, substring->length, | 
 | 5879 |         maxcount | 
 | 5880 |         ); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5881 | } | 
 | 5882 |  | 
 | 5883 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | PyObject *replace(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5885 |                   PyUnicodeObject *str1, | 
 | 5886 |                   PyUnicodeObject *str2, | 
 | 5887 |                   Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | { | 
 | 5889 |     PyUnicodeObject *u; | 
 | 5890 |  | 
 | 5891 |     if (maxcount < 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 5892 |         maxcount = PY_SSIZE_T_MAX; | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5893 |     else if (maxcount == 0 || self->length == 0) | 
 | 5894 |         goto nothing; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5895 |  | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5896 |     if (str1->length == str2->length) { | 
| Antoine Pitrou | 5c767c2 | 2010-01-13 08:55:20 +0000 | [diff] [blame] | 5897 |         Py_ssize_t i; | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5898 |         /* same length */ | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5899 |         if (str1->length == 0) | 
 | 5900 |             goto nothing; | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5901 |         if (str1->length == 1) { | 
 | 5902 |             /* replace characters */ | 
 | 5903 |             Py_UNICODE u1, u2; | 
 | 5904 |             if (!findchar(self->str, self->length, str1->str[0])) | 
 | 5905 |                 goto nothing; | 
 | 5906 |             u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); | 
 | 5907 |             if (!u) | 
 | 5908 |                 return NULL; | 
 | 5909 |             Py_UNICODE_COPY(u->str, self->str, self->length); | 
 | 5910 |             u1 = str1->str[0]; | 
 | 5911 |             u2 = str2->str[0]; | 
 | 5912 |             for (i = 0; i < u->length; i++) | 
 | 5913 |                 if (u->str[i] == u1) { | 
 | 5914 |                     if (--maxcount < 0) | 
 | 5915 |                         break; | 
 | 5916 |                     u->str[i] = u2; | 
 | 5917 |                 } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 |         } else { | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5919 |             i = stringlib_find( | 
 | 5920 |                 self->str, self->length, str1->str, str1->length, 0 | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 |                 ); | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5922 |             if (i < 0) | 
 | 5923 |                 goto nothing; | 
 | 5924 |             u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); | 
 | 5925 |             if (!u) | 
 | 5926 |                 return NULL; | 
 | 5927 |             Py_UNICODE_COPY(u->str, self->str, self->length); | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5928 |  | 
 | 5929 |             /* change everything in-place, starting with this one */ | 
 | 5930 |             Py_UNICODE_COPY(u->str+i, str2->str, str2->length); | 
 | 5931 |             i += str1->length; | 
 | 5932 |  | 
 | 5933 |             while ( --maxcount > 0) { | 
 | 5934 |                 i = stringlib_find(self->str+i, self->length-i, | 
 | 5935 |                                    str1->str, str1->length, | 
 | 5936 |                                    i); | 
 | 5937 |                 if (i == -1) | 
 | 5938 |                     break; | 
 | 5939 |                 Py_UNICODE_COPY(u->str+i, str2->str, str2->length); | 
 | 5940 |                 i += str1->length; | 
 | 5941 |             } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5943 |     } else { | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5944 |  | 
| Brett Cannon | a7f13ee | 2010-05-04 01:16:51 +0000 | [diff] [blame] | 5945 |         Py_ssize_t n, i, j; | 
| Xiang Zhang | 7bdb516 | 2017-01-09 11:13:20 +0800 | [diff] [blame] | 5946 |         Py_ssize_t new_size, delta; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5947 |         Py_UNICODE *p; | 
 | 5948 |  | 
 | 5949 |         /* replace strings */ | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5950 |         n = stringlib_count(self->str, self->length, str1->str, str1->length, | 
 | 5951 |                             maxcount); | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5952 |         if (n == 0) | 
 | 5953 |             goto nothing; | 
| Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5954 |         /* new_size = self->length + n * (str2->length - str1->length)); */ | 
 | 5955 |         delta = (str2->length - str1->length); | 
 | 5956 |         if (delta == 0) { | 
 | 5957 |             new_size = self->length; | 
 | 5958 |         } else { | 
| Xiang Zhang | 7bdb516 | 2017-01-09 11:13:20 +0800 | [diff] [blame] | 5959 |             assert(n > 0); | 
 | 5960 |             if (delta > (PY_SSIZE_T_MAX - self->length) / n) { | 
| Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5961 |                 PyErr_SetString(PyExc_OverflowError, | 
 | 5962 |                                 "replace string is too long"); | 
 | 5963 |                 return NULL; | 
 | 5964 |             } | 
| Xiang Zhang | 7bdb516 | 2017-01-09 11:13:20 +0800 | [diff] [blame] | 5965 |             new_size = self->length + delta * n; | 
| Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5966 |         } | 
 | 5967 |         u = _PyUnicode_New(new_size); | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5968 |         if (!u) | 
 | 5969 |             return NULL; | 
 | 5970 |         i = 0; | 
 | 5971 |         p = u->str; | 
 | 5972 |         if (str1->length > 0) { | 
| Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5973 |             while (n-- > 0) { | 
 | 5974 |                 /* look for next match */ | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 5975 |                 j = stringlib_find(self->str+i, self->length-i, | 
 | 5976 |                                    str1->str, str1->length, | 
 | 5977 |                                    i); | 
 | 5978 |                 if (j == -1) | 
 | 5979 |                     break; | 
 | 5980 |                 else if (j > i) { | 
| Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5981 |                     /* copy unchanged part [i:j] */ | 
 | 5982 |                     Py_UNICODE_COPY(p, self->str+i, j-i); | 
 | 5983 |                     p += j - i; | 
 | 5984 |                 } | 
 | 5985 |                 /* copy substitution string */ | 
 | 5986 |                 if (str2->length > 0) { | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5987 |                     Py_UNICODE_COPY(p, str2->str, str2->length); | 
 | 5988 |                     p += str2->length; | 
| Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5989 |                 } | 
 | 5990 |                 i = j + str1->length; | 
 | 5991 |             } | 
 | 5992 |             if (i < self->length) | 
 | 5993 |                 /* copy tail [i:] */ | 
 | 5994 |                 Py_UNICODE_COPY(p, self->str+i, self->length-i); | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5995 |         } else { | 
| Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5996 |             /* interleave */ | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5997 |             while (n > 0) { | 
 | 5998 |                 Py_UNICODE_COPY(p, str2->str, str2->length); | 
 | 5999 |                 p += str2->length; | 
 | 6000 |                 if (--n <= 0) | 
 | 6001 |                     break; | 
 | 6002 |                 *p++ = self->str[i++]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 |             } | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6004 |             Py_UNICODE_COPY(p, self->str+i, self->length-i); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 |         } | 
 | 6006 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 |     return (PyObject *) u; | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6008 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6009 |   nothing: | 
| Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6010 |     /* nothing to replace; return original string (when possible) */ | 
 | 6011 |     if (PyUnicode_CheckExact(self)) { | 
 | 6012 |         Py_INCREF(self); | 
 | 6013 |         return (PyObject *) self; | 
 | 6014 |     } | 
 | 6015 |     return PyUnicode_FromUnicode(self->str, self->length); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | } | 
 | 6017 |  | 
 | 6018 | /* --- Unicode Object Methods --------------------------------------------- */ | 
 | 6019 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6020 | PyDoc_STRVAR(title__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6021 |              "S.title() -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | \n\ | 
 | 6023 | 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] | 6024 | characters, all remaining cased characters have lower case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 |  | 
 | 6026 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6027 | unicode_title(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 |     return fixup(self, fixtitle); | 
 | 6030 | } | 
 | 6031 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6032 | PyDoc_STRVAR(capitalize__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6033 |              "S.capitalize() -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | \n\ | 
 | 6035 | Return a capitalized version of S, i.e. make the first character\n\ | 
| Senthil Kumaran | 5261b10 | 2010-07-05 12:04:07 +0000 | [diff] [blame] | 6036 | have upper case and the rest lower case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 |  | 
 | 6038 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6039 | unicode_capitalize(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6040 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 |     return fixup(self, fixcapitalize); | 
 | 6042 | } | 
 | 6043 |  | 
 | 6044 | #if 0 | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6045 | PyDoc_STRVAR(capwords__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6046 |              "S.capwords() -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6047 | \n\ | 
 | 6048 | 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] | 6049 | normalized whitespace (all whitespace strings are replaced by ' ')."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6050 |  | 
 | 6051 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6052 | unicode_capwords(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | { | 
 | 6054 |     PyObject *list; | 
 | 6055 |     PyObject *item; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6056 |     Py_ssize_t i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6058 |     /* Split into words */ | 
 | 6059 |     list = split(self, NULL, -1); | 
 | 6060 |     if (!list) | 
 | 6061 |         return NULL; | 
 | 6062 |  | 
 | 6063 |     /* Capitalize each word */ | 
 | 6064 |     for (i = 0; i < PyList_GET_SIZE(list); i++) { | 
 | 6065 |         item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6066 |                      fixcapitalize); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 |         if (item == NULL) | 
 | 6068 |             goto onError; | 
 | 6069 |         Py_DECREF(PyList_GET_ITEM(list, i)); | 
 | 6070 |         PyList_SET_ITEM(list, i, item); | 
 | 6071 |     } | 
 | 6072 |  | 
 | 6073 |     /* Join the words to form a new string */ | 
 | 6074 |     item = PyUnicode_Join(NULL, list); | 
 | 6075 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6076 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6077 |     Py_DECREF(list); | 
 | 6078 |     return (PyObject *)item; | 
 | 6079 | } | 
 | 6080 | #endif | 
 | 6081 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6082 | /* Argument converter.  Coerces to a single unicode character */ | 
 | 6083 |  | 
 | 6084 | static int | 
 | 6085 | convert_uc(PyObject *obj, void *addr) | 
 | 6086 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6087 |     Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; | 
 | 6088 |     PyObject *uniobj; | 
 | 6089 |     Py_UNICODE *unistr; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6090 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6091 |     uniobj = PyUnicode_FromObject(obj); | 
 | 6092 |     if (uniobj == NULL) { | 
 | 6093 |         PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6094 |                         "The fill character cannot be converted to Unicode"); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6095 |         return 0; | 
 | 6096 |     } | 
 | 6097 |     if (PyUnicode_GET_SIZE(uniobj) != 1) { | 
 | 6098 |         PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6099 |                         "The fill character must be exactly one character long"); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6100 |         Py_DECREF(uniobj); | 
 | 6101 |         return 0; | 
 | 6102 |     } | 
 | 6103 |     unistr = PyUnicode_AS_UNICODE(uniobj); | 
 | 6104 |     *fillcharloc = unistr[0]; | 
 | 6105 |     Py_DECREF(uniobj); | 
 | 6106 |     return 1; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6107 | } | 
 | 6108 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6109 | PyDoc_STRVAR(center__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6110 |              "S.center(width[, fillchar]) -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | \n\ | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6112 | Return S centered in a Unicode string of length width. Padding is\n\ | 
 | 6113 | done using the specified fill character (default is a space)"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 |  | 
 | 6115 | static PyObject * | 
 | 6116 | unicode_center(PyUnicodeObject *self, PyObject *args) | 
 | 6117 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6118 |     Py_ssize_t marg, left; | 
 | 6119 |     Py_ssize_t width; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6120 |     Py_UNICODE fillchar = ' '; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 |  | 
| Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6122 |     if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 |         return NULL; | 
 | 6124 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6125 |     if (self->length >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 |         Py_INCREF(self); | 
 | 6127 |         return (PyObject*) self; | 
 | 6128 |     } | 
 | 6129 |  | 
 | 6130 |     marg = width - self->length; | 
 | 6131 |     left = marg / 2 + (marg & width & 1); | 
 | 6132 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6133 |     return (PyObject*) pad(self, left, marg - left, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6134 | } | 
 | 6135 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6136 | #if 0 | 
 | 6137 |  | 
 | 6138 | /* This code should go into some future Unicode collation support | 
 | 6139 |    module. The basic comparison should compare ordinals on a naive | 
| Georg Brandl | 18187e2 | 2009-06-06 18:21:58 +0000 | [diff] [blame] | 6140 |    basis (this is what Java does and thus Jython too). */ | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6141 |  | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6142 | /* speedy UTF-16 code point order comparison */ | 
 | 6143 | /* gleaned from: */ | 
 | 6144 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ | 
 | 6145 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6146 | static short utf16Fixup[32] = | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6147 | { | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6148 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6149 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 6150 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6151 |     0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6152 | }; | 
 | 6153 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6154 | static int | 
 | 6155 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) | 
 | 6156 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6157 |     Py_ssize_t len1, len2; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6158 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 |     Py_UNICODE *s1 = str1->str; | 
 | 6160 |     Py_UNICODE *s2 = str2->str; | 
 | 6161 |  | 
 | 6162 |     len1 = str1->length; | 
 | 6163 |     len2 = str2->length; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6164 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 |     while (len1 > 0 && len2 > 0) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6166 |         Py_UNICODE c1, c2; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6167 |  | 
 | 6168 |         c1 = *s1++; | 
 | 6169 |         c2 = *s2++; | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6170 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6171 |         if (c1 > (1<<11) * 26) | 
 | 6172 |             c1 += utf16Fixup[c1>>11]; | 
 | 6173 |         if (c2 > (1<<11) * 26) | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6174 |             c2 += utf16Fixup[c2>>11]; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6175 |         /* now c1 and c2 are in UTF-32-compatible order */ | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6176 |  | 
 | 6177 |         if (c1 != c2) | 
 | 6178 |             return (c1 < c2) ? -1 : 1; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6179 |  | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6180 |         len1--; len2--; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 |     } | 
 | 6182 |  | 
 | 6183 |     return (len1 < len2) ? -1 : (len1 != len2); | 
 | 6184 | } | 
 | 6185 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6186 | #else | 
 | 6187 |  | 
 | 6188 | static int | 
 | 6189 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) | 
 | 6190 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6191 |     register Py_ssize_t len1, len2; | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6192 |  | 
 | 6193 |     Py_UNICODE *s1 = str1->str; | 
 | 6194 |     Py_UNICODE *s2 = str2->str; | 
 | 6195 |  | 
 | 6196 |     len1 = str1->length; | 
 | 6197 |     len2 = str2->length; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6198 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6199 |     while (len1 > 0 && len2 > 0) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6200 |         Py_UNICODE c1, c2; | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6201 |  | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6202 |         c1 = *s1++; | 
 | 6203 |         c2 = *s2++; | 
 | 6204 |  | 
 | 6205 |         if (c1 != c2) | 
 | 6206 |             return (c1 < c2) ? -1 : 1; | 
 | 6207 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6208 |         len1--; len2--; | 
 | 6209 |     } | 
 | 6210 |  | 
 | 6211 |     return (len1 < len2) ? -1 : (len1 != len2); | 
 | 6212 | } | 
 | 6213 |  | 
 | 6214 | #endif | 
 | 6215 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | int PyUnicode_Compare(PyObject *left, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6217 |                       PyObject *right) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | { | 
 | 6219 |     PyUnicodeObject *u = NULL, *v = NULL; | 
 | 6220 |     int result; | 
 | 6221 |  | 
 | 6222 |     /* Coerce the two arguments */ | 
 | 6223 |     u = (PyUnicodeObject *)PyUnicode_FromObject(left); | 
 | 6224 |     if (u == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6225 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 |     v = (PyUnicodeObject *)PyUnicode_FromObject(right); | 
 | 6227 |     if (v == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6228 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6229 |  | 
| Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6230 |     /* Shortcut for empty or interned objects */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 |     if (v == u) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6232 |         Py_DECREF(u); | 
 | 6233 |         Py_DECREF(v); | 
 | 6234 |         return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6235 |     } | 
 | 6236 |  | 
 | 6237 |     result = unicode_compare(u, v); | 
 | 6238 |  | 
 | 6239 |     Py_DECREF(u); | 
 | 6240 |     Py_DECREF(v); | 
 | 6241 |     return result; | 
 | 6242 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6243 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6244 |     Py_XDECREF(u); | 
 | 6245 |     Py_XDECREF(v); | 
 | 6246 |     return -1; | 
 | 6247 | } | 
 | 6248 |  | 
| Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6249 | PyObject *PyUnicode_RichCompare(PyObject *left, | 
 | 6250 |                                 PyObject *right, | 
 | 6251 |                                 int op) | 
 | 6252 | { | 
 | 6253 |     int result; | 
 | 6254 |  | 
 | 6255 |     result = PyUnicode_Compare(left, right); | 
 | 6256 |     if (result == -1 && PyErr_Occurred()) | 
 | 6257 |         goto onError; | 
 | 6258 |  | 
 | 6259 |     /* Convert the return value to a Boolean */ | 
 | 6260 |     switch (op) { | 
 | 6261 |     case Py_EQ: | 
 | 6262 |         result = (result == 0); | 
 | 6263 |         break; | 
 | 6264 |     case Py_NE: | 
 | 6265 |         result = (result != 0); | 
 | 6266 |         break; | 
 | 6267 |     case Py_LE: | 
 | 6268 |         result = (result <= 0); | 
 | 6269 |         break; | 
 | 6270 |     case Py_GE: | 
 | 6271 |         result = (result >= 0); | 
 | 6272 |         break; | 
 | 6273 |     case Py_LT: | 
 | 6274 |         result = (result == -1); | 
 | 6275 |         break; | 
 | 6276 |     case Py_GT: | 
 | 6277 |         result = (result == 1); | 
 | 6278 |         break; | 
 | 6279 |     } | 
 | 6280 |     return PyBool_FromLong(result); | 
 | 6281 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6282 |   onError: | 
| Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6283 |  | 
 | 6284 |     /* Standard case | 
 | 6285 |  | 
 | 6286 |        Type errors mean that PyUnicode_FromObject() could not convert | 
 | 6287 |        one of the arguments (usually the right hand side) to Unicode, | 
 | 6288 |        ie. we can't handle the comparison request. However, it is | 
 | 6289 |        possible that the other object knows a comparison method, which | 
 | 6290 |        is why we return Py_NotImplemented to give the other object a | 
 | 6291 |        chance. | 
 | 6292 |  | 
 | 6293 |     */ | 
 | 6294 |     if (PyErr_ExceptionMatches(PyExc_TypeError)) { | 
 | 6295 |         PyErr_Clear(); | 
 | 6296 |         Py_INCREF(Py_NotImplemented); | 
 | 6297 |         return Py_NotImplemented; | 
 | 6298 |     } | 
 | 6299 |     if (op != Py_EQ && op != Py_NE) | 
 | 6300 |         return NULL; | 
 | 6301 |  | 
 | 6302 |     /* Equality comparison. | 
 | 6303 |  | 
 | 6304 |        This is a special case: we silence any PyExc_UnicodeDecodeError | 
 | 6305 |        and instead turn it into a PyErr_UnicodeWarning. | 
 | 6306 |  | 
 | 6307 |     */ | 
 | 6308 |     if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) | 
 | 6309 |         return NULL; | 
 | 6310 |     PyErr_Clear(); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6311 |     if (PyErr_Warn(PyExc_UnicodeWarning, | 
 | 6312 |                    (op == Py_EQ) ? | 
| Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6313 |                    "Unicode equal comparison " | 
 | 6314 |                    "failed to convert both arguments to Unicode - " | 
 | 6315 |                    "interpreting them as being unequal" : | 
 | 6316 |                    "Unicode unequal comparison " | 
 | 6317 |                    "failed to convert both arguments to Unicode - " | 
 | 6318 |                    "interpreting them as being unequal" | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6319 |             ) < 0) | 
| Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6320 |         return NULL; | 
 | 6321 |     result = (op == Py_NE); | 
 | 6322 |     return PyBool_FromLong(result); | 
 | 6323 | } | 
 | 6324 |  | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6325 | int PyUnicode_Contains(PyObject *container, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6326 |                        PyObject *element) | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6327 | { | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6328 |     PyObject *str, *sub; | 
 | 6329 |     int result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6330 |  | 
 | 6331 |     /* Coerce the two arguments */ | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6332 |     sub = PyUnicode_FromObject(element); | 
 | 6333 |     if (!sub) { | 
| Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6334 |         return -1; | 
| Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6335 |     } | 
| Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6336 |  | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6337 |     str = PyUnicode_FromObject(container); | 
 | 6338 |     if (!str) { | 
 | 6339 |         Py_DECREF(sub); | 
| Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6340 |         return -1; | 
 | 6341 |     } | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6342 |  | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6343 |     result = stringlib_contains_obj(str, sub); | 
| Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6344 |  | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6345 |     Py_DECREF(str); | 
 | 6346 |     Py_DECREF(sub); | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6347 |  | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6348 |     return result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6349 | } | 
 | 6350 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6351 | /* Concat to string or Unicode object giving a new Unicode object. */ | 
 | 6352 |  | 
 | 6353 | PyObject *PyUnicode_Concat(PyObject *left, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6354 |                            PyObject *right) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6355 | { | 
 | 6356 |     PyUnicodeObject *u = NULL, *v = NULL, *w; | 
 | 6357 |  | 
 | 6358 |     /* Coerce the two arguments */ | 
 | 6359 |     u = (PyUnicodeObject *)PyUnicode_FromObject(left); | 
 | 6360 |     if (u == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6361 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6362 |     v = (PyUnicodeObject *)PyUnicode_FromObject(right); | 
 | 6363 |     if (v == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6364 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6365 |  | 
 | 6366 |     /* Shortcuts */ | 
 | 6367 |     if (v == unicode_empty) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6368 |         Py_DECREF(v); | 
 | 6369 |         return (PyObject *)u; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6370 |     } | 
 | 6371 |     if (u == unicode_empty) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6372 |         Py_DECREF(u); | 
 | 6373 |         return (PyObject *)v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6374 |     } | 
 | 6375 |  | 
| Serhiy Storchaka | 373773d | 2016-07-12 15:46:57 +0300 | [diff] [blame] | 6376 |     if (u->length > PY_SSIZE_T_MAX - v->length) { | 
 | 6377 |         PyErr_SetString(PyExc_OverflowError, | 
 | 6378 |                         "strings are too large to concat"); | 
 | 6379 |         goto onError; | 
 | 6380 |     } | 
 | 6381 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6382 |     /* Concat the two Unicode strings */ | 
 | 6383 |     w = _PyUnicode_New(u->length + v->length); | 
 | 6384 |     if (w == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6385 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 |     Py_UNICODE_COPY(w->str, u->str, u->length); | 
 | 6387 |     Py_UNICODE_COPY(w->str + u->length, v->str, v->length); | 
 | 6388 |  | 
 | 6389 |     Py_DECREF(u); | 
 | 6390 |     Py_DECREF(v); | 
 | 6391 |     return (PyObject *)w; | 
 | 6392 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6393 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 |     Py_XDECREF(u); | 
 | 6395 |     Py_XDECREF(v); | 
 | 6396 |     return NULL; | 
 | 6397 | } | 
 | 6398 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6399 | PyDoc_STRVAR(count__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6400 |              "S.count(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6401 | \n\ | 
| Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 6402 | Return the number of non-overlapping occurrences of substring sub in\n\ | 
 | 6403 | Unicode 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] | 6404 | interpreted as in slice notation."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 |  | 
 | 6406 | static PyObject * | 
 | 6407 | unicode_count(PyUnicodeObject *self, PyObject *args) | 
 | 6408 | { | 
 | 6409 |     PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6410 |     Py_ssize_t start = 0; | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6411 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 |     PyObject *result; | 
 | 6413 |  | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 6414 |     if (!stringlib_parse_args_finds_unicode("count", args, &substring, | 
 | 6415 |                                             &start, &end)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6416 |         return NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6417 |  | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 6418 |     ADJUST_INDICES(start, end, self->length); | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6419 |     result = PyInt_FromSsize_t( | 
 | 6420 |         stringlib_count(self->str + start, end - start, | 
| Antoine Pitrou | 6467213 | 2010-01-13 07:55:48 +0000 | [diff] [blame] | 6421 |                         substring->str, substring->length, | 
 | 6422 |                         PY_SSIZE_T_MAX) | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6423 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 |  | 
 | 6425 |     Py_DECREF(substring); | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6426 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6427 |     return result; | 
 | 6428 | } | 
 | 6429 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6430 | PyDoc_STRVAR(encode__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6431 |              "S.encode([encoding[,errors]]) -> string or unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6432 | \n\ | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6433 | Encodes S using the codec registered for encoding. encoding defaults\n\ | 
 | 6434 | 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] | 6435 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6436 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ | 
 | 6437 | 'xmlcharrefreplace' as well as any other name registered with\n\ | 
 | 6438 | codecs.register_error that can handle UnicodeEncodeErrors."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 |  | 
 | 6440 | static PyObject * | 
| Benjamin Peterson | 332d721 | 2009-09-18 21:14:55 +0000 | [diff] [blame] | 6441 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 | { | 
| Benjamin Peterson | 332d721 | 2009-09-18 21:14:55 +0000 | [diff] [blame] | 6443 |     static char *kwlist[] = {"encoding", "errors", 0}; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 |     char *encoding = NULL; | 
 | 6445 |     char *errors = NULL; | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6446 |     PyObject *v; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6447 |  | 
| Benjamin Peterson | 332d721 | 2009-09-18 21:14:55 +0000 | [diff] [blame] | 6448 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", | 
 | 6449 |                                      kwlist, &encoding, &errors)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6450 |         return NULL; | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6451 |     v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6452 |     if (v == NULL) | 
 | 6453 |         goto onError; | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6454 |     if (!PyString_Check(v) && !PyUnicode_Check(v)) { | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6455 |         PyErr_Format(PyExc_TypeError, | 
 | 6456 |                      "encoder did not return a string/unicode object " | 
 | 6457 |                      "(type=%.400s)", | 
| Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6458 |                      Py_TYPE(v)->tp_name); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6459 |         Py_DECREF(v); | 
 | 6460 |         return NULL; | 
 | 6461 |     } | 
 | 6462 |     return v; | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6463 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6464 |   onError: | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6465 |     return NULL; | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6466 | } | 
 | 6467 |  | 
 | 6468 | PyDoc_STRVAR(decode__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6469 |              "S.decode([encoding[,errors]]) -> string or unicode\n\ | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6470 | \n\ | 
 | 6471 | Decodes S using the codec registered for encoding. encoding defaults\n\ | 
 | 6472 | to the default encoding. errors may be given to set a different error\n\ | 
 | 6473 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ | 
 | 6474 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ | 
| Éric Araujo | a4c81b0 | 2012-02-20 02:07:31 +0100 | [diff] [blame] | 6475 | as well as any other name registered with codecs.register_error that is\n\ | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6476 | able to handle UnicodeDecodeErrors."); | 
 | 6477 |  | 
 | 6478 | static PyObject * | 
| Benjamin Peterson | 332d721 | 2009-09-18 21:14:55 +0000 | [diff] [blame] | 6479 | unicode_decode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6480 | { | 
| Benjamin Peterson | 332d721 | 2009-09-18 21:14:55 +0000 | [diff] [blame] | 6481 |     static char *kwlist[] = {"encoding", "errors", 0}; | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6482 |     char *encoding = NULL; | 
 | 6483 |     char *errors = NULL; | 
 | 6484 |     PyObject *v; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6485 |  | 
| Benjamin Peterson | 332d721 | 2009-09-18 21:14:55 +0000 | [diff] [blame] | 6486 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", | 
 | 6487 |                                      kwlist, &encoding, &errors)) | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6488 |         return NULL; | 
 | 6489 |     v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6490 |     if (v == NULL) | 
 | 6491 |         goto onError; | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6492 |     if (!PyString_Check(v) && !PyUnicode_Check(v)) { | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6493 |         PyErr_Format(PyExc_TypeError, | 
 | 6494 |                      "decoder did not return a string/unicode object " | 
 | 6495 |                      "(type=%.400s)", | 
| Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6496 |                      Py_TYPE(v)->tp_name); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6497 |         Py_DECREF(v); | 
 | 6498 |         return NULL; | 
 | 6499 |     } | 
 | 6500 |     return v; | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6501 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6502 |   onError: | 
| Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6503 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | } | 
 | 6505 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6506 | PyDoc_STRVAR(expandtabs__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6507 |              "S.expandtabs([tabsize]) -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6508 | \n\ | 
 | 6509 | 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] | 6510 | 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] | 6511 |  | 
 | 6512 | static PyObject* | 
 | 6513 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) | 
 | 6514 | { | 
 | 6515 |     Py_UNICODE *e; | 
 | 6516 |     Py_UNICODE *p; | 
 | 6517 |     Py_UNICODE *q; | 
| Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6518 |     Py_UNICODE *qe; | 
 | 6519 |     Py_ssize_t i, j, incr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6520 |     PyUnicodeObject *u; | 
 | 6521 |     int tabsize = 8; | 
 | 6522 |  | 
 | 6523 |     if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6524 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6525 |  | 
| Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6526 |     /* First pass: determine size of output string */ | 
| Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6527 |     i = 0; /* chars up to and including most recent \n or \r */ | 
 | 6528 |     j = 0; /* chars since most recent \n or \r (use in tab calculations) */ | 
 | 6529 |     e = self->str + self->length; /* end of input */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6530 |     for (p = self->str; p < e; p++) | 
 | 6531 |         if (*p == '\t') { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6532 |             if (tabsize > 0) { | 
 | 6533 |                 incr = tabsize - (j % tabsize); /* cannot overflow */ | 
 | 6534 |                 if (j > PY_SSIZE_T_MAX - incr) | 
 | 6535 |                     goto overflow1; | 
 | 6536 |                 j += incr; | 
| Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6537 |             } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6538 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 |         else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6540 |             if (j > PY_SSIZE_T_MAX - 1) | 
 | 6541 |                 goto overflow1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6542 |             j++; | 
 | 6543 |             if (*p == '\n' || *p == '\r') { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6544 |                 if (i > PY_SSIZE_T_MAX - j) | 
 | 6545 |                     goto overflow1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6546 |                 i += j; | 
| Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6547 |                 j = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6548 |             } | 
 | 6549 |         } | 
 | 6550 |  | 
| Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6551 |     if (i > PY_SSIZE_T_MAX - j) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6552 |         goto overflow1; | 
| Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6553 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6554 |     /* Second pass: create output string and fill it */ | 
 | 6555 |     u = _PyUnicode_New(i + j); | 
 | 6556 |     if (!u) | 
 | 6557 |         return NULL; | 
 | 6558 |  | 
| Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6559 |     j = 0; /* same as in first pass */ | 
 | 6560 |     q = u->str; /* next output char */ | 
 | 6561 |     qe = u->str + u->length; /* end of output */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6562 |  | 
 | 6563 |     for (p = self->str; p < e; p++) | 
 | 6564 |         if (*p == '\t') { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6565 |             if (tabsize > 0) { | 
 | 6566 |                 i = tabsize - (j % tabsize); | 
 | 6567 |                 j += i; | 
 | 6568 |                 while (i--) { | 
 | 6569 |                     if (q >= qe) | 
 | 6570 |                         goto overflow2; | 
 | 6571 |                     *q++ = ' '; | 
| Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6572 |                 } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6573 |             } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 6574 |         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6575 |         else { | 
 | 6576 |             if (q >= qe) | 
 | 6577 |                 goto overflow2; | 
 | 6578 |             *q++ = *p; | 
| Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6579 |             j++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6580 |             if (*p == '\n' || *p == '\r') | 
 | 6581 |                 j = 0; | 
 | 6582 |         } | 
 | 6583 |  | 
 | 6584 |     return (PyObject*) u; | 
| Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6585 |  | 
 | 6586 |   overflow2: | 
 | 6587 |     Py_DECREF(u); | 
 | 6588 |   overflow1: | 
 | 6589 |     PyErr_SetString(PyExc_OverflowError, "new string is too long"); | 
 | 6590 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6591 | } | 
 | 6592 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6593 | PyDoc_STRVAR(find__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6594 |              "S.find(sub [,start [,end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6595 | \n\ | 
 | 6596 | Return the lowest index in S where substring sub is found,\n\ | 
| Senthil Kumaran | 5e3a19d | 2011-07-27 23:36:51 +0800 | [diff] [blame] | 6597 | such that sub is contained within S[start:end].  Optional\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | arguments start and end are interpreted as in slice notation.\n\ | 
 | 6599 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6600 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6601 |  | 
 | 6602 | static PyObject * | 
 | 6603 | unicode_find(PyUnicodeObject *self, PyObject *args) | 
 | 6604 | { | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 6605 |     PyUnicodeObject *substring; | 
| Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6606 |     Py_ssize_t start; | 
 | 6607 |     Py_ssize_t end; | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6608 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6609 |  | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 6610 |     if (!stringlib_parse_args_finds_unicode("find", args, &substring, | 
 | 6611 |                                             &start, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6612 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 |  | 
| Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6614 |     result = stringlib_find_slice( | 
 | 6615 |         PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), | 
 | 6616 |         PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), | 
 | 6617 |         start, end | 
 | 6618 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6619 |  | 
 | 6620 |     Py_DECREF(substring); | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6621 |  | 
 | 6622 |     return PyInt_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6623 | } | 
 | 6624 |  | 
 | 6625 | static PyObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6626 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | { | 
 | 6628 |     if (index < 0 || index >= self->length) { | 
 | 6629 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 6630 |         return NULL; | 
 | 6631 |     } | 
 | 6632 |  | 
 | 6633 |     return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); | 
 | 6634 | } | 
 | 6635 |  | 
 | 6636 | static long | 
 | 6637 | unicode_hash(PyUnicodeObject *self) | 
 | 6638 | { | 
| Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6639 |     /* Since Unicode objects compare equal to their ASCII string | 
 | 6640 |        counterparts, they should use the individual character values | 
 | 6641 |        as basis for their hash value.  This is needed to assure that | 
 | 6642 |        strings and Unicode objects behave in the same way as | 
 | 6643 |        dictionary keys. */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6645 |     register Py_ssize_t len; | 
| Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6646 |     register Py_UNICODE *p; | 
 | 6647 |     register long x; | 
 | 6648 |  | 
| Benjamin Peterson | f51c384 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 6649 | #ifdef Py_DEBUG | 
| Benjamin Peterson | 26da920 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 6650 |     assert(_Py_HashSecret_Initialized); | 
| Benjamin Peterson | f51c384 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 6651 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6652 |     if (self->hash != -1) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6653 |         return self->hash; | 
| Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6654 |     len = PyUnicode_GET_SIZE(self); | 
| Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 6655 |     /* | 
 | 6656 |       We make the hash of the empty string be 0, rather than using | 
 | 6657 |       (prefix ^ suffix), since this slightly obfuscates the hash secret | 
 | 6658 |     */ | 
 | 6659 |     if (len == 0) { | 
 | 6660 |         self->hash = 0; | 
 | 6661 |         return 0; | 
 | 6662 |     } | 
| Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6663 |     p = PyUnicode_AS_UNICODE(self); | 
| Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 6664 |     x = _Py_HashSecret.prefix; | 
 | 6665 |     x ^= *p << 7; | 
| Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6666 |     while (--len >= 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6667 |         x = (1000003*x) ^ *p++; | 
| Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6668 |     x ^= PyUnicode_GET_SIZE(self); | 
| Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 6669 |     x ^= _Py_HashSecret.suffix; | 
| Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6670 |     if (x == -1) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6671 |         x = -2; | 
| Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6672 |     self->hash = x; | 
 | 6673 |     return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | } | 
 | 6675 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6676 | PyDoc_STRVAR(index__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6677 |              "S.index(sub [,start [,end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6679 | 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] | 6680 |  | 
 | 6681 | static PyObject * | 
 | 6682 | unicode_index(PyUnicodeObject *self, PyObject *args) | 
 | 6683 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6684 |     Py_ssize_t result; | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 6685 |     PyUnicodeObject *substring; | 
| Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6686 |     Py_ssize_t start; | 
 | 6687 |     Py_ssize_t end; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6688 |  | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 6689 |     if (!stringlib_parse_args_finds_unicode("index", args, &substring, | 
 | 6690 |                                             &start, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 |  | 
| Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6693 |     result = stringlib_find_slice( | 
 | 6694 |         PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), | 
 | 6695 |         PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), | 
 | 6696 |         start, end | 
 | 6697 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 |  | 
 | 6699 |     Py_DECREF(substring); | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6700 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6701 |     if (result < 0) { | 
 | 6702 |         PyErr_SetString(PyExc_ValueError, "substring not found"); | 
 | 6703 |         return NULL; | 
 | 6704 |     } | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6705 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6706 |     return PyInt_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 | } | 
 | 6708 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6709 | PyDoc_STRVAR(islower__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6710 |              "S.islower() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6711 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6712 | 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] | 6713 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6714 |  | 
 | 6715 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6716 | unicode_islower(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | { | 
 | 6718 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6719 |     register const Py_UNICODE *e; | 
 | 6720 |     int cased; | 
 | 6721 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6722 |     /* Shortcut for single character strings */ | 
 | 6723 |     if (PyUnicode_GET_SIZE(self) == 1) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6724 |         return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6726 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6727 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6728 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6729 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6730 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6731 |     cased = 0; | 
 | 6732 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6733 |         register const Py_UNICODE ch = *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6734 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6735 |         if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) | 
 | 6736 |             return PyBool_FromLong(0); | 
 | 6737 |         else if (!cased && Py_UNICODE_ISLOWER(ch)) | 
 | 6738 |             cased = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6739 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6740 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6741 | } | 
 | 6742 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6743 | PyDoc_STRVAR(isupper__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6744 |              "S.isupper() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6745 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6746 | 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] | 6747 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6748 |  | 
 | 6749 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6750 | unicode_isupper(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | { | 
 | 6752 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6753 |     register const Py_UNICODE *e; | 
 | 6754 |     int cased; | 
 | 6755 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6756 |     /* Shortcut for single character strings */ | 
 | 6757 |     if (PyUnicode_GET_SIZE(self) == 1) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6758 |         return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6760 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6761 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6762 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6763 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6764 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6765 |     cased = 0; | 
 | 6766 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6767 |         register const Py_UNICODE ch = *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6768 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6769 |         if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) | 
 | 6770 |             return PyBool_FromLong(0); | 
 | 6771 |         else if (!cased && Py_UNICODE_ISUPPER(ch)) | 
 | 6772 |             cased = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6773 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6774 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | } | 
 | 6776 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6777 | PyDoc_STRVAR(istitle__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6778 |              "S.istitle() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6779 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6780 | Return True if S is a titlecased string and there is at least one\n\ | 
 | 6781 | character in S, i.e. upper- and titlecase characters may only\n\ | 
 | 6782 | follow uncased characters and lowercase characters only cased ones.\n\ | 
 | 6783 | Return False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 |  | 
 | 6785 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6786 | unicode_istitle(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | { | 
 | 6788 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6789 |     register const Py_UNICODE *e; | 
 | 6790 |     int cased, previous_is_cased; | 
 | 6791 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 |     /* Shortcut for single character strings */ | 
 | 6793 |     if (PyUnicode_GET_SIZE(self) == 1) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6794 |         return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || | 
 | 6795 |                                (Py_UNICODE_ISUPPER(*p) != 0)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6796 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6797 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6798 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6799 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6800 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6801 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6802 |     cased = 0; | 
 | 6803 |     previous_is_cased = 0; | 
 | 6804 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6805 |         register const Py_UNICODE ch = *p; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6806 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6807 |         if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { | 
 | 6808 |             if (previous_is_cased) | 
 | 6809 |                 return PyBool_FromLong(0); | 
 | 6810 |             previous_is_cased = 1; | 
 | 6811 |             cased = 1; | 
 | 6812 |         } | 
 | 6813 |         else if (Py_UNICODE_ISLOWER(ch)) { | 
 | 6814 |             if (!previous_is_cased) | 
 | 6815 |                 return PyBool_FromLong(0); | 
 | 6816 |             previous_is_cased = 1; | 
 | 6817 |             cased = 1; | 
 | 6818 |         } | 
 | 6819 |         else | 
 | 6820 |             previous_is_cased = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6821 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6822 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6823 | } | 
 | 6824 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6825 | PyDoc_STRVAR(isspace__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6826 |              "S.isspace() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6827 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6828 | Return True if all characters in S are whitespace\n\ | 
 | 6829 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 |  | 
 | 6831 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6832 | unicode_isspace(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | { | 
 | 6834 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6835 |     register const Py_UNICODE *e; | 
 | 6836 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6837 |     /* Shortcut for single character strings */ | 
 | 6838 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6839 |         Py_UNICODE_ISSPACE(*p)) | 
 | 6840 |         return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6842 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6843 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6844 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6845 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6846 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6847 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6848 |         if (!Py_UNICODE_ISSPACE(*p)) | 
 | 6849 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6850 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6851 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6852 | } | 
 | 6853 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6854 | PyDoc_STRVAR(isalpha__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6855 |              "S.isalpha() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6856 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6857 | Return True if all characters in S are alphabetic\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6858 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6859 |  | 
 | 6860 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6861 | unicode_isalpha(PyUnicodeObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6862 | { | 
 | 6863 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6864 |     register const Py_UNICODE *e; | 
 | 6865 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6866 |     /* Shortcut for single character strings */ | 
 | 6867 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6868 |         Py_UNICODE_ISALPHA(*p)) | 
 | 6869 |         return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6870 |  | 
 | 6871 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6872 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6873 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6874 |  | 
 | 6875 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6876 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6877 |         if (!Py_UNICODE_ISALPHA(*p)) | 
 | 6878 |             return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6879 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6880 |     return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6881 | } | 
 | 6882 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6883 | PyDoc_STRVAR(isalnum__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6884 |              "S.isalnum() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6885 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6886 | Return True if all characters in S are alphanumeric\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6887 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6888 |  | 
 | 6889 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6890 | unicode_isalnum(PyUnicodeObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6891 | { | 
 | 6892 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6893 |     register const Py_UNICODE *e; | 
 | 6894 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6895 |     /* Shortcut for single character strings */ | 
 | 6896 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6897 |         Py_UNICODE_ISALNUM(*p)) | 
 | 6898 |         return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6899 |  | 
 | 6900 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6901 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6902 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6903 |  | 
 | 6904 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6905 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6906 |         if (!Py_UNICODE_ISALNUM(*p)) | 
 | 6907 |             return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6908 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6909 |     return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6910 | } | 
 | 6911 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6912 | PyDoc_STRVAR(isdecimal__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6913 |              "S.isdecimal() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6914 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6915 | 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] | 6916 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6917 |  | 
 | 6918 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6919 | unicode_isdecimal(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6920 | { | 
 | 6921 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6922 |     register const Py_UNICODE *e; | 
 | 6923 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6924 |     /* Shortcut for single character strings */ | 
 | 6925 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6926 |         Py_UNICODE_ISDECIMAL(*p)) | 
 | 6927 |         return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6928 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6929 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6930 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6931 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6932 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6933 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6934 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6935 |         if (!Py_UNICODE_ISDECIMAL(*p)) | 
 | 6936 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6937 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6938 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6939 | } | 
 | 6940 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6941 | PyDoc_STRVAR(isdigit__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6942 |              "S.isdigit() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6944 | Return True if all characters in S are digits\n\ | 
 | 6945 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 |  | 
 | 6947 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6948 | unicode_isdigit(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6949 | { | 
 | 6950 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6951 |     register const Py_UNICODE *e; | 
 | 6952 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 |     /* Shortcut for single character strings */ | 
 | 6954 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6955 |         Py_UNICODE_ISDIGIT(*p)) | 
 | 6956 |         return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6958 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6959 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6960 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6961 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6962 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6963 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6964 |         if (!Py_UNICODE_ISDIGIT(*p)) | 
 | 6965 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6966 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6967 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6968 | } | 
 | 6969 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6970 | PyDoc_STRVAR(isnumeric__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6971 |              "S.isnumeric() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6973 | 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] | 6974 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6975 |  | 
 | 6976 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6977 | unicode_isnumeric(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6978 | { | 
 | 6979 |     register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); | 
 | 6980 |     register const Py_UNICODE *e; | 
 | 6981 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 |     /* Shortcut for single character strings */ | 
 | 6983 |     if (PyUnicode_GET_SIZE(self) == 1 && | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6984 |         Py_UNICODE_ISNUMERIC(*p)) | 
 | 6985 |         return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6986 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6987 |     /* Special case for empty strings */ | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6988 |     if (PyUnicode_GET_SIZE(self) == 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6989 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6990 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6991 |     e = p + PyUnicode_GET_SIZE(self); | 
 | 6992 |     for (; p < e; p++) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 6993 |         if (!Py_UNICODE_ISNUMERIC(*p)) | 
 | 6994 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6995 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6996 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6997 | } | 
 | 6998 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6999 | PyDoc_STRVAR(join__doc__, | 
| Georg Brandl | 9b4e582 | 2009-10-14 18:48:32 +0000 | [diff] [blame] | 7000 |              "S.join(iterable) -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7001 | \n\ | 
 | 7002 | Return a string which is the concatenation of the strings in the\n\ | 
| Georg Brandl | 9b4e582 | 2009-10-14 18:48:32 +0000 | [diff] [blame] | 7003 | iterable.  The separator between elements is S."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 |  | 
 | 7005 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7006 | unicode_join(PyObject *self, PyObject *data) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7007 | { | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7008 |     return PyUnicode_Join(self, data); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | } | 
 | 7010 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7011 | static Py_ssize_t | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7012 | unicode_length(PyUnicodeObject *self) | 
 | 7013 | { | 
 | 7014 |     return self->length; | 
 | 7015 | } | 
 | 7016 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7017 | PyDoc_STRVAR(ljust__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7018 |              "S.ljust(width[, fillchar]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7019 | \n\ | 
| Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7020 | 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] | 7021 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7022 |  | 
 | 7023 | static PyObject * | 
 | 7024 | unicode_ljust(PyUnicodeObject *self, PyObject *args) | 
 | 7025 | { | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7026 |     Py_ssize_t width; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7027 |     Py_UNICODE fillchar = ' '; | 
 | 7028 |  | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7029 |     if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7030 |         return NULL; | 
 | 7031 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7032 |     if (self->length >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7033 |         Py_INCREF(self); | 
 | 7034 |         return (PyObject*) self; | 
 | 7035 |     } | 
 | 7036 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7037 |     return (PyObject*) pad(self, 0, width - self->length, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | } | 
 | 7039 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7040 | PyDoc_STRVAR(lower__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7041 |              "S.lower() -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7042 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7043 | Return a copy of the string S converted to lowercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7044 |  | 
 | 7045 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7046 | unicode_lower(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7047 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7048 |     return fixup(self, fixlower); | 
 | 7049 | } | 
 | 7050 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7051 | #define LEFTSTRIP 0 | 
 | 7052 | #define RIGHTSTRIP 1 | 
 | 7053 | #define BOTHSTRIP 2 | 
 | 7054 |  | 
 | 7055 | /* Arrays indexed by above */ | 
 | 7056 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; | 
 | 7057 |  | 
 | 7058 | #define STRIPNAME(i) (stripformat[i]+3) | 
 | 7059 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7060 | /* externally visible for str.strip(unicode) */ | 
 | 7061 | PyObject * | 
 | 7062 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) | 
 | 7063 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7064 |     Py_UNICODE *s = PyUnicode_AS_UNICODE(self); | 
 | 7065 |     Py_ssize_t len = PyUnicode_GET_SIZE(self); | 
 | 7066 |     Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); | 
 | 7067 |     Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); | 
 | 7068 |     Py_ssize_t i, j; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7069 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7070 |     BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7071 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7072 |     i = 0; | 
 | 7073 |     if (striptype != RIGHTSTRIP) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7074 |         while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { | 
 | 7075 |             i++; | 
 | 7076 |         } | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7077 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7078 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7079 |     j = len; | 
 | 7080 |     if (striptype != LEFTSTRIP) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7081 |         do { | 
 | 7082 |             j--; | 
 | 7083 |         } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); | 
 | 7084 |         j++; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7085 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7086 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7087 |     if (i == 0 && j == len && PyUnicode_CheckExact(self)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7088 |         Py_INCREF(self); | 
 | 7089 |         return (PyObject*)self; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7090 |     } | 
 | 7091 |     else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7092 |         return PyUnicode_FromUnicode(s+i, j-i); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7093 | } | 
 | 7094 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7095 |  | 
 | 7096 | static PyObject * | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7097 | do_strip(PyUnicodeObject *self, int striptype) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7098 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7099 |     Py_UNICODE *s = PyUnicode_AS_UNICODE(self); | 
 | 7100 |     Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7101 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7102 |     i = 0; | 
 | 7103 |     if (striptype != RIGHTSTRIP) { | 
 | 7104 |         while (i < len && Py_UNICODE_ISSPACE(s[i])) { | 
 | 7105 |             i++; | 
 | 7106 |         } | 
 | 7107 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7108 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7109 |     j = len; | 
 | 7110 |     if (striptype != LEFTSTRIP) { | 
 | 7111 |         do { | 
 | 7112 |             j--; | 
 | 7113 |         } while (j >= i && Py_UNICODE_ISSPACE(s[j])); | 
 | 7114 |         j++; | 
 | 7115 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7116 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7117 |     if (i == 0 && j == len && PyUnicode_CheckExact(self)) { | 
 | 7118 |         Py_INCREF(self); | 
 | 7119 |         return (PyObject*)self; | 
 | 7120 |     } | 
 | 7121 |     else | 
 | 7122 |         return PyUnicode_FromUnicode(s+i, j-i); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7123 | } | 
 | 7124 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7125 |  | 
 | 7126 | static PyObject * | 
 | 7127 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) | 
 | 7128 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7129 |     PyObject *sep = NULL; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7130 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7131 |     if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) | 
 | 7132 |         return NULL; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7133 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7134 |     if (sep != NULL && sep != Py_None) { | 
 | 7135 |         if (PyUnicode_Check(sep)) | 
 | 7136 |             return _PyUnicode_XStrip(self, striptype, sep); | 
 | 7137 |         else if (PyString_Check(sep)) { | 
 | 7138 |             PyObject *res; | 
 | 7139 |             sep = PyUnicode_FromObject(sep); | 
 | 7140 |             if (sep==NULL) | 
 | 7141 |                 return NULL; | 
 | 7142 |             res = _PyUnicode_XStrip(self, striptype, sep); | 
 | 7143 |             Py_DECREF(sep); | 
 | 7144 |             return res; | 
 | 7145 |         } | 
 | 7146 |         else { | 
 | 7147 |             PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7148 |                          "%s arg must be None, unicode or str", | 
 | 7149 |                          STRIPNAME(striptype)); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7150 |             return NULL; | 
 | 7151 |         } | 
 | 7152 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7153 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7154 |     return do_strip(self, striptype); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7155 | } | 
 | 7156 |  | 
 | 7157 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7158 | PyDoc_STRVAR(strip__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7159 |              "S.strip([chars]) -> unicode\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7160 | \n\ | 
 | 7161 | Return a copy of the string S with leading and trailing\n\ | 
 | 7162 | whitespace removed.\n\ | 
| Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7163 | If chars is given and not None, remove characters in chars instead.\n\ | 
 | 7164 | If chars is a str, it will be converted to unicode before stripping"); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7165 |  | 
 | 7166 | static PyObject * | 
 | 7167 | unicode_strip(PyUnicodeObject *self, PyObject *args) | 
 | 7168 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7169 |     if (PyTuple_GET_SIZE(args) == 0) | 
 | 7170 |         return do_strip(self, BOTHSTRIP); /* Common case */ | 
 | 7171 |     else | 
 | 7172 |         return do_argstrip(self, BOTHSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7173 | } | 
 | 7174 |  | 
 | 7175 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7176 | PyDoc_STRVAR(lstrip__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7177 |              "S.lstrip([chars]) -> unicode\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7178 | \n\ | 
 | 7179 | Return a copy of the string S with leading whitespace removed.\n\ | 
| Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7180 | If chars is given and not None, remove characters in chars instead.\n\ | 
 | 7181 | If chars is a str, it will be converted to unicode before stripping"); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7182 |  | 
 | 7183 | static PyObject * | 
 | 7184 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) | 
 | 7185 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7186 |     if (PyTuple_GET_SIZE(args) == 0) | 
 | 7187 |         return do_strip(self, LEFTSTRIP); /* Common case */ | 
 | 7188 |     else | 
 | 7189 |         return do_argstrip(self, LEFTSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7190 | } | 
 | 7191 |  | 
 | 7192 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7193 | PyDoc_STRVAR(rstrip__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7194 |              "S.rstrip([chars]) -> unicode\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7195 | \n\ | 
 | 7196 | Return a copy of the string S with trailing whitespace removed.\n\ | 
| Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7197 | If chars is given and not None, remove characters in chars instead.\n\ | 
 | 7198 | If chars is a str, it will be converted to unicode before stripping"); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7199 |  | 
 | 7200 | static PyObject * | 
 | 7201 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) | 
 | 7202 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7203 |     if (PyTuple_GET_SIZE(args) == 0) | 
 | 7204 |         return do_strip(self, RIGHTSTRIP); /* Common case */ | 
 | 7205 |     else | 
 | 7206 |         return do_argstrip(self, RIGHTSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7207 | } | 
 | 7208 |  | 
 | 7209 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7210 | static PyObject* | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7211 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7212 | { | 
 | 7213 |     PyUnicodeObject *u; | 
 | 7214 |     Py_UNICODE *p; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7215 |     Py_ssize_t nchars; | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7216 |     size_t nbytes; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7217 |  | 
 | 7218 |     if (len < 0) | 
 | 7219 |         len = 0; | 
 | 7220 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7221 |     if (len == 1 && PyUnicode_CheckExact(str)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7222 |         /* no repeat, return original string */ | 
 | 7223 |         Py_INCREF(str); | 
 | 7224 |         return (PyObject*) str; | 
 | 7225 |     } | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7226 |  | 
| Serhiy Storchaka | 373773d | 2016-07-12 15:46:57 +0300 | [diff] [blame] | 7227 |     /* ensure # of chars needed doesn't overflow Py_ssize_t and # of bytes | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7228 |      * needed doesn't overflow size_t | 
 | 7229 |      */ | 
| Serhiy Storchaka | 373773d | 2016-07-12 15:46:57 +0300 | [diff] [blame] | 7230 |     if (len && str->length > PY_SSIZE_T_MAX / len) { | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7231 |         PyErr_SetString(PyExc_OverflowError, | 
 | 7232 |                         "repeated string is too long"); | 
 | 7233 |         return NULL; | 
 | 7234 |     } | 
| Serhiy Storchaka | 373773d | 2016-07-12 15:46:57 +0300 | [diff] [blame] | 7235 |     nchars = len * str->length; | 
 | 7236 |     nbytes = ((size_t)nchars + 1u) * sizeof(Py_UNICODE); | 
 | 7237 |     if (nbytes / sizeof(Py_UNICODE) != ((size_t)nchars + 1u)) { | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7238 |         PyErr_SetString(PyExc_OverflowError, | 
 | 7239 |                         "repeated string is too long"); | 
 | 7240 |         return NULL; | 
 | 7241 |     } | 
 | 7242 |     u = _PyUnicode_New(nchars); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7243 |     if (!u) | 
 | 7244 |         return NULL; | 
 | 7245 |  | 
 | 7246 |     p = u->str; | 
 | 7247 |  | 
| Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7248 |     if (str->length == 1 && len > 0) { | 
 | 7249 |         Py_UNICODE_FILL(p, str->str[0], len); | 
| Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7250 |     } else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7251 |         Py_ssize_t done = 0; /* number of characters copied this far */ | 
 | 7252 |         if (done < nchars) { | 
| Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7253 |             Py_UNICODE_COPY(p, str->str, str->length); | 
| Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7254 |             done = str->length; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7255 |         } | 
 | 7256 |         while (done < nchars) { | 
| Neal Norwitz | 4677fbf7 | 2008-03-25 04:18:18 +0000 | [diff] [blame] | 7257 |             Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; | 
| Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7258 |             Py_UNICODE_COPY(p+done, p, n); | 
 | 7259 |             done += n; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7260 |         } | 
| Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7261 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7262 |  | 
 | 7263 |     return (PyObject*) u; | 
 | 7264 | } | 
 | 7265 |  | 
 | 7266 | PyObject *PyUnicode_Replace(PyObject *obj, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7267 |                             PyObject *subobj, | 
 | 7268 |                             PyObject *replobj, | 
 | 7269 |                             Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7270 | { | 
 | 7271 |     PyObject *self; | 
 | 7272 |     PyObject *str1; | 
 | 7273 |     PyObject *str2; | 
 | 7274 |     PyObject *result; | 
 | 7275 |  | 
 | 7276 |     self = PyUnicode_FromObject(obj); | 
 | 7277 |     if (self == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7278 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7279 |     str1 = PyUnicode_FromObject(subobj); | 
 | 7280 |     if (str1 == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7281 |         Py_DECREF(self); | 
 | 7282 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7283 |     } | 
 | 7284 |     str2 = PyUnicode_FromObject(replobj); | 
 | 7285 |     if (str2 == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7286 |         Py_DECREF(self); | 
 | 7287 |         Py_DECREF(str1); | 
 | 7288 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7289 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7290 |     result = replace((PyUnicodeObject *)self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7291 |                      (PyUnicodeObject *)str1, | 
 | 7292 |                      (PyUnicodeObject *)str2, | 
 | 7293 |                      maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7294 |     Py_DECREF(self); | 
 | 7295 |     Py_DECREF(str1); | 
 | 7296 |     Py_DECREF(str2); | 
 | 7297 |     return result; | 
 | 7298 | } | 
 | 7299 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7300 | PyDoc_STRVAR(replace__doc__, | 
| Ezio Melotti | 2f06b78 | 2010-06-26 18:44:42 +0000 | [diff] [blame] | 7301 |              "S.replace(old, new[, count]) -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7302 | \n\ | 
 | 7303 | Return a copy of S with all occurrences of substring\n\ | 
| Georg Brandl | 30fadc1 | 2008-05-30 07:54:16 +0000 | [diff] [blame] | 7304 | old replaced by new.  If the optional argument count is\n\ | 
 | 7305 | given, only the first count occurrences are replaced."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7306 |  | 
 | 7307 | static PyObject* | 
 | 7308 | unicode_replace(PyUnicodeObject *self, PyObject *args) | 
 | 7309 | { | 
 | 7310 |     PyUnicodeObject *str1; | 
 | 7311 |     PyUnicodeObject *str2; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7312 |     Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7313 |     PyObject *result; | 
 | 7314 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7315 |     if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7316 |         return NULL; | 
 | 7317 |     str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); | 
 | 7318 |     if (str1 == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7319 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7320 |     str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); | 
| Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7321 |     if (str2 == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7322 |         Py_DECREF(str1); | 
 | 7323 |         return NULL; | 
| Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7324 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7325 |  | 
 | 7326 |     result = replace(self, str1, str2, maxcount); | 
 | 7327 |  | 
 | 7328 |     Py_DECREF(str1); | 
 | 7329 |     Py_DECREF(str2); | 
 | 7330 |     return result; | 
 | 7331 | } | 
 | 7332 |  | 
 | 7333 | static | 
 | 7334 | PyObject *unicode_repr(PyObject *unicode) | 
 | 7335 | { | 
 | 7336 |     return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7337 |                                 PyUnicode_GET_SIZE(unicode), | 
 | 7338 |                                 1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7339 | } | 
 | 7340 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7341 | PyDoc_STRVAR(rfind__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7342 |              "S.rfind(sub [,start [,end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7343 | \n\ | 
 | 7344 | Return the highest index in S where substring sub is found,\n\ | 
| Senthil Kumaran | 5e3a19d | 2011-07-27 23:36:51 +0800 | [diff] [blame] | 7345 | such that sub is contained within S[start:end].  Optional\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7346 | arguments start and end are interpreted as in slice notation.\n\ | 
 | 7347 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7348 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7349 |  | 
 | 7350 | static PyObject * | 
 | 7351 | unicode_rfind(PyUnicodeObject *self, PyObject *args) | 
 | 7352 | { | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 7353 |     PyUnicodeObject *substring; | 
| Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7354 |     Py_ssize_t start; | 
 | 7355 |     Py_ssize_t end; | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7356 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 |  | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 7358 |     if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, | 
 | 7359 |                                             &start, &end)) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7360 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7361 |  | 
| Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7362 |     result = stringlib_rfind_slice( | 
 | 7363 |         PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), | 
 | 7364 |         PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), | 
 | 7365 |         start, end | 
 | 7366 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7367 |  | 
 | 7368 |     Py_DECREF(substring); | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7369 |  | 
 | 7370 |     return PyInt_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7371 | } | 
 | 7372 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7373 | PyDoc_STRVAR(rindex__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7374 |              "S.rindex(sub [,start [,end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7375 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7376 | 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] | 7377 |  | 
 | 7378 | static PyObject * | 
 | 7379 | unicode_rindex(PyUnicodeObject *self, PyObject *args) | 
 | 7380 | { | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 7381 |     PyUnicodeObject *substring; | 
| Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7382 |     Py_ssize_t start; | 
 | 7383 |     Py_ssize_t end; | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7384 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7385 |  | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 7386 |     if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, | 
 | 7387 |                                             &start, &end)) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7388 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7389 |  | 
| Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7390 |     result = stringlib_rfind_slice( | 
 | 7391 |         PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), | 
 | 7392 |         PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), | 
 | 7393 |         start, end | 
 | 7394 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7395 |  | 
 | 7396 |     Py_DECREF(substring); | 
| Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7397 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 |     if (result < 0) { | 
 | 7399 |         PyErr_SetString(PyExc_ValueError, "substring not found"); | 
 | 7400 |         return NULL; | 
 | 7401 |     } | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7402 |     return PyInt_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7403 | } | 
 | 7404 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7405 | PyDoc_STRVAR(rjust__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7406 |              "S.rjust(width[, fillchar]) -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7407 | \n\ | 
| Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7408 | Return S right-justified in a Unicode string of length width. Padding is\n\ | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7409 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7410 |  | 
 | 7411 | static PyObject * | 
 | 7412 | unicode_rjust(PyUnicodeObject *self, PyObject *args) | 
 | 7413 | { | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7414 |     Py_ssize_t width; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7415 |     Py_UNICODE fillchar = ' '; | 
 | 7416 |  | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7417 |     if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7418 |         return NULL; | 
 | 7419 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7420 |     if (self->length >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7421 |         Py_INCREF(self); | 
 | 7422 |         return (PyObject*) self; | 
 | 7423 |     } | 
 | 7424 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7425 |     return (PyObject*) pad(self, width - self->length, 0, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7426 | } | 
 | 7427 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7428 | static PyObject* | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7429 | unicode_slice(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t end) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7430 | { | 
 | 7431 |     /* standard clamping */ | 
 | 7432 |     if (start < 0) | 
 | 7433 |         start = 0; | 
 | 7434 |     if (end < 0) | 
 | 7435 |         end = 0; | 
 | 7436 |     if (end > self->length) | 
 | 7437 |         end = self->length; | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7438 |     if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7439 |         /* full slice, return original string */ | 
 | 7440 |         Py_INCREF(self); | 
 | 7441 |         return (PyObject*) self; | 
 | 7442 |     } | 
 | 7443 |     if (start > end) | 
 | 7444 |         start = end; | 
 | 7445 |     /* copy slice */ | 
 | 7446 |     return (PyObject*) PyUnicode_FromUnicode(self->str + start, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7447 |                                              end - start); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7448 | } | 
 | 7449 |  | 
 | 7450 | PyObject *PyUnicode_Split(PyObject *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7451 |                           PyObject *sep, | 
 | 7452 |                           Py_ssize_t maxsplit) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7453 | { | 
 | 7454 |     PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7455 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 |     s = PyUnicode_FromObject(s); | 
 | 7457 |     if (s == NULL) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7458 |         return NULL; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7459 |     if (sep != NULL) { | 
 | 7460 |         sep = PyUnicode_FromObject(sep); | 
 | 7461 |         if (sep == NULL) { | 
 | 7462 |             Py_DECREF(s); | 
 | 7463 |             return NULL; | 
 | 7464 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7465 |     } | 
 | 7466 |  | 
 | 7467 |     result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); | 
 | 7468 |  | 
 | 7469 |     Py_DECREF(s); | 
 | 7470 |     Py_XDECREF(sep); | 
 | 7471 |     return result; | 
 | 7472 | } | 
 | 7473 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7474 | PyDoc_STRVAR(split__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7475 |              "S.split([sep [,maxsplit]]) -> list of strings\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | \n\ | 
 | 7477 | Return a list of the words in S, using sep as the\n\ | 
 | 7478 | delimiter string.  If maxsplit is given, at most maxsplit\n\ | 
| Georg Brandl | dfb77db | 2008-05-11 09:11:40 +0000 | [diff] [blame] | 7479 | splits are done. If sep is not specified or is None, any\n\ | 
| Georg Brandl | ecbbd94 | 2008-05-11 20:53:55 +0000 | [diff] [blame] | 7480 | whitespace string is a separator and empty strings are\n\ | 
 | 7481 | removed from the result."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7482 |  | 
 | 7483 | static PyObject* | 
 | 7484 | unicode_split(PyUnicodeObject *self, PyObject *args) | 
 | 7485 | { | 
 | 7486 |     PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7487 |     Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7489 |     if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7490 |         return NULL; | 
 | 7491 |  | 
 | 7492 |     if (substring == Py_None) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7493 |         return split(self, NULL, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7494 |     else if (PyUnicode_Check(substring)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7495 |         return split(self, (PyUnicodeObject *)substring, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 |     else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7497 |         return PyUnicode_Split((PyObject *)self, substring, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7498 | } | 
 | 7499 |  | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7500 | PyObject * | 
 | 7501 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) | 
 | 7502 | { | 
 | 7503 |     PyObject* str_obj; | 
 | 7504 |     PyObject* sep_obj; | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7505 |     PyObject* out; | 
| Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7506 |  | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7507 |     str_obj = PyUnicode_FromObject(str_in); | 
 | 7508 |     if (!str_obj) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7509 |         return NULL; | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7510 |     sep_obj = PyUnicode_FromObject(sep_in); | 
| Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7511 |     if (!sep_obj) { | 
 | 7512 |         Py_DECREF(str_obj); | 
 | 7513 |         return NULL; | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7514 |     } | 
 | 7515 |  | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7516 |     out = stringlib_partition( | 
| Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7517 |         str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), | 
 | 7518 |         sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) | 
 | 7519 |         ); | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7520 |  | 
| Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7521 |     Py_DECREF(sep_obj); | 
 | 7522 |     Py_DECREF(str_obj); | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7523 |  | 
 | 7524 |     return out; | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7525 | } | 
 | 7526 |  | 
| Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7527 |  | 
 | 7528 | PyObject * | 
 | 7529 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) | 
 | 7530 | { | 
 | 7531 |     PyObject* str_obj; | 
 | 7532 |     PyObject* sep_obj; | 
 | 7533 |     PyObject* out; | 
 | 7534 |  | 
 | 7535 |     str_obj = PyUnicode_FromObject(str_in); | 
 | 7536 |     if (!str_obj) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7537 |         return NULL; | 
| Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7538 |     sep_obj = PyUnicode_FromObject(sep_in); | 
 | 7539 |     if (!sep_obj) { | 
 | 7540 |         Py_DECREF(str_obj); | 
 | 7541 |         return NULL; | 
 | 7542 |     } | 
 | 7543 |  | 
| Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7544 |     out = stringlib_rpartition( | 
| Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7545 |         str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), | 
 | 7546 |         sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) | 
 | 7547 |         ); | 
 | 7548 |  | 
 | 7549 |     Py_DECREF(sep_obj); | 
 | 7550 |     Py_DECREF(str_obj); | 
 | 7551 |  | 
 | 7552 |     return out; | 
 | 7553 | } | 
 | 7554 |  | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7555 | PyDoc_STRVAR(partition__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7556 |              "S.partition(sep) -> (head, sep, tail)\n\ | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7557 | \n\ | 
| Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7558 | Search for the separator sep in S, and return the part before it,\n\ | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7559 | the separator itself, and the part after it.  If the separator is not\n\ | 
| Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7560 | found, return S and two empty strings."); | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7561 |  | 
 | 7562 | static PyObject* | 
| Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7563 | unicode_partition(PyUnicodeObject *self, PyObject *separator) | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7564 | { | 
| Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7565 |     return PyUnicode_Partition((PyObject *)self, separator); | 
 | 7566 | } | 
 | 7567 |  | 
| Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7568 | PyDoc_STRVAR(rpartition__doc__, | 
| Ezio Melotti | 1fafaab | 2010-01-25 11:24:37 +0000 | [diff] [blame] | 7569 |              "S.rpartition(sep) -> (head, sep, tail)\n\ | 
| Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7570 | \n\ | 
| Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7571 | Search for the separator sep in S, starting at the end of S, and return\n\ | 
| Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7572 | the part before it, the separator itself, and the part after it.  If the\n\ | 
| Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7573 | separator is not found, return two empty strings and S."); | 
| Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7574 |  | 
 | 7575 | static PyObject* | 
 | 7576 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) | 
 | 7577 | { | 
 | 7578 |     return PyUnicode_RPartition((PyObject *)self, separator); | 
 | 7579 | } | 
 | 7580 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7581 | PyObject *PyUnicode_RSplit(PyObject *s, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7582 |                            PyObject *sep, | 
 | 7583 |                            Py_ssize_t maxsplit) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7584 | { | 
 | 7585 |     PyObject *result; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7586 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7587 |     s = PyUnicode_FromObject(s); | 
 | 7588 |     if (s == NULL) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7589 |         return NULL; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7590 |     if (sep != NULL) { | 
 | 7591 |         sep = PyUnicode_FromObject(sep); | 
 | 7592 |         if (sep == NULL) { | 
 | 7593 |             Py_DECREF(s); | 
 | 7594 |             return NULL; | 
 | 7595 |         } | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7596 |     } | 
 | 7597 |  | 
 | 7598 |     result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); | 
 | 7599 |  | 
 | 7600 |     Py_DECREF(s); | 
 | 7601 |     Py_XDECREF(sep); | 
 | 7602 |     return result; | 
 | 7603 | } | 
 | 7604 |  | 
 | 7605 | PyDoc_STRVAR(rsplit__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7606 |              "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7607 | \n\ | 
 | 7608 | Return a list of the words in S, using sep as the\n\ | 
 | 7609 | delimiter string, starting at the end of the string and\n\ | 
 | 7610 | working to the front.  If maxsplit is given, at most maxsplit\n\ | 
 | 7611 | splits are done. If sep is not specified, any whitespace string\n\ | 
 | 7612 | is a separator."); | 
 | 7613 |  | 
 | 7614 | static PyObject* | 
 | 7615 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) | 
 | 7616 | { | 
 | 7617 |     PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7618 |     Py_ssize_t maxcount = -1; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7619 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7620 |     if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7621 |         return NULL; | 
 | 7622 |  | 
 | 7623 |     if (substring == Py_None) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7624 |         return rsplit(self, NULL, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7625 |     else if (PyUnicode_Check(substring)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7626 |         return rsplit(self, (PyUnicodeObject *)substring, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7627 |     else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7628 |         return PyUnicode_RSplit((PyObject *)self, substring, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7629 | } | 
 | 7630 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7631 | PyDoc_STRVAR(splitlines__doc__, | 
| Raymond Hettinger | aad5b02 | 2012-06-02 01:42:58 -0400 | [diff] [blame] | 7632 |              "S.splitlines(keepends=False) -> list of strings\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7633 | \n\ | 
 | 7634 | 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] | 7635 | 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] | 7636 | is given and true."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7637 |  | 
 | 7638 | static PyObject* | 
 | 7639 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) | 
 | 7640 | { | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7641 |     int keepends = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7642 |  | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7643 |     if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7644 |         return NULL; | 
 | 7645 |  | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7646 |     return PyUnicode_Splitlines((PyObject *)self, keepends); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7647 | } | 
 | 7648 |  | 
 | 7649 | static | 
 | 7650 | PyObject *unicode_str(PyUnicodeObject *self) | 
 | 7651 | { | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7652 |     return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7653 | } | 
 | 7654 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7655 | PyDoc_STRVAR(swapcase__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7656 |              "S.swapcase() -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7657 | \n\ | 
 | 7658 | 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] | 7659 | and vice versa."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7660 |  | 
 | 7661 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7662 | unicode_swapcase(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7663 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7664 |     return fixup(self, fixswapcase); | 
 | 7665 | } | 
 | 7666 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7667 | PyDoc_STRVAR(translate__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7668 |              "S.translate(table) -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7669 | \n\ | 
 | 7670 | Return a copy of the string S, where all characters have been mapped\n\ | 
 | 7671 | through the given translation table, which must be a mapping of\n\ | 
| Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 7672 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ | 
 | 7673 | Unmapped characters are left untouched. Characters mapped to None\n\ | 
 | 7674 | are deleted."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7675 |  | 
 | 7676 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7677 | unicode_translate(PyUnicodeObject *self, PyObject *table) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7678 | { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7679 |     return PyUnicode_TranslateCharmap(self->str, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7680 |                                       self->length, | 
 | 7681 |                                       table, | 
 | 7682 |                                       "ignore"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7683 | } | 
 | 7684 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7685 | PyDoc_STRVAR(upper__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7686 |              "S.upper() -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7687 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7688 | Return a copy of S converted to uppercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7689 |  | 
 | 7690 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7691 | unicode_upper(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7692 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7693 |     return fixup(self, fixupper); | 
 | 7694 | } | 
 | 7695 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7696 | PyDoc_STRVAR(zfill__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7697 |              "S.zfill(width) -> unicode\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7698 | \n\ | 
| Georg Brandl | 9806407 | 2008-09-09 19:26:00 +0000 | [diff] [blame] | 7699 | Pad a numeric string S with zeros on the left, to fill a field\n\ | 
 | 7700 | of the specified width. The string S is never truncated."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7701 |  | 
 | 7702 | static PyObject * | 
 | 7703 | unicode_zfill(PyUnicodeObject *self, PyObject *args) | 
 | 7704 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7705 |     Py_ssize_t fill; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7706 |     PyUnicodeObject *u; | 
 | 7707 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7708 |     Py_ssize_t width; | 
 | 7709 |     if (!PyArg_ParseTuple(args, "n:zfill", &width)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7710 |         return NULL; | 
 | 7711 |  | 
 | 7712 |     if (self->length >= width) { | 
| Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7713 |         if (PyUnicode_CheckExact(self)) { | 
 | 7714 |             Py_INCREF(self); | 
 | 7715 |             return (PyObject*) self; | 
 | 7716 |         } | 
 | 7717 |         else | 
 | 7718 |             return PyUnicode_FromUnicode( | 
 | 7719 |                 PyUnicode_AS_UNICODE(self), | 
 | 7720 |                 PyUnicode_GET_SIZE(self) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7721 |                 ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 |     } | 
 | 7723 |  | 
 | 7724 |     fill = width - self->length; | 
 | 7725 |  | 
 | 7726 |     u = pad(self, fill, 0, '0'); | 
 | 7727 |  | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7728 |     if (u == NULL) | 
 | 7729 |         return NULL; | 
 | 7730 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7731 |     if (u->str[fill] == '+' || u->str[fill] == '-') { | 
 | 7732 |         /* move sign to beginning of string */ | 
 | 7733 |         u->str[0] = u->str[fill]; | 
 | 7734 |         u->str[fill] = '0'; | 
 | 7735 |     } | 
 | 7736 |  | 
 | 7737 |     return (PyObject*) u; | 
 | 7738 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7739 |  | 
 | 7740 | #if 0 | 
 | 7741 | static PyObject* | 
| Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7742 | free_listsize(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7743 | { | 
| Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7744 |     return PyInt_FromLong(numfree); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | } | 
 | 7746 | #endif | 
 | 7747 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7748 | PyDoc_STRVAR(startswith__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7749 |              "S.startswith(prefix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7750 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7751 | Return True if S starts with the specified prefix, False otherwise.\n\ | 
 | 7752 | With optional start, test S beginning at that position.\n\ | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7753 | With optional end, stop comparing S at that position.\n\ | 
 | 7754 | prefix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7755 |  | 
 | 7756 | static PyObject * | 
 | 7757 | unicode_startswith(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7758 |                    PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | { | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7760 |     PyObject *subobj; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 |     PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7762 |     Py_ssize_t start = 0; | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7763 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7764 |     int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7765 |  | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 7766 |     if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7767 |         return NULL; | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7768 |     if (PyTuple_Check(subobj)) { | 
 | 7769 |         Py_ssize_t i; | 
 | 7770 |         for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
 | 7771 |             substring = (PyUnicodeObject *)PyUnicode_FromObject( | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7772 |                 PyTuple_GET_ITEM(subobj, i)); | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7773 |             if (substring == NULL) | 
 | 7774 |                 return NULL; | 
 | 7775 |             result = tailmatch(self, substring, start, end, -1); | 
 | 7776 |             Py_DECREF(substring); | 
 | 7777 |             if (result) { | 
 | 7778 |                 Py_RETURN_TRUE; | 
 | 7779 |             } | 
 | 7780 |         } | 
 | 7781 |         /* nothing matched */ | 
 | 7782 |         Py_RETURN_FALSE; | 
 | 7783 |     } | 
 | 7784 |     substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); | 
| Ezio Melotti | e3685f6 | 2011-04-26 05:12:51 +0300 | [diff] [blame] | 7785 |     if (substring == NULL) { | 
 | 7786 |         if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 7787 |             PyErr_Format(PyExc_TypeError, "startswith first arg must be str, " | 
 | 7788 |                          "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7789 |         return NULL; | 
| Ezio Melotti | e3685f6 | 2011-04-26 05:12:51 +0300 | [diff] [blame] | 7790 |     } | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7791 |     result = tailmatch(self, substring, start, end, -1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7792 |     Py_DECREF(substring); | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7793 |     return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | } | 
 | 7795 |  | 
 | 7796 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7797 | PyDoc_STRVAR(endswith__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7798 |              "S.endswith(suffix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7799 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7800 | Return True if S ends with the specified suffix, False otherwise.\n\ | 
 | 7801 | With optional start, test S beginning at that position.\n\ | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7802 | With optional end, stop comparing S at that position.\n\ | 
 | 7803 | suffix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 |  | 
 | 7805 | static PyObject * | 
 | 7806 | unicode_endswith(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7807 |                  PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | { | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7809 |     PyObject *subobj; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7810 |     PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7811 |     Py_ssize_t start = 0; | 
| Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7812 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7813 |     int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7814 |  | 
| Jesus Cea | 44e8168 | 2011-04-20 16:39:15 +0200 | [diff] [blame] | 7815 |     if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7816 |         return NULL; | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7817 |     if (PyTuple_Check(subobj)) { | 
 | 7818 |         Py_ssize_t i; | 
 | 7819 |         for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
 | 7820 |             substring = (PyUnicodeObject *)PyUnicode_FromObject( | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7821 |                 PyTuple_GET_ITEM(subobj, i)); | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7822 |             if (substring == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7823 |                 return NULL; | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7824 |             result = tailmatch(self, substring, start, end, +1); | 
 | 7825 |             Py_DECREF(substring); | 
 | 7826 |             if (result) { | 
 | 7827 |                 Py_RETURN_TRUE; | 
 | 7828 |             } | 
 | 7829 |         } | 
 | 7830 |         Py_RETURN_FALSE; | 
 | 7831 |     } | 
 | 7832 |     substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); | 
| Ezio Melotti | e3685f6 | 2011-04-26 05:12:51 +0300 | [diff] [blame] | 7833 |     if (substring == NULL) { | 
 | 7834 |         if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 7835 |             PyErr_Format(PyExc_TypeError, "endswith first arg must be str, " | 
 | 7836 |                          "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7837 |         return NULL; | 
| Ezio Melotti | e3685f6 | 2011-04-26 05:12:51 +0300 | [diff] [blame] | 7838 |     } | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7839 |     result = tailmatch(self, substring, start, end, +1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7840 |     Py_DECREF(substring); | 
| Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7841 |     return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | } | 
 | 7843 |  | 
 | 7844 |  | 
| Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7845 | /* Implements do_string_format, which is unicode because of stringlib */ | 
 | 7846 | #include "stringlib/string_format.h" | 
 | 7847 |  | 
 | 7848 | PyDoc_STRVAR(format__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7849 |              "S.format(*args, **kwargs) -> unicode\n\ | 
| Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7850 | \n\ | 
| Eric Smith | 6c84085 | 2010-11-06 19:43:44 +0000 | [diff] [blame] | 7851 | Return a formatted version of S, using substitutions from args and kwargs.\n\ | 
 | 7852 | The substitutions are identified by braces ('{' and '}')."); | 
| Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7853 |  | 
| Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7854 | static PyObject * | 
 | 7855 | unicode__format__(PyObject *self, PyObject *args) | 
 | 7856 | { | 
 | 7857 |     PyObject *format_spec; | 
 | 7858 |     PyObject *result = NULL; | 
 | 7859 |     PyObject *tmp = NULL; | 
 | 7860 |  | 
 | 7861 |     /* If 2.x, convert format_spec to the same type as value */ | 
 | 7862 |     /* This is to allow things like u''.format('') */ | 
 | 7863 |     if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) | 
 | 7864 |         goto done; | 
 | 7865 |     if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) { | 
 | 7866 |         PyErr_Format(PyExc_TypeError, "__format__ arg must be str " | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7867 |                      "or unicode, not %s", Py_TYPE(format_spec)->tp_name); | 
| Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7868 |         goto done; | 
 | 7869 |     } | 
 | 7870 |     tmp = PyObject_Unicode(format_spec); | 
 | 7871 |     if (tmp == NULL) | 
 | 7872 |         goto done; | 
 | 7873 |     format_spec = tmp; | 
 | 7874 |  | 
 | 7875 |     result = _PyUnicode_FormatAdvanced(self, | 
 | 7876 |                                        PyUnicode_AS_UNICODE(format_spec), | 
 | 7877 |                                        PyUnicode_GET_SIZE(format_spec)); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7878 |   done: | 
| Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7879 |     Py_XDECREF(tmp); | 
 | 7880 |     return result; | 
 | 7881 | } | 
 | 7882 |  | 
| Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7883 | PyDoc_STRVAR(p_format__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7884 |              "S.__format__(format_spec) -> unicode\n\ | 
| Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7885 | \n\ | 
| Eric Smith | 6c84085 | 2010-11-06 19:43:44 +0000 | [diff] [blame] | 7886 | Return a formatted version of S as described by format_spec."); | 
| Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7887 |  | 
| Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7888 | static PyObject * | 
 | 7889 | unicode__sizeof__(PyUnicodeObject *v) | 
 | 7890 | { | 
| Robert Schuppenies | 9be2ec1 | 2008-07-10 15:24:04 +0000 | [diff] [blame] | 7891 |     return PyInt_FromSsize_t(sizeof(PyUnicodeObject) + | 
 | 7892 |                              sizeof(Py_UNICODE) * (v->length + 1)); | 
| Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7893 | } | 
 | 7894 |  | 
 | 7895 | PyDoc_STRVAR(sizeof__doc__, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7896 |              "S.__sizeof__() -> size of S in memory, in bytes\n\ | 
| Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7897 | \n\ | 
 | 7898 | "); | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7899 |  | 
 | 7900 | static PyObject * | 
 | 7901 | unicode_getnewargs(PyUnicodeObject *v) | 
 | 7902 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7903 |     return Py_BuildValue("(u#)", v->str, v->length); | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7904 | } | 
 | 7905 |  | 
 | 7906 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7907 | static PyMethodDef unicode_methods[] = { | 
| Benjamin Peterson | 332d721 | 2009-09-18 21:14:55 +0000 | [diff] [blame] | 7908 |     {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7909 |     {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, | 
 | 7910 |     {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7911 |     {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7912 |     {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, | 
 | 7913 |     {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, | 
 | 7914 |     {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, | 
 | 7915 |     {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, | 
 | 7916 |     {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, | 
 | 7917 |     {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, | 
 | 7918 |     {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, | 
| Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7919 |     {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7920 |     {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, | 
 | 7921 |     {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, | 
 | 7922 |     {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7923 |     {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, | 
| Benjamin Peterson | 332d721 | 2009-09-18 21:14:55 +0000 | [diff] [blame] | 7924 |     {"decode", (PyCFunction) unicode_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7925 | /*  {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ | 
 | 7926 |     {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, | 
 | 7927 |     {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, | 
 | 7928 |     {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7929 |     {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, | 
| Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7930 |     {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7931 |     {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7932 |     {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7933 |     {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, | 
 | 7934 |     {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, | 
 | 7935 |     {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, | 
 | 7936 |     {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, | 
 | 7937 |     {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, | 
 | 7938 |     {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, | 
 | 7939 |     {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, | 
 | 7940 |     {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, | 
 | 7941 |     {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, | 
 | 7942 |     {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, | 
 | 7943 |     {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, | 
 | 7944 |     {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, | 
 | 7945 |     {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, | 
 | 7946 |     {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7947 |     {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, | 
| Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7948 |     {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, | 
 | 7949 |     {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, | 
 | 7950 |     {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, | 
 | 7951 |     {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, | 
| Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7952 |     {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7953 | #if 0 | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7954 |     {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7955 | #endif | 
 | 7956 |  | 
 | 7957 | #if 0 | 
 | 7958 |     /* This one is just used for debugging the implementation. */ | 
| Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7959 |     {"freelistsize", (PyCFunction) free_listsize, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7960 | #endif | 
 | 7961 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7962 |     {"__getnewargs__",  (PyCFunction)unicode_getnewargs, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7963 |     {NULL, NULL} | 
 | 7964 | }; | 
 | 7965 |  | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7966 | static PyObject * | 
 | 7967 | unicode_mod(PyObject *v, PyObject *w) | 
 | 7968 | { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 7969 |     if (!PyUnicode_Check(v)) { | 
 | 7970 |         Py_INCREF(Py_NotImplemented); | 
 | 7971 |         return Py_NotImplemented; | 
 | 7972 |     } | 
 | 7973 |     return PyUnicode_Format(v, w); | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7974 | } | 
 | 7975 |  | 
 | 7976 | static PyNumberMethods unicode_as_number = { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7977 |     0,              /*nb_add*/ | 
 | 7978 |     0,              /*nb_subtract*/ | 
 | 7979 |     0,              /*nb_multiply*/ | 
 | 7980 |     0,              /*nb_divide*/ | 
 | 7981 |     unicode_mod,            /*nb_remainder*/ | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7982 | }; | 
 | 7983 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7984 | static PySequenceMethods unicode_as_sequence = { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 7985 |     (lenfunc) unicode_length,       /* sq_length */ | 
 | 7986 |     PyUnicode_Concat,           /* sq_concat */ | 
 | 7987 |     (ssizeargfunc) unicode_repeat,  /* sq_repeat */ | 
 | 7988 |     (ssizeargfunc) unicode_getitem,     /* sq_item */ | 
 | 7989 |     (ssizessizeargfunc) unicode_slice,  /* sq_slice */ | 
 | 7990 |     0,                  /* sq_ass_item */ | 
 | 7991 |     0,                  /* sq_ass_slice */ | 
 | 7992 |     PyUnicode_Contains,         /* sq_contains */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7993 | }; | 
 | 7994 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7995 | static PyObject* | 
 | 7996 | unicode_subscript(PyUnicodeObject* self, PyObject* item) | 
 | 7997 | { | 
| Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 7998 |     if (PyIndex_Check(item)) { | 
 | 7999 |         Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8000 |         if (i == -1 && PyErr_Occurred()) | 
 | 8001 |             return NULL; | 
 | 8002 |         if (i < 0) | 
| Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8003 |             i += PyUnicode_GET_SIZE(self); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8004 |         return unicode_getitem(self, i); | 
 | 8005 |     } else if (PySlice_Check(item)) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8006 |         Py_ssize_t start, stop, step, slicelength, cur, i; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8007 |         Py_UNICODE* source_buf; | 
 | 8008 |         Py_UNICODE* result_buf; | 
 | 8009 |         PyObject* result; | 
 | 8010 |  | 
| Serhiy Storchaka | 5e79321 | 2017-04-15 20:11:12 +0300 | [diff] [blame] | 8011 |         if (_PySlice_Unpack(item, &start, &stop, &step) < 0) { | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8012 |             return NULL; | 
 | 8013 |         } | 
| Serhiy Storchaka | e41390a | 2017-04-08 11:48:57 +0300 | [diff] [blame] | 8014 |         slicelength = _PySlice_AdjustIndices(PyUnicode_GET_SIZE(self), &start, | 
 | 8015 |                                             &stop, step); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8016 |  | 
 | 8017 |         if (slicelength <= 0) { | 
 | 8018 |             return PyUnicode_FromUnicode(NULL, 0); | 
| Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 8019 |         } else if (start == 0 && step == 1 && slicelength == self->length && | 
 | 8020 |                    PyUnicode_CheckExact(self)) { | 
 | 8021 |             Py_INCREF(self); | 
 | 8022 |             return (PyObject *)self; | 
 | 8023 |         } else if (step == 1) { | 
 | 8024 |             return PyUnicode_FromUnicode(self->str + start, slicelength); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8025 |         } else { | 
 | 8026 |             source_buf = PyUnicode_AS_UNICODE((PyObject*)self); | 
| Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8027 |             result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* | 
 | 8028 |                                                        sizeof(Py_UNICODE)); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8029 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8030 |             if (result_buf == NULL) | 
 | 8031 |                 return PyErr_NoMemory(); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8032 |  | 
 | 8033 |             for (cur = start, i = 0; i < slicelength; cur += step, i++) { | 
 | 8034 |                 result_buf[i] = source_buf[cur]; | 
 | 8035 |             } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8036 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8037 |             result = PyUnicode_FromUnicode(result_buf, slicelength); | 
| Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8038 |             PyObject_FREE(result_buf); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8039 |             return result; | 
 | 8040 |         } | 
 | 8041 |     } else { | 
 | 8042 |         PyErr_SetString(PyExc_TypeError, "string indices must be integers"); | 
 | 8043 |         return NULL; | 
 | 8044 |     } | 
 | 8045 | } | 
 | 8046 |  | 
 | 8047 | static PyMappingMethods unicode_as_mapping = { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8048 |     (lenfunc)unicode_length,        /* mp_length */ | 
 | 8049 |     (binaryfunc)unicode_subscript,  /* mp_subscript */ | 
 | 8050 |     (objobjargproc)0,           /* mp_ass_subscript */ | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8051 | }; | 
 | 8052 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8053 | static Py_ssize_t | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | unicode_buffer_getreadbuf(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8055 |                           Py_ssize_t index, | 
 | 8056 |                           const void **ptr) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8057 | { | 
 | 8058 |     if (index != 0) { | 
 | 8059 |         PyErr_SetString(PyExc_SystemError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8060 |                         "accessing non-existent unicode segment"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8061 |         return -1; | 
 | 8062 |     } | 
 | 8063 |     *ptr = (void *) self->str; | 
 | 8064 |     return PyUnicode_GET_DATA_SIZE(self); | 
 | 8065 | } | 
 | 8066 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8067 | static Py_ssize_t | 
 | 8068 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8069 |                            const void **ptr) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8070 | { | 
 | 8071 |     PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8072 |                     "cannot use unicode as modifiable buffer"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8073 |     return -1; | 
 | 8074 | } | 
 | 8075 |  | 
 | 8076 | static int | 
 | 8077 | unicode_buffer_getsegcount(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8078 |                            Py_ssize_t *lenp) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8079 | { | 
 | 8080 |     if (lenp) | 
 | 8081 |         *lenp = PyUnicode_GET_DATA_SIZE(self); | 
 | 8082 |     return 1; | 
 | 8083 | } | 
 | 8084 |  | 
| Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 8085 | static Py_ssize_t | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8086 | unicode_buffer_getcharbuf(PyUnicodeObject *self, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8087 |                           Py_ssize_t index, | 
 | 8088 |                           const void **ptr) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | { | 
 | 8090 |     PyObject *str; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8091 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8092 |     if (index != 0) { | 
 | 8093 |         PyErr_SetString(PyExc_SystemError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8094 |                         "accessing non-existent unicode segment"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8095 |         return -1; | 
 | 8096 |     } | 
| Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8097 |     str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8098 |     if (str == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8099 |         return -1; | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8100 |     *ptr = (void *) PyString_AS_STRING(str); | 
 | 8101 |     return PyString_GET_SIZE(str); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8102 | } | 
 | 8103 |  | 
 | 8104 | /* Helpers for PyUnicode_Format() */ | 
 | 8105 |  | 
 | 8106 | static PyObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8107 | 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] | 8108 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8109 |     Py_ssize_t argidx = *p_argidx; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8110 |     if (argidx < arglen) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8111 |         (*p_argidx)++; | 
 | 8112 |         if (arglen < 0) | 
 | 8113 |             return args; | 
 | 8114 |         else | 
 | 8115 |             return PyTuple_GetItem(args, argidx); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8116 |     } | 
 | 8117 |     PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8118 |                     "not enough arguments for format string"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8119 |     return NULL; | 
 | 8120 | } | 
 | 8121 |  | 
 | 8122 | #define F_LJUST (1<<0) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8123 | #define F_SIGN  (1<<1) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8124 | #define F_BLANK (1<<2) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8125 | #define F_ALT   (1<<3) | 
 | 8126 | #define F_ZERO  (1<<4) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8127 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8128 | static Py_ssize_t | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8129 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8130 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8131 |     register Py_ssize_t i; | 
 | 8132 |     Py_ssize_t len = strlen(charbuffer); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8133 |     for (i = len - 1; i >= 0; i--) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8134 |         buffer[i] = (Py_UNICODE) charbuffer[i]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8135 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8136 |     return len; | 
 | 8137 | } | 
 | 8138 |  | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8139 | static int | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8140 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) | 
 | 8141 | { | 
| Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8142 |     Py_ssize_t result; | 
 | 8143 |  | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8144 |     PyOS_snprintf((char *)buffer, len, format, x); | 
| Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8145 |     result = strtounicode(buffer, (char *)buffer); | 
 | 8146 |     return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8147 | } | 
 | 8148 |  | 
| Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8149 | /* XXX To save some code duplication, formatfloat/long/int could have been | 
 | 8150 |    shared with stringobject.c, converting from 8-bit to Unicode after the | 
 | 8151 |    formatting is done. */ | 
 | 8152 |  | 
| Mark Dickinson | 18cfada | 2009-11-23 18:46:41 +0000 | [diff] [blame] | 8153 | /* Returns a new reference to a PyUnicode object, or NULL on failure. */ | 
 | 8154 |  | 
 | 8155 | static PyObject * | 
 | 8156 | formatfloat(PyObject *v, int flags, int prec, int type) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8157 | { | 
| Mark Dickinson | 18cfada | 2009-11-23 18:46:41 +0000 | [diff] [blame] | 8158 |     char *p; | 
 | 8159 |     PyObject *result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8160 |     double x; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8161 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8162 |     x = PyFloat_AsDouble(v); | 
 | 8163 |     if (x == -1.0 && PyErr_Occurred()) | 
| Mark Dickinson | 18cfada | 2009-11-23 18:46:41 +0000 | [diff] [blame] | 8164 |         return NULL; | 
 | 8165 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8166 |     if (prec < 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8167 |         prec = 6; | 
| Mark Dickinson | d4814bf | 2009-03-29 16:24:29 +0000 | [diff] [blame] | 8168 |  | 
| Mark Dickinson | 18cfada | 2009-11-23 18:46:41 +0000 | [diff] [blame] | 8169 |     p = PyOS_double_to_string(x, type, prec, | 
 | 8170 |                               (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); | 
 | 8171 |     if (p == NULL) | 
 | 8172 |         return NULL; | 
 | 8173 |     result = PyUnicode_FromStringAndSize(p, strlen(p)); | 
 | 8174 |     PyMem_Free(p); | 
 | 8175 |     return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8176 | } | 
 | 8177 |  | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8178 | static PyObject* | 
 | 8179 | formatlong(PyObject *val, int flags, int prec, int type) | 
 | 8180 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8181 |     char *buf; | 
 | 8182 |     int i, len; | 
 | 8183 |     PyObject *str; /* temporary string object. */ | 
 | 8184 |     PyUnicodeObject *result; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8185 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8186 |     str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); | 
 | 8187 |     if (!str) | 
 | 8188 |         return NULL; | 
 | 8189 |     result = _PyUnicode_New(len); | 
 | 8190 |     if (!result) { | 
 | 8191 |         Py_DECREF(str); | 
 | 8192 |         return NULL; | 
 | 8193 |     } | 
 | 8194 |     for (i = 0; i < len; i++) | 
 | 8195 |         result->str[i] = buf[i]; | 
 | 8196 |     result->str[len] = 0; | 
 | 8197 |     Py_DECREF(str); | 
 | 8198 |     return (PyObject*)result; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8199 | } | 
 | 8200 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8201 | static int | 
 | 8202 | formatint(Py_UNICODE *buf, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8203 |           size_t buflen, | 
 | 8204 |           int flags, | 
 | 8205 |           int prec, | 
 | 8206 |           int type, | 
 | 8207 |           PyObject *v) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8208 | { | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8209 |     /* fmt = '%#.' + `prec` + 'l' + `type` | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8210 |      * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) | 
 | 8211 |      *                     + 1 + 1 | 
 | 8212 |      *                   = 24 | 
 | 8213 |      */ | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8214 |     char fmt[64]; /* plenty big enough! */ | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8215 |     char *sign; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8216 |     long x; | 
 | 8217 |  | 
 | 8218 |     x = PyInt_AsLong(v); | 
 | 8219 |     if (x == -1 && PyErr_Occurred()) | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8220 |         return -1; | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8221 |     if (x < 0 && type == 'u') { | 
 | 8222 |         type = 'd'; | 
| Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8223 |     } | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8224 |     if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) | 
 | 8225 |         sign = "-"; | 
 | 8226 |     else | 
 | 8227 |         sign = ""; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8228 |     if (prec < 0) | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8229 |         prec = 1; | 
 | 8230 |  | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8231 |     /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) | 
 | 8232 |      * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8233 |      */ | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8234 |     if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8235 |         PyErr_SetString(PyExc_OverflowError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8236 |                         "formatted integer is too long (precision too large?)"); | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8237 |         return -1; | 
 | 8238 |     } | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8239 |  | 
 | 8240 |     if ((flags & F_ALT) && | 
 | 8241 |         (type == 'x' || type == 'X')) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8242 |         /* When converting under %#x or %#X, there are a number | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8243 |          * of issues that cause pain: | 
 | 8244 |          * - when 0 is being converted, the C standard leaves off | 
 | 8245 |          *   the '0x' or '0X', which is inconsistent with other | 
 | 8246 |          *   %#x/%#X conversions and inconsistent with Python's | 
 | 8247 |          *   hex() function | 
 | 8248 |          * - there are platforms that violate the standard and | 
 | 8249 |          *   convert 0 with the '0x' or '0X' | 
 | 8250 |          *   (Metrowerks, Compaq Tru64) | 
 | 8251 |          * - there are platforms that give '0x' when converting | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8252 |          *   under %#X, but convert 0 in accordance with the | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8253 |          *   standard (OS/2 EMX) | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8254 |          * | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8255 |          * We can achieve the desired consistency by inserting our | 
 | 8256 |          * own '0x' or '0X' prefix, and substituting %x/%X in place | 
 | 8257 |          * of %#x/%#X. | 
 | 8258 |          * | 
 | 8259 |          * Note that this is the same approach as used in | 
 | 8260 |          * formatint() in stringobject.c | 
| Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8261 |          */ | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8262 |         PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", | 
 | 8263 |                       sign, type, prec, type); | 
| Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8264 |     } | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8265 |     else { | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8266 |         PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", | 
 | 8267 |                       sign, (flags&F_ALT) ? "#" : "", | 
| Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8268 |                       prec, type); | 
| Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8269 |     } | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8270 |     if (sign[0]) | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8271 |         return longtounicode(buf, buflen, fmt, -x); | 
| Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8272 |     else | 
| Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8273 |         return longtounicode(buf, buflen, fmt, x); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8274 | } | 
 | 8275 |  | 
 | 8276 | static int | 
 | 8277 | formatchar(Py_UNICODE *buf, | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8278 |            size_t buflen, | 
 | 8279 |            PyObject *v) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8280 | { | 
| Ezio Melotti | 3212515 | 2010-02-25 17:36:04 +0000 | [diff] [blame] | 8281 |     PyObject *unistr; | 
 | 8282 |     char *str; | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8283 |     /* presume that the buffer is at least 2 characters long */ | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8284 |     if (PyUnicode_Check(v)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8285 |         if (PyUnicode_GET_SIZE(v) != 1) | 
 | 8286 |             goto onError; | 
 | 8287 |         buf[0] = PyUnicode_AS_UNICODE(v)[0]; | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8288 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8289 |  | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8290 |     else if (PyString_Check(v)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8291 |         if (PyString_GET_SIZE(v) != 1) | 
 | 8292 |             goto onError; | 
| Ezio Melotti | 3212515 | 2010-02-25 17:36:04 +0000 | [diff] [blame] | 8293 |         /* #7649: "u'%c' % char" should behave like "u'%s' % char" and fail | 
 | 8294 |            with a UnicodeDecodeError if 'char' is not decodable with the | 
 | 8295 |            default encoding (usually ASCII, but it might be something else) */ | 
 | 8296 |         str = PyString_AS_STRING(v); | 
 | 8297 |         if ((unsigned char)str[0] > 0x7F) { | 
 | 8298 |             /* the char is not ASCII; try to decode the string using the | 
 | 8299 |                default encoding and return -1 to let the UnicodeDecodeError | 
 | 8300 |                be raised if the string can't be decoded */ | 
 | 8301 |             unistr = PyUnicode_Decode(str, 1, NULL, "strict"); | 
 | 8302 |             if (unistr == NULL) | 
 | 8303 |                 return -1; | 
 | 8304 |             buf[0] = PyUnicode_AS_UNICODE(unistr)[0]; | 
 | 8305 |             Py_DECREF(unistr); | 
 | 8306 |         } | 
 | 8307 |         else | 
 | 8308 |             buf[0] = (Py_UNICODE)str[0]; | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8309 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8310 |  | 
 | 8311 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8312 |         /* Integer input truncated to a character */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8313 |         long x; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8314 |         x = PyInt_AsLong(v); | 
 | 8315 |         if (x == -1 && PyErr_Occurred()) | 
 | 8316 |             goto onError; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8317 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8318 |         if (x < 0 || x > 0x10ffff) { | 
 | 8319 |             PyErr_SetString(PyExc_OverflowError, | 
 | 8320 |                             "%c arg not in range(0x110000) " | 
 | 8321 |                             "(wide Python build)"); | 
 | 8322 |             return -1; | 
 | 8323 |         } | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8324 | #else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8325 |         if (x < 0 || x > 0xffff) { | 
 | 8326 |             PyErr_SetString(PyExc_OverflowError, | 
 | 8327 |                             "%c arg not in range(0x10000) " | 
 | 8328 |                             "(narrow Python build)"); | 
 | 8329 |             return -1; | 
 | 8330 |         } | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8331 | #endif | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8332 |         buf[0] = (Py_UNICODE) x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8333 |     } | 
 | 8334 |     buf[1] = '\0'; | 
 | 8335 |     return 1; | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8336 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8337 |   onError: | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8338 |     PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8339 |                     "%c requires int or char"); | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8340 |     return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8341 | } | 
 | 8342 |  | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8343 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) | 
 | 8344 |  | 
| Mark Dickinson | 18cfada | 2009-11-23 18:46:41 +0000 | [diff] [blame] | 8345 |    FORMATBUFLEN is the length of the buffer in which the ints & | 
| Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8346 |    chars are formatted. XXX This is a magic number. Each formatting | 
 | 8347 |    routine does bounds checking to ensure no overflow, but a better | 
 | 8348 |    solution may be to malloc a buffer of appropriate size for each | 
 | 8349 |    format. For now, the current solution is sufficient. | 
 | 8350 | */ | 
 | 8351 | #define FORMATBUFLEN (size_t)120 | 
 | 8352 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | PyObject *PyUnicode_Format(PyObject *format, | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8354 |                            PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8355 | { | 
 | 8356 |     Py_UNICODE *fmt, *res; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8357 |     Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8358 |     int args_owned = 0; | 
 | 8359 |     PyUnicodeObject *result = NULL; | 
 | 8360 |     PyObject *dict = NULL; | 
 | 8361 |     PyObject *uformat; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8362 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8363 |     if (format == NULL || args == NULL) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8364 |         PyErr_BadInternalCall(); | 
 | 8365 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8366 |     } | 
 | 8367 |     uformat = PyUnicode_FromObject(format); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8368 |     if (uformat == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8369 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8370 |     fmt = PyUnicode_AS_UNICODE(uformat); | 
 | 8371 |     fmtcnt = PyUnicode_GET_SIZE(uformat); | 
 | 8372 |  | 
 | 8373 |     reslen = rescnt = fmtcnt + 100; | 
 | 8374 |     result = _PyUnicode_New(reslen); | 
 | 8375 |     if (result == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8376 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8377 |     res = PyUnicode_AS_UNICODE(result); | 
 | 8378 |  | 
 | 8379 |     if (PyTuple_Check(args)) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8380 |         arglen = PyTuple_Size(args); | 
 | 8381 |         argidx = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8382 |     } | 
 | 8383 |     else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8384 |         arglen = -1; | 
 | 8385 |         argidx = -2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8386 |     } | 
| Benjamin Peterson | da2c7eb | 2013-03-23 22:32:00 -0500 | [diff] [blame] | 8387 |     if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript && | 
 | 8388 |         !PyTuple_Check(args) && !PyObject_TypeCheck(args, &PyBaseString_Type)) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8389 |         dict = args; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8390 |  | 
 | 8391 |     while (--fmtcnt >= 0) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8392 |         if (*fmt != '%') { | 
 | 8393 |             if (--rescnt < 0) { | 
 | 8394 |                 rescnt = fmtcnt + 100; | 
 | 8395 |                 reslen += rescnt; | 
 | 8396 |                 if (_PyUnicode_Resize(&result, reslen) < 0) | 
 | 8397 |                     goto onError; | 
 | 8398 |                 res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; | 
 | 8399 |                 --rescnt; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8400 |             } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8401 |             *res++ = *fmt++; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8402 |         } | 
 | 8403 |         else { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8404 |             /* Got a format specifier */ | 
 | 8405 |             int flags = 0; | 
 | 8406 |             Py_ssize_t width = -1; | 
 | 8407 |             int prec = -1; | 
 | 8408 |             Py_UNICODE c = '\0'; | 
 | 8409 |             Py_UNICODE fill; | 
 | 8410 |             int isnumok; | 
 | 8411 |             PyObject *v = NULL; | 
 | 8412 |             PyObject *temp = NULL; | 
 | 8413 |             Py_UNICODE *pbuf; | 
 | 8414 |             Py_UNICODE sign; | 
 | 8415 |             Py_ssize_t len; | 
| Mark Dickinson | 18cfada | 2009-11-23 18:46:41 +0000 | [diff] [blame] | 8416 |             Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{int,char}() */ | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8417 |  | 
 | 8418 |             fmt++; | 
 | 8419 |             if (*fmt == '(') { | 
 | 8420 |                 Py_UNICODE *keystart; | 
 | 8421 |                 Py_ssize_t keylen; | 
 | 8422 |                 PyObject *key; | 
 | 8423 |                 int pcount = 1; | 
 | 8424 |  | 
 | 8425 |                 if (dict == NULL) { | 
 | 8426 |                     PyErr_SetString(PyExc_TypeError, | 
 | 8427 |                                     "format requires a mapping"); | 
 | 8428 |                     goto onError; | 
 | 8429 |                 } | 
 | 8430 |                 ++fmt; | 
 | 8431 |                 --fmtcnt; | 
 | 8432 |                 keystart = fmt; | 
 | 8433 |                 /* Skip over balanced parentheses */ | 
 | 8434 |                 while (pcount > 0 && --fmtcnt >= 0) { | 
 | 8435 |                     if (*fmt == ')') | 
 | 8436 |                         --pcount; | 
 | 8437 |                     else if (*fmt == '(') | 
 | 8438 |                         ++pcount; | 
 | 8439 |                     fmt++; | 
 | 8440 |                 } | 
 | 8441 |                 keylen = fmt - keystart - 1; | 
 | 8442 |                 if (fmtcnt < 0 || pcount > 0) { | 
 | 8443 |                     PyErr_SetString(PyExc_ValueError, | 
 | 8444 |                                     "incomplete format key"); | 
 | 8445 |                     goto onError; | 
 | 8446 |                 } | 
 | 8447 | #if 0 | 
 | 8448 |                 /* keys are converted to strings using UTF-8 and | 
 | 8449 |                    then looked up since Python uses strings to hold | 
 | 8450 |                    variables names etc. in its namespaces and we | 
 | 8451 |                    wouldn't want to break common idioms. */ | 
 | 8452 |                 key = PyUnicode_EncodeUTF8(keystart, | 
 | 8453 |                                            keylen, | 
 | 8454 |                                            NULL); | 
 | 8455 | #else | 
 | 8456 |                 key = PyUnicode_FromUnicode(keystart, keylen); | 
 | 8457 | #endif | 
 | 8458 |                 if (key == NULL) | 
 | 8459 |                     goto onError; | 
 | 8460 |                 if (args_owned) { | 
 | 8461 |                     Py_DECREF(args); | 
 | 8462 |                     args_owned = 0; | 
 | 8463 |                 } | 
 | 8464 |                 args = PyObject_GetItem(dict, key); | 
 | 8465 |                 Py_DECREF(key); | 
 | 8466 |                 if (args == NULL) { | 
 | 8467 |                     goto onError; | 
 | 8468 |                 } | 
 | 8469 |                 args_owned = 1; | 
 | 8470 |                 arglen = -1; | 
 | 8471 |                 argidx = -2; | 
 | 8472 |             } | 
 | 8473 |             while (--fmtcnt >= 0) { | 
 | 8474 |                 switch (c = *fmt++) { | 
 | 8475 |                 case '-': flags |= F_LJUST; continue; | 
 | 8476 |                 case '+': flags |= F_SIGN; continue; | 
 | 8477 |                 case ' ': flags |= F_BLANK; continue; | 
 | 8478 |                 case '#': flags |= F_ALT; continue; | 
 | 8479 |                 case '0': flags |= F_ZERO; continue; | 
 | 8480 |                 } | 
 | 8481 |                 break; | 
 | 8482 |             } | 
 | 8483 |             if (c == '*') { | 
 | 8484 |                 v = getnextarg(args, arglen, &argidx); | 
 | 8485 |                 if (v == NULL) | 
 | 8486 |                     goto onError; | 
 | 8487 |                 if (!PyInt_Check(v)) { | 
 | 8488 |                     PyErr_SetString(PyExc_TypeError, | 
 | 8489 |                                     "* wants int"); | 
 | 8490 |                     goto onError; | 
 | 8491 |                 } | 
| Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 8492 |                 width = PyInt_AsSsize_t(v); | 
 | 8493 |                 if (width == -1 && PyErr_Occurred()) | 
 | 8494 |                     goto onError; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8495 |                 if (width < 0) { | 
 | 8496 |                     flags |= F_LJUST; | 
 | 8497 |                     width = -width; | 
 | 8498 |                 } | 
 | 8499 |                 if (--fmtcnt >= 0) | 
 | 8500 |                     c = *fmt++; | 
 | 8501 |             } | 
 | 8502 |             else if (c >= '0' && c <= '9') { | 
 | 8503 |                 width = c - '0'; | 
 | 8504 |                 while (--fmtcnt >= 0) { | 
 | 8505 |                     c = *fmt++; | 
 | 8506 |                     if (c < '0' || c > '9') | 
 | 8507 |                         break; | 
| Mark Dickinson | 75d3600 | 2012-10-28 10:00:46 +0000 | [diff] [blame] | 8508 |                     if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8509 |                         PyErr_SetString(PyExc_ValueError, | 
 | 8510 |                                         "width too big"); | 
 | 8511 |                         goto onError; | 
 | 8512 |                     } | 
 | 8513 |                     width = width*10 + (c - '0'); | 
 | 8514 |                 } | 
 | 8515 |             } | 
 | 8516 |             if (c == '.') { | 
 | 8517 |                 prec = 0; | 
 | 8518 |                 if (--fmtcnt >= 0) | 
 | 8519 |                     c = *fmt++; | 
 | 8520 |                 if (c == '*') { | 
 | 8521 |                     v = getnextarg(args, arglen, &argidx); | 
 | 8522 |                     if (v == NULL) | 
 | 8523 |                         goto onError; | 
 | 8524 |                     if (!PyInt_Check(v)) { | 
 | 8525 |                         PyErr_SetString(PyExc_TypeError, | 
 | 8526 |                                         "* wants int"); | 
 | 8527 |                         goto onError; | 
 | 8528 |                     } | 
| Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 8529 |                     prec = _PyInt_AsInt(v); | 
 | 8530 |                     if (prec == -1 && PyErr_Occurred()) | 
 | 8531 |                         goto onError; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8532 |                     if (prec < 0) | 
 | 8533 |                         prec = 0; | 
 | 8534 |                     if (--fmtcnt >= 0) | 
 | 8535 |                         c = *fmt++; | 
 | 8536 |                 } | 
 | 8537 |                 else if (c >= '0' && c <= '9') { | 
 | 8538 |                     prec = c - '0'; | 
 | 8539 |                     while (--fmtcnt >= 0) { | 
| Stefan Krah | 0b9201f | 2010-07-19 18:06:46 +0000 | [diff] [blame] | 8540 |                         c = *fmt++; | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8541 |                         if (c < '0' || c > '9') | 
 | 8542 |                             break; | 
| Mark Dickinson | 75d3600 | 2012-10-28 10:00:46 +0000 | [diff] [blame] | 8543 |                         if (prec > (INT_MAX - ((int)c - '0')) / 10) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8544 |                             PyErr_SetString(PyExc_ValueError, | 
 | 8545 |                                             "prec too big"); | 
 | 8546 |                             goto onError; | 
 | 8547 |                         } | 
 | 8548 |                         prec = prec*10 + (c - '0'); | 
 | 8549 |                     } | 
 | 8550 |                 } | 
 | 8551 |             } /* prec */ | 
 | 8552 |             if (fmtcnt >= 0) { | 
 | 8553 |                 if (c == 'h' || c == 'l' || c == 'L') { | 
 | 8554 |                     if (--fmtcnt >= 0) | 
 | 8555 |                         c = *fmt++; | 
 | 8556 |                 } | 
 | 8557 |             } | 
 | 8558 |             if (fmtcnt < 0) { | 
 | 8559 |                 PyErr_SetString(PyExc_ValueError, | 
 | 8560 |                                 "incomplete format"); | 
 | 8561 |                 goto onError; | 
 | 8562 |             } | 
 | 8563 |             if (c != '%') { | 
 | 8564 |                 v = getnextarg(args, arglen, &argidx); | 
 | 8565 |                 if (v == NULL) | 
 | 8566 |                     goto onError; | 
 | 8567 |             } | 
 | 8568 |             sign = 0; | 
 | 8569 |             fill = ' '; | 
 | 8570 |             switch (c) { | 
 | 8571 |  | 
 | 8572 |             case '%': | 
 | 8573 |                 pbuf = formatbuf; | 
 | 8574 |                 /* presume that buffer length is at least 1 */ | 
 | 8575 |                 pbuf[0] = '%'; | 
 | 8576 |                 len = 1; | 
 | 8577 |                 break; | 
 | 8578 |  | 
 | 8579 |             case 's': | 
 | 8580 |             case 'r': | 
| Victor Stinner | 95affc4 | 2010-03-22 12:24:37 +0000 | [diff] [blame] | 8581 |                 if (PyUnicode_CheckExact(v) && c == 's') { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8582 |                     temp = v; | 
 | 8583 |                     Py_INCREF(temp); | 
 | 8584 |                 } | 
 | 8585 |                 else { | 
 | 8586 |                     PyObject *unicode; | 
 | 8587 |                     if (c == 's') | 
 | 8588 |                         temp = PyObject_Unicode(v); | 
 | 8589 |                     else | 
 | 8590 |                         temp = PyObject_Repr(v); | 
 | 8591 |                     if (temp == NULL) | 
 | 8592 |                         goto onError; | 
| Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8593 |                     if (PyUnicode_Check(temp)) | 
 | 8594 |                         /* nothing to do */; | 
| Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8595 |                     else if (PyString_Check(temp)) { | 
| Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8596 |                         /* convert to string to Unicode */ | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8597 |                         unicode = PyUnicode_Decode(PyString_AS_STRING(temp), | 
 | 8598 |                                                    PyString_GET_SIZE(temp), | 
 | 8599 |                                                    NULL, | 
 | 8600 |                                                    "strict"); | 
 | 8601 |                         Py_DECREF(temp); | 
 | 8602 |                         temp = unicode; | 
 | 8603 |                         if (temp == NULL) | 
 | 8604 |                             goto onError; | 
 | 8605 |                     } | 
 | 8606 |                     else { | 
 | 8607 |                         Py_DECREF(temp); | 
 | 8608 |                         PyErr_SetString(PyExc_TypeError, | 
 | 8609 |                                         "%s argument has non-string str()"); | 
 | 8610 |                         goto onError; | 
 | 8611 |                     } | 
 | 8612 |                 } | 
 | 8613 |                 pbuf = PyUnicode_AS_UNICODE(temp); | 
 | 8614 |                 len = PyUnicode_GET_SIZE(temp); | 
 | 8615 |                 if (prec >= 0 && len > prec) | 
 | 8616 |                     len = prec; | 
 | 8617 |                 break; | 
 | 8618 |  | 
 | 8619 |             case 'i': | 
 | 8620 |             case 'd': | 
 | 8621 |             case 'u': | 
 | 8622 |             case 'o': | 
 | 8623 |             case 'x': | 
 | 8624 |             case 'X': | 
 | 8625 |                 if (c == 'i') | 
 | 8626 |                     c = 'd'; | 
 | 8627 |                 isnumok = 0; | 
 | 8628 |                 if (PyNumber_Check(v)) { | 
 | 8629 |                     PyObject *iobj=NULL; | 
 | 8630 |  | 
 | 8631 |                     if (PyInt_Check(v) || (PyLong_Check(v))) { | 
 | 8632 |                         iobj = v; | 
 | 8633 |                         Py_INCREF(iobj); | 
 | 8634 |                     } | 
 | 8635 |                     else { | 
 | 8636 |                         iobj = PyNumber_Int(v); | 
| Serhiy Storchaka | 149d080 | 2016-04-10 15:26:52 +0300 | [diff] [blame] | 8637 |                         if (iobj==NULL) { | 
 | 8638 |                             PyErr_Clear(); | 
 | 8639 |                             iobj = PyNumber_Long(v); | 
 | 8640 |                         } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8641 |                     } | 
 | 8642 |                     if (iobj!=NULL) { | 
 | 8643 |                         if (PyInt_Check(iobj)) { | 
 | 8644 |                             isnumok = 1; | 
 | 8645 |                             pbuf = formatbuf; | 
 | 8646 |                             len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), | 
 | 8647 |                                             flags, prec, c, iobj); | 
 | 8648 |                             Py_DECREF(iobj); | 
 | 8649 |                             if (len < 0) | 
 | 8650 |                                 goto onError; | 
 | 8651 |                             sign = 1; | 
 | 8652 |                         } | 
 | 8653 |                         else if (PyLong_Check(iobj)) { | 
 | 8654 |                             isnumok = 1; | 
 | 8655 |                             temp = formatlong(iobj, flags, prec, c); | 
 | 8656 |                             Py_DECREF(iobj); | 
 | 8657 |                             if (!temp) | 
 | 8658 |                                 goto onError; | 
 | 8659 |                             pbuf = PyUnicode_AS_UNICODE(temp); | 
 | 8660 |                             len = PyUnicode_GET_SIZE(temp); | 
 | 8661 |                             sign = 1; | 
 | 8662 |                         } | 
 | 8663 |                         else { | 
 | 8664 |                             Py_DECREF(iobj); | 
 | 8665 |                         } | 
 | 8666 |                     } | 
 | 8667 |                 } | 
 | 8668 |                 if (!isnumok) { | 
 | 8669 |                     PyErr_Format(PyExc_TypeError, | 
 | 8670 |                                  "%%%c format: a number is required, " | 
 | 8671 |                                  "not %.200s", (char)c, Py_TYPE(v)->tp_name); | 
 | 8672 |                     goto onError; | 
 | 8673 |                 } | 
 | 8674 |                 if (flags & F_ZERO) | 
 | 8675 |                     fill = '0'; | 
 | 8676 |                 break; | 
 | 8677 |  | 
 | 8678 |             case 'e': | 
 | 8679 |             case 'E': | 
 | 8680 |             case 'f': | 
 | 8681 |             case 'F': | 
 | 8682 |             case 'g': | 
 | 8683 |             case 'G': | 
| Mark Dickinson | 18cfada | 2009-11-23 18:46:41 +0000 | [diff] [blame] | 8684 |                 temp = formatfloat(v, flags, prec, c); | 
 | 8685 |                 if (temp == NULL) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8686 |                     goto onError; | 
| Mark Dickinson | 18cfada | 2009-11-23 18:46:41 +0000 | [diff] [blame] | 8687 |                 pbuf = PyUnicode_AS_UNICODE(temp); | 
 | 8688 |                 len = PyUnicode_GET_SIZE(temp); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8689 |                 sign = 1; | 
 | 8690 |                 if (flags & F_ZERO) | 
 | 8691 |                     fill = '0'; | 
 | 8692 |                 break; | 
 | 8693 |  | 
 | 8694 |             case 'c': | 
 | 8695 |                 pbuf = formatbuf; | 
 | 8696 |                 len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); | 
 | 8697 |                 if (len < 0) | 
 | 8698 |                     goto onError; | 
 | 8699 |                 break; | 
 | 8700 |  | 
 | 8701 |             default: | 
 | 8702 |                 PyErr_Format(PyExc_ValueError, | 
 | 8703 |                              "unsupported format character '%c' (0x%x) " | 
 | 8704 |                              "at index %zd", | 
 | 8705 |                              (31<=c && c<=126) ? (char)c : '?', | 
 | 8706 |                              (int)c, | 
 | 8707 |                              (Py_ssize_t)(fmt - 1 - | 
 | 8708 |                                           PyUnicode_AS_UNICODE(uformat))); | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8709 |                 goto onError; | 
 | 8710 |             } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8711 |             if (sign) { | 
 | 8712 |                 if (*pbuf == '-' || *pbuf == '+') { | 
 | 8713 |                     sign = *pbuf++; | 
 | 8714 |                     len--; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8715 |                 } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8716 |                 else if (flags & F_SIGN) | 
 | 8717 |                     sign = '+'; | 
 | 8718 |                 else if (flags & F_BLANK) | 
 | 8719 |                     sign = ' '; | 
 | 8720 |                 else | 
 | 8721 |                     sign = 0; | 
 | 8722 |             } | 
 | 8723 |             if (width < len) | 
 | 8724 |                 width = len; | 
 | 8725 |             if (rescnt - (sign != 0) < width) { | 
 | 8726 |                 reslen -= rescnt; | 
 | 8727 |                 rescnt = width + fmtcnt + 100; | 
 | 8728 |                 reslen += rescnt; | 
 | 8729 |                 if (reslen < 0) { | 
 | 8730 |                     Py_XDECREF(temp); | 
 | 8731 |                     PyErr_NoMemory(); | 
 | 8732 |                     goto onError; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8733 |                 } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8734 |                 if (_PyUnicode_Resize(&result, reslen) < 0) { | 
 | 8735 |                     Py_XDECREF(temp); | 
 | 8736 |                     goto onError; | 
 | 8737 |                 } | 
 | 8738 |                 res = PyUnicode_AS_UNICODE(result) | 
 | 8739 |                     + reslen - rescnt; | 
 | 8740 |             } | 
 | 8741 |             if (sign) { | 
 | 8742 |                 if (fill != ' ') | 
 | 8743 |                     *res++ = sign; | 
 | 8744 |                 rescnt--; | 
 | 8745 |                 if (width > len) | 
 | 8746 |                     width--; | 
 | 8747 |             } | 
 | 8748 |             if ((flags & F_ALT) && (c == 'x' || c == 'X')) { | 
 | 8749 |                 assert(pbuf[0] == '0'); | 
 | 8750 |                 assert(pbuf[1] == c); | 
 | 8751 |                 if (fill != ' ') { | 
 | 8752 |                     *res++ = *pbuf++; | 
 | 8753 |                     *res++ = *pbuf++; | 
 | 8754 |                 } | 
 | 8755 |                 rescnt -= 2; | 
 | 8756 |                 width -= 2; | 
 | 8757 |                 if (width < 0) | 
 | 8758 |                     width = 0; | 
 | 8759 |                 len -= 2; | 
 | 8760 |             } | 
 | 8761 |             if (width > len && !(flags & F_LJUST)) { | 
 | 8762 |                 do { | 
 | 8763 |                     --rescnt; | 
 | 8764 |                     *res++ = fill; | 
 | 8765 |                 } while (--width > len); | 
 | 8766 |             } | 
 | 8767 |             if (fill == ' ') { | 
 | 8768 |                 if (sign) | 
 | 8769 |                     *res++ = sign; | 
 | 8770 |                 if ((flags & F_ALT) && (c == 'x' || c == 'X')) { | 
 | 8771 |                     assert(pbuf[0] == '0'); | 
 | 8772 |                     assert(pbuf[1] == c); | 
 | 8773 |                     *res++ = *pbuf++; | 
 | 8774 |                     *res++ = *pbuf++; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8775 |                 } | 
 | 8776 |             } | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8777 |             Py_UNICODE_COPY(res, pbuf, len); | 
 | 8778 |             res += len; | 
 | 8779 |             rescnt -= len; | 
 | 8780 |             while (--width >= len) { | 
 | 8781 |                 --rescnt; | 
 | 8782 |                 *res++ = ' '; | 
 | 8783 |             } | 
 | 8784 |             if (dict && (argidx < arglen) && c != '%') { | 
 | 8785 |                 PyErr_SetString(PyExc_TypeError, | 
 | 8786 |                                 "not all arguments converted during string formatting"); | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8787 |                 Py_XDECREF(temp); | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8788 |                 goto onError; | 
 | 8789 |             } | 
 | 8790 |             Py_XDECREF(temp); | 
 | 8791 |         } /* '%' */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8792 |     } /* until end */ | 
 | 8793 |     if (argidx < arglen && !dict) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8794 |         PyErr_SetString(PyExc_TypeError, | 
 | 8795 |                         "not all arguments converted during string formatting"); | 
 | 8796 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8797 |     } | 
 | 8798 |  | 
| Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8799 |     if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8800 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8801 |     if (args_owned) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8802 |         Py_DECREF(args); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8803 |     } | 
 | 8804 |     Py_DECREF(uformat); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8805 |     return (PyObject *)result; | 
 | 8806 |  | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8807 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8808 |     Py_XDECREF(result); | 
 | 8809 |     Py_DECREF(uformat); | 
 | 8810 |     if (args_owned) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8811 |         Py_DECREF(args); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8812 |     } | 
 | 8813 |     return NULL; | 
 | 8814 | } | 
 | 8815 |  | 
 | 8816 | static PyBufferProcs unicode_as_buffer = { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8817 |     (readbufferproc) unicode_buffer_getreadbuf, | 
 | 8818 |     (writebufferproc) unicode_buffer_getwritebuf, | 
 | 8819 |     (segcountproc) unicode_buffer_getsegcount, | 
 | 8820 |     (charbufferproc) unicode_buffer_getcharbuf, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8821 | }; | 
 | 8822 |  | 
| Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8823 | static PyObject * | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8824 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | 
 | 8825 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8826 | static PyObject * | 
 | 8827 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 8828 | { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8829 |     PyObject *x = NULL; | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8830 |     static char *kwlist[] = {"string", "encoding", "errors", 0}; | 
 | 8831 |     char *encoding = NULL; | 
 | 8832 |     char *errors = NULL; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8833 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8834 |     if (type != &PyUnicode_Type) | 
 | 8835 |         return unicode_subtype_new(type, args, kwds); | 
 | 8836 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8837 |                                      kwlist, &x, &encoding, &errors)) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8838 |         return NULL; | 
 | 8839 |     if (x == NULL) | 
 | 8840 |         return (PyObject *)_PyUnicode_New(0); | 
 | 8841 |     if (encoding == NULL && errors == NULL) | 
 | 8842 |         return PyObject_Unicode(x); | 
 | 8843 |     else | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8844 |         return PyUnicode_FromEncodedObject(x, encoding, errors); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8845 | } | 
 | 8846 |  | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8847 | static PyObject * | 
 | 8848 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 8849 | { | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8850 |     PyUnicodeObject *tmp, *pnew; | 
 | 8851 |     Py_ssize_t n; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8852 |  | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8853 |     assert(PyType_IsSubtype(type, &PyUnicode_Type)); | 
 | 8854 |     tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); | 
 | 8855 |     if (tmp == NULL) | 
 | 8856 |         return NULL; | 
 | 8857 |     assert(PyUnicode_Check(tmp)); | 
 | 8858 |     pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); | 
 | 8859 |     if (pnew == NULL) { | 
 | 8860 |         Py_DECREF(tmp); | 
 | 8861 |         return NULL; | 
 | 8862 |     } | 
 | 8863 |     pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); | 
 | 8864 |     if (pnew->str == NULL) { | 
 | 8865 |         _Py_ForgetReference((PyObject *)pnew); | 
 | 8866 |         PyObject_Del(pnew); | 
 | 8867 |         Py_DECREF(tmp); | 
 | 8868 |         return PyErr_NoMemory(); | 
 | 8869 |     } | 
 | 8870 |     Py_UNICODE_COPY(pnew->str, tmp->str, n+1); | 
 | 8871 |     pnew->length = n; | 
 | 8872 |     pnew->hash = tmp->hash; | 
 | 8873 |     Py_DECREF(tmp); | 
 | 8874 |     return (PyObject *)pnew; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8875 | } | 
 | 8876 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8877 | PyDoc_STRVAR(unicode_doc, | 
| Chris Jerdonek | ad4b000 | 2012-10-07 20:37:54 -0700 | [diff] [blame] | 8878 |              "unicode(object='') -> unicode object\n\ | 
 | 8879 | unicode(string[, encoding[, errors]]) -> unicode object\n\ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8880 | \n\ | 
 | 8881 | Create a new Unicode object from the given encoded string.\n\ | 
| Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8882 | encoding defaults to the current default string encoding.\n\ | 
 | 8883 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8884 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8885 | PyTypeObject PyUnicode_Type = { | 
| Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 8886 |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8887 |     "unicode",              /* tp_name */ | 
 | 8888 |     sizeof(PyUnicodeObject),        /* tp_size */ | 
 | 8889 |     0,                  /* tp_itemsize */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8890 |     /* Slots */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8891 |     (destructor)unicode_dealloc,    /* tp_dealloc */ | 
 | 8892 |     0,                  /* tp_print */ | 
 | 8893 |     0,                  /* tp_getattr */ | 
 | 8894 |     0,                  /* tp_setattr */ | 
 | 8895 |     0,                  /* tp_compare */ | 
 | 8896 |     unicode_repr,           /* tp_repr */ | 
 | 8897 |     &unicode_as_number,         /* tp_as_number */ | 
 | 8898 |     &unicode_as_sequence,       /* tp_as_sequence */ | 
 | 8899 |     &unicode_as_mapping,        /* tp_as_mapping */ | 
 | 8900 |     (hashfunc) unicode_hash,        /* tp_hash*/ | 
 | 8901 |     0,                  /* tp_call*/ | 
 | 8902 |     (reprfunc) unicode_str,     /* tp_str */ | 
 | 8903 |     PyObject_GenericGetAttr,        /* tp_getattro */ | 
 | 8904 |     0,                  /* tp_setattro */ | 
 | 8905 |     &unicode_as_buffer,         /* tp_as_buffer */ | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8906 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8907 |     Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS,  /* tp_flags */ | 
| Benjamin Peterson | 857ce15 | 2009-01-31 16:29:18 +0000 | [diff] [blame] | 8908 |     unicode_doc,            /* tp_doc */ | 
 | 8909 |     0,                  /* tp_traverse */ | 
 | 8910 |     0,                  /* tp_clear */ | 
 | 8911 |     PyUnicode_RichCompare,      /* tp_richcompare */ | 
 | 8912 |     0,                  /* tp_weaklistoffset */ | 
 | 8913 |     0,                  /* tp_iter */ | 
 | 8914 |     0,                  /* tp_iternext */ | 
 | 8915 |     unicode_methods,            /* tp_methods */ | 
 | 8916 |     0,                  /* tp_members */ | 
 | 8917 |     0,                  /* tp_getset */ | 
 | 8918 |     &PyBaseString_Type,         /* tp_base */ | 
 | 8919 |     0,                  /* tp_dict */ | 
 | 8920 |     0,                  /* tp_descr_get */ | 
 | 8921 |     0,                  /* tp_descr_set */ | 
 | 8922 |     0,                  /* tp_dictoffset */ | 
 | 8923 |     0,                  /* tp_init */ | 
 | 8924 |     0,                  /* tp_alloc */ | 
 | 8925 |     unicode_new,            /* tp_new */ | 
 | 8926 |     PyObject_Del,           /* tp_free */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8927 | }; | 
 | 8928 |  | 
 | 8929 | /* Initialize the Unicode implementation */ | 
 | 8930 |  | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8931 | void _PyUnicode_Init(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8932 | { | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8933 |     /* XXX - move this array to unicodectype.c ? */ | 
 | 8934 |     Py_UNICODE linebreak[] = { | 
 | 8935 |         0x000A, /* LINE FEED */ | 
 | 8936 |         0x000D, /* CARRIAGE RETURN */ | 
 | 8937 |         0x001C, /* FILE SEPARATOR */ | 
 | 8938 |         0x001D, /* GROUP SEPARATOR */ | 
 | 8939 |         0x001E, /* RECORD SEPARATOR */ | 
 | 8940 |         0x0085, /* NEXT LINE */ | 
 | 8941 |         0x2028, /* LINE SEPARATOR */ | 
 | 8942 |         0x2029, /* PARAGRAPH SEPARATOR */ | 
 | 8943 |     }; | 
 | 8944 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8945 |     /* Init the implementation */ | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 8946 |     if (!unicode_empty) { | 
 | 8947 |         unicode_empty = _PyUnicode_New(0); | 
 | 8948 |         if (!unicode_empty) | 
 | 8949 |             return; | 
 | 8950 |     } | 
| Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 8951 |  | 
| Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8952 |     if (PyType_Ready(&PyUnicode_Type) < 0) | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8953 |         Py_FatalError("Can't initialize 'unicode'"); | 
| Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8954 |  | 
 | 8955 |     /* initialize the linebreak bloom filter */ | 
 | 8956 |     bloom_linebreak = make_bloom_mask( | 
 | 8957 |         linebreak, sizeof(linebreak) / sizeof(linebreak[0]) | 
 | 8958 |         ); | 
| Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 8959 |  | 
 | 8960 |     PyType_Ready(&EncodingMapType); | 
| Benjamin Peterson | 6da3ed6 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 8961 |  | 
 | 8962 |     if (PyType_Ready(&PyFieldNameIter_Type) < 0) | 
 | 8963 |         Py_FatalError("Can't initialize field name iterator type"); | 
 | 8964 |  | 
 | 8965 |     if (PyType_Ready(&PyFormatterIter_Type) < 0) | 
 | 8966 |         Py_FatalError("Can't initialize formatter iter type"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8967 | } | 
 | 8968 |  | 
 | 8969 | /* Finalize the Unicode implementation */ | 
 | 8970 |  | 
| Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 8971 | int | 
 | 8972 | PyUnicode_ClearFreeList(void) | 
 | 8973 | { | 
 | 8974 |     int freelist_size = numfree; | 
 | 8975 |     PyUnicodeObject *u; | 
 | 8976 |  | 
 | 8977 |     for (u = free_list; u != NULL;) { | 
| Benjamin Peterson | be1399e | 2009-01-31 22:03:19 +0000 | [diff] [blame] | 8978 |         PyUnicodeObject *v = u; | 
 | 8979 |         u = *(PyUnicodeObject **)u; | 
 | 8980 |         if (v->str) | 
 | 8981 |             PyObject_DEL(v->str); | 
 | 8982 |         Py_XDECREF(v->defenc); | 
 | 8983 |         PyObject_Del(v); | 
 | 8984 |         numfree--; | 
| Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 8985 |     } | 
 | 8986 |     free_list = NULL; | 
 | 8987 |     assert(numfree == 0); | 
 | 8988 |     return freelist_size; | 
 | 8989 | } | 
 | 8990 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8991 | void | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8992 | _PyUnicode_Fini(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | { | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8994 |     int i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8995 |  | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 8996 |     Py_CLEAR(unicode_empty); | 
| Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8997 |  | 
| Serhiy Storchaka | c59c85c | 2013-01-26 12:13:40 +0200 | [diff] [blame] | 8998 |     for (i = 0; i < 256; i++) | 
 | 8999 |         Py_CLEAR(unicode_latin1[i]); | 
 | 9000 |  | 
| Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9001 |     (void)PyUnicode_ClearFreeList(); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9003 |  | 
| Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 9004 | #ifdef __cplusplus | 
 | 9005 | } | 
 | 9006 | #endif |