| 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, | 
| Benjamin Peterson | 31616ea | 2011-10-01 00:11:09 -0400 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com>. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | Major speed upgrades to the method implementations at the Reykjavik | 
 | 7 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. | 
 | 8 |  | 
| Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 9 | Copyright (c) Corporation for National Research Initiatives. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10 |  | 
| Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 11 | -------------------------------------------------------------------- | 
 | 12 | The original string type implementation is: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14 |   Copyright (c) 1999 by Secret Labs AB | 
 | 15 |   Copyright (c) 1999 by Fredrik Lundh | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 16 |  | 
| Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 17 | By obtaining, using, and/or copying this software and/or its | 
 | 18 | associated documentation, you agree that you have read, understood, | 
 | 19 | and will comply with the following terms and conditions: | 
 | 20 |  | 
 | 21 | Permission to use, copy, modify, and distribute this software and its | 
 | 22 | associated documentation for any purpose and without fee is hereby | 
 | 23 | granted, provided that the above copyright notice appears in all | 
 | 24 | copies, and that both that copyright notice and this permission notice | 
 | 25 | appear in supporting documentation, and that the name of Secret Labs | 
 | 26 | AB or the author not be used in advertising or publicity pertaining to | 
 | 27 | distribution of the software without specific, written prior | 
 | 28 | permission. | 
 | 29 |  | 
 | 30 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO | 
 | 31 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | 
 | 32 | FITNESS.  IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR | 
 | 33 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | 
 | 34 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 
 | 35 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | 
 | 36 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 
 | 37 | -------------------------------------------------------------------- | 
 | 38 |  | 
 | 39 | */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | #define PY_SSIZE_T_CLEAN | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "Python.h" | 
| Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 44 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 45 | #ifdef MS_WINDOWS | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 46 | #include <windows.h> | 
 | 47 | #endif | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 48 |  | 
| Victor Stinner | ce5faf6 | 2011-10-05 00:42:43 +0200 | [diff] [blame] | 49 | #ifdef Py_DEBUG | 
 | 50 | #  define DONT_MAKE_RESULT_READY | 
 | 51 | #endif | 
 | 52 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Limit for the Unicode object free list */ | 
 | 54 |  | 
| Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST       1024 | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 |  | 
 | 57 | /* Limit for the Unicode object free list stay alive optimization. | 
 | 58 |  | 
 | 59 |    The implementation will keep allocated Unicode memory intact for | 
 | 60 |    all objects on the free list having a size less than this | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 |    limit. This reduces malloc() overhead for small Unicode objects. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 |  | 
| Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 |    At worst this will result in PyUnicode_MAXFREELIST * | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 |    (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 |    malloc()-overhead) bytes of unused garbage. | 
 | 66 |  | 
 | 67 |    Setting the limit to 0 effectively turns the feature off. | 
 | 68 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 |    Note: This is an experimental feature ! If you get core dumps when | 
 | 70 |    using Unicode objects, turn this feature off. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 |  | 
 | 72 | */ | 
 | 73 |  | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT       9 | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 |  | 
 | 76 | /* Endianness switches; defaults to little endian */ | 
 | 77 |  | 
 | 78 | #ifdef WORDS_BIGENDIAN | 
 | 79 | # define BYTEORDER_IS_BIG_ENDIAN | 
 | 80 | #else | 
 | 81 | # define BYTEORDER_IS_LITTLE_ENDIAN | 
 | 82 | #endif | 
 | 83 |  | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ | 
 | 85 |  | 
 | 86 |    The globals are initialized by the _PyUnicode_Init() API and should | 
 | 87 |    not be used before calling that API. | 
 | 88 |  | 
 | 89 | */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 |  | 
 | 92 | #ifdef __cplusplus | 
 | 93 | extern "C" { | 
 | 94 | #endif | 
 | 95 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 96 | #ifdef Py_DEBUG | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 97 | #  define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 98 | #else | 
 | 99 | #  define _PyUnicode_CHECK(op) PyUnicode_Check(op) | 
 | 100 | #endif | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 101 |  | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 102 | #define _PyUnicode_UTF8(op)                             \ | 
 | 103 |     (((PyCompactUnicodeObject*)(op))->utf8) | 
 | 104 | #define PyUnicode_UTF8(op)                              \ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 105 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 106 |      assert(PyUnicode_IS_READY(op)),                    \ | 
 | 107 |      PyUnicode_IS_COMPACT_ASCII(op) ?                   \ | 
 | 108 |          ((char*)((PyASCIIObject*)(op) + 1)) :          \ | 
 | 109 |          _PyUnicode_UTF8(op)) | 
| Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 110 | #define _PyUnicode_UTF8_LENGTH(op)                      \ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 111 |     (((PyCompactUnicodeObject*)(op))->utf8_length) | 
 | 112 | #define PyUnicode_UTF8_LENGTH(op)                       \ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 113 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 114 |      assert(PyUnicode_IS_READY(op)),                    \ | 
 | 115 |      PyUnicode_IS_COMPACT_ASCII(op) ?                   \ | 
 | 116 |          ((PyASCIIObject*)(op))->length :               \ | 
 | 117 |          _PyUnicode_UTF8_LENGTH(op)) | 
| Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 118 | #define _PyUnicode_WSTR(op)                             \ | 
 | 119 |     (((PyASCIIObject*)(op))->wstr) | 
 | 120 | #define _PyUnicode_WSTR_LENGTH(op)                      \ | 
 | 121 |     (((PyCompactUnicodeObject*)(op))->wstr_length) | 
 | 122 | #define _PyUnicode_LENGTH(op)                           \ | 
 | 123 |     (((PyASCIIObject *)(op))->length) | 
 | 124 | #define _PyUnicode_STATE(op)                            \ | 
 | 125 |     (((PyASCIIObject *)(op))->state) | 
 | 126 | #define _PyUnicode_HASH(op)                             \ | 
 | 127 |     (((PyASCIIObject *)(op))->hash) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 128 | #define _PyUnicode_KIND(op)                             \ | 
 | 129 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 130 |      ((PyASCIIObject *)(op))->state.kind) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 131 | #define _PyUnicode_GET_LENGTH(op)                       \ | 
 | 132 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 133 |      ((PyASCIIObject *)(op))->length) | 
| Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 134 | #define _PyUnicode_DATA_ANY(op)                         \ | 
 | 135 |     (((PyUnicodeObject*)(op))->data.any) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 136 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 137 | #undef PyUnicode_READY | 
 | 138 | #define PyUnicode_READY(op)                             \ | 
 | 139 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
 | 140 |      (PyUnicode_IS_READY(op) ?                          \ | 
| Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 141 |       0 :                                               \ | 
 | 142 |       _PyUnicode_Ready((PyObject *)(op)))) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 143 |  | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 144 | #define _PyUnicode_READY_REPLACE(p_obj)                 \ | 
 | 145 |     (assert(_PyUnicode_CHECK(*p_obj)),                  \ | 
 | 146 |      (PyUnicode_IS_READY(*p_obj) ?                      \ | 
 | 147 |       0 : _PyUnicode_ReadyReplace((PyObject **)(p_obj)))) | 
 | 148 |  | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 149 | #define _PyUnicode_SHARE_UTF8(op)                       \ | 
 | 150 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
 | 151 |      assert(!PyUnicode_IS_COMPACT_ASCII(op)),           \ | 
 | 152 |      (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) | 
 | 153 | #define _PyUnicode_SHARE_WSTR(op)                       \ | 
 | 154 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
 | 155 |      (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) | 
 | 156 |  | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 157 | /* true if the Unicode object has an allocated UTF-8 memory block | 
 | 158 |    (not shared with other data) */ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 159 | #define _PyUnicode_HAS_UTF8_MEMORY(op)                  \ | 
 | 160 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
 | 161 |      (!PyUnicode_IS_COMPACT_ASCII(op)                   \ | 
 | 162 |       && _PyUnicode_UTF8(op)                            \ | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 163 |       && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) | 
 | 164 |  | 
| Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 165 | /* true if the Unicode object has an allocated wstr memory block | 
 | 166 |    (not shared with other data) */ | 
 | 167 | #define _PyUnicode_HAS_WSTR_MEMORY(op)                  \ | 
 | 168 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
 | 169 |      (_PyUnicode_WSTR(op) &&                            \ | 
 | 170 |       (!PyUnicode_IS_READY(op) ||                       \ | 
 | 171 |        _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) | 
 | 172 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 173 | /* Generic helper macro to convert characters of different types. | 
 | 174 |    from_type and to_type have to be valid type names, begin and end | 
 | 175 |    are pointers to the source characters which should be of type | 
 | 176 |    "from_type *".  to is a pointer of type "to_type *" and points to the | 
 | 177 |    buffer where the result characters are written to. */ | 
 | 178 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ | 
 | 179 |     do {                                                \ | 
 | 180 |         const from_type *iter_; to_type *to_;           \ | 
 | 181 |         for (iter_ = (begin), to_ = (to_type *)(to);    \ | 
 | 182 |              iter_ < (end);                             \ | 
 | 183 |              ++iter_, ++to_) {                          \ | 
 | 184 |             *to_ = (to_type)*iter_;                     \ | 
 | 185 |         }                                               \ | 
 | 186 |     } while (0) | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 187 |  | 
| Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 188 | /* The Unicode string has been modified: reset the hash */ | 
 | 189 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) | 
 | 190 |  | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 191 | /* This dictionary holds all interned unicode strings.  Note that references | 
 | 192 |    to strings in this dictionary are *not* counted in the string's ob_refcnt. | 
 | 193 |    When the interned string reaches a refcnt of 0 the string deallocation | 
 | 194 |    function will delete the reference from this dictionary. | 
 | 195 |  | 
 | 196 |    Another way to look at this is that to say that the actual reference | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 197 |    count of a string is:  s->ob_refcnt + (s->state ? 2 : 0) | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 198 | */ | 
 | 199 | static PyObject *interned; | 
 | 200 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 201 | /* The empty Unicode object is shared to improve performance. */ | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 202 | static PyObject *unicode_empty; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 203 |  | 
 | 204 | /* Single character Unicode strings in the Latin-1 range are being | 
 | 205 |    shared as well. */ | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 206 | static PyObject *unicode_latin1[256]; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 207 |  | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 208 | /* Fast detection of the most frequent whitespace characters */ | 
 | 209 | const unsigned char _Py_ascii_whitespace[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 211 | /*     case 0x0009: * CHARACTER TABULATION */ | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 212 | /*     case 0x000A: * LINE FEED */ | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 213 | /*     case 0x000B: * LINE TABULATION */ | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 214 | /*     case 0x000C: * FORM FEED */ | 
 | 215 | /*     case 0x000D: * CARRIAGE RETURN */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 216 |     0, 1, 1, 1, 1, 1, 0, 0, | 
 | 217 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 218 | /*     case 0x001C: * FILE SEPARATOR */ | 
 | 219 | /*     case 0x001D: * GROUP SEPARATOR */ | 
 | 220 | /*     case 0x001E: * RECORD SEPARATOR */ | 
 | 221 | /*     case 0x001F: * UNIT SEPARATOR */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 222 |     0, 0, 0, 0, 1, 1, 1, 1, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 223 | /*     case 0x0020: * SPACE */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 224 |     1, 0, 0, 0, 0, 0, 0, 0, | 
 | 225 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 226 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 227 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 228 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 229 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 230 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 231 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 232 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 233 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 234 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 235 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 236 |     0, 0, 0, 0, 0, 0, 0, 0 | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 237 | }; | 
 | 238 |  | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 239 | /* forward */ | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 240 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 241 | static PyObject* get_latin1_char(unsigned char ch); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 242 | static void copy_characters( | 
 | 243 |     PyObject *to, Py_ssize_t to_start, | 
 | 244 |     PyObject *from, Py_ssize_t from_start, | 
 | 245 |     Py_ssize_t how_many); | 
| Victor Stinner | c729b8e | 2011-10-06 02:36:59 +0200 | [diff] [blame] | 246 | #ifdef Py_DEBUG | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 247 | static int unicode_is_singleton(PyObject *unicode); | 
| Victor Stinner | c729b8e | 2011-10-06 02:36:59 +0200 | [diff] [blame] | 248 | #endif | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 249 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 250 | static PyObject * | 
 | 251 | unicode_encode_call_errorhandler(const char *errors, | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 252 |        PyObject **errorHandler,const char *encoding, const char *reason, | 
 | 253 |        const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | 
 | 254 |        Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); | 
 | 255 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 256 | static void | 
 | 257 | raise_encode_exception(PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 258 |                        const char *encoding, | 
 | 259 |                        const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 260 |                        Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 261 |                        const char *reason); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 262 |  | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 263 | /* Same for linebreaks */ | 
 | 264 | static unsigned char ascii_linebreak[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 265 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 266 | /*         0x000A, * LINE FEED */ | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 267 | /*         0x000B, * LINE TABULATION */ | 
 | 268 | /*         0x000C, * FORM FEED */ | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 269 | /*         0x000D, * CARRIAGE RETURN */ | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 270 |     0, 0, 1, 1, 1, 1, 0, 0, | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 271 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 272 | /*         0x001C, * FILE SEPARATOR */ | 
 | 273 | /*         0x001D, * GROUP SEPARATOR */ | 
 | 274 | /*         0x001E, * RECORD SEPARATOR */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 275 |     0, 0, 0, 0, 1, 1, 1, 0, | 
 | 276 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 277 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 278 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 279 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 280 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 281 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 282 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 283 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 284 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 285 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 286 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 287 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 288 |     0, 0, 0, 0, 0, 0, 0, 0 | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 289 | }; | 
 | 290 |  | 
| Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 291 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. | 
 | 292 |    This function is kept for backward compatibility with the old API. */ | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 293 | Py_UNICODE | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 294 | PyUnicode_GetMax(void) | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 295 | { | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 296 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 297 |     return 0x10FFFF; | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 298 | #else | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 299 |     /* This is actually an illegal character, so it should | 
 | 300 |        not be passed to unichr. */ | 
 | 301 |     return 0xFFFF; | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 302 | #endif | 
 | 303 | } | 
 | 304 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 305 | #ifdef Py_DEBUG | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 306 | int | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 307 | /* FIXME: use PyObject* type for op */ | 
 | 308 | _PyUnicode_CheckConsistency(void *op, int check_content) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 309 | { | 
 | 310 |     PyASCIIObject *ascii; | 
 | 311 |     unsigned int kind; | 
 | 312 |  | 
 | 313 |     assert(PyUnicode_Check(op)); | 
 | 314 |  | 
 | 315 |     ascii = (PyASCIIObject *)op; | 
 | 316 |     kind = ascii->state.kind; | 
 | 317 |  | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 318 |     if (ascii->state.ascii == 1 && ascii->state.compact == 1) { | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 319 |         assert(kind == PyUnicode_1BYTE_KIND); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 320 |         assert(ascii->state.ready == 1); | 
 | 321 |     } | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 322 |     else { | 
| Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 323 |         PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; | 
| Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 324 |         void *data; | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 325 |  | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 326 |         if (ascii->state.compact == 1) { | 
 | 327 |             data = compact + 1; | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 328 |             assert(kind == PyUnicode_1BYTE_KIND | 
 | 329 |                    || kind == PyUnicode_2BYTE_KIND | 
 | 330 |                    || kind == PyUnicode_4BYTE_KIND); | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 331 |             assert(ascii->state.ascii == 0); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 332 |             assert(ascii->state.ready == 1); | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 333 |             assert (compact->utf8 != data); | 
 | 334 |         } else { | 
 | 335 |             PyUnicodeObject *unicode = (PyUnicodeObject *)op; | 
 | 336 |  | 
 | 337 |             data = unicode->data.any; | 
 | 338 |             if (kind == PyUnicode_WCHAR_KIND) { | 
 | 339 |                 assert(ascii->state.compact == 0); | 
 | 340 |                 assert(ascii->state.ascii == 0); | 
 | 341 |                 assert(ascii->state.ready == 0); | 
 | 342 |                 assert(ascii->wstr != NULL); | 
 | 343 |                 assert(data == NULL); | 
 | 344 |                 assert(compact->utf8 == NULL); | 
 | 345 |                 assert(ascii->state.interned == SSTATE_NOT_INTERNED); | 
 | 346 |             } | 
 | 347 |             else { | 
 | 348 |                 assert(kind == PyUnicode_1BYTE_KIND | 
 | 349 |                        || kind == PyUnicode_2BYTE_KIND | 
 | 350 |                        || kind == PyUnicode_4BYTE_KIND); | 
 | 351 |                 assert(ascii->state.compact == 0); | 
 | 352 |                 assert(ascii->state.ready == 1); | 
 | 353 |                 assert(data != NULL); | 
 | 354 |                 if (ascii->state.ascii) { | 
 | 355 |                     assert (compact->utf8 == data); | 
 | 356 |                     assert (compact->utf8_length == ascii->length); | 
 | 357 |                 } | 
 | 358 |                 else | 
 | 359 |                     assert (compact->utf8 != data); | 
 | 360 |             } | 
 | 361 |         } | 
 | 362 |         if (kind != PyUnicode_WCHAR_KIND) { | 
| Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 363 |             if ( | 
 | 364 | #if SIZEOF_WCHAR_T == 2 | 
 | 365 |                 kind == PyUnicode_2BYTE_KIND | 
 | 366 | #else | 
 | 367 |                 kind == PyUnicode_4BYTE_KIND | 
 | 368 | #endif | 
 | 369 |                ) | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 370 |             { | 
 | 371 |                 assert(ascii->wstr == data); | 
 | 372 |                 assert(compact->wstr_length == ascii->length); | 
 | 373 |             } else | 
 | 374 |                 assert(ascii->wstr != data); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 375 |         } | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 376 |  | 
 | 377 |         if (compact->utf8 == NULL) | 
 | 378 |             assert(compact->utf8_length == 0); | 
 | 379 |         if (ascii->wstr == NULL) | 
 | 380 |             assert(compact->wstr_length == 0); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 381 |     } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 382 |     /* check that the best kind is used */ | 
 | 383 |     if (check_content && kind != PyUnicode_WCHAR_KIND) | 
 | 384 |     { | 
 | 385 |         Py_ssize_t i; | 
 | 386 |         Py_UCS4 maxchar = 0; | 
 | 387 |         void *data = PyUnicode_DATA(ascii); | 
 | 388 |         for (i=0; i < ascii->length; i++) | 
 | 389 |         { | 
 | 390 |             Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 391 |             if (ch > maxchar) | 
 | 392 |                 maxchar = ch; | 
 | 393 |         } | 
 | 394 |         if (kind == PyUnicode_1BYTE_KIND) { | 
 | 395 |             if (ascii->state.ascii == 0) | 
 | 396 |                 assert(maxchar >= 128); | 
 | 397 |             else | 
 | 398 |                 assert(maxchar < 128); | 
 | 399 |         } | 
 | 400 |         else if (kind == PyUnicode_2BYTE_KIND) | 
 | 401 |             assert(maxchar >= 0x100); | 
 | 402 |         else | 
 | 403 |             assert(maxchar >= 0x10000); | 
 | 404 |     } | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 405 |     if (check_content && !unicode_is_singleton((PyObject*)ascii)) | 
 | 406 |         assert(ascii->hash == -1); | 
| Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 407 |     return 1; | 
 | 408 | } | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 409 | #endif | 
 | 410 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 411 | /* --- Bloom Filters ----------------------------------------------------- */ | 
 | 412 |  | 
 | 413 | /* stuff to implement simple "bloom filters" for Unicode characters. | 
 | 414 |    to keep things simple, we use a single bitmask, using the least 5 | 
 | 415 |    bits from each unicode characters as the bit index. */ | 
 | 416 |  | 
 | 417 | /* the linebreak mask is set up by Unicode_Init below */ | 
 | 418 |  | 
| Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 419 | #if LONG_BIT >= 128 | 
 | 420 | #define BLOOM_WIDTH 128 | 
 | 421 | #elif LONG_BIT >= 64 | 
 | 422 | #define BLOOM_WIDTH 64 | 
 | 423 | #elif LONG_BIT >= 32 | 
 | 424 | #define BLOOM_WIDTH 32 | 
 | 425 | #else | 
 | 426 | #error "LONG_BIT is smaller than 32" | 
 | 427 | #endif | 
 | 428 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 429 | #define BLOOM_MASK unsigned long | 
 | 430 |  | 
 | 431 | static BLOOM_MASK bloom_linebreak; | 
 | 432 |  | 
| Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 433 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) | 
 | 434 | #define BLOOM(mask, ch)     ((mask &  (1UL << ((ch) & (BLOOM_WIDTH - 1))))) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 435 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 436 | #define BLOOM_LINEBREAK(ch)                                             \ | 
 | 437 |     ((ch) < 128U ? ascii_linebreak[(ch)] :                              \ | 
 | 438 |      (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 439 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 440 | Py_LOCAL_INLINE(BLOOM_MASK) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 441 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 442 | { | 
 | 443 |     /* calculate simple bloom-style bitmask for a given unicode string */ | 
 | 444 |  | 
| Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 445 |     BLOOM_MASK mask; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 446 |     Py_ssize_t i; | 
 | 447 |  | 
 | 448 |     mask = 0; | 
 | 449 |     for (i = 0; i < len; i++) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 450 |         BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 451 |  | 
 | 452 |     return mask; | 
 | 453 | } | 
 | 454 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 455 | #define BLOOM_MEMBER(mask, chr, str) \ | 
 | 456 |     (BLOOM(mask, chr) \ | 
 | 457 |      && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 458 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 459 | /* --- Unicode Object ----------------------------------------------------- */ | 
 | 460 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 461 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 462 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 463 |  | 
 | 464 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, | 
 | 465 |                                  Py_ssize_t size, Py_UCS4 ch, | 
 | 466 |                                  int direction) | 
 | 467 | { | 
 | 468 |     /* like wcschr, but doesn't stop at NULL characters */ | 
 | 469 |     Py_ssize_t i; | 
 | 470 |     if (direction == 1) { | 
 | 471 |         for(i = 0; i < size; i++) | 
 | 472 |             if (PyUnicode_READ(kind, s, i) == ch) | 
 | 473 |                 return (char*)s + PyUnicode_KIND_SIZE(kind, i); | 
 | 474 |     } | 
 | 475 |     else { | 
 | 476 |         for(i = size-1; i >= 0; i--) | 
 | 477 |             if (PyUnicode_READ(kind, s, i) == ch) | 
 | 478 |                 return (char*)s + PyUnicode_KIND_SIZE(kind, i); | 
 | 479 |     } | 
 | 480 |     return NULL; | 
 | 481 | } | 
 | 482 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 483 | static PyObject* | 
 | 484 | resize_compact(PyObject *unicode, Py_ssize_t length) | 
 | 485 | { | 
 | 486 |     Py_ssize_t char_size; | 
 | 487 |     Py_ssize_t struct_size; | 
 | 488 |     Py_ssize_t new_size; | 
 | 489 |     int share_wstr; | 
 | 490 |  | 
 | 491 |     assert(PyUnicode_IS_READY(unicode)); | 
 | 492 |     char_size = PyUnicode_CHARACTER_SIZE(unicode); | 
 | 493 |     if (PyUnicode_IS_COMPACT_ASCII(unicode)) | 
 | 494 |         struct_size = sizeof(PyASCIIObject); | 
 | 495 |     else | 
 | 496 |         struct_size = sizeof(PyCompactUnicodeObject); | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 497 |     share_wstr = _PyUnicode_SHARE_WSTR(unicode); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 498 |  | 
 | 499 |     _Py_DEC_REFTOTAL; | 
 | 500 |     _Py_ForgetReference(unicode); | 
 | 501 |  | 
 | 502 |     if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { | 
 | 503 |         PyErr_NoMemory(); | 
 | 504 |         return NULL; | 
 | 505 |     } | 
 | 506 |     new_size = (struct_size + (length + 1) * char_size); | 
 | 507 |  | 
 | 508 |     unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); | 
 | 509 |     if (unicode == NULL) { | 
 | 510 |         PyObject_Del(unicode); | 
 | 511 |         PyErr_NoMemory(); | 
 | 512 |         return NULL; | 
 | 513 |     } | 
 | 514 |     _Py_NewReference(unicode); | 
 | 515 |     _PyUnicode_LENGTH(unicode) = length; | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 516 |     if (share_wstr) { | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 517 |         _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 518 |         if (!PyUnicode_IS_COMPACT_ASCII(unicode)) | 
 | 519 |             _PyUnicode_WSTR_LENGTH(unicode) = length; | 
 | 520 |     } | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 521 |     PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), | 
 | 522 |                     length, 0); | 
 | 523 |     return unicode; | 
 | 524 | } | 
 | 525 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 526 | static int | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 527 | resize_inplace(PyUnicodeObject *unicode, Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 528 | { | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 529 |     wchar_t *wstr; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 530 |     assert(!PyUnicode_IS_COMPACT(unicode)); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 531 |     assert(Py_REFCNT(unicode) == 1); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 532 |  | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 533 |     _PyUnicode_DIRTY(unicode); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 534 |  | 
 | 535 |     if (PyUnicode_IS_READY(unicode)) { | 
 | 536 |         Py_ssize_t char_size; | 
 | 537 |         Py_ssize_t new_size; | 
| Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 538 |         int share_wstr, share_utf8; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 539 |         void *data; | 
 | 540 |  | 
 | 541 |         data = _PyUnicode_DATA_ANY(unicode); | 
 | 542 |         assert(data != NULL); | 
 | 543 |         char_size = PyUnicode_CHARACTER_SIZE(unicode); | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 544 |         share_wstr = _PyUnicode_SHARE_WSTR(unicode); | 
 | 545 |         share_utf8 = _PyUnicode_SHARE_UTF8(unicode); | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 546 |         if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) | 
 | 547 |         { | 
 | 548 |             PyObject_DEL(_PyUnicode_UTF8(unicode)); | 
 | 549 |             _PyUnicode_UTF8(unicode) = NULL; | 
 | 550 |             _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
 | 551 |         } | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 552 |  | 
 | 553 |         if (length > (PY_SSIZE_T_MAX / char_size - 1)) { | 
 | 554 |             PyErr_NoMemory(); | 
 | 555 |             return -1; | 
 | 556 |         } | 
 | 557 |         new_size = (length + 1) * char_size; | 
 | 558 |  | 
 | 559 |         data = (PyObject *)PyObject_REALLOC(data, new_size); | 
 | 560 |         if (data == NULL) { | 
 | 561 |             PyErr_NoMemory(); | 
 | 562 |             return -1; | 
 | 563 |         } | 
 | 564 |         _PyUnicode_DATA_ANY(unicode) = data; | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 565 |         if (share_wstr) { | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 566 |             _PyUnicode_WSTR(unicode) = data; | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 567 |             _PyUnicode_WSTR_LENGTH(unicode) = length; | 
 | 568 |         } | 
 | 569 |         if (share_utf8) { | 
| Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 570 |             _PyUnicode_UTF8(unicode) = data; | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 571 |             _PyUnicode_UTF8_LENGTH(unicode) = length; | 
 | 572 |         } | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 573 |         _PyUnicode_LENGTH(unicode) = length; | 
 | 574 |         PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 575 |         if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 576 |             assert(_PyUnicode_CheckConsistency(unicode, 0)); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 577 |             return 0; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 578 |         } | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 579 |     } | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 580 |     assert(_PyUnicode_WSTR(unicode) != NULL); | 
 | 581 |  | 
 | 582 |     /* check for integer overflow */ | 
 | 583 |     if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { | 
 | 584 |         PyErr_NoMemory(); | 
 | 585 |         return -1; | 
 | 586 |     } | 
 | 587 |     wstr =  _PyUnicode_WSTR(unicode); | 
 | 588 |     wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); | 
 | 589 |     if (!wstr) { | 
 | 590 |         PyErr_NoMemory(); | 
 | 591 |         return -1; | 
 | 592 |     } | 
 | 593 |     _PyUnicode_WSTR(unicode) = wstr; | 
 | 594 |     _PyUnicode_WSTR(unicode)[length] = 0; | 
 | 595 |     _PyUnicode_WSTR_LENGTH(unicode) = length; | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 596 |     assert(_PyUnicode_CheckConsistency(unicode, 0)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 597 |     return 0; | 
 | 598 | } | 
 | 599 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 600 | static PyObject* | 
 | 601 | resize_copy(PyObject *unicode, Py_ssize_t length) | 
 | 602 | { | 
 | 603 |     Py_ssize_t copy_length; | 
 | 604 |     if (PyUnicode_IS_COMPACT(unicode)) { | 
 | 605 |         PyObject *copy; | 
 | 606 |         assert(PyUnicode_IS_READY(unicode)); | 
 | 607 |  | 
 | 608 |         copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); | 
 | 609 |         if (copy == NULL) | 
 | 610 |             return NULL; | 
 | 611 |  | 
 | 612 |         copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 613 |         copy_characters(copy, 0, unicode, 0, copy_length); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 614 |         return copy; | 
| Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 615 |     } | 
 | 616 |     else { | 
| Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 617 |         PyUnicodeObject *w; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 618 |         assert(_PyUnicode_WSTR(unicode) != NULL); | 
 | 619 |         assert(_PyUnicode_DATA_ANY(unicode) == NULL); | 
| Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 620 |         w = _PyUnicode_New(length); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 621 |         if (w == NULL) | 
 | 622 |             return NULL; | 
 | 623 |         copy_length = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 624 |         copy_length = Py_MIN(copy_length, length); | 
 | 625 |         Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), | 
 | 626 |                         copy_length); | 
 | 627 |         return (PyObject*)w; | 
 | 628 |     } | 
 | 629 | } | 
 | 630 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 631 | /* We allocate one more byte to make sure the string is | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 632 |    Ux0000 terminated; some code (e.g. new_identifier) | 
 | 633 |    relies on that. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 634 |  | 
 | 635 |    XXX This allocator could further be enhanced by assuring that the | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 636 |    free list never reduces its size below 1. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 637 |  | 
 | 638 | */ | 
 | 639 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 640 | #ifdef Py_DEBUG | 
 | 641 | int unicode_old_new_calls = 0; | 
 | 642 | #endif | 
 | 643 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 644 | static PyUnicodeObject * | 
 | 645 | _PyUnicode_New(Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 646 | { | 
 | 647 |     register PyUnicodeObject *unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 648 |     size_t new_size; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 649 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 650 |     /* Optimization for empty strings */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 651 |     if (length == 0 && unicode_empty != NULL) { | 
 | 652 |         Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 653 |         return (PyUnicodeObject*)unicode_empty; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 654 |     } | 
 | 655 |  | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 656 |     /* Ensure we won't overflow the size. */ | 
 | 657 |     if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { | 
 | 658 |         return (PyUnicodeObject *)PyErr_NoMemory(); | 
 | 659 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 660 |     if (length < 0) { | 
 | 661 |         PyErr_SetString(PyExc_SystemError, | 
 | 662 |                         "Negative size passed to _PyUnicode_New"); | 
 | 663 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 664 |     } | 
 | 665 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 666 | #ifdef Py_DEBUG | 
 | 667 |     ++unicode_old_new_calls; | 
 | 668 | #endif | 
 | 669 |  | 
 | 670 |     unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); | 
 | 671 |     if (unicode == NULL) | 
 | 672 |         return NULL; | 
 | 673 |     new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); | 
 | 674 |     _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); | 
 | 675 |     if (!_PyUnicode_WSTR(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 676 |         PyErr_NoMemory(); | 
 | 677 |         goto onError; | 
| Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 678 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 679 |  | 
| Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 680 |     /* Initialize the first element to guard against cases where | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 681 |      * the caller fails before initializing str -- unicode_resize() | 
 | 682 |      * reads str[0], and the Keep-Alive optimization can keep memory | 
 | 683 |      * allocated for str alive across a call to unicode_dealloc(unicode). | 
 | 684 |      * We don't want unicode_resize to read uninitialized memory in | 
 | 685 |      * that case. | 
 | 686 |      */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 687 |     _PyUnicode_WSTR(unicode)[0] = 0; | 
 | 688 |     _PyUnicode_WSTR(unicode)[length] = 0; | 
 | 689 |     _PyUnicode_WSTR_LENGTH(unicode) = length; | 
 | 690 |     _PyUnicode_HASH(unicode) = -1; | 
 | 691 |     _PyUnicode_STATE(unicode).interned = 0; | 
 | 692 |     _PyUnicode_STATE(unicode).kind = 0; | 
 | 693 |     _PyUnicode_STATE(unicode).compact = 0; | 
 | 694 |     _PyUnicode_STATE(unicode).ready = 0; | 
 | 695 |     _PyUnicode_STATE(unicode).ascii = 0; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 696 |     _PyUnicode_DATA_ANY(unicode) = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 697 |     _PyUnicode_LENGTH(unicode) = 0; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 698 |     _PyUnicode_UTF8(unicode) = NULL; | 
 | 699 |     _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 700 |     return unicode; | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 701 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 702 |   onError: | 
| Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 703 |     /* XXX UNREF/NEWREF interface should be more symmetrical */ | 
 | 704 |     _Py_DEC_REFTOTAL; | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 705 |     _Py_ForgetReference((PyObject *)unicode); | 
| Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 706 |     PyObject_Del(unicode); | 
| Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 707 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 708 | } | 
 | 709 |  | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 710 | static const char* | 
 | 711 | unicode_kind_name(PyObject *unicode) | 
 | 712 | { | 
| Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 713 |     /* don't check consistency: unicode_kind_name() is called from | 
 | 714 |        _PyUnicode_Dump() */ | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 715 |     if (!PyUnicode_IS_COMPACT(unicode)) | 
 | 716 |     { | 
 | 717 |         if (!PyUnicode_IS_READY(unicode)) | 
 | 718 |             return "wstr"; | 
 | 719 |         switch(PyUnicode_KIND(unicode)) | 
 | 720 |         { | 
 | 721 |         case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 722 |             if (PyUnicode_IS_ASCII(unicode)) | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 723 |                 return "legacy ascii"; | 
 | 724 |             else | 
 | 725 |                 return "legacy latin1"; | 
 | 726 |         case PyUnicode_2BYTE_KIND: | 
 | 727 |             return "legacy UCS2"; | 
 | 728 |         case PyUnicode_4BYTE_KIND: | 
 | 729 |             return "legacy UCS4"; | 
 | 730 |         default: | 
 | 731 |             return "<legacy invalid kind>"; | 
 | 732 |         } | 
 | 733 |     } | 
 | 734 |     assert(PyUnicode_IS_READY(unicode)); | 
 | 735 |     switch(PyUnicode_KIND(unicode)) | 
 | 736 |     { | 
 | 737 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 738 |         if (PyUnicode_IS_ASCII(unicode)) | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 739 |             return "ascii"; | 
 | 740 |         else | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 741 |             return "latin1"; | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 742 |     case PyUnicode_2BYTE_KIND: | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 743 |         return "UCS2"; | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 744 |     case PyUnicode_4BYTE_KIND: | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 745 |         return "UCS4"; | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 746 |     default: | 
 | 747 |         return "<invalid compact kind>"; | 
 | 748 |     } | 
 | 749 | } | 
 | 750 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 751 | #ifdef Py_DEBUG | 
 | 752 | int unicode_new_new_calls = 0; | 
 | 753 |  | 
 | 754 | /* Functions wrapping macros for use in debugger */ | 
 | 755 | char *_PyUnicode_utf8(void *unicode){ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 756 |     return PyUnicode_UTF8(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 757 | } | 
 | 758 |  | 
 | 759 | void *_PyUnicode_compact_data(void *unicode) { | 
 | 760 |     return _PyUnicode_COMPACT_DATA(unicode); | 
 | 761 | } | 
 | 762 | void *_PyUnicode_data(void *unicode){ | 
 | 763 |     printf("obj %p\n", unicode); | 
 | 764 |     printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); | 
 | 765 |     printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); | 
 | 766 |     printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); | 
 | 767 |     printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); | 
 | 768 |     printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); | 
 | 769 |     return PyUnicode_DATA(unicode); | 
 | 770 | } | 
| Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 771 |  | 
 | 772 | void | 
 | 773 | _PyUnicode_Dump(PyObject *op) | 
 | 774 | { | 
 | 775 |     PyASCIIObject *ascii = (PyASCIIObject *)op; | 
| Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 776 |     PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; | 
 | 777 |     PyUnicodeObject *unicode = (PyUnicodeObject *)op; | 
 | 778 |     void *data; | 
 | 779 |     printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); | 
 | 780 |     if (ascii->state.compact) | 
 | 781 |         data = (compact + 1); | 
 | 782 |     else | 
 | 783 |         data = unicode->data.any; | 
 | 784 |     if (ascii->wstr == data) | 
 | 785 |         printf("shared "); | 
 | 786 |     printf("wstr=%p", ascii->wstr); | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 787 |     if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { | 
| Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 788 |         printf(" (%zu), ", compact->wstr_length); | 
 | 789 |         if (!ascii->state.compact && compact->utf8 == unicode->data.any) | 
 | 790 |             printf("shared "); | 
 | 791 |         printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); | 
| Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 792 |     } | 
| Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 793 |     printf(", data=%p\n", data); | 
| Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 794 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 795 | #endif | 
 | 796 |  | 
 | 797 | PyObject * | 
 | 798 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) | 
 | 799 | { | 
 | 800 |     PyObject *obj; | 
 | 801 |     PyCompactUnicodeObject *unicode; | 
 | 802 |     void *data; | 
 | 803 |     int kind_state; | 
| Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 804 |     int is_sharing, is_ascii; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 805 |     Py_ssize_t char_size; | 
 | 806 |     Py_ssize_t struct_size; | 
 | 807 |  | 
 | 808 |     /* Optimization for empty strings */ | 
 | 809 |     if (size == 0 && unicode_empty != NULL) { | 
 | 810 |         Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 811 |         return unicode_empty; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 812 |     } | 
 | 813 |  | 
 | 814 | #ifdef Py_DEBUG | 
 | 815 |     ++unicode_new_new_calls; | 
 | 816 | #endif | 
 | 817 |  | 
| Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 818 |     is_ascii = 0; | 
 | 819 |     is_sharing = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 820 |     struct_size = sizeof(PyCompactUnicodeObject); | 
 | 821 |     if (maxchar < 128) { | 
 | 822 |         kind_state = PyUnicode_1BYTE_KIND; | 
 | 823 |         char_size = 1; | 
 | 824 |         is_ascii = 1; | 
 | 825 |         struct_size = sizeof(PyASCIIObject); | 
 | 826 |     } | 
 | 827 |     else if (maxchar < 256) { | 
 | 828 |         kind_state = PyUnicode_1BYTE_KIND; | 
 | 829 |         char_size = 1; | 
 | 830 |     } | 
 | 831 |     else if (maxchar < 65536) { | 
 | 832 |         kind_state = PyUnicode_2BYTE_KIND; | 
 | 833 |         char_size = 2; | 
 | 834 |         if (sizeof(wchar_t) == 2) | 
 | 835 |             is_sharing = 1; | 
 | 836 |     } | 
 | 837 |     else { | 
 | 838 |         kind_state = PyUnicode_4BYTE_KIND; | 
 | 839 |         char_size = 4; | 
 | 840 |         if (sizeof(wchar_t) == 4) | 
 | 841 |             is_sharing = 1; | 
 | 842 |     } | 
 | 843 |  | 
 | 844 |     /* Ensure we won't overflow the size. */ | 
 | 845 |     if (size < 0) { | 
 | 846 |         PyErr_SetString(PyExc_SystemError, | 
 | 847 |                         "Negative size passed to PyUnicode_New"); | 
 | 848 |         return NULL; | 
 | 849 |     } | 
 | 850 |     if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) | 
 | 851 |         return PyErr_NoMemory(); | 
 | 852 |  | 
 | 853 |     /* Duplicated allocation code from _PyObject_New() instead of a call to | 
 | 854 |      * PyObject_New() so we are able to allocate space for the object and | 
 | 855 |      * it's data buffer. | 
 | 856 |      */ | 
 | 857 |     obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); | 
 | 858 |     if (obj == NULL) | 
 | 859 |         return PyErr_NoMemory(); | 
 | 860 |     obj = PyObject_INIT(obj, &PyUnicode_Type); | 
 | 861 |     if (obj == NULL) | 
 | 862 |         return NULL; | 
 | 863 |  | 
 | 864 |     unicode = (PyCompactUnicodeObject *)obj; | 
 | 865 |     if (is_ascii) | 
 | 866 |         data = ((PyASCIIObject*)obj) + 1; | 
 | 867 |     else | 
 | 868 |         data = unicode + 1; | 
 | 869 |     _PyUnicode_LENGTH(unicode) = size; | 
 | 870 |     _PyUnicode_HASH(unicode) = -1; | 
 | 871 |     _PyUnicode_STATE(unicode).interned = 0; | 
 | 872 |     _PyUnicode_STATE(unicode).kind = kind_state; | 
 | 873 |     _PyUnicode_STATE(unicode).compact = 1; | 
 | 874 |     _PyUnicode_STATE(unicode).ready = 1; | 
 | 875 |     _PyUnicode_STATE(unicode).ascii = is_ascii; | 
 | 876 |     if (is_ascii) { | 
 | 877 |         ((char*)data)[size] = 0; | 
 | 878 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 879 |     } | 
 | 880 |     else if (kind_state == PyUnicode_1BYTE_KIND) { | 
 | 881 |         ((char*)data)[size] = 0; | 
 | 882 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 883 |         _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 884 |         unicode->utf8 = NULL; | 
| Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 885 |         unicode->utf8_length = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 886 |         } | 
 | 887 |     else { | 
 | 888 |         unicode->utf8 = NULL; | 
| Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 889 |         unicode->utf8_length = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 890 |         if (kind_state == PyUnicode_2BYTE_KIND) | 
 | 891 |             ((Py_UCS2*)data)[size] = 0; | 
 | 892 |         else /* kind_state == PyUnicode_4BYTE_KIND */ | 
 | 893 |             ((Py_UCS4*)data)[size] = 0; | 
 | 894 |         if (is_sharing) { | 
 | 895 |             _PyUnicode_WSTR_LENGTH(unicode) = size; | 
 | 896 |             _PyUnicode_WSTR(unicode) = (wchar_t *)data; | 
 | 897 |         } | 
 | 898 |         else { | 
 | 899 |             _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
 | 900 |             _PyUnicode_WSTR(unicode) = NULL; | 
 | 901 |         } | 
 | 902 |     } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 903 |     assert(_PyUnicode_CheckConsistency(unicode, 0)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 904 |     return obj; | 
 | 905 | } | 
 | 906 |  | 
 | 907 | #if SIZEOF_WCHAR_T == 2 | 
 | 908 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this | 
 | 909 |    will decode surrogate pairs, the other conversions are implemented as macros | 
| Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 910 |    for efficiency. | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 911 |  | 
 | 912 |    This function assumes that unicode can hold one more code point than wstr | 
 | 913 |    characters for a terminating null character. */ | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 914 | static void | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 915 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, | 
 | 916 |                               PyUnicodeObject *unicode) | 
 | 917 | { | 
 | 918 |     const wchar_t *iter; | 
 | 919 |     Py_UCS4 *ucs4_out; | 
 | 920 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 921 |     assert(unicode != NULL); | 
 | 922 |     assert(_PyUnicode_CHECK(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 923 |     assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); | 
 | 924 |     ucs4_out = PyUnicode_4BYTE_DATA(unicode); | 
 | 925 |  | 
 | 926 |     for (iter = begin; iter < end; ) { | 
 | 927 |         assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + | 
 | 928 |                            _PyUnicode_GET_LENGTH(unicode))); | 
 | 929 |         if (*iter >= 0xD800 && *iter <= 0xDBFF | 
 | 930 |             && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) | 
 | 931 |         { | 
 | 932 |             *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; | 
 | 933 |             iter += 2; | 
 | 934 |         } | 
 | 935 |         else { | 
 | 936 |             *ucs4_out++ = *iter; | 
 | 937 |             iter++; | 
 | 938 |         } | 
 | 939 |     } | 
 | 940 |     assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + | 
 | 941 |                         _PyUnicode_GET_LENGTH(unicode))); | 
 | 942 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 943 | } | 
 | 944 | #endif | 
 | 945 |  | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 946 | static int | 
 | 947 | _PyUnicode_Dirty(PyObject *unicode) | 
 | 948 | { | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 949 |     assert(_PyUnicode_CHECK(unicode)); | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 950 |     if (Py_REFCNT(unicode) != 1) { | 
| Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 951 |         PyErr_SetString(PyExc_SystemError, | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 952 |                         "Cannot modify a string having more than 1 reference"); | 
 | 953 |         return -1; | 
 | 954 |     } | 
 | 955 |     _PyUnicode_DIRTY(unicode); | 
 | 956 |     return 0; | 
 | 957 | } | 
 | 958 |  | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 959 | static int | 
 | 960 | _copy_characters(PyObject *to, Py_ssize_t to_start, | 
 | 961 |                  PyObject *from, Py_ssize_t from_start, | 
 | 962 |                  Py_ssize_t how_many, int check_maxchar) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 963 | { | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 964 |     unsigned int from_kind, to_kind; | 
 | 965 |     void *from_data, *to_data; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 966 |     int fast; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 967 |  | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 968 |     assert(PyUnicode_Check(from)); | 
 | 969 |     assert(PyUnicode_Check(to)); | 
 | 970 |     assert(PyUnicode_IS_READY(from)); | 
 | 971 |     assert(PyUnicode_IS_READY(to)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 972 |  | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 973 |     assert(PyUnicode_GET_LENGTH(from) >= how_many); | 
 | 974 |     assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); | 
 | 975 |     assert(0 <= how_many); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 976 |  | 
| Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 977 |     if (how_many == 0) | 
 | 978 |         return 0; | 
 | 979 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 980 |     from_kind = PyUnicode_KIND(from); | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 981 |     from_data = PyUnicode_DATA(from); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 982 |     to_kind = PyUnicode_KIND(to); | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 983 |     to_data = PyUnicode_DATA(to); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 984 |  | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 985 | #ifdef Py_DEBUG | 
 | 986 |     if (!check_maxchar | 
 | 987 |         && (from_kind > to_kind | 
 | 988 |             || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 989 |     { | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 990 |         const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); | 
 | 991 |         Py_UCS4 ch; | 
 | 992 |         Py_ssize_t i; | 
 | 993 |         for (i=0; i < how_many; i++) { | 
 | 994 |             ch = PyUnicode_READ(from_kind, from_data, from_start + i); | 
 | 995 |             assert(ch <= to_maxchar); | 
 | 996 |         } | 
 | 997 |     } | 
 | 998 | #endif | 
 | 999 |     fast = (from_kind == to_kind); | 
 | 1000 |     if (check_maxchar | 
 | 1001 |         && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) | 
 | 1002 |     { | 
 | 1003 |         /* deny latin1 => ascii */ | 
 | 1004 |         fast = 0; | 
 | 1005 |     } | 
 | 1006 |  | 
 | 1007 |     if (fast) { | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1008 |         Py_MEMCPY((char*)to_data | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1009 |                       + PyUnicode_KIND_SIZE(to_kind, to_start), | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1010 |                   (char*)from_data | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1011 |                       + PyUnicode_KIND_SIZE(from_kind, from_start), | 
 | 1012 |                   PyUnicode_KIND_SIZE(to_kind, how_many)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1013 |     } | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1014 |     else if (from_kind == PyUnicode_1BYTE_KIND | 
 | 1015 |              && to_kind == PyUnicode_2BYTE_KIND) | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1016 |     { | 
 | 1017 |         _PyUnicode_CONVERT_BYTES( | 
 | 1018 |             Py_UCS1, Py_UCS2, | 
 | 1019 |             PyUnicode_1BYTE_DATA(from) + from_start, | 
 | 1020 |             PyUnicode_1BYTE_DATA(from) + from_start + how_many, | 
 | 1021 |             PyUnicode_2BYTE_DATA(to) + to_start | 
 | 1022 |             ); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1023 |     } | 
| Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1024 |     else if (from_kind == PyUnicode_1BYTE_KIND | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1025 |              && to_kind == PyUnicode_4BYTE_KIND) | 
 | 1026 |     { | 
 | 1027 |         _PyUnicode_CONVERT_BYTES( | 
 | 1028 |             Py_UCS1, Py_UCS4, | 
 | 1029 |             PyUnicode_1BYTE_DATA(from) + from_start, | 
 | 1030 |             PyUnicode_1BYTE_DATA(from) + from_start + how_many, | 
 | 1031 |             PyUnicode_4BYTE_DATA(to) + to_start | 
 | 1032 |             ); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1033 |     } | 
 | 1034 |     else if (from_kind == PyUnicode_2BYTE_KIND | 
 | 1035 |              && to_kind == PyUnicode_4BYTE_KIND) | 
 | 1036 |     { | 
 | 1037 |         _PyUnicode_CONVERT_BYTES( | 
 | 1038 |             Py_UCS2, Py_UCS4, | 
 | 1039 |             PyUnicode_2BYTE_DATA(from) + from_start, | 
 | 1040 |             PyUnicode_2BYTE_DATA(from) + from_start + how_many, | 
 | 1041 |             PyUnicode_4BYTE_DATA(to) + to_start | 
 | 1042 |             ); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1043 |     } | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1044 |     else { | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1045 |         /* check if max_char(from substring) <= max_char(to) */ | 
 | 1046 |         if (from_kind > to_kind | 
 | 1047 |                 /* latin1 => ascii */ | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1048 |             || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1049 |         { | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1050 |             /* slow path to check for character overflow */ | 
 | 1051 |             const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1052 |             Py_UCS4 ch; | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1053 |             Py_ssize_t i; | 
 | 1054 |  | 
| Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1055 | #ifdef Py_DEBUG | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1056 |             for (i=0; i < how_many; i++) { | 
 | 1057 |                 ch = PyUnicode_READ(from_kind, from_data, from_start + i); | 
| Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1058 |                 assert(ch <= to_maxchar); | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1059 |                 PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); | 
 | 1060 |             } | 
| Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1061 | #else | 
 | 1062 |             if (!check_maxchar) { | 
 | 1063 |                 for (i=0; i < how_many; i++) { | 
 | 1064 |                     ch = PyUnicode_READ(from_kind, from_data, from_start + i); | 
 | 1065 |                     PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); | 
 | 1066 |                 } | 
 | 1067 |             } | 
 | 1068 |             else { | 
 | 1069 |                 for (i=0; i < how_many; i++) { | 
 | 1070 |                     ch = PyUnicode_READ(from_kind, from_data, from_start + i); | 
 | 1071 |                     if (ch > to_maxchar) | 
 | 1072 |                         return 1; | 
 | 1073 |                     PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); | 
 | 1074 |                 } | 
 | 1075 |             } | 
 | 1076 | #endif | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1077 |         } | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1078 |         else { | 
| Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1079 |             assert(0 && "inconsistent state"); | 
 | 1080 |             return 1; | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1081 |         } | 
 | 1082 |     } | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1083 |     return 0; | 
 | 1084 | } | 
 | 1085 |  | 
 | 1086 | static void | 
 | 1087 | copy_characters(PyObject *to, Py_ssize_t to_start, | 
 | 1088 |                        PyObject *from, Py_ssize_t from_start, | 
 | 1089 |                        Py_ssize_t how_many) | 
 | 1090 | { | 
 | 1091 |     (void)_copy_characters(to, to_start, from, from_start, how_many, 0); | 
 | 1092 | } | 
 | 1093 |  | 
 | 1094 | Py_ssize_t | 
 | 1095 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, | 
 | 1096 |                          PyObject *from, Py_ssize_t from_start, | 
 | 1097 |                          Py_ssize_t how_many) | 
 | 1098 | { | 
 | 1099 |     int err; | 
 | 1100 |  | 
 | 1101 |     if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { | 
 | 1102 |         PyErr_BadInternalCall(); | 
 | 1103 |         return -1; | 
 | 1104 |     } | 
 | 1105 |  | 
 | 1106 |     if (PyUnicode_READY(from)) | 
 | 1107 |         return -1; | 
 | 1108 |     if (PyUnicode_READY(to)) | 
 | 1109 |         return -1; | 
 | 1110 |  | 
 | 1111 |     how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); | 
 | 1112 |     if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { | 
 | 1113 |         PyErr_Format(PyExc_SystemError, | 
 | 1114 |                      "Cannot write %zi characters at %zi " | 
 | 1115 |                      "in a string of %zi characters", | 
 | 1116 |                      how_many, to_start, PyUnicode_GET_LENGTH(to)); | 
 | 1117 |         return -1; | 
 | 1118 |     } | 
 | 1119 |  | 
 | 1120 |     if (how_many == 0) | 
 | 1121 |         return 0; | 
 | 1122 |  | 
 | 1123 |     if (_PyUnicode_Dirty(to)) | 
 | 1124 |         return -1; | 
 | 1125 |  | 
 | 1126 |     err = _copy_characters(to, to_start, from, from_start, how_many, 1); | 
 | 1127 |     if (err) { | 
 | 1128 |         PyErr_Format(PyExc_SystemError, | 
 | 1129 |                      "Cannot copy %s characters " | 
 | 1130 |                      "into a string of %s characters", | 
 | 1131 |                      unicode_kind_name(from), | 
 | 1132 |                      unicode_kind_name(to)); | 
 | 1133 |         return -1; | 
 | 1134 |     } | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1135 |     return how_many; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1136 | } | 
 | 1137 |  | 
| Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1138 | /* Find the maximum code point and count the number of surrogate pairs so a | 
 | 1139 |    correct string length can be computed before converting a string to UCS4. | 
 | 1140 |    This function counts single surrogates as a character and not as a pair. | 
 | 1141 |  | 
 | 1142 |    Return 0 on success, or -1 on error. */ | 
 | 1143 | static int | 
 | 1144 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, | 
 | 1145 |                         Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | { | 
 | 1147 |     const wchar_t *iter; | 
 | 1148 |  | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1149 |     assert(num_surrogates != NULL && maxchar != NULL); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 |     *num_surrogates = 0; | 
 | 1151 |     *maxchar = 0; | 
 | 1152 |  | 
 | 1153 |     for (iter = begin; iter < end; ) { | 
| Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1154 |         if (*iter > *maxchar) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1155 |             *maxchar = *iter; | 
| Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1156 | #if SIZEOF_WCHAR_T != 2 | 
 | 1157 |             if (*maxchar >= 0x10000) | 
 | 1158 |                 return 0; | 
 | 1159 | #endif | 
 | 1160 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1161 | #if SIZEOF_WCHAR_T == 2 | 
 | 1162 |         if (*iter >= 0xD800 && *iter <= 0xDBFF | 
 | 1163 |             && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) | 
 | 1164 |         { | 
 | 1165 |             Py_UCS4 surrogate_val; | 
 | 1166 |             surrogate_val = (((iter[0] & 0x3FF)<<10) | 
 | 1167 |                              | (iter[1] & 0x3FF)) + 0x10000; | 
 | 1168 |             ++(*num_surrogates); | 
 | 1169 |             if (surrogate_val > *maxchar) | 
 | 1170 |                 *maxchar = surrogate_val; | 
 | 1171 |             iter += 2; | 
 | 1172 |         } | 
 | 1173 |         else | 
 | 1174 |             iter++; | 
 | 1175 | #else | 
 | 1176 |         iter++; | 
 | 1177 | #endif | 
 | 1178 |     } | 
 | 1179 |     return 0; | 
 | 1180 | } | 
 | 1181 |  | 
 | 1182 | #ifdef Py_DEBUG | 
 | 1183 | int unicode_ready_calls = 0; | 
 | 1184 | #endif | 
 | 1185 |  | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1186 | static int | 
 | 1187 | unicode_ready(PyObject **p_obj, int replace) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1188 | { | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1189 |     PyUnicodeObject *unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1190 |     wchar_t *end; | 
 | 1191 |     Py_UCS4 maxchar = 0; | 
 | 1192 |     Py_ssize_t num_surrogates; | 
 | 1193 | #if SIZEOF_WCHAR_T == 2 | 
 | 1194 |     Py_ssize_t length_wo_surrogates; | 
 | 1195 | #endif | 
 | 1196 |  | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1197 |     assert(p_obj != NULL); | 
 | 1198 |     unicode = (PyUnicodeObject *)*p_obj; | 
 | 1199 |  | 
| Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1200 |     /* _PyUnicode_Ready() is only intended for old-style API usage where | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1201 |        strings were created using _PyObject_New() and where no canonical | 
 | 1202 |        representation (the str field) has been set yet aka strings | 
 | 1203 |        which are not yet ready. */ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1204 |     assert(_PyUnicode_CHECK(unicode)); | 
 | 1205 |     assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1206 |     assert(_PyUnicode_WSTR(unicode) != NULL); | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1207 |     assert(_PyUnicode_DATA_ANY(unicode) == NULL); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1208 |     assert(_PyUnicode_UTF8(unicode) == NULL); | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1209 |     /* Actually, it should neither be interned nor be anything else: */ | 
 | 1210 |     assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1211 |  | 
 | 1212 | #ifdef Py_DEBUG | 
 | 1213 |     ++unicode_ready_calls; | 
 | 1214 | #endif | 
 | 1215 |  | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1216 | #ifdef Py_DEBUG | 
 | 1217 |     assert(!replace || Py_REFCNT(unicode) == 1); | 
 | 1218 | #else | 
 | 1219 |     if (replace && Py_REFCNT(unicode) != 1) | 
 | 1220 |         replace = 0; | 
 | 1221 | #endif | 
 | 1222 |     if (replace) { | 
 | 1223 |         Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 1224 |         wchar_t *wstr = _PyUnicode_WSTR(unicode); | 
 | 1225 |         /* Optimization for empty strings */ | 
 | 1226 |         if (len == 0) { | 
 | 1227 |             Py_INCREF(unicode_empty); | 
 | 1228 |             Py_DECREF(*p_obj); | 
 | 1229 |             *p_obj = unicode_empty; | 
 | 1230 |             return 0; | 
 | 1231 |         } | 
 | 1232 |         if (len == 1 && wstr[0] < 256) { | 
 | 1233 |             PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); | 
 | 1234 |             if (latin1_char == NULL) | 
 | 1235 |                 return -1; | 
 | 1236 |             Py_DECREF(*p_obj); | 
 | 1237 |             *p_obj = latin1_char; | 
 | 1238 |             return 0; | 
 | 1239 |         } | 
 | 1240 |     } | 
 | 1241 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1242 |     end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); | 
| Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1243 |     if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1244 |                                 &maxchar, &num_surrogates) == -1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1245 |         return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1246 |  | 
 | 1247 |     if (maxchar < 256) { | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1248 |         _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); | 
 | 1249 |         if (!_PyUnicode_DATA_ANY(unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1250 |             PyErr_NoMemory(); | 
 | 1251 |             return -1; | 
 | 1252 |         } | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1253 |         _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1254 |                                 _PyUnicode_WSTR(unicode), end, | 
 | 1255 |                                 PyUnicode_1BYTE_DATA(unicode)); | 
 | 1256 |         PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; | 
 | 1257 |         _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 1258 |         _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; | 
 | 1259 |         if (maxchar < 128) { | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1260 |             _PyUnicode_STATE(unicode).ascii = 1; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1261 |             _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1262 |             _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1263 |         } | 
 | 1264 |         else { | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1265 |             _PyUnicode_STATE(unicode).ascii = 0; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1266 |             _PyUnicode_UTF8(unicode) = NULL; | 
 | 1267 |             _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1268 |         } | 
 | 1269 |         PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
 | 1270 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 1271 |         _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
 | 1272 |     } | 
 | 1273 |     /* In this case we might have to convert down from 4-byte native | 
 | 1274 |        wchar_t to 2-byte unicode. */ | 
 | 1275 |     else if (maxchar < 65536) { | 
 | 1276 |         assert(num_surrogates == 0 && | 
 | 1277 |                "FindMaxCharAndNumSurrogatePairs() messed up"); | 
 | 1278 |  | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1279 | #if SIZEOF_WCHAR_T == 2 | 
 | 1280 |         /* We can share representations and are done. */ | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1281 |         _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1282 |         PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; | 
 | 1283 |         _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 1284 |         _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1285 |         _PyUnicode_UTF8(unicode) = NULL; | 
 | 1286 |         _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1287 | #else | 
 | 1288 |         /* sizeof(wchar_t) == 4 */ | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1289 |         _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1290 |             2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1291 |         if (!_PyUnicode_DATA_ANY(unicode)) { | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1292 |             PyErr_NoMemory(); | 
 | 1293 |             return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1294 |         } | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1295 |         _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, | 
 | 1296 |                                 _PyUnicode_WSTR(unicode), end, | 
 | 1297 |                                 PyUnicode_2BYTE_DATA(unicode)); | 
 | 1298 |         PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; | 
 | 1299 |         _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 1300 |         _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1301 |         _PyUnicode_UTF8(unicode) = NULL; | 
 | 1302 |         _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1303 |         PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
 | 1304 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 1305 |         _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
 | 1306 | #endif | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1307 |     } | 
 | 1308 |     /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ | 
 | 1309 |     else { | 
 | 1310 | #if SIZEOF_WCHAR_T == 2 | 
 | 1311 |         /* in case the native representation is 2-bytes, we need to allocate a | 
 | 1312 |            new normalized 4-byte version. */ | 
 | 1313 |         length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1314 |         _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); | 
 | 1315 |         if (!_PyUnicode_DATA_ANY(unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1316 |             PyErr_NoMemory(); | 
 | 1317 |             return -1; | 
 | 1318 |         } | 
 | 1319 |         _PyUnicode_LENGTH(unicode) = length_wo_surrogates; | 
 | 1320 |         _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1321 |         _PyUnicode_UTF8(unicode) = NULL; | 
 | 1322 |         _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1323 |         /* unicode_convert_wchar_to_ucs4() requires a ready string */ | 
 | 1324 |         _PyUnicode_STATE(unicode).ready = 1; | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1325 |         unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1326 |         PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
 | 1327 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 1328 |         _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
 | 1329 | #else | 
 | 1330 |         assert(num_surrogates == 0); | 
 | 1331 |  | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1332 |         _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1333 |         _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1334 |         _PyUnicode_UTF8(unicode) = NULL; | 
 | 1335 |         _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1336 |         _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; | 
 | 1337 | #endif | 
 | 1338 |         PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; | 
 | 1339 |     } | 
 | 1340 |     _PyUnicode_STATE(unicode).ready = 1; | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1341 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1342 |     return 0; | 
 | 1343 | } | 
 | 1344 |  | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1345 | int | 
 | 1346 | _PyUnicode_ReadyReplace(PyObject **op) | 
 | 1347 | { | 
 | 1348 |     return unicode_ready(op, 1); | 
 | 1349 | } | 
 | 1350 |  | 
 | 1351 | int | 
 | 1352 | _PyUnicode_Ready(PyObject *op) | 
 | 1353 | { | 
 | 1354 |     return unicode_ready(&op, 0); | 
 | 1355 | } | 
 | 1356 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1357 | static void | 
 | 1358 | unicode_dealloc(register PyUnicodeObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1359 | { | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1360 |     switch (PyUnicode_CHECK_INTERNED(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1361 |     case SSTATE_NOT_INTERNED: | 
 | 1362 |         break; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1363 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1364 |     case SSTATE_INTERNED_MORTAL: | 
 | 1365 |         /* revive dead object temporarily for DelItem */ | 
 | 1366 |         Py_REFCNT(unicode) = 3; | 
 | 1367 |         if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) | 
 | 1368 |             Py_FatalError( | 
 | 1369 |                 "deletion of interned string failed"); | 
 | 1370 |         break; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1371 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1372 |     case SSTATE_INTERNED_IMMORTAL: | 
 | 1373 |         Py_FatalError("Immortal interned string died."); | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1374 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1375 |     default: | 
 | 1376 |         Py_FatalError("Inconsistent interned string state."); | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1377 |     } | 
 | 1378 |  | 
| Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1379 |     if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1380 |         PyObject_DEL(_PyUnicode_WSTR(unicode)); | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1381 |     if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1382 |         PyObject_DEL(_PyUnicode_UTF8(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1383 |  | 
 | 1384 |     if (PyUnicode_IS_COMPACT(unicode)) { | 
 | 1385 |         Py_TYPE(unicode)->tp_free((PyObject *)unicode); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1386 |     } | 
 | 1387 |     else { | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1388 |         if (_PyUnicode_DATA_ANY(unicode)) | 
 | 1389 |             PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1390 |         Py_TYPE(unicode)->tp_free((PyObject *)unicode); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1391 |     } | 
 | 1392 | } | 
 | 1393 |  | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1394 | #ifdef Py_DEBUG | 
 | 1395 | static int | 
 | 1396 | unicode_is_singleton(PyObject *unicode) | 
 | 1397 | { | 
 | 1398 |     PyASCIIObject *ascii = (PyASCIIObject *)unicode; | 
 | 1399 |     if (unicode == unicode_empty) | 
 | 1400 |         return 1; | 
 | 1401 |     if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) | 
 | 1402 |     { | 
 | 1403 |         Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); | 
 | 1404 |         if (ch < 256 && unicode_latin1[ch] == unicode) | 
 | 1405 |             return 1; | 
 | 1406 |     } | 
 | 1407 |     return 0; | 
 | 1408 | } | 
 | 1409 | #endif | 
 | 1410 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1411 | static int | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1412 | unicode_resizable(PyObject *unicode) | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1413 | { | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1414 |     if (Py_REFCNT(unicode) != 1) | 
 | 1415 |         return 0; | 
 | 1416 |     if (PyUnicode_CHECK_INTERNED(unicode)) | 
 | 1417 |         return 0; | 
| Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1418 | #ifdef Py_DEBUG | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1419 |     /* singleton refcount is greater than 1 */ | 
 | 1420 |     assert(!unicode_is_singleton(unicode)); | 
| Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1421 | #endif | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1422 |     return 1; | 
 | 1423 | } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1424 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1425 | static int | 
 | 1426 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) | 
 | 1427 | { | 
 | 1428 |     PyObject *unicode; | 
 | 1429 |     Py_ssize_t old_length; | 
 | 1430 |  | 
 | 1431 |     assert(p_unicode != NULL); | 
 | 1432 |     unicode = *p_unicode; | 
 | 1433 |  | 
 | 1434 |     assert(unicode != NULL); | 
 | 1435 |     assert(PyUnicode_Check(unicode)); | 
 | 1436 |     assert(0 <= length); | 
 | 1437 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1438 |     if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1439 |         old_length = PyUnicode_WSTR_LENGTH(unicode); | 
 | 1440 |     else | 
 | 1441 |         old_length = PyUnicode_GET_LENGTH(unicode); | 
 | 1442 |     if (old_length == length) | 
 | 1443 |         return 0; | 
 | 1444 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1445 |     if (!unicode_resizable(unicode)) { | 
 | 1446 |         PyObject *copy = resize_copy(unicode, length); | 
 | 1447 |         if (copy == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1448 |             return -1; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1449 |         Py_DECREF(*p_unicode); | 
 | 1450 |         *p_unicode = copy; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1451 |         return 0; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1452 |     } | 
 | 1453 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1454 |     if (PyUnicode_IS_COMPACT(unicode)) { | 
 | 1455 |         *p_unicode = resize_compact(unicode, length); | 
 | 1456 |         if (*p_unicode == NULL) | 
 | 1457 |             return -1; | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1458 |         assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1459 |         return 0; | 
| Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1460 |     } | 
 | 1461 |     return resize_inplace((PyUnicodeObject*)unicode, length); | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1462 | } | 
 | 1463 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1464 | int | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1465 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) | 
| Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1466 | { | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1467 |     PyObject *unicode; | 
 | 1468 |     if (p_unicode == NULL) { | 
 | 1469 |         PyErr_BadInternalCall(); | 
 | 1470 |         return -1; | 
 | 1471 |     } | 
 | 1472 |     unicode = *p_unicode; | 
 | 1473 |     if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 | 
 | 1474 |         || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) | 
 | 1475 |     { | 
 | 1476 |         PyErr_BadInternalCall(); | 
 | 1477 |         return -1; | 
 | 1478 |     } | 
 | 1479 |     return unicode_resize(p_unicode, length); | 
| Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1480 | } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1481 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1482 | static PyObject* | 
 | 1483 | get_latin1_char(unsigned char ch) | 
 | 1484 | { | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1485 |     PyObject *unicode = unicode_latin1[ch]; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1486 |     if (!unicode) { | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1487 |         unicode = PyUnicode_New(1, ch); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1488 |         if (!unicode) | 
 | 1489 |             return NULL; | 
 | 1490 |         PyUnicode_1BYTE_DATA(unicode)[0] = ch; | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1491 |         assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1492 |         unicode_latin1[ch] = unicode; | 
 | 1493 |     } | 
 | 1494 |     Py_INCREF(unicode); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1495 |     return unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1496 | } | 
 | 1497 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1498 | PyObject * | 
 | 1499 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1500 | { | 
 | 1501 |     PyUnicodeObject *unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1502 |     Py_UCS4 maxchar = 0; | 
 | 1503 |     Py_ssize_t num_surrogates; | 
 | 1504 |  | 
 | 1505 |     if (u == NULL) | 
 | 1506 |         return (PyObject*)_PyUnicode_New(size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1507 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1508 |     /* If the Unicode data is known at construction time, we can apply | 
 | 1509 |        some optimizations which share commonly used objects. */ | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1510 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1511 |     /* Optimization for empty strings */ | 
 | 1512 |     if (size == 0 && unicode_empty != NULL) { | 
 | 1513 |         Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1514 |         return unicode_empty; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1515 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1516 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1517 |     /* Single character Unicode objects in the Latin-1 range are | 
 | 1518 |        shared when using this constructor */ | 
 | 1519 |     if (size == 1 && *u < 256) | 
 | 1520 |         return get_latin1_char((unsigned char)*u); | 
 | 1521 |  | 
 | 1522 |     /* If not empty and not single character, copy the Unicode data | 
 | 1523 |        into the new object */ | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1524 |     if (find_maxchar_surrogates(u, u + size, | 
 | 1525 |                                 &maxchar, &num_surrogates) == -1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1526 |         return NULL; | 
 | 1527 |  | 
 | 1528 |     unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, | 
 | 1529 |                                                 maxchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1530 |     if (!unicode) | 
 | 1531 |         return NULL; | 
 | 1532 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1533 |     switch (PyUnicode_KIND(unicode)) { | 
 | 1534 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1535 |         _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1536 |                                 u, u + size, PyUnicode_1BYTE_DATA(unicode)); | 
 | 1537 |         break; | 
 | 1538 |     case PyUnicode_2BYTE_KIND: | 
 | 1539 | #if Py_UNICODE_SIZE == 2 | 
 | 1540 |         Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); | 
 | 1541 | #else | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1542 |         _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1543 |                                 u, u + size, PyUnicode_2BYTE_DATA(unicode)); | 
 | 1544 | #endif | 
 | 1545 |         break; | 
 | 1546 |     case PyUnicode_4BYTE_KIND: | 
 | 1547 | #if SIZEOF_WCHAR_T == 2 | 
 | 1548 |         /* This is the only case which has to process surrogates, thus | 
 | 1549 |            a simple copy loop is not enough and we need a function. */ | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1550 |         unicode_convert_wchar_to_ucs4(u, u + size, unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1551 | #else | 
 | 1552 |         assert(num_surrogates == 0); | 
 | 1553 |         Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); | 
 | 1554 | #endif | 
 | 1555 |         break; | 
 | 1556 |     default: | 
 | 1557 |         assert(0 && "Impossible state"); | 
 | 1558 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1559 |  | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1560 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1561 |     return (PyObject *)unicode; | 
 | 1562 | } | 
 | 1563 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1564 | PyObject * | 
 | 1565 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1566 | { | 
 | 1567 |     PyUnicodeObject *unicode; | 
| Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1568 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1569 |     if (size < 0) { | 
 | 1570 |         PyErr_SetString(PyExc_SystemError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1571 |                         "Negative size passed to PyUnicode_FromStringAndSize"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1572 |         return NULL; | 
 | 1573 |     } | 
| Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1574 |  | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1575 |     /* If the Unicode data is known at construction time, we can apply | 
| Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1576 |        some optimizations which share commonly used objects. | 
 | 1577 |        Also, this means the input must be UTF-8, so fall back to the | 
 | 1578 |        UTF-8 decoder at the end. */ | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1579 |     if (u != NULL) { | 
 | 1580 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1581 |         /* Optimization for empty strings */ | 
 | 1582 |         if (size == 0 && unicode_empty != NULL) { | 
 | 1583 |             Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1584 |             return unicode_empty; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1585 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1586 |  | 
 | 1587 |         /* Single characters are shared when using this constructor. | 
 | 1588 |            Restrict to ASCII, since the input must be UTF-8. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1589 |         if (size == 1 && Py_CHARMASK(*u) < 128) | 
 | 1590 |             return get_latin1_char(Py_CHARMASK(*u)); | 
| Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1591 |  | 
 | 1592 |         return PyUnicode_DecodeUTF8(u, size, NULL); | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1593 |     } | 
 | 1594 |  | 
| Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1595 |     unicode = _PyUnicode_New(size); | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1596 |     if (!unicode) | 
 | 1597 |         return NULL; | 
 | 1598 |  | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1599 |     return (PyObject *)unicode; | 
 | 1600 | } | 
 | 1601 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1602 | PyObject * | 
 | 1603 | PyUnicode_FromString(const char *u) | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1604 | { | 
 | 1605 |     size_t size = strlen(u); | 
 | 1606 |     if (size > PY_SSIZE_T_MAX) { | 
 | 1607 |         PyErr_SetString(PyExc_OverflowError, "input too long"); | 
 | 1608 |         return NULL; | 
 | 1609 |     } | 
 | 1610 |  | 
 | 1611 |     return PyUnicode_FromStringAndSize(u, size); | 
 | 1612 | } | 
 | 1613 |  | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1614 | static PyObject* | 
| Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1615 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1616 | { | 
| Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1617 |     PyObject *res; | 
 | 1618 | #ifdef Py_DEBUG | 
 | 1619 |     const unsigned char *p; | 
 | 1620 |     const unsigned char *end = s + size; | 
 | 1621 |     for (p=s; p < end; p++) { | 
 | 1622 |         assert(*p < 128); | 
 | 1623 |     } | 
 | 1624 | #endif | 
| Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1625 |     if (size == 1) | 
 | 1626 |         return get_latin1_char(s[0]); | 
| Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1627 |     res = PyUnicode_New(size, 127); | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1628 |     if (!res) | 
 | 1629 |         return NULL; | 
| Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1630 |     memcpy(PyUnicode_1BYTE_DATA(res), s, size); | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1631 |     return res; | 
 | 1632 | } | 
 | 1633 |  | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1634 | static Py_UCS4 | 
 | 1635 | kind_maxchar_limit(unsigned int kind) | 
 | 1636 | { | 
 | 1637 |     switch(kind) { | 
 | 1638 |     case PyUnicode_1BYTE_KIND: | 
 | 1639 |         return 0x80; | 
 | 1640 |     case PyUnicode_2BYTE_KIND: | 
 | 1641 |         return 0x100; | 
 | 1642 |     case PyUnicode_4BYTE_KIND: | 
 | 1643 |         return 0x10000; | 
 | 1644 |     default: | 
 | 1645 |         assert(0 && "invalid kind"); | 
 | 1646 |         return 0x10ffff; | 
 | 1647 |     } | 
 | 1648 | } | 
 | 1649 |  | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1650 | static PyObject* | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1651 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1652 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1653 |     PyObject *res; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1654 |     unsigned char max_char = 127; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1655 |     Py_ssize_t i; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1656 |  | 
 | 1657 |     assert(size >= 0); | 
| Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1658 |     if (size == 1) | 
 | 1659 |         return get_latin1_char(u[0]); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1660 |     for (i = 0; i < size; i++) { | 
 | 1661 |         if (u[i] & 0x80) { | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1662 |             max_char = 255; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1663 |             break; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1664 |         } | 
 | 1665 |     } | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1666 |     res = PyUnicode_New(size, max_char); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1667 |     if (!res) | 
 | 1668 |         return NULL; | 
 | 1669 |     memcpy(PyUnicode_1BYTE_DATA(res), u, size); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1670 |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1671 |     return res; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1672 | } | 
 | 1673 |  | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1674 | static PyObject* | 
 | 1675 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1676 | { | 
 | 1677 |     PyObject *res; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1678 |     Py_UCS2 max_char = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1679 |     Py_ssize_t i; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1680 |  | 
 | 1681 |     assert(size >= 0); | 
| Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1682 |     if (size == 1 && u[0] < 256) | 
 | 1683 |         return get_latin1_char(u[0]); | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1684 |     for (i = 0; i < size; i++) { | 
 | 1685 |         if (u[i] > max_char) { | 
 | 1686 |             max_char = u[i]; | 
 | 1687 |             if (max_char >= 256) | 
 | 1688 |                 break; | 
 | 1689 |         } | 
 | 1690 |     } | 
 | 1691 |     res = PyUnicode_New(size, max_char); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1692 |     if (!res) | 
 | 1693 |         return NULL; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1694 |     if (max_char >= 256) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1695 |         memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); | 
 | 1696 |     else | 
 | 1697 |         for (i = 0; i < size; i++) | 
 | 1698 |             PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1699 |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1700 |     return res; | 
 | 1701 | } | 
 | 1702 |  | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1703 | static PyObject* | 
 | 1704 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1705 | { | 
 | 1706 |     PyObject *res; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1707 |     Py_UCS4 max_char = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1708 |     Py_ssize_t i; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1709 |  | 
 | 1710 |     assert(size >= 0); | 
| Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1711 |     if (size == 1 && u[0] < 256) | 
 | 1712 |         return get_latin1_char(u[0]); | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1713 |     for (i = 0; i < size; i++) { | 
 | 1714 |         if (u[i] > max_char) { | 
 | 1715 |             max_char = u[i]; | 
 | 1716 |             if (max_char >= 0x10000) | 
 | 1717 |                 break; | 
 | 1718 |         } | 
 | 1719 |     } | 
 | 1720 |     res = PyUnicode_New(size, max_char); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1721 |     if (!res) | 
 | 1722 |         return NULL; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1723 |     if (max_char >= 0x10000) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1724 |         memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); | 
 | 1725 |     else { | 
 | 1726 |         int kind = PyUnicode_KIND(res); | 
 | 1727 |         void *data = PyUnicode_DATA(res); | 
 | 1728 |         for (i = 0; i < size; i++) | 
 | 1729 |             PyUnicode_WRITE(kind, data, i, u[i]); | 
 | 1730 |     } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1731 |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1732 |     return res; | 
 | 1733 | } | 
 | 1734 |  | 
 | 1735 | PyObject* | 
 | 1736 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) | 
 | 1737 | { | 
 | 1738 |     switch(kind) { | 
 | 1739 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1740 |         return _PyUnicode_FromUCS1(buffer, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1741 |     case PyUnicode_2BYTE_KIND: | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1742 |         return _PyUnicode_FromUCS2(buffer, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1743 |     case PyUnicode_4BYTE_KIND: | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1744 |         return _PyUnicode_FromUCS4(buffer, size); | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1745 |     default: | 
 | 1746 |         assert(0 && "invalid kind"); | 
 | 1747 |         PyErr_SetString(PyExc_SystemError, "invalid kind"); | 
 | 1748 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1749 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1750 | } | 
 | 1751 |  | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1752 | /* Ensure that a string uses the most efficient storage, if it is not the | 
 | 1753 |    case: create a new string with of the right kind. Write NULL into *p_unicode | 
 | 1754 |    on error. */ | 
 | 1755 | void | 
 | 1756 | unicode_adjust_maxchar(PyObject **p_unicode) | 
 | 1757 | { | 
 | 1758 |     PyObject *unicode, *copy; | 
 | 1759 |     Py_UCS4 max_char; | 
 | 1760 |     Py_ssize_t i, len; | 
 | 1761 |     unsigned int kind; | 
 | 1762 |  | 
 | 1763 |     assert(p_unicode != NULL); | 
 | 1764 |     unicode = *p_unicode; | 
 | 1765 |     assert(PyUnicode_IS_READY(unicode)); | 
 | 1766 |     if (PyUnicode_IS_ASCII(unicode)) | 
 | 1767 |         return; | 
 | 1768 |  | 
 | 1769 |     len = PyUnicode_GET_LENGTH(unicode); | 
 | 1770 |     kind = PyUnicode_KIND(unicode); | 
 | 1771 |     if (kind == PyUnicode_1BYTE_KIND) { | 
 | 1772 |         const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); | 
 | 1773 |         for (i = 0; i < len; i++) { | 
 | 1774 |             if (u[i] & 0x80) | 
 | 1775 |                 return; | 
 | 1776 |         } | 
 | 1777 |         max_char = 127; | 
 | 1778 |     } | 
 | 1779 |     else if (kind == PyUnicode_2BYTE_KIND) { | 
 | 1780 |         const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); | 
 | 1781 |         max_char = 0; | 
 | 1782 |         for (i = 0; i < len; i++) { | 
 | 1783 |             if (u[i] > max_char) { | 
 | 1784 |                 max_char = u[i]; | 
 | 1785 |                 if (max_char >= 256) | 
 | 1786 |                     return; | 
 | 1787 |             } | 
 | 1788 |         } | 
 | 1789 |     } | 
 | 1790 |     else { | 
| Antoine Pitrou | 15a66cf | 2011-10-06 15:25:32 +0200 | [diff] [blame] | 1791 |         const Py_UCS4 *u; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1792 |         assert(kind == PyUnicode_4BYTE_KIND); | 
| Antoine Pitrou | 15a66cf | 2011-10-06 15:25:32 +0200 | [diff] [blame] | 1793 |         u = PyUnicode_4BYTE_DATA(unicode); | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1794 |         max_char = 0; | 
 | 1795 |         for (i = 0; i < len; i++) { | 
 | 1796 |             if (u[i] > max_char) { | 
 | 1797 |                 max_char = u[i]; | 
 | 1798 |                 if (max_char >= 0x10000) | 
 | 1799 |                     return; | 
 | 1800 |             } | 
 | 1801 |         } | 
 | 1802 |     } | 
| Victor Stinner | 200f213 | 2011-10-06 13:27:56 +0200 | [diff] [blame] | 1803 |     assert(max_char < PyUnicode_MAX_CHAR_VALUE(unicode)); | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1804 |     copy = PyUnicode_New(len, max_char); | 
 | 1805 |     copy_characters(copy, 0, unicode, 0, len); | 
 | 1806 |     Py_DECREF(unicode); | 
 | 1807 |     *p_unicode = copy; | 
 | 1808 | } | 
 | 1809 |  | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1810 | PyObject* | 
 | 1811 | PyUnicode_Copy(PyObject *unicode) | 
 | 1812 | { | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1813 |     Py_ssize_t size; | 
 | 1814 |     PyObject *copy; | 
 | 1815 |     void *data; | 
 | 1816 |  | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1817 |     if (!PyUnicode_Check(unicode)) { | 
 | 1818 |         PyErr_BadInternalCall(); | 
 | 1819 |         return NULL; | 
 | 1820 |     } | 
 | 1821 |     if (PyUnicode_READY(unicode)) | 
 | 1822 |         return NULL; | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1823 |  | 
 | 1824 |     size = PyUnicode_GET_LENGTH(unicode); | 
 | 1825 |     copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); | 
 | 1826 |     if (!copy) | 
 | 1827 |         return NULL; | 
 | 1828 |     assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); | 
 | 1829 |  | 
 | 1830 |     data = PyUnicode_DATA(unicode); | 
 | 1831 |     switch (PyUnicode_KIND(unicode)) | 
 | 1832 |     { | 
 | 1833 |     case PyUnicode_1BYTE_KIND: | 
 | 1834 |         memcpy(PyUnicode_1BYTE_DATA(copy), data, size); | 
 | 1835 |         break; | 
 | 1836 |     case PyUnicode_2BYTE_KIND: | 
 | 1837 |         memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); | 
 | 1838 |         break; | 
 | 1839 |     case PyUnicode_4BYTE_KIND: | 
 | 1840 |         memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); | 
 | 1841 |         break; | 
 | 1842 |     default: | 
 | 1843 |         assert(0); | 
 | 1844 |         break; | 
 | 1845 |     } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1846 |     assert(_PyUnicode_CheckConsistency(copy, 1)); | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1847 |     return copy; | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1848 | } | 
 | 1849 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1850 |  | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1851 | /* Widen Unicode objects to larger buffers. Don't write terminating null | 
 | 1852 |    character. Return NULL on error. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1853 |  | 
 | 1854 | void* | 
 | 1855 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) | 
 | 1856 | { | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1857 |     Py_ssize_t len; | 
 | 1858 |     void *result; | 
 | 1859 |     unsigned int skind; | 
 | 1860 |  | 
 | 1861 |     if (PyUnicode_READY(s)) | 
 | 1862 |         return NULL; | 
 | 1863 |  | 
 | 1864 |     len = PyUnicode_GET_LENGTH(s); | 
 | 1865 |     skind = PyUnicode_KIND(s); | 
 | 1866 |     if (skind >= kind) { | 
| Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1867 |         PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1868 |         return NULL; | 
 | 1869 |     } | 
 | 1870 |     switch(kind) { | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1871 |     case PyUnicode_2BYTE_KIND: | 
 | 1872 |         result = PyMem_Malloc(len * sizeof(Py_UCS2)); | 
 | 1873 |         if (!result) | 
 | 1874 |             return PyErr_NoMemory(); | 
 | 1875 |         assert(skind == PyUnicode_1BYTE_KIND); | 
 | 1876 |         _PyUnicode_CONVERT_BYTES( | 
 | 1877 |             Py_UCS1, Py_UCS2, | 
 | 1878 |             PyUnicode_1BYTE_DATA(s), | 
 | 1879 |             PyUnicode_1BYTE_DATA(s) + len, | 
 | 1880 |             result); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1881 |         return result; | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1882 |     case PyUnicode_4BYTE_KIND: | 
 | 1883 |         result = PyMem_Malloc(len * sizeof(Py_UCS4)); | 
 | 1884 |         if (!result) | 
 | 1885 |             return PyErr_NoMemory(); | 
 | 1886 |         if (skind == PyUnicode_2BYTE_KIND) { | 
 | 1887 |             _PyUnicode_CONVERT_BYTES( | 
 | 1888 |                 Py_UCS2, Py_UCS4, | 
 | 1889 |                 PyUnicode_2BYTE_DATA(s), | 
 | 1890 |                 PyUnicode_2BYTE_DATA(s) + len, | 
 | 1891 |                 result); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1892 |         } | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1893 |         else { | 
 | 1894 |             assert(skind == PyUnicode_1BYTE_KIND); | 
 | 1895 |             _PyUnicode_CONVERT_BYTES( | 
 | 1896 |                 Py_UCS1, Py_UCS4, | 
 | 1897 |                 PyUnicode_1BYTE_DATA(s), | 
 | 1898 |                 PyUnicode_1BYTE_DATA(s) + len, | 
 | 1899 |                 result); | 
 | 1900 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1901 |         return result; | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1902 |     default: | 
 | 1903 |         break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1904 |     } | 
| Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1905 |     PyErr_SetString(PyExc_SystemError, "invalid kind"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1906 |     return NULL; | 
 | 1907 | } | 
 | 1908 |  | 
 | 1909 | static Py_UCS4* | 
 | 1910 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, | 
 | 1911 |         int copy_null) | 
 | 1912 | { | 
 | 1913 |     int kind; | 
 | 1914 |     void *data; | 
 | 1915 |     Py_ssize_t len, targetlen; | 
 | 1916 |     if (PyUnicode_READY(string) == -1) | 
 | 1917 |         return NULL; | 
 | 1918 |     kind = PyUnicode_KIND(string); | 
 | 1919 |     data = PyUnicode_DATA(string); | 
 | 1920 |     len = PyUnicode_GET_LENGTH(string); | 
 | 1921 |     targetlen = len; | 
 | 1922 |     if (copy_null) | 
 | 1923 |         targetlen++; | 
 | 1924 |     if (!target) { | 
 | 1925 |         if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { | 
 | 1926 |             PyErr_NoMemory(); | 
 | 1927 |             return NULL; | 
 | 1928 |         } | 
 | 1929 |         target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); | 
 | 1930 |         if (!target) { | 
 | 1931 |             PyErr_NoMemory(); | 
 | 1932 |             return NULL; | 
 | 1933 |         } | 
 | 1934 |     } | 
 | 1935 |     else { | 
 | 1936 |         if (targetsize < targetlen) { | 
 | 1937 |             PyErr_Format(PyExc_SystemError, | 
 | 1938 |                          "string is longer than the buffer"); | 
 | 1939 |             if (copy_null && 0 < targetsize) | 
 | 1940 |                 target[0] = 0; | 
 | 1941 |             return NULL; | 
 | 1942 |         } | 
 | 1943 |     } | 
 | 1944 |     if (kind != PyUnicode_4BYTE_KIND) { | 
 | 1945 |         Py_ssize_t i; | 
 | 1946 |         for (i = 0; i < len; i++) | 
 | 1947 |             target[i] = PyUnicode_READ(kind, data, i); | 
 | 1948 |     } | 
 | 1949 |     else | 
 | 1950 |         Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); | 
 | 1951 |     if (copy_null) | 
 | 1952 |         target[len] = 0; | 
 | 1953 |     return target; | 
 | 1954 | } | 
 | 1955 |  | 
 | 1956 | Py_UCS4* | 
 | 1957 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, | 
 | 1958 |                  int copy_null) | 
 | 1959 | { | 
 | 1960 |     if (target == NULL || targetsize < 1) { | 
 | 1961 |         PyErr_BadInternalCall(); | 
 | 1962 |         return NULL; | 
 | 1963 |     } | 
 | 1964 |     return as_ucs4(string, target, targetsize, copy_null); | 
 | 1965 | } | 
 | 1966 |  | 
 | 1967 | Py_UCS4* | 
 | 1968 | PyUnicode_AsUCS4Copy(PyObject *string) | 
 | 1969 | { | 
 | 1970 |     return as_ucs4(string, NULL, 0, 1); | 
 | 1971 | } | 
 | 1972 |  | 
 | 1973 | #ifdef HAVE_WCHAR_H | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1974 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1975 | PyObject * | 
 | 1976 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1977 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1978 |     if (w == NULL) { | 
| Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1979 |         if (size == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1980 |             return PyUnicode_New(0, 0); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1981 |         PyErr_BadInternalCall(); | 
 | 1982 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1983 |     } | 
 | 1984 |  | 
| Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1985 |     if (size == -1) { | 
 | 1986 |         size = wcslen(w); | 
 | 1987 |     } | 
 | 1988 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1989 |     return PyUnicode_FromUnicode(w, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1990 | } | 
 | 1991 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | #endif /* HAVE_WCHAR_H */ | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1993 |  | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1994 | static void | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1995 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, | 
 | 1996 |         int zeropad, int width, int precision, char c) | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1997 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1998 |     *fmt++ = '%'; | 
 | 1999 |     if (width) { | 
 | 2000 |         if (zeropad) | 
 | 2001 |             *fmt++ = '0'; | 
 | 2002 |         fmt += sprintf(fmt, "%d", width); | 
 | 2003 |     } | 
 | 2004 |     if (precision) | 
 | 2005 |         fmt += sprintf(fmt, ".%d", precision); | 
 | 2006 |     if (longflag) | 
 | 2007 |         *fmt++ = 'l'; | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2008 |     else if (longlongflag) { | 
 | 2009 |         /* longlongflag should only ever be nonzero on machines with | 
 | 2010 |            HAVE_LONG_LONG defined */ | 
 | 2011 | #ifdef HAVE_LONG_LONG | 
 | 2012 |         char *f = PY_FORMAT_LONG_LONG; | 
 | 2013 |         while (*f) | 
 | 2014 |             *fmt++ = *f++; | 
 | 2015 | #else | 
 | 2016 |         /* we shouldn't ever get here */ | 
 | 2017 |         assert(0); | 
 | 2018 |         *fmt++ = 'l'; | 
 | 2019 | #endif | 
 | 2020 |     } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2021 |     else if (size_tflag) { | 
 | 2022 |         char *f = PY_FORMAT_SIZE_T; | 
 | 2023 |         while (*f) | 
 | 2024 |             *fmt++ = *f++; | 
 | 2025 |     } | 
 | 2026 |     *fmt++ = c; | 
 | 2027 |     *fmt = '\0'; | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2028 | } | 
 | 2029 |  | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2030 | /* helper for PyUnicode_FromFormatV() */ | 
 | 2031 |  | 
 | 2032 | static const char* | 
 | 2033 | parse_format_flags(const char *f, | 
 | 2034 |                    int *p_width, int *p_precision, | 
 | 2035 |                    int *p_longflag, int *p_longlongflag, int *p_size_tflag) | 
 | 2036 | { | 
 | 2037 |     int width, precision, longflag, longlongflag, size_tflag; | 
 | 2038 |  | 
 | 2039 |     /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ | 
 | 2040 |     f++; | 
 | 2041 |     width = 0; | 
 | 2042 |     while (Py_ISDIGIT((unsigned)*f)) | 
 | 2043 |         width = (width*10) + *f++ - '0'; | 
 | 2044 |     precision = 0; | 
 | 2045 |     if (*f == '.') { | 
 | 2046 |         f++; | 
 | 2047 |         while (Py_ISDIGIT((unsigned)*f)) | 
 | 2048 |             precision = (precision*10) + *f++ - '0'; | 
 | 2049 |         if (*f == '%') { | 
 | 2050 |             /* "%.3%s" => f points to "3" */ | 
 | 2051 |             f--; | 
 | 2052 |         } | 
 | 2053 |     } | 
 | 2054 |     if (*f == '\0') { | 
 | 2055 |         /* bogus format "%.1" => go backward, f points to "1" */ | 
 | 2056 |         f--; | 
 | 2057 |     } | 
 | 2058 |     if (p_width != NULL) | 
 | 2059 |         *p_width = width; | 
 | 2060 |     if (p_precision != NULL) | 
 | 2061 |         *p_precision = precision; | 
 | 2062 |  | 
 | 2063 |     /* Handle %ld, %lu, %lld and %llu. */ | 
 | 2064 |     longflag = 0; | 
 | 2065 |     longlongflag = 0; | 
| Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2066 |     size_tflag = 0; | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2067 |  | 
 | 2068 |     if (*f == 'l') { | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2069 |         if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2070 |             longflag = 1; | 
 | 2071 |             ++f; | 
 | 2072 |         } | 
 | 2073 | #ifdef HAVE_LONG_LONG | 
 | 2074 |         else if (f[1] == 'l' && | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2075 |                  (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2076 |             longlongflag = 1; | 
 | 2077 |             f += 2; | 
 | 2078 |         } | 
 | 2079 | #endif | 
 | 2080 |     } | 
 | 2081 |     /* handle the size_t flag. */ | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2082 |     else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u' || f[1] == 'i')) { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2083 |         size_tflag = 1; | 
 | 2084 |         ++f; | 
 | 2085 |     } | 
 | 2086 |     if (p_longflag != NULL) | 
 | 2087 |         *p_longflag = longflag; | 
 | 2088 |     if (p_longlongflag != NULL) | 
 | 2089 |         *p_longlongflag = longlongflag; | 
 | 2090 |     if (p_size_tflag != NULL) | 
 | 2091 |         *p_size_tflag = size_tflag; | 
 | 2092 |     return f; | 
 | 2093 | } | 
 | 2094 |  | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2095 | /* maximum number of characters required for output of %ld.  21 characters | 
 | 2096 |    allows for 64-bit integers (in decimal) and an optional sign. */ | 
 | 2097 | #define MAX_LONG_CHARS 21 | 
 | 2098 | /* maximum number of characters required for output of %lld. | 
 | 2099 |    We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, | 
 | 2100 |    plus 1 for the sign.  53/22 is an upper bound for log10(256). */ | 
 | 2101 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) | 
 | 2102 |  | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2103 | PyObject * | 
 | 2104 | PyUnicode_FromFormatV(const char *format, va_list vargs) | 
 | 2105 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2106 |     va_list count; | 
 | 2107 |     Py_ssize_t callcount = 0; | 
 | 2108 |     PyObject **callresults = NULL; | 
 | 2109 |     PyObject **callresult = NULL; | 
 | 2110 |     Py_ssize_t n = 0; | 
 | 2111 |     int width = 0; | 
 | 2112 |     int precision = 0; | 
 | 2113 |     int zeropad; | 
 | 2114 |     const char* f; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2115 |     PyObject *string; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2116 |     /* used by sprintf */ | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2117 |     char fmt[61]; /* should be enough for %0width.precisionlld */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2118 |     Py_UCS4 maxchar = 127; /* result is ASCII by default */ | 
 | 2119 |     Py_UCS4 argmaxchar; | 
 | 2120 |     Py_ssize_t numbersize = 0; | 
 | 2121 |     char *numberresults = NULL; | 
 | 2122 |     char *numberresult = NULL; | 
 | 2123 |     Py_ssize_t i; | 
 | 2124 |     int kind; | 
 | 2125 |     void *data; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2126 |  | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2127 |     Py_VA_COPY(count, vargs); | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2128 |     /* step 1: count the number of %S/%R/%A/%s format specifications | 
 | 2129 |      * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ | 
 | 2130 |      * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2131 |      * result in an array) | 
| Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2132 |      * also estimate a upper bound for all the number formats in the string, | 
 | 2133 |      * numbers will be formatted in step 3 and be kept in a '\0'-separated | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2134 |      * buffer before putting everything together. */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2135 |     for (f = format; *f; f++) { | 
 | 2136 |         if (*f == '%') { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2137 |             int longlongflag; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2138 |             /* skip width or width.precision (eg. "1.2" of "%1.2f") */ | 
 | 2139 |             f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); | 
 | 2140 |             if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') | 
 | 2141 |                 ++callcount; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2142 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2143 |             else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2144 | #ifdef HAVE_LONG_LONG | 
 | 2145 |                 if (longlongflag) { | 
 | 2146 |                     if (width < MAX_LONG_LONG_CHARS) | 
 | 2147 |                         width = MAX_LONG_LONG_CHARS; | 
 | 2148 |                 } | 
 | 2149 |                 else | 
 | 2150 | #endif | 
 | 2151 |                     /* MAX_LONG_CHARS is enough to hold a 64-bit integer, | 
 | 2152 |                        including sign.  Decimal takes the most space.  This | 
 | 2153 |                        isn't enough for octal.  If a width is specified we | 
 | 2154 |                        need more (which we allocate later). */ | 
 | 2155 |                     if (width < MAX_LONG_CHARS) | 
 | 2156 |                         width = MAX_LONG_CHARS; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2157 |  | 
 | 2158 |                 /* account for the size + '\0' to separate numbers | 
 | 2159 |                    inside of the numberresults buffer */ | 
 | 2160 |                 numbersize += (width + 1); | 
 | 2161 |             } | 
 | 2162 |         } | 
 | 2163 |         else if ((unsigned char)*f > 127) { | 
 | 2164 |             PyErr_Format(PyExc_ValueError, | 
 | 2165 |                 "PyUnicode_FromFormatV() expects an ASCII-encoded format " | 
 | 2166 |                 "string, got a non-ASCII byte: 0x%02x", | 
 | 2167 |                 (unsigned char)*f); | 
 | 2168 |             return NULL; | 
 | 2169 |         } | 
 | 2170 |     } | 
 | 2171 |     /* step 2: allocate memory for the results of | 
 | 2172 |      * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ | 
 | 2173 |     if (callcount) { | 
 | 2174 |         callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); | 
 | 2175 |         if (!callresults) { | 
 | 2176 |             PyErr_NoMemory(); | 
 | 2177 |             return NULL; | 
 | 2178 |         } | 
 | 2179 |         callresult = callresults; | 
 | 2180 |     } | 
 | 2181 |     /* step 2.5: allocate memory for the results of formating numbers */ | 
 | 2182 |     if (numbersize) { | 
 | 2183 |         numberresults = PyObject_Malloc(numbersize); | 
 | 2184 |         if (!numberresults) { | 
 | 2185 |             PyErr_NoMemory(); | 
 | 2186 |             goto fail; | 
 | 2187 |         } | 
 | 2188 |         numberresult = numberresults; | 
 | 2189 |     } | 
 | 2190 |  | 
 | 2191 |     /* step 3: format numbers and figure out how large a buffer we need */ | 
 | 2192 |     for (f = format; *f; f++) { | 
 | 2193 |         if (*f == '%') { | 
 | 2194 |             const char* p; | 
 | 2195 |             int longflag; | 
 | 2196 |             int longlongflag; | 
 | 2197 |             int size_tflag; | 
 | 2198 |             int numprinted; | 
 | 2199 |  | 
 | 2200 |             p = f; | 
 | 2201 |             zeropad = (f[1] == '0'); | 
 | 2202 |             f = parse_format_flags(f, &width, &precision, | 
 | 2203 |                                    &longflag, &longlongflag, &size_tflag); | 
 | 2204 |             switch (*f) { | 
 | 2205 |             case 'c': | 
 | 2206 |             { | 
 | 2207 |                 Py_UCS4 ordinal = va_arg(count, int); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2208 |                 maxchar = Py_MAX(maxchar, ordinal); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2209 |                 n++; | 
 | 2210 |                 break; | 
 | 2211 |             } | 
 | 2212 |             case '%': | 
 | 2213 |                 n++; | 
 | 2214 |                 break; | 
 | 2215 |             case 'i': | 
 | 2216 |             case 'd': | 
 | 2217 |                 makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, | 
 | 2218 |                         width, precision, *f); | 
 | 2219 |                 if (longflag) | 
 | 2220 |                     numprinted = sprintf(numberresult, fmt, | 
 | 2221 |                                          va_arg(count, long)); | 
 | 2222 | #ifdef HAVE_LONG_LONG | 
 | 2223 |                 else if (longlongflag) | 
 | 2224 |                     numprinted = sprintf(numberresult, fmt, | 
 | 2225 |                                          va_arg(count, PY_LONG_LONG)); | 
 | 2226 | #endif | 
 | 2227 |                 else if (size_tflag) | 
 | 2228 |                     numprinted = sprintf(numberresult, fmt, | 
 | 2229 |                                          va_arg(count, Py_ssize_t)); | 
 | 2230 |                 else | 
 | 2231 |                     numprinted = sprintf(numberresult, fmt, | 
 | 2232 |                                          va_arg(count, int)); | 
 | 2233 |                 n += numprinted; | 
 | 2234 |                 /* advance by +1 to skip over the '\0' */ | 
 | 2235 |                 numberresult += (numprinted + 1); | 
 | 2236 |                 assert(*(numberresult - 1) == '\0'); | 
 | 2237 |                 assert(*(numberresult - 2) != '\0'); | 
 | 2238 |                 assert(numprinted >= 0); | 
 | 2239 |                 assert(numberresult <= numberresults + numbersize); | 
 | 2240 |                 break; | 
 | 2241 |             case 'u': | 
 | 2242 |                 makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, | 
 | 2243 |                         width, precision, 'u'); | 
 | 2244 |                 if (longflag) | 
 | 2245 |                     numprinted = sprintf(numberresult, fmt, | 
 | 2246 |                                          va_arg(count, unsigned long)); | 
 | 2247 | #ifdef HAVE_LONG_LONG | 
 | 2248 |                 else if (longlongflag) | 
 | 2249 |                     numprinted = sprintf(numberresult, fmt, | 
 | 2250 |                                          va_arg(count, unsigned PY_LONG_LONG)); | 
 | 2251 | #endif | 
 | 2252 |                 else if (size_tflag) | 
 | 2253 |                     numprinted = sprintf(numberresult, fmt, | 
 | 2254 |                                          va_arg(count, size_t)); | 
 | 2255 |                 else | 
 | 2256 |                     numprinted = sprintf(numberresult, fmt, | 
 | 2257 |                                          va_arg(count, unsigned int)); | 
 | 2258 |                 n += numprinted; | 
 | 2259 |                 numberresult += (numprinted + 1); | 
 | 2260 |                 assert(*(numberresult - 1) == '\0'); | 
 | 2261 |                 assert(*(numberresult - 2) != '\0'); | 
 | 2262 |                 assert(numprinted >= 0); | 
 | 2263 |                 assert(numberresult <= numberresults + numbersize); | 
 | 2264 |                 break; | 
 | 2265 |             case 'x': | 
 | 2266 |                 makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); | 
 | 2267 |                 numprinted = sprintf(numberresult, fmt, va_arg(count, int)); | 
 | 2268 |                 n += numprinted; | 
 | 2269 |                 numberresult += (numprinted + 1); | 
 | 2270 |                 assert(*(numberresult - 1) == '\0'); | 
 | 2271 |                 assert(*(numberresult - 2) != '\0'); | 
 | 2272 |                 assert(numprinted >= 0); | 
 | 2273 |                 assert(numberresult <= numberresults + numbersize); | 
 | 2274 |                 break; | 
 | 2275 |             case 'p': | 
 | 2276 |                 numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); | 
 | 2277 |                 /* %p is ill-defined:  ensure leading 0x. */ | 
 | 2278 |                 if (numberresult[1] == 'X') | 
 | 2279 |                     numberresult[1] = 'x'; | 
 | 2280 |                 else if (numberresult[1] != 'x') { | 
 | 2281 |                     memmove(numberresult + 2, numberresult, | 
 | 2282 |                             strlen(numberresult) + 1); | 
 | 2283 |                     numberresult[0] = '0'; | 
 | 2284 |                     numberresult[1] = 'x'; | 
 | 2285 |                     numprinted += 2; | 
 | 2286 |                 } | 
 | 2287 |                 n += numprinted; | 
 | 2288 |                 numberresult += (numprinted + 1); | 
 | 2289 |                 assert(*(numberresult - 1) == '\0'); | 
 | 2290 |                 assert(*(numberresult - 2) != '\0'); | 
 | 2291 |                 assert(numprinted >= 0); | 
 | 2292 |                 assert(numberresult <= numberresults + numbersize); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2293 |                 break; | 
 | 2294 |             case 's': | 
 | 2295 |             { | 
 | 2296 |                 /* UTF-8 */ | 
| Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2297 |                 const char *s = va_arg(count, const char*); | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2298 |                 PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); | 
 | 2299 |                 if (!str) | 
 | 2300 |                     goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2301 |                 /* since PyUnicode_DecodeUTF8 returns already flexible | 
 | 2302 |                    unicode objects, there is no need to call ready on them */ | 
 | 2303 |                 argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2304 |                 maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2305 |                 n += PyUnicode_GET_LENGTH(str); | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2306 |                 /* Remember the str and switch to the next slot */ | 
 | 2307 |                 *callresult++ = str; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2308 |                 break; | 
 | 2309 |             } | 
 | 2310 |             case 'U': | 
 | 2311 |             { | 
 | 2312 |                 PyObject *obj = va_arg(count, PyObject *); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2313 |                 assert(obj && _PyUnicode_CHECK(obj)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2314 |                 if (PyUnicode_READY(obj) == -1) | 
 | 2315 |                     goto fail; | 
 | 2316 |                 argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2317 |                 maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2318 |                 n += PyUnicode_GET_LENGTH(obj); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2319 |                 break; | 
 | 2320 |             } | 
 | 2321 |             case 'V': | 
 | 2322 |             { | 
 | 2323 |                 PyObject *obj = va_arg(count, PyObject *); | 
 | 2324 |                 const char *str = va_arg(count, const char *); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2325 |                 PyObject *str_obj; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2326 |                 assert(obj || str); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2327 |                 assert(!obj || _PyUnicode_CHECK(obj)); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2328 |                 if (obj) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2329 |                     if (PyUnicode_READY(obj) == -1) | 
 | 2330 |                         goto fail; | 
 | 2331 |                     argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2332 |                     maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2333 |                     n += PyUnicode_GET_LENGTH(obj); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2334 |                     *callresult++ = NULL; | 
 | 2335 |                 } | 
 | 2336 |                 else { | 
 | 2337 |                     str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); | 
 | 2338 |                     if (!str_obj) | 
 | 2339 |                         goto fail; | 
| Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2340 |                     if (PyUnicode_READY(str_obj)) { | 
 | 2341 |                         Py_DECREF(str_obj); | 
 | 2342 |                         goto fail; | 
 | 2343 |                     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2344 |                     argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2345 |                     maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2346 |                     n += PyUnicode_GET_LENGTH(str_obj); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2347 |                     *callresult++ = str_obj; | 
 | 2348 |                 } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2349 |                 break; | 
 | 2350 |             } | 
 | 2351 |             case 'S': | 
 | 2352 |             { | 
 | 2353 |                 PyObject *obj = va_arg(count, PyObject *); | 
 | 2354 |                 PyObject *str; | 
 | 2355 |                 assert(obj); | 
 | 2356 |                 str = PyObject_Str(obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2357 |                 if (!str || PyUnicode_READY(str) == -1) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2358 |                     goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2359 |                 argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2360 |                 maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2361 |                 n += PyUnicode_GET_LENGTH(str); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2362 |                 /* Remember the str and switch to the next slot */ | 
 | 2363 |                 *callresult++ = str; | 
 | 2364 |                 break; | 
 | 2365 |             } | 
 | 2366 |             case 'R': | 
 | 2367 |             { | 
 | 2368 |                 PyObject *obj = va_arg(count, PyObject *); | 
 | 2369 |                 PyObject *repr; | 
 | 2370 |                 assert(obj); | 
 | 2371 |                 repr = PyObject_Repr(obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2372 |                 if (!repr || PyUnicode_READY(repr) == -1) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2373 |                     goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2374 |                 argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2375 |                 maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2376 |                 n += PyUnicode_GET_LENGTH(repr); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2377 |                 /* Remember the repr and switch to the next slot */ | 
 | 2378 |                 *callresult++ = repr; | 
 | 2379 |                 break; | 
 | 2380 |             } | 
 | 2381 |             case 'A': | 
 | 2382 |             { | 
 | 2383 |                 PyObject *obj = va_arg(count, PyObject *); | 
 | 2384 |                 PyObject *ascii; | 
 | 2385 |                 assert(obj); | 
 | 2386 |                 ascii = PyObject_ASCII(obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2387 |                 if (!ascii || PyUnicode_READY(ascii) == -1) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2388 |                     goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2389 |                 argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2390 |                 maxchar = Py_MAX(maxchar, argmaxchar); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2391 |                 n += PyUnicode_GET_LENGTH(ascii); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2392 |                 /* Remember the repr and switch to the next slot */ | 
 | 2393 |                 *callresult++ = ascii; | 
 | 2394 |                 break; | 
 | 2395 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2396 |             default: | 
 | 2397 |                 /* if we stumble upon an unknown | 
 | 2398 |                    formatting code, copy the rest of | 
 | 2399 |                    the format string to the output | 
 | 2400 |                    string. (we cannot just skip the | 
 | 2401 |                    code, since there's no way to know | 
 | 2402 |                    what's in the argument list) */ | 
 | 2403 |                 n += strlen(p); | 
 | 2404 |                 goto expand; | 
 | 2405 |             } | 
 | 2406 |         } else | 
 | 2407 |             n++; | 
 | 2408 |     } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2409 |   expand: | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2410 |     /* step 4: fill the buffer */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2411 |     /* Since we've analyzed how much space we need, | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2412 |        we don't have to resize the string. | 
 | 2413 |        There can be no errors beyond this point. */ | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2414 |     string = PyUnicode_New(n, maxchar); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2415 |     if (!string) | 
 | 2416 |         goto fail; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2417 |     kind = PyUnicode_KIND(string); | 
 | 2418 |     data = PyUnicode_DATA(string); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2419 |     callresult = callresults; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2420 |     numberresult = numberresults; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2421 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2422 |     for (i = 0, f = format; *f; f++) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2423 |         if (*f == '%') { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2424 |             const char* p; | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2425 |  | 
 | 2426 |             p = f; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2427 |             f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); | 
 | 2428 |             /* checking for == because the last argument could be a empty | 
 | 2429 |                string, which causes i to point to end, the assert at the end of | 
 | 2430 |                the loop */ | 
 | 2431 |             assert(i <= PyUnicode_GET_LENGTH(string)); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2432 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2433 |             switch (*f) { | 
 | 2434 |             case 'c': | 
| Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2435 |             { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2436 |                 const int ordinal = va_arg(vargs, int); | 
 | 2437 |                 PyUnicode_WRITE(kind, data, i++, ordinal); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2438 |                 break; | 
| Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2439 |             } | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2440 |             case 'i': | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2441 |             case 'd': | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2442 |             case 'u': | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2443 |             case 'x': | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2444 |             case 'p': | 
 | 2445 |                 /* unused, since we already have the result */ | 
 | 2446 |                 if (*f == 'p') | 
 | 2447 |                     (void) va_arg(vargs, void *); | 
 | 2448 |                 else | 
 | 2449 |                     (void) va_arg(vargs, int); | 
 | 2450 |                 /* extract the result from numberresults and append. */ | 
 | 2451 |                 for (; *numberresult; ++i, ++numberresult) | 
 | 2452 |                     PyUnicode_WRITE(kind, data, i, *numberresult); | 
 | 2453 |                 /* skip over the separating '\0' */ | 
 | 2454 |                 assert(*numberresult == '\0'); | 
 | 2455 |                 numberresult++; | 
 | 2456 |                 assert(numberresult <= numberresults + numbersize); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2457 |                 break; | 
 | 2458 |             case 's': | 
 | 2459 |             { | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2460 |                 /* unused, since we already have the result */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2461 |                 Py_ssize_t size; | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2462 |                 (void) va_arg(vargs, char *); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2463 |                 size = PyUnicode_GET_LENGTH(*callresult); | 
 | 2464 |                 assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2465 |                 copy_characters(string, i, *callresult, 0, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2466 |                 i += size; | 
| Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2467 |                 /* We're done with the unicode()/repr() => forget it */ | 
 | 2468 |                 Py_DECREF(*callresult); | 
 | 2469 |                 /* switch to next unicode()/repr() result */ | 
 | 2470 |                 ++callresult; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2471 |                 break; | 
 | 2472 |             } | 
 | 2473 |             case 'U': | 
 | 2474 |             { | 
 | 2475 |                 PyObject *obj = va_arg(vargs, PyObject *); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2476 |                 Py_ssize_t size; | 
 | 2477 |                 assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); | 
 | 2478 |                 size = PyUnicode_GET_LENGTH(obj); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2479 |                 copy_characters(string, i, obj, 0, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2480 |                 i += size; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2481 |                 break; | 
 | 2482 |             } | 
 | 2483 |             case 'V': | 
 | 2484 |             { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2485 |                 Py_ssize_t size; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2486 |                 PyObject *obj = va_arg(vargs, PyObject *); | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2487 |                 va_arg(vargs, const char *); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2488 |                 if (obj) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2489 |                     size = PyUnicode_GET_LENGTH(obj); | 
 | 2490 |                     assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2491 |                     copy_characters(string, i, obj, 0, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2492 |                     i += size; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2493 |                 } else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2494 |                     size = PyUnicode_GET_LENGTH(*callresult); | 
 | 2495 |                     assert(PyUnicode_KIND(*callresult) <= | 
 | 2496 |                            PyUnicode_KIND(string)); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2497 |                     copy_characters(string, i, *callresult, 0, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2498 |                     i += size; | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2499 |                     Py_DECREF(*callresult); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2500 |                 } | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2501 |                 ++callresult; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2502 |                 break; | 
 | 2503 |             } | 
 | 2504 |             case 'S': | 
 | 2505 |             case 'R': | 
| Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2506 |             case 'A': | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2507 |             { | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2508 |                 Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2509 |                 /* unused, since we already have the result */ | 
 | 2510 |                 (void) va_arg(vargs, PyObject *); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2511 |                 assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2512 |                 copy_characters(string, i, *callresult, 0,  size); | 
 | 2513 |                 i += size; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2514 |                 /* We're done with the unicode()/repr() => forget it */ | 
 | 2515 |                 Py_DECREF(*callresult); | 
 | 2516 |                 /* switch to next unicode()/repr() result */ | 
 | 2517 |                 ++callresult; | 
 | 2518 |                 break; | 
 | 2519 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2520 |             case '%': | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2521 |                 PyUnicode_WRITE(kind, data, i++, '%'); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2522 |                 break; | 
 | 2523 |             default: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2524 |                 for (; *p; ++p, ++i) | 
 | 2525 |                     PyUnicode_WRITE(kind, data, i, *p); | 
 | 2526 |                 assert(i == PyUnicode_GET_LENGTH(string)); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2527 |                 goto end; | 
 | 2528 |             } | 
| Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2529 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2530 |         else { | 
 | 2531 |             assert(i < PyUnicode_GET_LENGTH(string)); | 
 | 2532 |             PyUnicode_WRITE(kind, data, i++, *f); | 
 | 2533 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2534 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2535 |     assert(i == PyUnicode_GET_LENGTH(string)); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2536 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2537 |   end: | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2538 |     if (callresults) | 
 | 2539 |         PyObject_Free(callresults); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2540 |     if (numberresults) | 
 | 2541 |         PyObject_Free(numberresults); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2542 |     assert(_PyUnicode_CheckConsistency(string, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2543 |     return (PyObject *)string; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2544 |   fail: | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2545 |     if (callresults) { | 
 | 2546 |         PyObject **callresult2 = callresults; | 
 | 2547 |         while (callresult2 < callresult) { | 
| Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2548 |             Py_XDECREF(*callresult2); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2549 |             ++callresult2; | 
 | 2550 |         } | 
 | 2551 |         PyObject_Free(callresults); | 
 | 2552 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2553 |     if (numberresults) | 
 | 2554 |         PyObject_Free(numberresults); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2555 |     return NULL; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2556 | } | 
 | 2557 |  | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2558 | PyObject * | 
 | 2559 | PyUnicode_FromFormat(const char *format, ...) | 
 | 2560 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2561 |     PyObject* ret; | 
 | 2562 |     va_list vargs; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2563 |  | 
 | 2564 | #ifdef HAVE_STDARG_PROTOTYPES | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2565 |     va_start(vargs, format); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2566 | #else | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2567 |     va_start(vargs); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2568 | #endif | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2569 |     ret = PyUnicode_FromFormatV(format, vargs); | 
 | 2570 |     va_end(vargs); | 
 | 2571 |     return ret; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2572 | } | 
 | 2573 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2574 | #ifdef HAVE_WCHAR_H | 
 | 2575 |  | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2576 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): | 
 | 2577 |    convert a Unicode object to a wide character string. | 
 | 2578 |  | 
| Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2579 |    - If w is NULL: return the number of wide characters (including the null | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2580 |      character) required to convert the unicode object. Ignore size argument. | 
 | 2581 |  | 
| Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2582 |    - Otherwise: return the number of wide characters (excluding the null | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2583 |      character) written into w. Write at most size wide characters (including | 
| Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2584 |      the null character). */ | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2585 | static Py_ssize_t | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2586 | unicode_aswidechar(PyUnicodeObject *unicode, | 
 | 2587 |                    wchar_t *w, | 
 | 2588 |                    Py_ssize_t size) | 
 | 2589 | { | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2590 |     Py_ssize_t res; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2591 |     const wchar_t *wstr; | 
 | 2592 |  | 
 | 2593 |     wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); | 
 | 2594 |     if (wstr == NULL) | 
 | 2595 |         return -1; | 
 | 2596 |  | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2597 |     if (w != NULL) { | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2598 |         if (size > res) | 
 | 2599 |             size = res + 1; | 
 | 2600 |         else | 
 | 2601 |             res = size; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2602 |         Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2603 |         return res; | 
 | 2604 |     } | 
 | 2605 |     else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2606 |         return res + 1; | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2607 | } | 
 | 2608 |  | 
 | 2609 | Py_ssize_t | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2610 | PyUnicode_AsWideChar(PyObject *unicode, | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2611 |                      wchar_t *w, | 
 | 2612 |                      Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2613 | { | 
 | 2614 |     if (unicode == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2615 |         PyErr_BadInternalCall(); | 
 | 2616 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2617 |     } | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2618 |     return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2619 | } | 
 | 2620 |  | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2621 | wchar_t* | 
| Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2622 | PyUnicode_AsWideCharString(PyObject *unicode, | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2623 |                            Py_ssize_t *size) | 
 | 2624 | { | 
 | 2625 |     wchar_t* buffer; | 
 | 2626 |     Py_ssize_t buflen; | 
 | 2627 |  | 
 | 2628 |     if (unicode == NULL) { | 
 | 2629 |         PyErr_BadInternalCall(); | 
 | 2630 |         return NULL; | 
 | 2631 |     } | 
 | 2632 |  | 
| Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2633 |     buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2634 |     if (buflen == -1) | 
 | 2635 |         return NULL; | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2636 |     if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2637 |         PyErr_NoMemory(); | 
 | 2638 |         return NULL; | 
 | 2639 |     } | 
 | 2640 |  | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2641 |     buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); | 
 | 2642 |     if (buffer == NULL) { | 
 | 2643 |         PyErr_NoMemory(); | 
 | 2644 |         return NULL; | 
 | 2645 |     } | 
| Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2646 |     buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2647 |     if (buflen == -1) | 
 | 2648 |         return NULL; | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2649 |     if (size != NULL) | 
 | 2650 |         *size = buflen; | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2651 |     return buffer; | 
 | 2652 | } | 
 | 2653 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2654 | #endif /* HAVE_WCHAR_H */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2655 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2656 | PyObject * | 
 | 2657 | PyUnicode_FromOrdinal(int ordinal) | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2658 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2659 |     PyObject *v; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2660 |     if (ordinal < 0 || ordinal > 0x10ffff) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2661 |         PyErr_SetString(PyExc_ValueError, | 
 | 2662 |                         "chr() arg not in range(0x110000)"); | 
 | 2663 |         return NULL; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2664 |     } | 
| Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2665 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2666 |     if (ordinal < 256) | 
 | 2667 |         return get_latin1_char(ordinal); | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2668 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2669 |     v = PyUnicode_New(1, ordinal); | 
 | 2670 |     if (v == NULL) | 
 | 2671 |         return NULL; | 
 | 2672 |     PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2673 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2674 |     return v; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2675 | } | 
 | 2676 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2677 | PyObject * | 
 | 2678 | PyUnicode_FromObject(register PyObject *obj) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2679 | { | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2680 |     /* XXX Perhaps we should make this API an alias of | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2681 |        PyObject_Str() instead ?! */ | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2682 |     if (PyUnicode_CheckExact(obj)) { | 
| Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2683 |         if (PyUnicode_READY(obj)) | 
 | 2684 |             return NULL; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2685 |         Py_INCREF(obj); | 
 | 2686 |         return obj; | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2687 |     } | 
 | 2688 |     if (PyUnicode_Check(obj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2689 |         /* For a Unicode subtype that's not a Unicode object, | 
 | 2690 |            return a true Unicode object with the same data. */ | 
| Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2691 |         return PyUnicode_Copy(obj); | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2692 |     } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2693 |     PyErr_Format(PyExc_TypeError, | 
 | 2694 |                  "Can't convert '%.100s' object to str implicitly", | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2695 |                  Py_TYPE(obj)->tp_name); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2696 |     return NULL; | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2697 | } | 
 | 2698 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2699 | PyObject * | 
 | 2700 | PyUnicode_FromEncodedObject(register PyObject *obj, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2701 |                             const char *encoding, | 
 | 2702 |                             const char *errors) | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2703 | { | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2704 |     Py_buffer buffer; | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2705 |     PyObject *v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2706 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2707 |     if (obj == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2708 |         PyErr_BadInternalCall(); | 
 | 2709 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2710 |     } | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2711 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2712 |     /* Decoding bytes objects is the most common case and should be fast */ | 
 | 2713 |     if (PyBytes_Check(obj)) { | 
 | 2714 |         if (PyBytes_GET_SIZE(obj) == 0) { | 
 | 2715 |             Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2716 |             v = unicode_empty; | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2717 |         } | 
 | 2718 |         else { | 
 | 2719 |             v = PyUnicode_Decode( | 
 | 2720 |                     PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), | 
 | 2721 |                     encoding, errors); | 
 | 2722 |         } | 
 | 2723 |         return v; | 
 | 2724 |     } | 
 | 2725 |  | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2726 |     if (PyUnicode_Check(obj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2727 |         PyErr_SetString(PyExc_TypeError, | 
 | 2728 |                         "decoding str is not supported"); | 
 | 2729 |         return NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2730 |     } | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2731 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2732 |     /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ | 
 | 2733 |     if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { | 
 | 2734 |         PyErr_Format(PyExc_TypeError, | 
 | 2735 |                      "coercing to str: need bytes, bytearray " | 
 | 2736 |                      "or buffer-like object, %.80s found", | 
 | 2737 |                      Py_TYPE(obj)->tp_name); | 
 | 2738 |         return NULL; | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2739 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2740 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2741 |     if (buffer.len == 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2742 |         Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2743 |         v = unicode_empty; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2744 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2745 |     else | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2746 |         v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); | 
| Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2747 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2748 |     PyBuffer_Release(&buffer); | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2749 |     return v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2750 | } | 
 | 2751 |  | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2752 | /* Convert encoding to lower case and replace '_' with '-' in order to | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2753 |    catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), | 
 | 2754 |    1 on success. */ | 
 | 2755 | static int | 
 | 2756 | normalize_encoding(const char *encoding, | 
 | 2757 |                    char *lower, | 
 | 2758 |                    size_t lower_len) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2759 | { | 
| Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2760 |     const char *e; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2761 |     char *l; | 
 | 2762 |     char *l_end; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2763 |  | 
| Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2764 |     e = encoding; | 
 | 2765 |     l = lower; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2766 |     l_end = &lower[lower_len - 1]; | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2767 |     while (*e) { | 
 | 2768 |         if (l == l_end) | 
 | 2769 |             return 0; | 
| David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2770 |         if (Py_ISUPPER(*e)) { | 
 | 2771 |             *l++ = Py_TOLOWER(*e++); | 
| Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2772 |         } | 
 | 2773 |         else if (*e == '_') { | 
 | 2774 |             *l++ = '-'; | 
 | 2775 |             e++; | 
 | 2776 |         } | 
 | 2777 |         else { | 
 | 2778 |             *l++ = *e++; | 
 | 2779 |         } | 
 | 2780 |     } | 
 | 2781 |     *l = '\0'; | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2782 |     return 1; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2783 | } | 
 | 2784 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2785 | PyObject * | 
 | 2786 | PyUnicode_Decode(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2787 |                  Py_ssize_t size, | 
 | 2788 |                  const char *encoding, | 
 | 2789 |                  const char *errors) | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2790 | { | 
 | 2791 |     PyObject *buffer = NULL, *unicode; | 
 | 2792 |     Py_buffer info; | 
 | 2793 |     char lower[11];  /* Enough for any encoding shortcut */ | 
 | 2794 |  | 
 | 2795 |     if (encoding == NULL) | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2796 |         return PyUnicode_DecodeUTF8(s, size, errors); | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2797 |  | 
 | 2798 |     /* Shortcuts for common default encodings */ | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2799 |     if (normalize_encoding(encoding, lower, sizeof(lower))) { | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2800 |         if ((strcmp(lower, "utf-8") == 0) || | 
 | 2801 |             (strcmp(lower, "utf8") == 0)) | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2802 |             return PyUnicode_DecodeUTF8(s, size, errors); | 
 | 2803 |         else if ((strcmp(lower, "latin-1") == 0) || | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2804 |                  (strcmp(lower, "latin1") == 0) || | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2805 |                  (strcmp(lower, "iso-8859-1") == 0)) | 
 | 2806 |             return PyUnicode_DecodeLatin1(s, size, errors); | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2807 | #ifdef HAVE_MBCS | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2808 |         else if (strcmp(lower, "mbcs") == 0) | 
 | 2809 |             return PyUnicode_DecodeMBCS(s, size, errors); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2810 | #endif | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2811 |         else if (strcmp(lower, "ascii") == 0) | 
 | 2812 |             return PyUnicode_DecodeASCII(s, size, errors); | 
 | 2813 |         else if (strcmp(lower, "utf-16") == 0) | 
 | 2814 |             return PyUnicode_DecodeUTF16(s, size, errors, 0); | 
 | 2815 |         else if (strcmp(lower, "utf-32") == 0) | 
 | 2816 |             return PyUnicode_DecodeUTF32(s, size, errors, 0); | 
 | 2817 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2818 |  | 
 | 2819 |     /* Decode via the codec registry */ | 
| Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2820 |     buffer = NULL; | 
| Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2821 |     if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_FULL_RO) < 0) | 
| Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2822 |         goto onError; | 
| Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2823 |     buffer = PyMemoryView_FromBuffer(&info); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2824 |     if (buffer == NULL) | 
 | 2825 |         goto onError; | 
 | 2826 |     unicode = PyCodec_Decode(buffer, encoding, errors); | 
 | 2827 |     if (unicode == NULL) | 
 | 2828 |         goto onError; | 
 | 2829 |     if (!PyUnicode_Check(unicode)) { | 
 | 2830 |         PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2831 |                      "decoder did not return a str object (type=%.400s)", | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2832 |                      Py_TYPE(unicode)->tp_name); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2833 |         Py_DECREF(unicode); | 
 | 2834 |         goto onError; | 
 | 2835 |     } | 
 | 2836 |     Py_DECREF(buffer); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2837 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2838 |     if (_PyUnicode_READY_REPLACE(&unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2839 |         Py_DECREF(unicode); | 
 | 2840 |         return NULL; | 
 | 2841 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2842 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2843 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2844 |     return unicode; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2845 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2846 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2847 |     Py_XDECREF(buffer); | 
 | 2848 |     return NULL; | 
 | 2849 | } | 
 | 2850 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2851 | PyObject * | 
 | 2852 | PyUnicode_AsDecodedObject(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2853 |                           const char *encoding, | 
 | 2854 |                           const char *errors) | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2855 | { | 
 | 2856 |     PyObject *v; | 
 | 2857 |  | 
 | 2858 |     if (!PyUnicode_Check(unicode)) { | 
 | 2859 |         PyErr_BadArgument(); | 
 | 2860 |         goto onError; | 
 | 2861 |     } | 
 | 2862 |  | 
 | 2863 |     if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2864 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2865 |  | 
 | 2866 |     /* Decode via the codec registry */ | 
 | 2867 |     v = PyCodec_Decode(unicode, encoding, errors); | 
 | 2868 |     if (v == NULL) | 
 | 2869 |         goto onError; | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2870 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2871 |     return v; | 
 | 2872 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2873 |   onError: | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2874 |     return NULL; | 
 | 2875 | } | 
 | 2876 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2877 | PyObject * | 
 | 2878 | PyUnicode_AsDecodedUnicode(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2879 |                            const char *encoding, | 
 | 2880 |                            const char *errors) | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2881 | { | 
 | 2882 |     PyObject *v; | 
 | 2883 |  | 
 | 2884 |     if (!PyUnicode_Check(unicode)) { | 
 | 2885 |         PyErr_BadArgument(); | 
 | 2886 |         goto onError; | 
 | 2887 |     } | 
 | 2888 |  | 
 | 2889 |     if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2890 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2891 |  | 
 | 2892 |     /* Decode via the codec registry */ | 
 | 2893 |     v = PyCodec_Decode(unicode, encoding, errors); | 
 | 2894 |     if (v == NULL) | 
 | 2895 |         goto onError; | 
 | 2896 |     if (!PyUnicode_Check(v)) { | 
 | 2897 |         PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2898 |                      "decoder did not return a str object (type=%.400s)", | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2899 |                      Py_TYPE(v)->tp_name); | 
 | 2900 |         Py_DECREF(v); | 
 | 2901 |         goto onError; | 
 | 2902 |     } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2903 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2904 |     return v; | 
 | 2905 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2906 |   onError: | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2907 |     return NULL; | 
 | 2908 | } | 
 | 2909 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2910 | PyObject * | 
 | 2911 | PyUnicode_Encode(const Py_UNICODE *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2912 |                  Py_ssize_t size, | 
 | 2913 |                  const char *encoding, | 
 | 2914 |                  const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2915 | { | 
 | 2916 |     PyObject *v, *unicode; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2917 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2918 |     unicode = PyUnicode_FromUnicode(s, size); | 
 | 2919 |     if (unicode == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2920 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2921 |     v = PyUnicode_AsEncodedString(unicode, encoding, errors); | 
 | 2922 |     Py_DECREF(unicode); | 
 | 2923 |     return v; | 
 | 2924 | } | 
 | 2925 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2926 | PyObject * | 
 | 2927 | PyUnicode_AsEncodedObject(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2928 |                           const char *encoding, | 
 | 2929 |                           const char *errors) | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2930 | { | 
 | 2931 |     PyObject *v; | 
 | 2932 |  | 
 | 2933 |     if (!PyUnicode_Check(unicode)) { | 
 | 2934 |         PyErr_BadArgument(); | 
 | 2935 |         goto onError; | 
 | 2936 |     } | 
 | 2937 |  | 
 | 2938 |     if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2939 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2940 |  | 
 | 2941 |     /* Encode via the codec registry */ | 
 | 2942 |     v = PyCodec_Encode(unicode, encoding, errors); | 
 | 2943 |     if (v == NULL) | 
 | 2944 |         goto onError; | 
 | 2945 |     return v; | 
 | 2946 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2947 |   onError: | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2948 |     return NULL; | 
 | 2949 | } | 
 | 2950 |  | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2951 | PyObject * | 
 | 2952 | PyUnicode_EncodeFSDefault(PyObject *unicode) | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2953 | { | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2954 | #ifdef HAVE_MBCS | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2955 |     return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), | 
 | 2956 |                                 PyUnicode_GET_SIZE(unicode), | 
 | 2957 |                                 NULL); | 
 | 2958 | #elif defined(__APPLE__) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2959 |     return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2960 | #else | 
| Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2961 |     PyInterpreterState *interp = PyThreadState_GET()->interp; | 
 | 2962 |     /* Bootstrap check: if the filesystem codec is implemented in Python, we | 
 | 2963 |        cannot use it to encode and decode filenames before it is loaded. Load | 
 | 2964 |        the Python codec requires to encode at least its own filename. Use the C | 
 | 2965 |        version of the locale codec until the codec registry is initialized and | 
 | 2966 |        the Python codec is loaded. | 
 | 2967 |  | 
 | 2968 |        Py_FileSystemDefaultEncoding is shared between all interpreters, we | 
 | 2969 |        cannot only rely on it: check also interp->fscodec_initialized for | 
 | 2970 |        subinterpreters. */ | 
 | 2971 |     if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2972 |         return PyUnicode_AsEncodedString(unicode, | 
 | 2973 |                                          Py_FileSystemDefaultEncoding, | 
 | 2974 |                                          "surrogateescape"); | 
| Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2975 |     } | 
 | 2976 |     else { | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2977 |         /* locale encoding with surrogateescape */ | 
 | 2978 |         wchar_t *wchar; | 
 | 2979 |         char *bytes; | 
 | 2980 |         PyObject *bytes_obj; | 
| Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2981 |         size_t error_pos; | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2982 |  | 
 | 2983 |         wchar = PyUnicode_AsWideCharString(unicode, NULL); | 
 | 2984 |         if (wchar == NULL) | 
 | 2985 |             return NULL; | 
| Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2986 |         bytes = _Py_wchar2char(wchar, &error_pos); | 
 | 2987 |         if (bytes == NULL) { | 
 | 2988 |             if (error_pos != (size_t)-1) { | 
 | 2989 |                 char *errmsg = strerror(errno); | 
 | 2990 |                 PyObject *exc = NULL; | 
 | 2991 |                 if (errmsg == NULL) | 
 | 2992 |                     errmsg = "Py_wchar2char() failed"; | 
 | 2993 |                 raise_encode_exception(&exc, | 
 | 2994 |                     "filesystemencoding", | 
 | 2995 |                     PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), | 
 | 2996 |                     error_pos, error_pos+1, | 
 | 2997 |                     errmsg); | 
 | 2998 |                 Py_XDECREF(exc); | 
 | 2999 |             } | 
 | 3000 |             else | 
 | 3001 |                 PyErr_NoMemory(); | 
 | 3002 |             PyMem_Free(wchar); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3003 |             return NULL; | 
| Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3004 |         } | 
 | 3005 |         PyMem_Free(wchar); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3006 |  | 
 | 3007 |         bytes_obj = PyBytes_FromString(bytes); | 
 | 3008 |         PyMem_Free(bytes); | 
 | 3009 |         return bytes_obj; | 
| Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3010 |     } | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3011 | #endif | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3012 | } | 
 | 3013 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3014 | PyObject * | 
 | 3015 | PyUnicode_AsEncodedString(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3016 |                           const char *encoding, | 
 | 3017 |                           const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3018 | { | 
 | 3019 |     PyObject *v; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3020 |     char lower[11];  /* Enough for any encoding shortcut */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3021 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3022 |     if (!PyUnicode_Check(unicode)) { | 
 | 3023 |         PyErr_BadArgument(); | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3024 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3025 |     } | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3026 |  | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3027 |     if (encoding == NULL) { | 
 | 3028 |         if (errors == NULL || strcmp(errors, "strict") == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3029 |             return _PyUnicode_AsUTF8String(unicode, NULL); | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3030 |         else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3031 |             return _PyUnicode_AsUTF8String(unicode, errors); | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3032 |     } | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3033 |  | 
 | 3034 |     /* Shortcuts for common default encodings */ | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3035 |     if (normalize_encoding(encoding, lower, sizeof(lower))) { | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3036 |         if ((strcmp(lower, "utf-8") == 0) || | 
 | 3037 |             (strcmp(lower, "utf8") == 0)) | 
| Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3038 |         { | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3039 |             if (errors == NULL || strcmp(errors, "strict") == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3040 |                 return _PyUnicode_AsUTF8String(unicode, NULL); | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3041 |             else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3042 |                 return _PyUnicode_AsUTF8String(unicode, errors); | 
| Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3043 |         } | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3044 |         else if ((strcmp(lower, "latin-1") == 0) || | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3045 |                  (strcmp(lower, "latin1") == 0) || | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3046 |                  (strcmp(lower, "iso-8859-1") == 0)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3047 |             return _PyUnicode_AsLatin1String(unicode, errors); | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3048 | #ifdef HAVE_MBCS | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3049 |         else if (strcmp(lower, "mbcs") == 0) | 
 | 3050 |             return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), | 
 | 3051 |                                         PyUnicode_GET_SIZE(unicode), | 
 | 3052 |                                         errors); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3053 | #endif | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3054 |         else if (strcmp(lower, "ascii") == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3055 |             return _PyUnicode_AsASCIIString(unicode, errors); | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3056 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3057 |  | 
 | 3058 |     /* Encode via the codec registry */ | 
 | 3059 |     v = PyCodec_Encode(unicode, encoding, errors); | 
 | 3060 |     if (v == NULL) | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3061 |         return NULL; | 
 | 3062 |  | 
 | 3063 |     /* The normal path */ | 
 | 3064 |     if (PyBytes_Check(v)) | 
 | 3065 |         return v; | 
 | 3066 |  | 
 | 3067 |     /* If the codec returns a buffer, raise a warning and convert to bytes */ | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3068 |     if (PyByteArray_Check(v)) { | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3069 |         int error; | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3070 |         PyObject *b; | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3071 |  | 
 | 3072 |         error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, | 
 | 3073 |             "encoder %s returned bytearray instead of bytes", | 
 | 3074 |             encoding); | 
 | 3075 |         if (error) { | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3076 |             Py_DECREF(v); | 
 | 3077 |             return NULL; | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3078 |         } | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3079 |  | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3080 |         b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); | 
 | 3081 |         Py_DECREF(v); | 
 | 3082 |         return b; | 
 | 3083 |     } | 
 | 3084 |  | 
 | 3085 |     PyErr_Format(PyExc_TypeError, | 
 | 3086 |                  "encoder did not return a bytes object (type=%.400s)", | 
 | 3087 |                  Py_TYPE(v)->tp_name); | 
 | 3088 |     Py_DECREF(v); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3089 |     return NULL; | 
 | 3090 | } | 
 | 3091 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3092 | PyObject * | 
 | 3093 | PyUnicode_AsEncodedUnicode(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3094 |                            const char *encoding, | 
 | 3095 |                            const char *errors) | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3096 | { | 
 | 3097 |     PyObject *v; | 
 | 3098 |  | 
 | 3099 |     if (!PyUnicode_Check(unicode)) { | 
 | 3100 |         PyErr_BadArgument(); | 
 | 3101 |         goto onError; | 
 | 3102 |     } | 
 | 3103 |  | 
 | 3104 |     if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3105 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3106 |  | 
 | 3107 |     /* Encode via the codec registry */ | 
 | 3108 |     v = PyCodec_Encode(unicode, encoding, errors); | 
 | 3109 |     if (v == NULL) | 
 | 3110 |         goto onError; | 
 | 3111 |     if (!PyUnicode_Check(v)) { | 
 | 3112 |         PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3113 |                      "encoder did not return an str object (type=%.400s)", | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3114 |                      Py_TYPE(v)->tp_name); | 
 | 3115 |         Py_DECREF(v); | 
 | 3116 |         goto onError; | 
 | 3117 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3118 |     return v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3119 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3120 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3121 |     return NULL; | 
 | 3122 | } | 
 | 3123 |  | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3124 | PyObject* | 
| Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3125 | PyUnicode_DecodeFSDefault(const char *s) { | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3126 |     Py_ssize_t size = (Py_ssize_t)strlen(s); | 
| Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3127 |     return PyUnicode_DecodeFSDefaultAndSize(s, size); | 
 | 3128 | } | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3129 |  | 
| Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3130 | PyObject* | 
 | 3131 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) | 
 | 3132 | { | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3133 | #ifdef HAVE_MBCS | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3134 |     return PyUnicode_DecodeMBCS(s, size, NULL); | 
 | 3135 | #elif defined(__APPLE__) | 
 | 3136 |     return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); | 
 | 3137 | #else | 
| Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3138 |     PyInterpreterState *interp = PyThreadState_GET()->interp; | 
 | 3139 |     /* Bootstrap check: if the filesystem codec is implemented in Python, we | 
 | 3140 |        cannot use it to encode and decode filenames before it is loaded. Load | 
 | 3141 |        the Python codec requires to encode at least its own filename. Use the C | 
 | 3142 |        version of the locale codec until the codec registry is initialized and | 
 | 3143 |        the Python codec is loaded. | 
 | 3144 |  | 
 | 3145 |        Py_FileSystemDefaultEncoding is shared between all interpreters, we | 
 | 3146 |        cannot only rely on it: check also interp->fscodec_initialized for | 
 | 3147 |        subinterpreters. */ | 
 | 3148 |     if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3149 |         return PyUnicode_Decode(s, size, | 
 | 3150 |                                 Py_FileSystemDefaultEncoding, | 
| Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3151 |                                 "surrogateescape"); | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3152 |     } | 
 | 3153 |     else { | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3154 |         /* locale encoding with surrogateescape */ | 
 | 3155 |         wchar_t *wchar; | 
 | 3156 |         PyObject *unicode; | 
| Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3157 |         size_t len; | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3158 |  | 
 | 3159 |         if (s[size] != '\0' || size != strlen(s)) { | 
 | 3160 |             PyErr_SetString(PyExc_TypeError, "embedded NUL character"); | 
 | 3161 |             return NULL; | 
 | 3162 |         } | 
 | 3163 |  | 
| Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3164 |         wchar = _Py_char2wchar(s, &len); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3165 |         if (wchar == NULL) | 
| Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3166 |             return PyErr_NoMemory(); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3167 |  | 
| Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3168 |         unicode = PyUnicode_FromWideChar(wchar, len); | 
| Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3169 |         PyMem_Free(wchar); | 
 | 3170 |         return unicode; | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3171 |     } | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3172 | #endif | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3173 | } | 
 | 3174 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3175 |  | 
 | 3176 | int | 
 | 3177 | PyUnicode_FSConverter(PyObject* arg, void* addr) | 
 | 3178 | { | 
 | 3179 |     PyObject *output = NULL; | 
 | 3180 |     Py_ssize_t size; | 
 | 3181 |     void *data; | 
| Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3182 |     if (arg == NULL) { | 
 | 3183 |         Py_DECREF(*(PyObject**)addr); | 
 | 3184 |         return 1; | 
 | 3185 |     } | 
| Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3186 |     if (PyBytes_Check(arg)) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3187 |         output = arg; | 
 | 3188 |         Py_INCREF(output); | 
 | 3189 |     } | 
 | 3190 |     else { | 
 | 3191 |         arg = PyUnicode_FromObject(arg); | 
 | 3192 |         if (!arg) | 
 | 3193 |             return 0; | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3194 |         output = PyUnicode_EncodeFSDefault(arg); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3195 |         Py_DECREF(arg); | 
 | 3196 |         if (!output) | 
 | 3197 |             return 0; | 
 | 3198 |         if (!PyBytes_Check(output)) { | 
 | 3199 |             Py_DECREF(output); | 
 | 3200 |             PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); | 
 | 3201 |             return 0; | 
 | 3202 |         } | 
 | 3203 |     } | 
| Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3204 |     size = PyBytes_GET_SIZE(output); | 
 | 3205 |     data = PyBytes_AS_STRING(output); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3206 |     if (size != strlen(data)) { | 
| Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3207 |         PyErr_SetString(PyExc_TypeError, "embedded NUL character"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3208 |         Py_DECREF(output); | 
 | 3209 |         return 0; | 
 | 3210 |     } | 
 | 3211 |     *(PyObject**)addr = output; | 
| Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3212 |     return Py_CLEANUP_SUPPORTED; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3213 | } | 
 | 3214 |  | 
 | 3215 |  | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3216 | int | 
 | 3217 | PyUnicode_FSDecoder(PyObject* arg, void* addr) | 
 | 3218 | { | 
 | 3219 |     PyObject *output = NULL; | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3220 |     if (arg == NULL) { | 
 | 3221 |         Py_DECREF(*(PyObject**)addr); | 
 | 3222 |         return 1; | 
 | 3223 |     } | 
 | 3224 |     if (PyUnicode_Check(arg)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3225 |         if (PyUnicode_READY(arg)) | 
 | 3226 |             return 0; | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3227 |         output = arg; | 
 | 3228 |         Py_INCREF(output); | 
 | 3229 |     } | 
 | 3230 |     else { | 
 | 3231 |         arg = PyBytes_FromObject(arg); | 
 | 3232 |         if (!arg) | 
 | 3233 |             return 0; | 
 | 3234 |         output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), | 
 | 3235 |                                                   PyBytes_GET_SIZE(arg)); | 
 | 3236 |         Py_DECREF(arg); | 
 | 3237 |         if (!output) | 
 | 3238 |             return 0; | 
 | 3239 |         if (!PyUnicode_Check(output)) { | 
 | 3240 |             Py_DECREF(output); | 
 | 3241 |             PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); | 
 | 3242 |             return 0; | 
 | 3243 |         } | 
 | 3244 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3245 |     if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), | 
 | 3246 |                  PyUnicode_GET_LENGTH(output), 0, 1)) { | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3247 |         PyErr_SetString(PyExc_TypeError, "embedded NUL character"); | 
 | 3248 |         Py_DECREF(output); | 
 | 3249 |         return 0; | 
 | 3250 |     } | 
 | 3251 |     *(PyObject**)addr = output; | 
 | 3252 |     return Py_CLEANUP_SUPPORTED; | 
 | 3253 | } | 
 | 3254 |  | 
 | 3255 |  | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3256 | char* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3257 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3258 | { | 
| Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3259 |     PyObject *bytes; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3260 |     PyUnicodeObject *u = (PyUnicodeObject *)unicode; | 
 | 3261 |  | 
| Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3262 |     if (!PyUnicode_Check(unicode)) { | 
 | 3263 |         PyErr_BadArgument(); | 
 | 3264 |         return NULL; | 
 | 3265 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3266 |     if (PyUnicode_READY(u) == -1) | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3267 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3268 |  | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3269 |     if (PyUnicode_UTF8(unicode) == NULL) { | 
 | 3270 |         assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3271 |         bytes = _PyUnicode_AsUTF8String(unicode, "strict"); | 
 | 3272 |         if (bytes == NULL) | 
 | 3273 |             return NULL; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3274 |         _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); | 
 | 3275 |         if (_PyUnicode_UTF8(u) == NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3276 |             Py_DECREF(bytes); | 
 | 3277 |             return NULL; | 
 | 3278 |         } | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3279 |         _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); | 
 | 3280 |         Py_MEMCPY(_PyUnicode_UTF8(u), PyBytes_AS_STRING(bytes), _PyUnicode_UTF8_LENGTH(u) + 1); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3281 |         Py_DECREF(bytes); | 
 | 3282 |     } | 
 | 3283 |  | 
 | 3284 |     if (psize) | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3285 |         *psize = PyUnicode_UTF8_LENGTH(unicode); | 
 | 3286 |     return PyUnicode_UTF8(unicode); | 
| Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3287 | } | 
 | 3288 |  | 
 | 3289 | char* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3290 | PyUnicode_AsUTF8(PyObject *unicode) | 
| Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3291 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3292 |     return PyUnicode_AsUTF8AndSize(unicode, NULL); | 
 | 3293 | } | 
 | 3294 |  | 
 | 3295 | #ifdef Py_DEBUG | 
 | 3296 | int unicode_as_unicode_calls = 0; | 
 | 3297 | #endif | 
 | 3298 |  | 
 | 3299 |  | 
 | 3300 | Py_UNICODE * | 
 | 3301 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) | 
 | 3302 | { | 
 | 3303 |     PyUnicodeObject *u; | 
 | 3304 |     const unsigned char *one_byte; | 
 | 3305 | #if SIZEOF_WCHAR_T == 4 | 
 | 3306 |     const Py_UCS2 *two_bytes; | 
 | 3307 | #else | 
 | 3308 |     const Py_UCS4 *four_bytes; | 
 | 3309 |     const Py_UCS4 *ucs4_end; | 
 | 3310 |     Py_ssize_t num_surrogates; | 
 | 3311 | #endif | 
 | 3312 |     wchar_t *w; | 
 | 3313 |     wchar_t *wchar_end; | 
 | 3314 |  | 
 | 3315 |     if (!PyUnicode_Check(unicode)) { | 
 | 3316 |         PyErr_BadArgument(); | 
 | 3317 |         return NULL; | 
 | 3318 |     } | 
 | 3319 |     u = (PyUnicodeObject*)unicode; | 
 | 3320 |     if (_PyUnicode_WSTR(u) == NULL) { | 
 | 3321 |         /* Non-ASCII compact unicode object */ | 
 | 3322 |         assert(_PyUnicode_KIND(u) != 0); | 
 | 3323 |         assert(PyUnicode_IS_READY(u)); | 
 | 3324 |  | 
 | 3325 | #ifdef Py_DEBUG | 
 | 3326 |         ++unicode_as_unicode_calls; | 
 | 3327 | #endif | 
 | 3328 |  | 
 | 3329 |         if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { | 
 | 3330 | #if SIZEOF_WCHAR_T == 2 | 
 | 3331 |             four_bytes = PyUnicode_4BYTE_DATA(u); | 
 | 3332 |             ucs4_end = four_bytes + _PyUnicode_LENGTH(u); | 
 | 3333 |             num_surrogates = 0; | 
 | 3334 |  | 
 | 3335 |             for (; four_bytes < ucs4_end; ++four_bytes) { | 
 | 3336 |                 if (*four_bytes > 0xFFFF) | 
 | 3337 |                     ++num_surrogates; | 
 | 3338 |             } | 
 | 3339 |  | 
 | 3340 |             _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( | 
 | 3341 |                     sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); | 
 | 3342 |             if (!_PyUnicode_WSTR(u)) { | 
 | 3343 |                 PyErr_NoMemory(); | 
 | 3344 |                 return NULL; | 
 | 3345 |             } | 
 | 3346 |             _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; | 
 | 3347 |  | 
 | 3348 |             w = _PyUnicode_WSTR(u); | 
 | 3349 |             wchar_end = w + _PyUnicode_WSTR_LENGTH(u); | 
 | 3350 |             four_bytes = PyUnicode_4BYTE_DATA(u); | 
 | 3351 |             for (; four_bytes < ucs4_end; ++four_bytes, ++w) { | 
 | 3352 |                 if (*four_bytes > 0xFFFF) { | 
 | 3353 |                     /* encode surrogate pair in this case */ | 
 | 3354 |                     *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); | 
 | 3355 |                     *w   = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); | 
 | 3356 |                 } | 
 | 3357 |                 else | 
 | 3358 |                     *w = *four_bytes; | 
 | 3359 |  | 
 | 3360 |                 if (w > wchar_end) { | 
 | 3361 |                     assert(0 && "Miscalculated string end"); | 
 | 3362 |                 } | 
 | 3363 |             } | 
 | 3364 |             *w = 0; | 
 | 3365 | #else | 
 | 3366 |             /* sizeof(wchar_t) == 4 */ | 
 | 3367 |             Py_FatalError("Impossible unicode object state, wstr and str " | 
 | 3368 |                           "should share memory already."); | 
 | 3369 |             return NULL; | 
 | 3370 | #endif | 
 | 3371 |         } | 
 | 3372 |         else { | 
 | 3373 |             _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * | 
 | 3374 |                                                   (_PyUnicode_LENGTH(u) + 1)); | 
 | 3375 |             if (!_PyUnicode_WSTR(u)) { | 
 | 3376 |                 PyErr_NoMemory(); | 
 | 3377 |                 return NULL; | 
 | 3378 |             } | 
 | 3379 |             if (!PyUnicode_IS_COMPACT_ASCII(u)) | 
 | 3380 |                 _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); | 
 | 3381 |             w = _PyUnicode_WSTR(u); | 
 | 3382 |             wchar_end = w + _PyUnicode_LENGTH(u); | 
 | 3383 |  | 
 | 3384 |             if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { | 
 | 3385 |                 one_byte = PyUnicode_1BYTE_DATA(u); | 
 | 3386 |                 for (; w < wchar_end; ++one_byte, ++w) | 
 | 3387 |                     *w = *one_byte; | 
 | 3388 |                 /* null-terminate the wstr */ | 
 | 3389 |                 *w = 0; | 
 | 3390 |             } | 
 | 3391 |             else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { | 
 | 3392 | #if SIZEOF_WCHAR_T == 4 | 
 | 3393 |                 two_bytes = PyUnicode_2BYTE_DATA(u); | 
 | 3394 |                 for (; w < wchar_end; ++two_bytes, ++w) | 
 | 3395 |                     *w = *two_bytes; | 
 | 3396 |                 /* null-terminate the wstr */ | 
 | 3397 |                 *w = 0; | 
 | 3398 | #else | 
 | 3399 |                 /* sizeof(wchar_t) == 2 */ | 
 | 3400 |                 PyObject_FREE(_PyUnicode_WSTR(u)); | 
 | 3401 |                 _PyUnicode_WSTR(u) = NULL; | 
 | 3402 |                 Py_FatalError("Impossible unicode object state, wstr " | 
 | 3403 |                               "and str should share memory already."); | 
 | 3404 |                 return NULL; | 
 | 3405 | #endif | 
 | 3406 |             } | 
 | 3407 |             else { | 
 | 3408 |                 assert(0 && "This should never happen."); | 
 | 3409 |             } | 
 | 3410 |         } | 
 | 3411 |     } | 
 | 3412 |     if (size != NULL) | 
 | 3413 |         *size = PyUnicode_WSTR_LENGTH(u); | 
 | 3414 |     return _PyUnicode_WSTR(u); | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3415 | } | 
 | 3416 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3417 | Py_UNICODE * | 
 | 3418 | PyUnicode_AsUnicode(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3419 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3420 |     return PyUnicode_AsUnicodeAndSize(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3421 | } | 
 | 3422 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3423 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3424 | Py_ssize_t | 
 | 3425 | PyUnicode_GetSize(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3426 | { | 
 | 3427 |     if (!PyUnicode_Check(unicode)) { | 
 | 3428 |         PyErr_BadArgument(); | 
 | 3429 |         goto onError; | 
 | 3430 |     } | 
 | 3431 |     return PyUnicode_GET_SIZE(unicode); | 
 | 3432 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3433 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3434 |     return -1; | 
 | 3435 | } | 
 | 3436 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3437 | Py_ssize_t | 
 | 3438 | PyUnicode_GetLength(PyObject *unicode) | 
 | 3439 | { | 
| Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3440 |     if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3441 |         PyErr_BadArgument(); | 
 | 3442 |         return -1; | 
 | 3443 |     } | 
 | 3444 |  | 
 | 3445 |     return PyUnicode_GET_LENGTH(unicode); | 
 | 3446 | } | 
 | 3447 |  | 
 | 3448 | Py_UCS4 | 
 | 3449 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) | 
 | 3450 | { | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3451 |     if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { | 
 | 3452 |         PyErr_BadArgument(); | 
 | 3453 |         return (Py_UCS4)-1; | 
 | 3454 |     } | 
 | 3455 |     if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { | 
 | 3456 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3457 |         return (Py_UCS4)-1; | 
 | 3458 |     } | 
 | 3459 |     return PyUnicode_READ_CHAR(unicode, index); | 
 | 3460 | } | 
 | 3461 |  | 
 | 3462 | int | 
 | 3463 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) | 
 | 3464 | { | 
 | 3465 |     if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3466 |         PyErr_BadArgument(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3467 |         return -1; | 
 | 3468 |     } | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3469 |     if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { | 
 | 3470 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 3471 |         return -1; | 
 | 3472 |     } | 
 | 3473 |     if (_PyUnicode_Dirty(unicode)) | 
 | 3474 |         return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3475 |     PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), | 
 | 3476 |                     index, ch); | 
 | 3477 |     return 0; | 
 | 3478 | } | 
 | 3479 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3480 | const char * | 
 | 3481 | PyUnicode_GetDefaultEncoding(void) | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3482 | { | 
| Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3483 |     return "utf-8"; | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3484 | } | 
 | 3485 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3486 | /* create or adjust a UnicodeDecodeError */ | 
 | 3487 | static void | 
 | 3488 | make_decode_exception(PyObject **exceptionObject, | 
 | 3489 |                       const char *encoding, | 
 | 3490 |                       const char *input, Py_ssize_t length, | 
 | 3491 |                       Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 3492 |                       const char *reason) | 
 | 3493 | { | 
 | 3494 |     if (*exceptionObject == NULL) { | 
 | 3495 |         *exceptionObject = PyUnicodeDecodeError_Create( | 
 | 3496 |             encoding, input, length, startpos, endpos, reason); | 
 | 3497 |     } | 
 | 3498 |     else { | 
 | 3499 |         if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) | 
 | 3500 |             goto onError; | 
 | 3501 |         if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) | 
 | 3502 |             goto onError; | 
 | 3503 |         if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) | 
 | 3504 |             goto onError; | 
 | 3505 |     } | 
 | 3506 |     return; | 
 | 3507 |  | 
 | 3508 | onError: | 
 | 3509 |     Py_DECREF(*exceptionObject); | 
 | 3510 |     *exceptionObject = NULL; | 
 | 3511 | } | 
 | 3512 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3513 | /* error handling callback helper: | 
 | 3514 |    build arguments, call the callback and check the arguments, | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3515 |    if no exception occurred, copy the replacement to the output | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3516 |    and adjust various state variables. | 
 | 3517 |    return 0 on success, -1 on error | 
 | 3518 | */ | 
 | 3519 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3520 | static int | 
 | 3521 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3522 |                                  const char *encoding, const char *reason, | 
 | 3523 |                                  const char **input, const char **inend, Py_ssize_t *startinpos, | 
 | 3524 |                                  Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | 
 | 3525 |                                  PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3526 | { | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3527 |     static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3528 |  | 
 | 3529 |     PyObject *restuple = NULL; | 
 | 3530 |     PyObject *repunicode = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3531 |     Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3532 |     Py_ssize_t insize; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3533 |     Py_ssize_t requiredsize; | 
 | 3534 |     Py_ssize_t newpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3535 |     const Py_UNICODE *repptr; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3536 |     PyObject *inputobj = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3537 |     Py_ssize_t repsize; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3538 |     int res = -1; | 
 | 3539 |  | 
 | 3540 |     if (*errorHandler == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3541 |         *errorHandler = PyCodec_LookupError(errors); | 
 | 3542 |         if (*errorHandler == NULL) | 
 | 3543 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3544 |     } | 
 | 3545 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3546 |     make_decode_exception(exceptionObject, | 
 | 3547 |         encoding, | 
 | 3548 |         *input, *inend - *input, | 
 | 3549 |         *startinpos, *endinpos, | 
 | 3550 |         reason); | 
 | 3551 |     if (*exceptionObject == NULL) | 
 | 3552 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3553 |  | 
 | 3554 |     restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); | 
 | 3555 |     if (restuple == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3556 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3557 |     if (!PyTuple_Check(restuple)) { | 
| Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3558 |         PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3559 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3560 |     } | 
 | 3561 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3562 |         goto onError; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3563 |  | 
 | 3564 |     /* Copy back the bytes variables, which might have been modified by the | 
 | 3565 |        callback */ | 
 | 3566 |     inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); | 
 | 3567 |     if (!inputobj) | 
 | 3568 |         goto onError; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3569 |     if (!PyBytes_Check(inputobj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3570 |         PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3571 |     } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3572 |     *input = PyBytes_AS_STRING(inputobj); | 
 | 3573 |     insize = PyBytes_GET_SIZE(inputobj); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3574 |     *inend = *input + insize; | 
| Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3575 |     /* we can DECREF safely, as the exception has another reference, | 
 | 3576 |        so the object won't go away. */ | 
 | 3577 |     Py_DECREF(inputobj); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3578 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3579 |     if (newpos<0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3580 |         newpos = insize+newpos; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3581 |     if (newpos<0 || newpos>insize) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3582 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); | 
 | 3583 |         goto onError; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3584 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3585 |  | 
 | 3586 |     /* need more space? (at least enough for what we | 
 | 3587 |        have+the replacement+the rest of the string (starting | 
 | 3588 |        at the new input position), so we won't have to check space | 
 | 3589 |        when there are no errors in the rest of the string) */ | 
 | 3590 |     repptr = PyUnicode_AS_UNICODE(repunicode); | 
 | 3591 |     repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 3592 |     requiredsize = *outpos + repsize + insize-newpos; | 
 | 3593 |     if (requiredsize > outsize) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3594 |         if (requiredsize<2*outsize) | 
 | 3595 |             requiredsize = 2*outsize; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3596 |         if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3597 |             goto onError; | 
 | 3598 |         *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3599 |     } | 
 | 3600 |     *endinpos = newpos; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3601 |     *inptr = *input + newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3602 |     Py_UNICODE_COPY(*outptr, repptr, repsize); | 
 | 3603 |     *outptr += repsize; | 
 | 3604 |     *outpos += repsize; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3605 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3606 |     /* we made it! */ | 
 | 3607 |     res = 0; | 
 | 3608 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3609 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3610 |     Py_XDECREF(restuple); | 
 | 3611 |     return res; | 
 | 3612 | } | 
 | 3613 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3614 | /* --- UTF-7 Codec -------------------------------------------------------- */ | 
 | 3615 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3616 | /* See RFC2152 for details.  We encode conservatively and decode liberally. */ | 
 | 3617 |  | 
 | 3618 | /* Three simple macros defining base-64. */ | 
 | 3619 |  | 
 | 3620 | /* Is c a base-64 character? */ | 
 | 3621 |  | 
 | 3622 | #define IS_BASE64(c) \ | 
 | 3623 |     (((c) >= 'A' && (c) <= 'Z') ||     \ | 
 | 3624 |      ((c) >= 'a' && (c) <= 'z') ||     \ | 
 | 3625 |      ((c) >= '0' && (c) <= '9') ||     \ | 
 | 3626 |      (c) == '+' || (c) == '/') | 
 | 3627 |  | 
 | 3628 | /* given that c is a base-64 character, what is its base-64 value? */ | 
 | 3629 |  | 
 | 3630 | #define FROM_BASE64(c)                                                  \ | 
 | 3631 |     (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' :                           \ | 
 | 3632 |      ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 :                      \ | 
 | 3633 |      ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 :                      \ | 
 | 3634 |      (c) == '+' ? 62 : 63) | 
 | 3635 |  | 
 | 3636 | /* What is the base-64 character of the bottom 6 bits of n? */ | 
 | 3637 |  | 
 | 3638 | #define TO_BASE64(n)  \ | 
 | 3639 |     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) | 
 | 3640 |  | 
 | 3641 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be | 
 | 3642 |  * decoded as itself.  We are permissive on decoding; the only ASCII | 
 | 3643 |  * byte not decoding to itself is the + which begins a base64 | 
 | 3644 |  * string. */ | 
 | 3645 |  | 
 | 3646 | #define DECODE_DIRECT(c)                                \ | 
 | 3647 |     ((c) <= 127 && (c) != '+') | 
 | 3648 |  | 
 | 3649 | /* The UTF-7 encoder treats ASCII characters differently according to | 
 | 3650 |  * whether they are Set D, Set O, Whitespace, or special (i.e. none of | 
 | 3651 |  * the above).  See RFC2152.  This array identifies these different | 
 | 3652 |  * sets: | 
 | 3653 |  * 0 : "Set D" | 
 | 3654 |  *     alphanumeric and '(),-./:? | 
 | 3655 |  * 1 : "Set O" | 
 | 3656 |  *     !"#$%&*;<=>@[]^_`{|} | 
 | 3657 |  * 2 : "whitespace" | 
 | 3658 |  *     ht nl cr sp | 
 | 3659 |  * 3 : special (must be base64 encoded) | 
 | 3660 |  *     everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) | 
 | 3661 |  */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3662 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3663 | static | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3664 | char utf7_category[128] = { | 
 | 3665 | /* nul soh stx etx eot enq ack bel bs  ht  nl  vt  np  cr  so  si  */ | 
 | 3666 |     3,  3,  3,  3,  3,  3,  3,  3,  3,  2,  2,  3,  3,  2,  3,  3, | 
 | 3667 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em  sub esc fs  gs  rs  us  */ | 
 | 3668 |     3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3, | 
 | 3669 | /* sp   !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /  */ | 
 | 3670 |     2,  1,  1,  1,  1,  1,  1,  0,  0,  0,  1,  3,  0,  0,  0,  0, | 
 | 3671 | /*  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?  */ | 
 | 3672 |     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  0, | 
 | 3673 | /*  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O  */ | 
 | 3674 |     1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, | 
 | 3675 | /*  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _  */ | 
 | 3676 |     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  3,  1,  1,  1, | 
 | 3677 | /*  `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o  */ | 
 | 3678 |     1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, | 
 | 3679 | /*  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~  del */ | 
 | 3680 |     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] | 3681 | }; | 
 | 3682 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3683 | /* ENCODE_DIRECT: this character should be encoded as itself.  The | 
 | 3684 |  * answer depends on whether we are encoding set O as itself, and also | 
 | 3685 |  * on whether we are encoding whitespace as itself.  RFC2152 makes it | 
 | 3686 |  * clear that the answers to these questions vary between | 
 | 3687 |  * applications, so this code needs to be flexible.  */ | 
| Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3688 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3689 | #define ENCODE_DIRECT(c, directO, directWS)             \ | 
 | 3690 |     ((c) < 128 && (c) > 0 &&                            \ | 
 | 3691 |      ((utf7_category[(c)] == 0) ||                      \ | 
 | 3692 |       (directWS && (utf7_category[(c)] == 2)) ||        \ | 
 | 3693 |       (directO && (utf7_category[(c)] == 1)))) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3694 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3695 | PyObject * | 
 | 3696 | PyUnicode_DecodeUTF7(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3697 |                      Py_ssize_t size, | 
 | 3698 |                      const char *errors) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3699 | { | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3700 |     return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); | 
 | 3701 | } | 
 | 3702 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3703 | /* The decoder.  The only state we preserve is our read position, | 
 | 3704 |  * i.e. how many characters we have consumed.  So if we end in the | 
 | 3705 |  * middle of a shift sequence we have to back off the read position | 
 | 3706 |  * and the output to the beginning of the sequence, otherwise we lose | 
 | 3707 |  * all the shift state (seen bits, number of bits seen, high | 
 | 3708 |  * surrogate). */ | 
 | 3709 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3710 | PyObject * | 
 | 3711 | PyUnicode_DecodeUTF7Stateful(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3712 |                              Py_ssize_t size, | 
 | 3713 |                              const char *errors, | 
 | 3714 |                              Py_ssize_t *consumed) | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3715 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3716 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3717 |     Py_ssize_t startinpos; | 
 | 3718 |     Py_ssize_t endinpos; | 
 | 3719 |     Py_ssize_t outpos; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3720 |     const char *e; | 
 | 3721 |     PyUnicodeObject *unicode; | 
 | 3722 |     Py_UNICODE *p; | 
 | 3723 |     const char *errmsg = ""; | 
 | 3724 |     int inShift = 0; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3725 |     Py_UNICODE *shiftOutStart; | 
 | 3726 |     unsigned int base64bits = 0; | 
 | 3727 |     unsigned long base64buffer = 0; | 
 | 3728 |     Py_UNICODE surrogate = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3729 |     PyObject *errorHandler = NULL; | 
 | 3730 |     PyObject *exc = NULL; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3731 |  | 
 | 3732 |     unicode = _PyUnicode_New(size); | 
 | 3733 |     if (!unicode) | 
 | 3734 |         return NULL; | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3735 |     if (size == 0) { | 
 | 3736 |         if (consumed) | 
 | 3737 |             *consumed = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3738 |         return (PyObject *)unicode; | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3739 |     } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3740 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3741 |     p = PyUnicode_AS_UNICODE(unicode); | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3742 |     shiftOutStart = p; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3743 |     e = s + size; | 
 | 3744 |  | 
 | 3745 |     while (s < e) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3746 |         Py_UNICODE ch; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3747 |       restart: | 
| Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3748 |         ch = (unsigned char) *s; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3749 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3750 |         if (inShift) { /* in a base-64 section */ | 
 | 3751 |             if (IS_BASE64(ch)) { /* consume a base-64 character */ | 
 | 3752 |                 base64buffer = (base64buffer << 6) | FROM_BASE64(ch); | 
 | 3753 |                 base64bits += 6; | 
 | 3754 |                 s++; | 
 | 3755 |                 if (base64bits >= 16) { | 
 | 3756 |                     /* we have enough bits for a UTF-16 value */ | 
 | 3757 |                     Py_UNICODE outCh = (Py_UNICODE) | 
 | 3758 |                                        (base64buffer >> (base64bits-16)); | 
 | 3759 |                     base64bits -= 16; | 
 | 3760 |                     base64buffer &= (1 << base64bits) - 1; /* clear high bits */ | 
 | 3761 |                     if (surrogate) { | 
 | 3762 |                         /* expecting a second surrogate */ | 
 | 3763 |                         if (outCh >= 0xDC00 && outCh <= 0xDFFF) { | 
 | 3764 | #ifdef Py_UNICODE_WIDE | 
 | 3765 |                             *p++ = (((surrogate & 0x3FF)<<10) | 
 | 3766 |                                     | (outCh & 0x3FF)) + 0x10000; | 
 | 3767 | #else | 
 | 3768 |                             *p++ = surrogate; | 
 | 3769 |                             *p++ = outCh; | 
 | 3770 | #endif | 
 | 3771 |                             surrogate = 0; | 
 | 3772 |                         } | 
 | 3773 |                         else { | 
 | 3774 |                             surrogate = 0; | 
 | 3775 |                             errmsg = "second surrogate missing"; | 
 | 3776 |                             goto utf7Error; | 
 | 3777 |                         } | 
 | 3778 |                     } | 
 | 3779 |                     else if (outCh >= 0xD800 && outCh <= 0xDBFF) { | 
 | 3780 |                         /* first surrogate */ | 
 | 3781 |                         surrogate = outCh; | 
 | 3782 |                     } | 
 | 3783 |                     else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { | 
 | 3784 |                         errmsg = "unexpected second surrogate"; | 
 | 3785 |                         goto utf7Error; | 
 | 3786 |                     } | 
 | 3787 |                     else { | 
 | 3788 |                         *p++ = outCh; | 
 | 3789 |                     } | 
 | 3790 |                 } | 
 | 3791 |             } | 
 | 3792 |             else { /* now leaving a base-64 section */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3793 |                 inShift = 0; | 
 | 3794 |                 s++; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3795 |                 if (surrogate) { | 
 | 3796 |                     errmsg = "second surrogate missing at end of shift sequence"; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3797 |                     goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3798 |                 } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3799 |                 if (base64bits > 0) { /* left-over bits */ | 
 | 3800 |                     if (base64bits >= 6) { | 
 | 3801 |                         /* We've seen at least one base-64 character */ | 
 | 3802 |                         errmsg = "partial character in shift sequence"; | 
 | 3803 |                         goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3804 |                     } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3805 |                     else { | 
 | 3806 |                         /* Some bits remain; they should be zero */ | 
 | 3807 |                         if (base64buffer != 0) { | 
 | 3808 |                             errmsg = "non-zero padding bits in shift sequence"; | 
 | 3809 |                             goto utf7Error; | 
 | 3810 |                         } | 
 | 3811 |                     } | 
 | 3812 |                 } | 
 | 3813 |                 if (ch != '-') { | 
 | 3814 |                     /* '-' is absorbed; other terminating | 
 | 3815 |                        characters are preserved */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3816 |                     *p++ = ch; | 
 | 3817 |                 } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3818 |             } | 
 | 3819 |         } | 
 | 3820 |         else if ( ch == '+' ) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3821 |             startinpos = s-starts; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3822 |             s++; /* consume '+' */ | 
 | 3823 |             if (s < e && *s == '-') { /* '+-' encodes '+' */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3824 |                 s++; | 
 | 3825 |                 *p++ = '+'; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3826 |             } | 
 | 3827 |             else { /* begin base64-encoded section */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3828 |                 inShift = 1; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3829 |                 shiftOutStart = p; | 
 | 3830 |                 base64bits = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3831 |             } | 
 | 3832 |         } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3833 |         else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3834 |             *p++ = ch; | 
 | 3835 |             s++; | 
 | 3836 |         } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3837 |         else { | 
 | 3838 |             startinpos = s-starts; | 
 | 3839 |             s++; | 
 | 3840 |             errmsg = "unexpected special character"; | 
 | 3841 |             goto utf7Error; | 
 | 3842 |         } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3843 |         continue; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3844 | utf7Error: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3845 |         outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 3846 |         endinpos = s-starts; | 
 | 3847 |         if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3848 |                 errors, &errorHandler, | 
 | 3849 |                 "utf7", errmsg, | 
 | 3850 |                 &starts, &e, &startinpos, &endinpos, &exc, &s, | 
 | 3851 |                 &unicode, &outpos, &p)) | 
 | 3852 |             goto onError; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3853 |     } | 
 | 3854 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3855 |     /* end of string */ | 
 | 3856 |  | 
 | 3857 |     if (inShift && !consumed) { /* in shift sequence, no more to follow */ | 
 | 3858 |         /* if we're in an inconsistent state, that's an error */ | 
 | 3859 |         if (surrogate || | 
 | 3860 |                 (base64bits >= 6) || | 
 | 3861 |                 (base64bits > 0 && base64buffer != 0)) { | 
 | 3862 |             outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 3863 |             endinpos = size; | 
 | 3864 |             if (unicode_decode_call_errorhandler( | 
 | 3865 |                     errors, &errorHandler, | 
 | 3866 |                     "utf7", "unterminated shift sequence", | 
 | 3867 |                     &starts, &e, &startinpos, &endinpos, &exc, &s, | 
 | 3868 |                     &unicode, &outpos, &p)) | 
 | 3869 |                 goto onError; | 
 | 3870 |             if (s < e) | 
 | 3871 |                 goto restart; | 
 | 3872 |         } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3873 |     } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3874 |  | 
 | 3875 |     /* return state */ | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3876 |     if (consumed) { | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3877 |         if (inShift) { | 
 | 3878 |             p = shiftOutStart; /* back off output */ | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3879 |             *consumed = startinpos; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3880 |         } | 
 | 3881 |         else { | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3882 |             *consumed = s-starts; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3883 |         } | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3884 |     } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3885 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3886 |     if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3887 |         goto onError; | 
 | 3888 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3889 |     Py_XDECREF(errorHandler); | 
 | 3890 |     Py_XDECREF(exc); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3891 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3892 |     if (_PyUnicode_READY_REPLACE(&unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3893 |         Py_DECREF(unicode); | 
 | 3894 |         return NULL; | 
 | 3895 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3896 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3897 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3898 |     return (PyObject *)unicode; | 
 | 3899 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3900 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3901 |     Py_XDECREF(errorHandler); | 
 | 3902 |     Py_XDECREF(exc); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3903 |     Py_DECREF(unicode); | 
 | 3904 |     return NULL; | 
 | 3905 | } | 
 | 3906 |  | 
 | 3907 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3908 | PyObject * | 
 | 3909 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3910 |                      Py_ssize_t size, | 
 | 3911 |                      int base64SetO, | 
 | 3912 |                      int base64WhiteSpace, | 
 | 3913 |                      const char *errors) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3914 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3915 |     PyObject *v; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3916 |     /* It might be possible to tighten this worst case */ | 
| Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3917 |     Py_ssize_t allocated = 8 * size; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3918 |     int inShift = 0; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3919 |     Py_ssize_t i = 0; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3920 |     unsigned int base64bits = 0; | 
 | 3921 |     unsigned long base64buffer = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3922 |     char * out; | 
 | 3923 |     char * start; | 
 | 3924 |  | 
 | 3925 |     if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3926 |         return PyBytes_FromStringAndSize(NULL, 0); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3927 |  | 
| Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3928 |     if (allocated / 8 != size) | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3929 |         return PyErr_NoMemory(); | 
 | 3930 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3931 |     v = PyBytes_FromStringAndSize(NULL, allocated); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3932 |     if (v == NULL) | 
 | 3933 |         return NULL; | 
 | 3934 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3935 |     start = out = PyBytes_AS_STRING(v); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3936 |     for (;i < size; ++i) { | 
 | 3937 |         Py_UNICODE ch = s[i]; | 
 | 3938 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3939 |         if (inShift) { | 
 | 3940 |             if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { | 
 | 3941 |                 /* shifting out */ | 
 | 3942 |                 if (base64bits) { /* output remaining bits */ | 
 | 3943 |                     *out++ = TO_BASE64(base64buffer << (6-base64bits)); | 
 | 3944 |                     base64buffer = 0; | 
 | 3945 |                     base64bits = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3946 |                 } | 
 | 3947 |                 inShift = 0; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3948 |                 /* Characters not in the BASE64 set implicitly unshift the sequence | 
 | 3949 |                    so no '-' is required, except if the character is itself a '-' */ | 
 | 3950 |                 if (IS_BASE64(ch) || ch == '-') { | 
 | 3951 |                     *out++ = '-'; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3952 |                 } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3953 |                 *out++ = (char) ch; | 
 | 3954 |             } | 
 | 3955 |             else { | 
 | 3956 |                 goto encode_char; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3957 |             } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3958 |         } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3959 |         else { /* not in a shift sequence */ | 
 | 3960 |             if (ch == '+') { | 
 | 3961 |                 *out++ = '+'; | 
 | 3962 |                         *out++ = '-'; | 
 | 3963 |             } | 
 | 3964 |             else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { | 
 | 3965 |                 *out++ = (char) ch; | 
 | 3966 |             } | 
 | 3967 |             else { | 
 | 3968 |                 *out++ = '+'; | 
 | 3969 |                 inShift = 1; | 
 | 3970 |                 goto encode_char; | 
 | 3971 |             } | 
 | 3972 |         } | 
 | 3973 |         continue; | 
 | 3974 | encode_char: | 
 | 3975 | #ifdef Py_UNICODE_WIDE | 
 | 3976 |         if (ch >= 0x10000) { | 
 | 3977 |             /* code first surrogate */ | 
 | 3978 |             base64bits += 16; | 
 | 3979 |             base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); | 
 | 3980 |             while (base64bits >= 6) { | 
 | 3981 |                 *out++ = TO_BASE64(base64buffer >> (base64bits-6)); | 
 | 3982 |                 base64bits -= 6; | 
 | 3983 |             } | 
 | 3984 |             /* prepare second surrogate */ | 
 | 3985 |             ch =  0xDC00 | ((ch-0x10000) & 0x3FF); | 
 | 3986 |         } | 
 | 3987 | #endif | 
 | 3988 |         base64bits += 16; | 
 | 3989 |         base64buffer = (base64buffer << 16) | ch; | 
 | 3990 |         while (base64bits >= 6) { | 
 | 3991 |             *out++ = TO_BASE64(base64buffer >> (base64bits-6)); | 
 | 3992 |             base64bits -= 6; | 
 | 3993 |         } | 
| Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3994 |     } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3995 |     if (base64bits) | 
 | 3996 |         *out++= TO_BASE64(base64buffer << (6-base64bits) ); | 
 | 3997 |     if (inShift) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3998 |         *out++ = '-'; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3999 |     if (_PyBytes_Resize(&v, out - start) < 0) | 
 | 4000 |         return NULL; | 
 | 4001 |     return v; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4002 | } | 
 | 4003 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4004 | #undef IS_BASE64 | 
 | 4005 | #undef FROM_BASE64 | 
 | 4006 | #undef TO_BASE64 | 
 | 4007 | #undef DECODE_DIRECT | 
 | 4008 | #undef ENCODE_DIRECT | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4009 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4010 | /* --- UTF-8 Codec -------------------------------------------------------- */ | 
 | 4011 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4012 | static | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4013 | char utf8_code_length[256] = { | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4014 |     /* Map UTF-8 encoded prefix byte to sequence length.  Zero means | 
 | 4015 |        illegal prefix.  See RFC 3629 for details */ | 
 | 4016 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ | 
 | 4017 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 4018 |     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] | 4019 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 4020 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 4021 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
 | 4022 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4023 |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ | 
 | 4024 |     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] | 4025 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
 | 4026 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4027 |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ | 
 | 4028 |     0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ | 
 | 4029 |     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ | 
 | 4030 |     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ | 
 | 4031 |     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] | 4032 | }; | 
 | 4033 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4034 | PyObject * | 
 | 4035 | PyUnicode_DecodeUTF8(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4036 |                      Py_ssize_t size, | 
 | 4037 |                      const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4038 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4039 |     return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); | 
 | 4040 | } | 
 | 4041 |  | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4042 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ | 
 | 4043 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) | 
 | 4044 |  | 
 | 4045 | /* Mask to quickly check whether a C 'long' contains a | 
 | 4046 |    non-ASCII, UTF8-encoded char. */ | 
 | 4047 | #if (SIZEOF_LONG == 8) | 
 | 4048 | # define ASCII_CHAR_MASK 0x8080808080808080L | 
 | 4049 | #elif (SIZEOF_LONG == 4) | 
 | 4050 | # define ASCII_CHAR_MASK 0x80808080L | 
 | 4051 | #else | 
 | 4052 | # error C 'long' size should be either 4 or 8! | 
 | 4053 | #endif | 
 | 4054 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4055 | /* Scans a UTF-8 string and returns the maximum character to be expected, | 
 | 4056 |    the size of the decoded unicode string and if any major errors were | 
 | 4057 |    encountered. | 
 | 4058 |  | 
 | 4059 |    This function does check basic UTF-8 sanity, it does however NOT CHECK | 
 | 4060 |    if the string contains surrogates, and if all continuation bytes are | 
 | 4061 |    within the correct ranges, these checks are performed in | 
 | 4062 |    PyUnicode_DecodeUTF8Stateful. | 
 | 4063 |  | 
 | 4064 |    If it sets has_errors to 1, it means the value of unicode_size and max_char | 
 | 4065 |    will be bogus and you should not rely on useful information in them. | 
 | 4066 |    */ | 
 | 4067 | static Py_UCS4 | 
 | 4068 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, | 
 | 4069 |                                   Py_ssize_t *unicode_size, Py_ssize_t* consumed, | 
 | 4070 |                                   int *has_errors) | 
 | 4071 | { | 
 | 4072 |     Py_ssize_t n; | 
 | 4073 |     Py_ssize_t char_count = 0; | 
 | 4074 |     Py_UCS4 max_char = 127, new_max; | 
 | 4075 |     Py_UCS4 upper_bound; | 
 | 4076 |     const unsigned char *p = (const unsigned char *)s; | 
 | 4077 |     const unsigned char *end = p + string_size; | 
 | 4078 |     const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); | 
 | 4079 |     int err = 0; | 
 | 4080 |  | 
 | 4081 |     for (; p < end && !err; ++p, ++char_count) { | 
 | 4082 |         /* Only check value if it's not a ASCII char... */ | 
 | 4083 |         if (*p < 0x80) { | 
 | 4084 |             /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for | 
 | 4085 |                an explanation. */ | 
 | 4086 |             if (!((size_t) p & LONG_PTR_MASK)) { | 
 | 4087 |                 /* Help register allocation */ | 
 | 4088 |                 register const unsigned char *_p = p; | 
 | 4089 |                 while (_p < aligned_end) { | 
 | 4090 |                     unsigned long value = *(unsigned long *) _p; | 
 | 4091 |                     if (value & ASCII_CHAR_MASK) | 
 | 4092 |                         break; | 
 | 4093 |                     _p += SIZEOF_LONG; | 
 | 4094 |                     char_count += SIZEOF_LONG; | 
 | 4095 |                 } | 
 | 4096 |                 p = _p; | 
 | 4097 |                 if (p == end) | 
 | 4098 |                     break; | 
 | 4099 |             } | 
 | 4100 |         } | 
 | 4101 |         if (*p >= 0x80) { | 
 | 4102 |             n = utf8_code_length[*p]; | 
 | 4103 |             new_max = max_char; | 
 | 4104 |             switch (n) { | 
 | 4105 |             /* invalid start byte */ | 
 | 4106 |             case 0: | 
 | 4107 |                 err = 1; | 
 | 4108 |                 break; | 
 | 4109 |             case 2: | 
 | 4110 |                 /* Code points between 0x00FF and 0x07FF inclusive. | 
 | 4111 |                    Approximate the upper bound of the code point, | 
 | 4112 |                    if this flips over 255 we can be sure it will be more | 
 | 4113 |                    than 255 and the string will need 2 bytes per code coint, | 
 | 4114 |                    if it stays under or equal to 255, we can be sure 1 byte | 
 | 4115 |                    is enough. | 
 | 4116 |                    ((*p & 0b00011111) << 6) | 0b00111111 */ | 
 | 4117 |                 upper_bound = ((*p & 0x1F) << 6) | 0x3F; | 
 | 4118 |                 if (max_char < upper_bound) | 
 | 4119 |                     new_max = upper_bound; | 
 | 4120 |                 /* Ensure we track at least that we left ASCII space. */ | 
 | 4121 |                 if (new_max < 128) | 
 | 4122 |                     new_max = 128; | 
 | 4123 |                 break; | 
 | 4124 |             case 3: | 
 | 4125 |                 /* Between 0x0FFF and 0xFFFF inclusive, so values are | 
 | 4126 |                    always > 255 and <= 65535 and will always need 2 bytes. */ | 
 | 4127 |                 if (max_char < 65535) | 
 | 4128 |                     new_max = 65535; | 
 | 4129 |                 break; | 
 | 4130 |             case 4: | 
 | 4131 |                 /* Code point will be above 0xFFFF for sure in this case. */ | 
 | 4132 |                 new_max = 65537; | 
 | 4133 |                 break; | 
 | 4134 |             /* Internal error, this should be caught by the first if */ | 
 | 4135 |             case 1: | 
 | 4136 |             default: | 
 | 4137 |                 assert(0 && "Impossible case in utf8_max_char_and_size"); | 
 | 4138 |                 err = 1; | 
 | 4139 |             } | 
 | 4140 |             /* Instead of number of overall bytes for this code point, | 
| Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4141 |                n contains the number of following bytes: */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4142 |             --n; | 
 | 4143 |             /* Check if the follow up chars are all valid continuation bytes */ | 
 | 4144 |             if (n >= 1) { | 
 | 4145 |                 const unsigned char *cont; | 
 | 4146 |                 if ((p + n) >= end) { | 
 | 4147 |                     if (consumed == 0) | 
 | 4148 |                         /* incomplete data, non-incremental decoding */ | 
 | 4149 |                         err = 1; | 
 | 4150 |                     break; | 
 | 4151 |                 } | 
 | 4152 |                 for (cont = p + 1; cont < (p + n); ++cont) { | 
 | 4153 |                     if ((*cont & 0xc0) != 0x80) { | 
 | 4154 |                         err = 1; | 
 | 4155 |                         break; | 
 | 4156 |                     } | 
 | 4157 |                 } | 
 | 4158 |                 p += n; | 
 | 4159 |             } | 
 | 4160 |             else | 
 | 4161 |                 err = 1; | 
 | 4162 |             max_char = new_max; | 
 | 4163 |         } | 
 | 4164 |     } | 
 | 4165 |  | 
 | 4166 |     if (unicode_size) | 
 | 4167 |         *unicode_size = char_count; | 
 | 4168 |     if (has_errors) | 
 | 4169 |         *has_errors = err; | 
 | 4170 |     return max_char; | 
 | 4171 | } | 
 | 4172 |  | 
 | 4173 | /* Similar to PyUnicode_WRITE but can also write into wstr field | 
 | 4174 |    of the legacy unicode representation */ | 
 | 4175 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ | 
 | 4176 |     do { \ | 
 | 4177 |         const int k_ = (kind); \ | 
 | 4178 |         if (k_ == PyUnicode_WCHAR_KIND) \ | 
 | 4179 |             ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ | 
 | 4180 |         else if (k_ == PyUnicode_1BYTE_KIND) \ | 
 | 4181 |             ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ | 
 | 4182 |         else if (k_ == PyUnicode_2BYTE_KIND) \ | 
 | 4183 |             ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ | 
 | 4184 |         else \ | 
 | 4185 |             ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ | 
 | 4186 |     } while (0) | 
 | 4187 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4188 | PyObject * | 
 | 4189 | PyUnicode_DecodeUTF8Stateful(const char *s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4190 |                              Py_ssize_t size, | 
 | 4191 |                              const char *errors, | 
 | 4192 |                              Py_ssize_t *consumed) | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4193 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4194 |     const char *starts = s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4195 |     int n; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4196 |     int k; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4197 |     Py_ssize_t startinpos; | 
 | 4198 |     Py_ssize_t endinpos; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4199 |     const char *e, *aligned_end; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4200 |     PyUnicodeObject *unicode; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4201 |     const char *errmsg = ""; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4202 |     PyObject *errorHandler = NULL; | 
 | 4203 |     PyObject *exc = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4204 |     Py_UCS4 maxchar = 0; | 
 | 4205 |     Py_ssize_t unicode_size; | 
 | 4206 |     Py_ssize_t i; | 
 | 4207 |     int kind; | 
 | 4208 |     void *data; | 
 | 4209 |     int has_errors; | 
 | 4210 |     Py_UNICODE *error_outptr; | 
 | 4211 | #if SIZEOF_WCHAR_T == 2 | 
 | 4212 |     Py_ssize_t wchar_offset = 0; | 
 | 4213 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4214 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4215 |     if (size == 0) { | 
 | 4216 |         if (consumed) | 
 | 4217 |             *consumed = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4218 |         return (PyObject *)PyUnicode_New(0, 0); | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4219 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4220 |     maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, | 
 | 4221 |                                                 consumed, &has_errors); | 
 | 4222 |     if (has_errors) { | 
 | 4223 |         unicode = _PyUnicode_New(size); | 
 | 4224 |         if (!unicode) | 
 | 4225 |             return NULL; | 
 | 4226 |         kind = PyUnicode_WCHAR_KIND; | 
 | 4227 |         data = PyUnicode_AS_UNICODE(unicode); | 
 | 4228 |         assert(data != NULL); | 
 | 4229 |     } | 
 | 4230 |     else { | 
 | 4231 |         unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); | 
 | 4232 |         if (!unicode) | 
 | 4233 |             return NULL; | 
 | 4234 |         /* When the string is ASCII only, just use memcpy and return. | 
 | 4235 |            unicode_size may be != size if there is an incomplete UTF-8 | 
 | 4236 |            sequence at the end of the ASCII block.  */ | 
 | 4237 |         if (maxchar < 128 && size == unicode_size) { | 
 | 4238 |             Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); | 
 | 4239 |             return (PyObject *)unicode; | 
 | 4240 |         } | 
 | 4241 |         kind = PyUnicode_KIND(unicode); | 
 | 4242 |         data = PyUnicode_DATA(unicode); | 
 | 4243 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4244 |     /* Unpack UTF-8 encoded data */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4245 |     i = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4246 |     e = s + size; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4247 |     aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4248 |  | 
 | 4249 |     while (s < e) { | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4250 |         Py_UCS4 ch = (unsigned char)*s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4251 |  | 
 | 4252 |         if (ch < 0x80) { | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4253 |             /* Fast path for runs of ASCII characters. Given that common UTF-8 | 
 | 4254 |                input will consist of an overwhelming majority of ASCII | 
 | 4255 |                characters, we try to optimize for this case by checking | 
 | 4256 |                as many characters as a C 'long' can contain. | 
 | 4257 |                First, check if we can do an aligned read, as most CPUs have | 
 | 4258 |                a penalty for unaligned reads. | 
 | 4259 |             */ | 
 | 4260 |             if (!((size_t) s & LONG_PTR_MASK)) { | 
 | 4261 |                 /* Help register allocation */ | 
 | 4262 |                 register const char *_s = s; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4263 |                 register Py_ssize_t _i = i; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4264 |                 while (_s < aligned_end) { | 
 | 4265 |                     /* Read a whole long at a time (either 4 or 8 bytes), | 
 | 4266 |                        and do a fast unrolled copy if it only contains ASCII | 
 | 4267 |                        characters. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4268 |                     unsigned long value = *(unsigned long *) _s; | 
 | 4269 |                     if (value & ASCII_CHAR_MASK) | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4270 |                         break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4271 |                     WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); | 
 | 4272 |                     WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); | 
 | 4273 |                     WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); | 
 | 4274 |                     WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4275 | #if (SIZEOF_LONG == 8) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4276 |                     WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); | 
 | 4277 |                     WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); | 
 | 4278 |                     WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); | 
 | 4279 |                     WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4280 | #endif | 
 | 4281 |                     _s += SIZEOF_LONG; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4282 |                     _i += SIZEOF_LONG; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4283 |                 } | 
 | 4284 |                 s = _s; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4285 |                 i = _i; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4286 |                 if (s == e) | 
 | 4287 |                     break; | 
 | 4288 |                 ch = (unsigned char)*s; | 
 | 4289 |             } | 
 | 4290 |         } | 
 | 4291 |  | 
 | 4292 |         if (ch < 0x80) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4293 |             WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4294 |             s++; | 
 | 4295 |             continue; | 
 | 4296 |         } | 
 | 4297 |  | 
 | 4298 |         n = utf8_code_length[ch]; | 
 | 4299 |  | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4300 |         if (s + n > e) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4301 |             if (consumed) | 
 | 4302 |                 break; | 
 | 4303 |             else { | 
 | 4304 |                 errmsg = "unexpected end of data"; | 
 | 4305 |                 startinpos = s-starts; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4306 |                 endinpos = startinpos+1; | 
 | 4307 |                 for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) | 
 | 4308 |                     endinpos++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4309 |                 goto utf8Error; | 
 | 4310 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4311 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4312 |  | 
 | 4313 |         switch (n) { | 
 | 4314 |  | 
 | 4315 |         case 0: | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4316 |             errmsg = "invalid start byte"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4317 |             startinpos = s-starts; | 
 | 4318 |             endinpos = startinpos+1; | 
 | 4319 |             goto utf8Error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4320 |  | 
 | 4321 |         case 1: | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4322 |             errmsg = "internal error"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4323 |             startinpos = s-starts; | 
 | 4324 |             endinpos = startinpos+1; | 
 | 4325 |             goto utf8Error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4326 |  | 
 | 4327 |         case 2: | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4328 |             if ((s[1] & 0xc0) != 0x80) { | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4329 |                 errmsg = "invalid continuation byte"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4330 |                 startinpos = s-starts; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4331 |                 endinpos = startinpos + 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4332 |                 goto utf8Error; | 
 | 4333 |             } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4334 |             ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4335 |             assert ((ch > 0x007F) && (ch <= 0x07FF)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4336 |             WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4337 |             break; | 
 | 4338 |  | 
 | 4339 |         case 3: | 
| Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4340 |             /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf | 
 | 4341 |                will result in surrogates in range d800-dfff. Surrogates are | 
 | 4342 |                not valid UTF-8 so they are rejected. | 
 | 4343 |                See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf | 
 | 4344 |                (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4345 |             if ((s[1] & 0xc0) != 0x80 || | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4346 |                 (s[2] & 0xc0) != 0x80 || | 
 | 4347 |                 ((unsigned char)s[0] == 0xE0 && | 
 | 4348 |                  (unsigned char)s[1] < 0xA0) || | 
 | 4349 |                 ((unsigned char)s[0] == 0xED && | 
 | 4350 |                  (unsigned char)s[1] > 0x9F)) { | 
 | 4351 |                 errmsg = "invalid continuation byte"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4352 |                 startinpos = s-starts; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4353 |                 endinpos = startinpos + 1; | 
 | 4354 |  | 
 | 4355 |                 /* if s[1] first two bits are 1 and 0, then the invalid | 
 | 4356 |                    continuation byte is s[2], so increment endinpos by 1, | 
 | 4357 |                    if not, s[1] is invalid and endinpos doesn't need to | 
 | 4358 |                    be incremented. */ | 
 | 4359 |                 if ((s[1] & 0xC0) == 0x80) | 
 | 4360 |                     endinpos++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4361 |                 goto utf8Error; | 
 | 4362 |             } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4363 |             ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4364 |             assert ((ch > 0x07FF) && (ch <= 0xFFFF)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4365 |             WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4366 |             break; | 
 | 4367 |  | 
 | 4368 |         case 4: | 
 | 4369 |             if ((s[1] & 0xc0) != 0x80 || | 
 | 4370 |                 (s[2] & 0xc0) != 0x80 || | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4371 |                 (s[3] & 0xc0) != 0x80 || | 
 | 4372 |                 ((unsigned char)s[0] == 0xF0 && | 
 | 4373 |                  (unsigned char)s[1] < 0x90) || | 
 | 4374 |                 ((unsigned char)s[0] == 0xF4 && | 
 | 4375 |                  (unsigned char)s[1] > 0x8F)) { | 
 | 4376 |                 errmsg = "invalid continuation byte"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4377 |                 startinpos = s-starts; | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4378 |                 endinpos = startinpos + 1; | 
 | 4379 |                 if ((s[1] & 0xC0) == 0x80) { | 
 | 4380 |                     endinpos++; | 
 | 4381 |                     if ((s[2] & 0xC0) == 0x80) | 
 | 4382 |                         endinpos++; | 
 | 4383 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4384 |                 goto utf8Error; | 
 | 4385 |             } | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4386 |             ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + | 
| Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4387 |                  ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); | 
 | 4388 |             assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); | 
 | 4389 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4390 |             /* If the string is flexible or we have native UCS-4, write | 
 | 4391 |                directly.. */ | 
 | 4392 |             if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) | 
 | 4393 |                 WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4394 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4395 |             else { | 
 | 4396 |                 /* compute and append the two surrogates: */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4397 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4398 |                 /* translate from 10000..10FFFF to 0..FFFF */ | 
 | 4399 |                 ch -= 0x10000; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4400 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4401 |                 /* high surrogate = top 10 bits added to D800 */ | 
 | 4402 |                 WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, | 
 | 4403 |                                        (Py_UNICODE)(0xD800 + (ch >> 10))); | 
 | 4404 |  | 
 | 4405 |                 /* low surrogate = bottom 10 bits added to DC00 */ | 
 | 4406 |                 WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, | 
 | 4407 |                                        (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); | 
 | 4408 |             } | 
 | 4409 | #if SIZEOF_WCHAR_T == 2 | 
 | 4410 |             wchar_offset++; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4411 | #endif | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4412 |             break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4413 |         } | 
 | 4414 |         s += n; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4415 |         continue; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4416 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4417 |       utf8Error: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4418 |         /* If this is not yet a resizable string, make it one.. */ | 
 | 4419 |         if (kind != PyUnicode_WCHAR_KIND) { | 
 | 4420 |             const Py_UNICODE *u; | 
 | 4421 |             PyUnicodeObject *new_unicode = _PyUnicode_New(size); | 
 | 4422 |             if (!new_unicode) | 
 | 4423 |                 goto onError; | 
 | 4424 |             u = PyUnicode_AsUnicode((PyObject *)unicode); | 
 | 4425 |             if (!u) | 
 | 4426 |                 goto onError; | 
 | 4427 | #if SIZEOF_WCHAR_T == 2 | 
 | 4428 |             i += wchar_offset; | 
 | 4429 | #endif | 
 | 4430 |             Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); | 
 | 4431 |             Py_DECREF(unicode); | 
 | 4432 |             unicode = new_unicode; | 
 | 4433 |             kind = 0; | 
 | 4434 |             data = PyUnicode_AS_UNICODE(new_unicode); | 
 | 4435 |             assert(data != NULL); | 
 | 4436 |         } | 
 | 4437 |         error_outptr = PyUnicode_AS_UNICODE(unicode) + i; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4438 |         if (unicode_decode_call_errorhandler( | 
 | 4439 |                 errors, &errorHandler, | 
 | 4440 |                 "utf8", errmsg, | 
 | 4441 |                 &starts, &e, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4442 |                 &unicode, &i, &error_outptr)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4443 |             goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4444 |         /* Update data because unicode_decode_call_errorhandler might have | 
 | 4445 |            re-created or resized the unicode object. */ | 
 | 4446 |         data = PyUnicode_AS_UNICODE(unicode); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4447 |         aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4448 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4449 |     /* Ensure the unicode_size calculation above was correct: */ | 
 | 4450 |     assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); | 
 | 4451 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4452 |     if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4453 |         *consumed = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4454 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4455 |     /* Adjust length and ready string when it contained errors and | 
 | 4456 |        is of the old resizable kind. */ | 
 | 4457 |     if (kind == PyUnicode_WCHAR_KIND) { | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4458 |         if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4459 |             goto onError; | 
 | 4460 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4461 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4462 |     Py_XDECREF(errorHandler); | 
 | 4463 |     Py_XDECREF(exc); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4464 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4465 |     if (_PyUnicode_READY_REPLACE(&unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4466 |         Py_DECREF(unicode); | 
 | 4467 |         return NULL; | 
 | 4468 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4469 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4470 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4471 |     return (PyObject *)unicode; | 
 | 4472 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4473 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4474 |     Py_XDECREF(errorHandler); | 
 | 4475 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 |     Py_DECREF(unicode); | 
 | 4477 |     return NULL; | 
 | 4478 | } | 
 | 4479 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4480 | #undef WRITE_FLEXIBLE_OR_WSTR | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4481 |  | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4482 | #ifdef __APPLE__ | 
 | 4483 |  | 
 | 4484 | /* Simplified UTF-8 decoder using surrogateescape error handler, | 
 | 4485 |    used to decode the command line arguments on Mac OS X. */ | 
 | 4486 |  | 
 | 4487 | wchar_t* | 
 | 4488 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) | 
 | 4489 | { | 
 | 4490 |     int n; | 
 | 4491 |     const char *e; | 
 | 4492 |     wchar_t *unicode, *p; | 
 | 4493 |  | 
 | 4494 |     /* Note: size will always be longer than the resulting Unicode | 
 | 4495 |        character count */ | 
 | 4496 |     if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { | 
 | 4497 |         PyErr_NoMemory(); | 
 | 4498 |         return NULL; | 
 | 4499 |     } | 
 | 4500 |     unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); | 
 | 4501 |     if (!unicode) | 
 | 4502 |         return NULL; | 
 | 4503 |  | 
 | 4504 |     /* Unpack UTF-8 encoded data */ | 
 | 4505 |     p = unicode; | 
 | 4506 |     e = s + size; | 
 | 4507 |     while (s < e) { | 
 | 4508 |         Py_UCS4 ch = (unsigned char)*s; | 
 | 4509 |  | 
 | 4510 |         if (ch < 0x80) { | 
 | 4511 |             *p++ = (wchar_t)ch; | 
 | 4512 |             s++; | 
 | 4513 |             continue; | 
 | 4514 |         } | 
 | 4515 |  | 
 | 4516 |         n = utf8_code_length[ch]; | 
 | 4517 |         if (s + n > e) { | 
 | 4518 |             goto surrogateescape; | 
 | 4519 |         } | 
 | 4520 |  | 
 | 4521 |         switch (n) { | 
 | 4522 |         case 0: | 
 | 4523 |         case 1: | 
 | 4524 |             goto surrogateescape; | 
 | 4525 |  | 
 | 4526 |         case 2: | 
 | 4527 |             if ((s[1] & 0xc0) != 0x80) | 
 | 4528 |                 goto surrogateescape; | 
 | 4529 |             ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); | 
 | 4530 |             assert ((ch > 0x007F) && (ch <= 0x07FF)); | 
 | 4531 |             *p++ = (wchar_t)ch; | 
 | 4532 |             break; | 
 | 4533 |  | 
 | 4534 |         case 3: | 
 | 4535 |             /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf | 
 | 4536 |                will result in surrogates in range d800-dfff. Surrogates are | 
 | 4537 |                not valid UTF-8 so they are rejected. | 
 | 4538 |                See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf | 
 | 4539 |                (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ | 
 | 4540 |             if ((s[1] & 0xc0) != 0x80 || | 
 | 4541 |                 (s[2] & 0xc0) != 0x80 || | 
 | 4542 |                 ((unsigned char)s[0] == 0xE0 && | 
 | 4543 |                  (unsigned char)s[1] < 0xA0) || | 
 | 4544 |                 ((unsigned char)s[0] == 0xED && | 
 | 4545 |                  (unsigned char)s[1] > 0x9F)) { | 
 | 4546 |  | 
 | 4547 |                 goto surrogateescape; | 
 | 4548 |             } | 
 | 4549 |             ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); | 
 | 4550 |             assert ((ch > 0x07FF) && (ch <= 0xFFFF)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4551 |             *p++ = (wchar_t)ch; | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4552 |             break; | 
 | 4553 |  | 
 | 4554 |         case 4: | 
 | 4555 |             if ((s[1] & 0xc0) != 0x80 || | 
 | 4556 |                 (s[2] & 0xc0) != 0x80 || | 
 | 4557 |                 (s[3] & 0xc0) != 0x80 || | 
 | 4558 |                 ((unsigned char)s[0] == 0xF0 && | 
 | 4559 |                  (unsigned char)s[1] < 0x90) || | 
 | 4560 |                 ((unsigned char)s[0] == 0xF4 && | 
 | 4561 |                  (unsigned char)s[1] > 0x8F)) { | 
 | 4562 |                 goto surrogateescape; | 
 | 4563 |             } | 
 | 4564 |             ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + | 
 | 4565 |                  ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); | 
 | 4566 |             assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); | 
 | 4567 |  | 
 | 4568 | #if SIZEOF_WCHAR_T == 4 | 
 | 4569 |             *p++ = (wchar_t)ch; | 
 | 4570 | #else | 
 | 4571 |             /*  compute and append the two surrogates: */ | 
 | 4572 |  | 
 | 4573 |             /*  translate from 10000..10FFFF to 0..FFFF */ | 
 | 4574 |             ch -= 0x10000; | 
 | 4575 |  | 
 | 4576 |             /*  high surrogate = top 10 bits added to D800 */ | 
 | 4577 |             *p++ = (wchar_t)(0xD800 + (ch >> 10)); | 
 | 4578 |  | 
 | 4579 |             /*  low surrogate = bottom 10 bits added to DC00 */ | 
 | 4580 |             *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); | 
 | 4581 | #endif | 
 | 4582 |             break; | 
 | 4583 |         } | 
 | 4584 |         s += n; | 
 | 4585 |         continue; | 
 | 4586 |  | 
 | 4587 |       surrogateescape: | 
 | 4588 |         *p++ = 0xDC00 + ch; | 
 | 4589 |         s++; | 
 | 4590 |     } | 
 | 4591 |     *p = L'\0'; | 
 | 4592 |     return unicode; | 
 | 4593 | } | 
 | 4594 |  | 
 | 4595 | #endif /* __APPLE__ */ | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4596 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4597 | /* Primary internal function which creates utf8 encoded bytes objects. | 
 | 4598 |  | 
 | 4599 |    Allocation strategy:  if the string is short, convert into a stack buffer | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4600 |    and allocate exactly as much space needed at the end.  Else allocate the | 
 | 4601 |    maximum possible needed (4 result bytes per Unicode character), and return | 
 | 4602 |    the excess memory at the end. | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4603 | */ | 
| Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4604 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4605 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4606 | { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4607 | #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] | 4608 |  | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4609 |     Py_ssize_t i;                /* index into s of next input byte */ | 
 | 4610 |     PyObject *result;            /* result string object */ | 
 | 4611 |     char *p;                     /* next free byte in output buffer */ | 
 | 4612 |     Py_ssize_t nallocated;      /* number of result bytes allocated */ | 
 | 4613 |     Py_ssize_t nneeded;            /* number of result bytes needed */ | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4614 |     char stackbuf[MAX_SHORT_UNICHARS * 4]; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4615 |     PyObject *errorHandler = NULL; | 
 | 4616 |     PyObject *exc = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4617 |     int kind; | 
 | 4618 |     void *data; | 
 | 4619 |     Py_ssize_t size; | 
 | 4620 |     PyUnicodeObject *unicode = (PyUnicodeObject *)obj; | 
 | 4621 | #if SIZEOF_WCHAR_T == 2 | 
 | 4622 |     Py_ssize_t wchar_offset = 0; | 
 | 4623 | #endif | 
| Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4624 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4625 |     if (!PyUnicode_Check(unicode)) { | 
 | 4626 |         PyErr_BadArgument(); | 
 | 4627 |         return NULL; | 
 | 4628 |     } | 
 | 4629 |  | 
 | 4630 |     if (PyUnicode_READY(unicode) == -1) | 
 | 4631 |         return NULL; | 
 | 4632 |  | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4633 |     if (PyUnicode_UTF8(unicode)) | 
 | 4634 |         return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), | 
 | 4635 |                                          PyUnicode_UTF8_LENGTH(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4636 |  | 
 | 4637 |     kind = PyUnicode_KIND(unicode); | 
 | 4638 |     data = PyUnicode_DATA(unicode); | 
 | 4639 |     size = PyUnicode_GET_LENGTH(unicode); | 
 | 4640 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4641 |     assert(size >= 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4642 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4643 |     if (size <= MAX_SHORT_UNICHARS) { | 
 | 4644 |         /* Write into the stack buffer; nallocated can't overflow. | 
 | 4645 |          * At the end, we'll allocate exactly as much heap space as it | 
 | 4646 |          * turns out we need. | 
 | 4647 |          */ | 
 | 4648 |         nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4649 |         result = NULL;   /* will allocate after we're done */ | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4650 |         p = stackbuf; | 
 | 4651 |     } | 
 | 4652 |     else { | 
 | 4653 |         /* Overallocate on the heap, and give the excess back at the end. */ | 
 | 4654 |         nallocated = size * 4; | 
 | 4655 |         if (nallocated / 4 != size)  /* overflow! */ | 
 | 4656 |             return PyErr_NoMemory(); | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4657 |         result = PyBytes_FromStringAndSize(NULL, nallocated); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4658 |         if (result == NULL) | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4659 |             return NULL; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4660 |         p = PyBytes_AS_STRING(result); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4661 |     } | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4662 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4663 |     for (i = 0; i < size;) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4664 |         Py_UCS4 ch = PyUnicode_READ(kind, data, i++); | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4665 |  | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4666 |         if (ch < 0x80) | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4667 |             /* Encode ASCII */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4668 |             *p++ = (char) ch; | 
| Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4669 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4670 |         else if (ch < 0x0800) { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4671 |             /* Encode Latin-1 */ | 
| Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4672 |             *p++ = (char)(0xc0 | (ch >> 6)); | 
 | 4673 |             *p++ = (char)(0x80 | (ch & 0x3f)); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4674 |         } else if (0xD800 <= ch && ch <= 0xDFFF) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4675 |             Py_ssize_t newpos; | 
 | 4676 |             PyObject *rep; | 
 | 4677 |             Py_ssize_t repsize, k, startpos; | 
 | 4678 |             startpos = i-1; | 
 | 4679 | #if SIZEOF_WCHAR_T == 2 | 
 | 4680 |             startpos += wchar_offset; | 
| Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4681 | #endif | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4682 |             rep = unicode_encode_call_errorhandler( | 
 | 4683 |                   errors, &errorHandler, "utf-8", "surrogates not allowed", | 
 | 4684 |                   PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), | 
 | 4685 |                   &exc, startpos, startpos+1, &newpos); | 
 | 4686 |             if (!rep) | 
 | 4687 |                 goto error; | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4688 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4689 |             if (PyBytes_Check(rep)) | 
 | 4690 |                 repsize = PyBytes_GET_SIZE(rep); | 
 | 4691 |             else | 
 | 4692 |                 repsize = PyUnicode_GET_SIZE(rep); | 
 | 4693 |  | 
 | 4694 |             if (repsize > 4) { | 
 | 4695 |                 Py_ssize_t offset; | 
 | 4696 |  | 
 | 4697 |                 if (result == NULL) | 
 | 4698 |                     offset = p - stackbuf; | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4699 |                 else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4700 |                     offset = p - PyBytes_AS_STRING(result); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4701 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4702 |                 if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { | 
 | 4703 |                     /* integer overflow */ | 
 | 4704 |                     PyErr_NoMemory(); | 
 | 4705 |                     goto error; | 
 | 4706 |                 } | 
 | 4707 |                 nallocated += repsize - 4; | 
 | 4708 |                 if (result != NULL) { | 
 | 4709 |                     if (_PyBytes_Resize(&result, nallocated) < 0) | 
 | 4710 |                         goto error; | 
 | 4711 |                 } else { | 
 | 4712 |                     result = PyBytes_FromStringAndSize(NULL, nallocated); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4713 |                     if (result == NULL) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4714 |                         goto error; | 
 | 4715 |                     Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); | 
 | 4716 |                 } | 
 | 4717 |                 p = PyBytes_AS_STRING(result) + offset; | 
 | 4718 |             } | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4719 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4720 |             if (PyBytes_Check(rep)) { | 
 | 4721 |                 char *prep = PyBytes_AS_STRING(rep); | 
 | 4722 |                 for(k = repsize; k > 0; k--) | 
 | 4723 |                     *p++ = *prep++; | 
 | 4724 |             } else /* rep is unicode */ { | 
 | 4725 |                 const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); | 
 | 4726 |                 Py_UNICODE c; | 
 | 4727 |  | 
 | 4728 |                 for(k=0; k<repsize; k++) { | 
 | 4729 |                     c = prep[k]; | 
 | 4730 |                     if (0x80 <= c) { | 
 | 4731 |                         raise_encode_exception(&exc, "utf-8", | 
 | 4732 |                                                PyUnicode_AS_UNICODE(unicode), | 
 | 4733 |                                                size, i-1, i, | 
 | 4734 |                                                "surrogates not allowed"); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4735 |                         goto error; | 
 | 4736 |                     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4737 |                     *p++ = (char)prep[k]; | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4738 |                 } | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4739 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4740 |             Py_DECREF(rep); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4741 |         } else if (ch < 0x10000) { | 
 | 4742 |             *p++ = (char)(0xe0 | (ch >> 12)); | 
 | 4743 |             *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); | 
 | 4744 |             *p++ = (char)(0x80 | (ch & 0x3f)); | 
 | 4745 |         } else /* ch >= 0x10000 */ { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4746 |             /* Encode UCS4 Unicode ordinals */ | 
 | 4747 |             *p++ = (char)(0xf0 | (ch >> 18)); | 
 | 4748 |             *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); | 
 | 4749 |             *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); | 
 | 4750 |             *p++ = (char)(0x80 | (ch & 0x3f)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4751 | #if SIZEOF_WCHAR_T == 2 | 
 | 4752 |             wchar_offset++; | 
 | 4753 | #endif | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4754 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4755 |     } | 
| Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4756 |  | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4757 |     if (result == NULL) { | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4758 |         /* This was stack allocated. */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4759 |         nneeded = p - stackbuf; | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4760 |         assert(nneeded <= nallocated); | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4761 |         result = PyBytes_FromStringAndSize(stackbuf, nneeded); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4762 |     } | 
 | 4763 |     else { | 
| Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4764 |         /* Cut back to size actually needed. */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4765 |         nneeded = p - PyBytes_AS_STRING(result); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4766 |         assert(nneeded <= nallocated); | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4767 |         _PyBytes_Resize(&result, nneeded); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4768 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4769 |  | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4770 |     Py_XDECREF(errorHandler); | 
 | 4771 |     Py_XDECREF(exc); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4772 |     return result; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4773 |  error: | 
 | 4774 |     Py_XDECREF(errorHandler); | 
 | 4775 |     Py_XDECREF(exc); | 
 | 4776 |     Py_XDECREF(result); | 
 | 4777 |     return NULL; | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4778 |  | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4779 | #undef MAX_SHORT_UNICHARS | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4780 | } | 
 | 4781 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4782 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4783 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, | 
 | 4784 |                      Py_ssize_t size, | 
 | 4785 |                      const char *errors) | 
 | 4786 | { | 
 | 4787 |     PyObject *v, *unicode; | 
 | 4788 |  | 
 | 4789 |     unicode = PyUnicode_FromUnicode(s, size); | 
 | 4790 |     if (unicode == NULL) | 
 | 4791 |         return NULL; | 
 | 4792 |     v = _PyUnicode_AsUTF8String(unicode, errors); | 
 | 4793 |     Py_DECREF(unicode); | 
 | 4794 |     return v; | 
 | 4795 | } | 
 | 4796 |  | 
 | 4797 | PyObject * | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4798 | PyUnicode_AsUTF8String(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4799 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4800 |     return _PyUnicode_AsUTF8String(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4801 | } | 
 | 4802 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4803 | /* --- UTF-32 Codec ------------------------------------------------------- */ | 
 | 4804 |  | 
 | 4805 | PyObject * | 
 | 4806 | PyUnicode_DecodeUTF32(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4807 |                       Py_ssize_t size, | 
 | 4808 |                       const char *errors, | 
 | 4809 |                       int *byteorder) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4810 | { | 
 | 4811 |     return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); | 
 | 4812 | } | 
 | 4813 |  | 
 | 4814 | PyObject * | 
 | 4815 | PyUnicode_DecodeUTF32Stateful(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4816 |                               Py_ssize_t size, | 
 | 4817 |                               const char *errors, | 
 | 4818 |                               int *byteorder, | 
 | 4819 |                               Py_ssize_t *consumed) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4820 | { | 
 | 4821 |     const char *starts = s; | 
 | 4822 |     Py_ssize_t startinpos; | 
 | 4823 |     Py_ssize_t endinpos; | 
 | 4824 |     Py_ssize_t outpos; | 
 | 4825 |     PyUnicodeObject *unicode; | 
 | 4826 |     Py_UNICODE *p; | 
 | 4827 | #ifndef Py_UNICODE_WIDE | 
| Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4828 |     int pairs = 0; | 
| Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4829 |     const unsigned char *qq; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4830 | #else | 
 | 4831 |     const int pairs = 0; | 
 | 4832 | #endif | 
| Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4833 |     const unsigned char *q, *e; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4834 |     int bo = 0;       /* assume native ordering by default */ | 
 | 4835 |     const char *errmsg = ""; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4836 |     /* Offsets from q for retrieving bytes in the right order. */ | 
 | 4837 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 4838 |     int iorder[] = {0, 1, 2, 3}; | 
 | 4839 | #else | 
 | 4840 |     int iorder[] = {3, 2, 1, 0}; | 
 | 4841 | #endif | 
 | 4842 |     PyObject *errorHandler = NULL; | 
 | 4843 |     PyObject *exc = NULL; | 
| Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4844 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4845 |     q = (unsigned char *)s; | 
 | 4846 |     e = q + size; | 
 | 4847 |  | 
 | 4848 |     if (byteorder) | 
 | 4849 |         bo = *byteorder; | 
 | 4850 |  | 
 | 4851 |     /* Check for BOM marks (U+FEFF) in the input and adjust current | 
 | 4852 |        byte order setting accordingly. In native mode, the leading BOM | 
 | 4853 |        mark is skipped, in all other modes, it is copied to the output | 
 | 4854 |        stream as-is (giving a ZWNBSP character). */ | 
 | 4855 |     if (bo == 0) { | 
 | 4856 |         if (size >= 4) { | 
 | 4857 |             const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4858 |                 (q[iorder[1]] << 8) | q[iorder[0]]; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4859 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4860 |             if (bom == 0x0000FEFF) { | 
 | 4861 |                 q += 4; | 
 | 4862 |                 bo = -1; | 
 | 4863 |             } | 
 | 4864 |             else if (bom == 0xFFFE0000) { | 
 | 4865 |                 q += 4; | 
 | 4866 |                 bo = 1; | 
 | 4867 |             } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4868 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4869 |             if (bom == 0x0000FEFF) { | 
 | 4870 |                 q += 4; | 
 | 4871 |                 bo = 1; | 
 | 4872 |             } | 
 | 4873 |             else if (bom == 0xFFFE0000) { | 
 | 4874 |                 q += 4; | 
 | 4875 |                 bo = -1; | 
 | 4876 |             } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4877 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4878 |         } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4879 |     } | 
 | 4880 |  | 
 | 4881 |     if (bo == -1) { | 
 | 4882 |         /* force LE */ | 
 | 4883 |         iorder[0] = 0; | 
 | 4884 |         iorder[1] = 1; | 
 | 4885 |         iorder[2] = 2; | 
 | 4886 |         iorder[3] = 3; | 
 | 4887 |     } | 
 | 4888 |     else if (bo == 1) { | 
 | 4889 |         /* force BE */ | 
 | 4890 |         iorder[0] = 3; | 
 | 4891 |         iorder[1] = 2; | 
 | 4892 |         iorder[2] = 1; | 
 | 4893 |         iorder[3] = 0; | 
 | 4894 |     } | 
 | 4895 |  | 
| Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4896 |     /* On narrow builds we split characters outside the BMP into two | 
 | 4897 |        codepoints => count how much extra space we need. */ | 
 | 4898 | #ifndef Py_UNICODE_WIDE | 
 | 4899 |     for (qq = q; qq < e; qq += 4) | 
 | 4900 |         if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) | 
 | 4901 |             pairs++; | 
 | 4902 | #endif | 
 | 4903 |  | 
 | 4904 |     /* This might be one to much, because of a BOM */ | 
 | 4905 |     unicode = _PyUnicode_New((size+3)/4+pairs); | 
 | 4906 |     if (!unicode) | 
 | 4907 |         return NULL; | 
 | 4908 |     if (size == 0) | 
 | 4909 |         return (PyObject *)unicode; | 
 | 4910 |  | 
 | 4911 |     /* Unpack UTF-32 encoded data */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4912 |     p = PyUnicode_AS_UNICODE(unicode); | 
| Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4913 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4914 |     while (q < e) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4915 |         Py_UCS4 ch; | 
 | 4916 |         /* remaining bytes at the end? (size should be divisible by 4) */ | 
 | 4917 |         if (e-q<4) { | 
 | 4918 |             if (consumed) | 
 | 4919 |                 break; | 
 | 4920 |             errmsg = "truncated data"; | 
 | 4921 |             startinpos = ((const char *)q)-starts; | 
 | 4922 |             endinpos = ((const char *)e)-starts; | 
 | 4923 |             goto utf32Error; | 
 | 4924 |             /* The remaining input chars are ignored if the callback | 
 | 4925 |                chooses to skip the input */ | 
 | 4926 |         } | 
 | 4927 |         ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | | 
 | 4928 |             (q[iorder[1]] << 8) | q[iorder[0]]; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4929 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4930 |         if (ch >= 0x110000) | 
 | 4931 |         { | 
 | 4932 |             errmsg = "codepoint not in range(0x110000)"; | 
 | 4933 |             startinpos = ((const char *)q)-starts; | 
 | 4934 |             endinpos = startinpos+4; | 
 | 4935 |             goto utf32Error; | 
 | 4936 |         } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4937 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4938 |         if (ch >= 0x10000) | 
 | 4939 |         { | 
 | 4940 |             *p++ = 0xD800 | ((ch-0x10000) >> 10); | 
 | 4941 |             *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); | 
 | 4942 |         } | 
 | 4943 |         else | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4944 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4945 |             *p++ = ch; | 
 | 4946 |         q += 4; | 
 | 4947 |         continue; | 
 | 4948 |       utf32Error: | 
 | 4949 |         outpos = p-PyUnicode_AS_UNICODE(unicode); | 
 | 4950 |         if (unicode_decode_call_errorhandler( | 
 | 4951 |                 errors, &errorHandler, | 
 | 4952 |                 "utf32", errmsg, | 
 | 4953 |                 &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, | 
 | 4954 |                 &unicode, &outpos, &p)) | 
 | 4955 |             goto onError; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4956 |     } | 
 | 4957 |  | 
 | 4958 |     if (byteorder) | 
 | 4959 |         *byteorder = bo; | 
 | 4960 |  | 
 | 4961 |     if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4962 |         *consumed = (const char *)q-starts; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4963 |  | 
 | 4964 |     /* Adjust length */ | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 4965 |     if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4966 |         goto onError; | 
 | 4967 |  | 
 | 4968 |     Py_XDECREF(errorHandler); | 
 | 4969 |     Py_XDECREF(exc); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4970 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4971 |     if (_PyUnicode_READY_REPLACE(&unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4972 |         Py_DECREF(unicode); | 
 | 4973 |         return NULL; | 
 | 4974 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4975 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4976 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4977 |     return (PyObject *)unicode; | 
 | 4978 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4979 |   onError: | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4980 |     Py_DECREF(unicode); | 
 | 4981 |     Py_XDECREF(errorHandler); | 
 | 4982 |     Py_XDECREF(exc); | 
 | 4983 |     return NULL; | 
 | 4984 | } | 
 | 4985 |  | 
 | 4986 | PyObject * | 
 | 4987 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4988 |                       Py_ssize_t size, | 
 | 4989 |                       const char *errors, | 
 | 4990 |                       int byteorder) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4991 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4992 |     PyObject *v; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4993 |     unsigned char *p; | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4994 |     Py_ssize_t nsize, bytesize; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4995 | #ifndef Py_UNICODE_WIDE | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4996 |     Py_ssize_t i, pairs; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4997 | #else | 
 | 4998 |     const int pairs = 0; | 
 | 4999 | #endif | 
 | 5000 |     /* Offsets from p for storing byte pairs in the right order. */ | 
 | 5001 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 5002 |     int iorder[] = {0, 1, 2, 3}; | 
 | 5003 | #else | 
 | 5004 |     int iorder[] = {3, 2, 1, 0}; | 
 | 5005 | #endif | 
 | 5006 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5007 | #define STORECHAR(CH)                           \ | 
 | 5008 |     do {                                        \ | 
 | 5009 |         p[iorder[3]] = ((CH) >> 24) & 0xff;     \ | 
 | 5010 |         p[iorder[2]] = ((CH) >> 16) & 0xff;     \ | 
 | 5011 |         p[iorder[1]] = ((CH) >> 8) & 0xff;      \ | 
 | 5012 |         p[iorder[0]] = (CH) & 0xff;             \ | 
 | 5013 |         p += 4;                                 \ | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5014 |     } while(0) | 
 | 5015 |  | 
 | 5016 |     /* In narrow builds we can output surrogate pairs as one codepoint, | 
 | 5017 |        so we need less space. */ | 
 | 5018 | #ifndef Py_UNICODE_WIDE | 
 | 5019 |     for (i = pairs = 0; i < size-1; i++) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5020 |         if (0xD800 <= s[i] && s[i] <= 0xDBFF && | 
 | 5021 |             0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) | 
 | 5022 |             pairs++; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5023 | #endif | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5024 |     nsize = (size - pairs + (byteorder == 0)); | 
 | 5025 |     bytesize = nsize * 4; | 
 | 5026 |     if (bytesize / 4 != nsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5027 |         return PyErr_NoMemory(); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5028 |     v = PyBytes_FromStringAndSize(NULL, bytesize); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5029 |     if (v == NULL) | 
 | 5030 |         return NULL; | 
 | 5031 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5032 |     p = (unsigned char *)PyBytes_AS_STRING(v); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5033 |     if (byteorder == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5034 |         STORECHAR(0xFEFF); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5035 |     if (size == 0) | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5036 |         goto done; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5037 |  | 
 | 5038 |     if (byteorder == -1) { | 
 | 5039 |         /* force LE */ | 
 | 5040 |         iorder[0] = 0; | 
 | 5041 |         iorder[1] = 1; | 
 | 5042 |         iorder[2] = 2; | 
 | 5043 |         iorder[3] = 3; | 
 | 5044 |     } | 
 | 5045 |     else if (byteorder == 1) { | 
 | 5046 |         /* force BE */ | 
 | 5047 |         iorder[0] = 3; | 
 | 5048 |         iorder[1] = 2; | 
 | 5049 |         iorder[2] = 1; | 
 | 5050 |         iorder[3] = 0; | 
 | 5051 |     } | 
 | 5052 |  | 
 | 5053 |     while (size-- > 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5054 |         Py_UCS4 ch = *s++; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5055 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5056 |         if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { | 
 | 5057 |             Py_UCS4 ch2 = *s; | 
 | 5058 |             if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | 
 | 5059 |                 ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | 
 | 5060 |                 s++; | 
 | 5061 |                 size--; | 
 | 5062 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5063 |         } | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5064 | #endif | 
 | 5065 |         STORECHAR(ch); | 
 | 5066 |     } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5067 |  | 
 | 5068 |   done: | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5069 |     return v; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5070 | #undef STORECHAR | 
 | 5071 | } | 
 | 5072 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5073 | PyObject * | 
 | 5074 | PyUnicode_AsUTF32String(PyObject *unicode) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5075 | { | 
 | 5076 |     if (!PyUnicode_Check(unicode)) { | 
 | 5077 |         PyErr_BadArgument(); | 
 | 5078 |         return NULL; | 
 | 5079 |     } | 
 | 5080 |     return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5081 |                                  PyUnicode_GET_SIZE(unicode), | 
 | 5082 |                                  NULL, | 
 | 5083 |                                  0); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5084 | } | 
 | 5085 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5086 | /* --- UTF-16 Codec ------------------------------------------------------- */ | 
 | 5087 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5088 | PyObject * | 
 | 5089 | PyUnicode_DecodeUTF16(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5090 |                       Py_ssize_t size, | 
 | 5091 |                       const char *errors, | 
 | 5092 |                       int *byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5093 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5094 |     return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); | 
 | 5095 | } | 
 | 5096 |  | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5097 | /* Two masks for fast checking of whether a C 'long' may contain | 
 | 5098 |    UTF16-encoded surrogate characters. This is an efficient heuristic, | 
 | 5099 |    assuming that non-surrogate characters with a code point >= 0x8000 are | 
 | 5100 |    rare in most input. | 
 | 5101 |    FAST_CHAR_MASK is used when the input is in native byte ordering, | 
 | 5102 |    SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5103 | */ | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5104 | #if (SIZEOF_LONG == 8) | 
 | 5105 | # define FAST_CHAR_MASK         0x8000800080008000L | 
 | 5106 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L | 
 | 5107 | #elif (SIZEOF_LONG == 4) | 
 | 5108 | # define FAST_CHAR_MASK         0x80008000L | 
 | 5109 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L | 
 | 5110 | #else | 
 | 5111 | # error C 'long' size should be either 4 or 8! | 
 | 5112 | #endif | 
 | 5113 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5114 | PyObject * | 
 | 5115 | PyUnicode_DecodeUTF16Stateful(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5116 |                               Py_ssize_t size, | 
 | 5117 |                               const char *errors, | 
 | 5118 |                               int *byteorder, | 
 | 5119 |                               Py_ssize_t *consumed) | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5120 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5121 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5122 |     Py_ssize_t startinpos; | 
 | 5123 |     Py_ssize_t endinpos; | 
 | 5124 |     Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5125 |     PyUnicodeObject *unicode; | 
 | 5126 |     Py_UNICODE *p; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5127 |     const unsigned char *q, *e, *aligned_end; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5128 |     int bo = 0;       /* assume native ordering by default */ | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5129 |     int native_ordering = 0; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5130 |     const char *errmsg = ""; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5131 |     /* Offsets from q for retrieving byte pairs in the right order. */ | 
 | 5132 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 5133 |     int ihi = 1, ilo = 0; | 
 | 5134 | #else | 
 | 5135 |     int ihi = 0, ilo = 1; | 
 | 5136 | #endif | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5137 |     PyObject *errorHandler = NULL; | 
 | 5138 |     PyObject *exc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5139 |  | 
 | 5140 |     /* Note: size will always be longer than the resulting Unicode | 
 | 5141 |        character count */ | 
 | 5142 |     unicode = _PyUnicode_New(size); | 
 | 5143 |     if (!unicode) | 
 | 5144 |         return NULL; | 
 | 5145 |     if (size == 0) | 
 | 5146 |         return (PyObject *)unicode; | 
 | 5147 |  | 
 | 5148 |     /* Unpack UTF-16 encoded data */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5149 |     p = PyUnicode_AS_UNICODE(unicode); | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5150 |     q = (unsigned char *)s; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5151 |     e = q + size - 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 |  | 
 | 5153 |     if (byteorder) | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5154 |         bo = *byteorder; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5155 |  | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5156 |     /* Check for BOM marks (U+FEFF) in the input and adjust current | 
 | 5157 |        byte order setting accordingly. In native mode, the leading BOM | 
 | 5158 |        mark is skipped, in all other modes, it is copied to the output | 
 | 5159 |        stream as-is (giving a ZWNBSP character). */ | 
 | 5160 |     if (bo == 0) { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5161 |         if (size >= 2) { | 
 | 5162 |             const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5163 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5164 |             if (bom == 0xFEFF) { | 
 | 5165 |                 q += 2; | 
 | 5166 |                 bo = -1; | 
 | 5167 |             } | 
 | 5168 |             else if (bom == 0xFFFE) { | 
 | 5169 |                 q += 2; | 
 | 5170 |                 bo = 1; | 
 | 5171 |             } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5172 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5173 |             if (bom == 0xFEFF) { | 
 | 5174 |                 q += 2; | 
 | 5175 |                 bo = 1; | 
 | 5176 |             } | 
 | 5177 |             else if (bom == 0xFFFE) { | 
 | 5178 |                 q += 2; | 
 | 5179 |                 bo = -1; | 
 | 5180 |             } | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5181 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5182 |         } | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5183 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5184 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5185 |     if (bo == -1) { | 
 | 5186 |         /* force LE */ | 
 | 5187 |         ihi = 1; | 
 | 5188 |         ilo = 0; | 
 | 5189 |     } | 
 | 5190 |     else if (bo == 1) { | 
 | 5191 |         /* force BE */ | 
 | 5192 |         ihi = 0; | 
 | 5193 |         ilo = 1; | 
 | 5194 |     } | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5195 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 5196 |     native_ordering = ilo < ihi; | 
 | 5197 | #else | 
 | 5198 |     native_ordering = ilo > ihi; | 
 | 5199 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5200 |  | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5201 |     aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5202 |     while (q < e) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5203 |         Py_UNICODE ch; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5204 |         /* First check for possible aligned read of a C 'long'. Unaligned | 
 | 5205 |            reads are more expensive, better to defer to another iteration. */ | 
 | 5206 |         if (!((size_t) q & LONG_PTR_MASK)) { | 
 | 5207 |             /* Fast path for runs of non-surrogate chars. */ | 
 | 5208 |             register const unsigned char *_q = q; | 
 | 5209 |             Py_UNICODE *_p = p; | 
 | 5210 |             if (native_ordering) { | 
 | 5211 |                 /* Native ordering is simple: as long as the input cannot | 
 | 5212 |                    possibly contain a surrogate char, do an unrolled copy | 
 | 5213 |                    of several 16-bit code points to the target object. | 
 | 5214 |                    The non-surrogate check is done on several input bytes | 
 | 5215 |                    at a time (as many as a C 'long' can contain). */ | 
 | 5216 |                 while (_q < aligned_end) { | 
 | 5217 |                     unsigned long data = * (unsigned long *) _q; | 
 | 5218 |                     if (data & FAST_CHAR_MASK) | 
 | 5219 |                         break; | 
 | 5220 |                     _p[0] = ((unsigned short *) _q)[0]; | 
 | 5221 |                     _p[1] = ((unsigned short *) _q)[1]; | 
 | 5222 | #if (SIZEOF_LONG == 8) | 
 | 5223 |                     _p[2] = ((unsigned short *) _q)[2]; | 
 | 5224 |                     _p[3] = ((unsigned short *) _q)[3]; | 
 | 5225 | #endif | 
 | 5226 |                     _q += SIZEOF_LONG; | 
 | 5227 |                     _p += SIZEOF_LONG / 2; | 
 | 5228 |                 } | 
 | 5229 |             } | 
 | 5230 |             else { | 
 | 5231 |                 /* Byteswapped ordering is similar, but we must decompose | 
 | 5232 |                    the copy bytewise, and take care of zero'ing out the | 
 | 5233 |                    upper bytes if the target object is in 32-bit units | 
 | 5234 |                    (that is, in UCS-4 builds). */ | 
 | 5235 |                 while (_q < aligned_end) { | 
 | 5236 |                     unsigned long data = * (unsigned long *) _q; | 
 | 5237 |                     if (data & SWAPPED_FAST_CHAR_MASK) | 
 | 5238 |                         break; | 
 | 5239 |                     /* Zero upper bytes in UCS-4 builds */ | 
 | 5240 | #if (Py_UNICODE_SIZE > 2) | 
 | 5241 |                     _p[0] = 0; | 
 | 5242 |                     _p[1] = 0; | 
 | 5243 | #if (SIZEOF_LONG == 8) | 
 | 5244 |                     _p[2] = 0; | 
 | 5245 |                     _p[3] = 0; | 
 | 5246 | #endif | 
 | 5247 | #endif | 
| Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5248 |                     /* Issue #4916; UCS-4 builds on big endian machines must | 
 | 5249 |                        fill the two last bytes of each 4-byte unit. */ | 
 | 5250 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) | 
 | 5251 | # define OFF 2 | 
 | 5252 | #else | 
 | 5253 | # define OFF 0 | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5254 | #endif | 
| Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5255 |                     ((unsigned char *) _p)[OFF + 1] = _q[0]; | 
 | 5256 |                     ((unsigned char *) _p)[OFF + 0] = _q[1]; | 
 | 5257 |                     ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; | 
 | 5258 |                     ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; | 
 | 5259 | #if (SIZEOF_LONG == 8) | 
 | 5260 |                     ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; | 
 | 5261 |                     ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; | 
 | 5262 |                     ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; | 
 | 5263 |                     ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; | 
 | 5264 | #endif | 
 | 5265 | #undef OFF | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5266 |                     _q += SIZEOF_LONG; | 
 | 5267 |                     _p += SIZEOF_LONG / 2; | 
 | 5268 |                 } | 
 | 5269 |             } | 
 | 5270 |             p = _p; | 
 | 5271 |             q = _q; | 
 | 5272 |             if (q >= e) | 
 | 5273 |                 break; | 
 | 5274 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5275 |         ch = (q[ihi] << 8) | q[ilo]; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5276 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5277 |         q += 2; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5278 |  | 
 | 5279 |         if (ch < 0xD800 || ch > 0xDFFF) { | 
 | 5280 |             *p++ = ch; | 
 | 5281 |             continue; | 
 | 5282 |         } | 
 | 5283 |  | 
 | 5284 |         /* UTF-16 code pair: */ | 
 | 5285 |         if (q > e) { | 
 | 5286 |             errmsg = "unexpected end of data"; | 
 | 5287 |             startinpos = (((const char *)q) - 2) - starts; | 
 | 5288 |             endinpos = ((const char *)e) + 1 - starts; | 
 | 5289 |             goto utf16Error; | 
 | 5290 |         } | 
 | 5291 |         if (0xD800 <= ch && ch <= 0xDBFF) { | 
 | 5292 |             Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; | 
 | 5293 |             q += 2; | 
 | 5294 |             if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5295 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5296 |                 *p++ = ch; | 
 | 5297 |                 *p++ = ch2; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5298 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5299 |                 *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5300 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5301 |                 continue; | 
 | 5302 |             } | 
 | 5303 |             else { | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5304 |                 errmsg = "illegal UTF-16 surrogate"; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5305 |                 startinpos = (((const char *)q)-4)-starts; | 
 | 5306 |                 endinpos = startinpos+2; | 
 | 5307 |                 goto utf16Error; | 
 | 5308 |             } | 
 | 5309 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5310 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5311 |         errmsg = "illegal encoding"; | 
 | 5312 |         startinpos = (((const char *)q)-2)-starts; | 
 | 5313 |         endinpos = startinpos+2; | 
 | 5314 |         /* Fall through to report the error */ | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5315 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5316 |       utf16Error: | 
 | 5317 |         outpos = p - PyUnicode_AS_UNICODE(unicode); | 
 | 5318 |         if (unicode_decode_call_errorhandler( | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5319 |                 errors, | 
 | 5320 |                 &errorHandler, | 
 | 5321 |                 "utf16", errmsg, | 
 | 5322 |                 &starts, | 
 | 5323 |                 (const char **)&e, | 
 | 5324 |                 &startinpos, | 
 | 5325 |                 &endinpos, | 
 | 5326 |                 &exc, | 
 | 5327 |                 (const char **)&q, | 
 | 5328 |                 &unicode, | 
 | 5329 |                 &outpos, | 
 | 5330 |                 &p)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5331 |             goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5332 |     } | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5333 |     /* remaining byte at the end? (size should be even) */ | 
 | 5334 |     if (e == q) { | 
 | 5335 |         if (!consumed) { | 
 | 5336 |             errmsg = "truncated data"; | 
 | 5337 |             startinpos = ((const char *)q) - starts; | 
 | 5338 |             endinpos = ((const char *)e) + 1 - starts; | 
 | 5339 |             outpos = p - PyUnicode_AS_UNICODE(unicode); | 
 | 5340 |             if (unicode_decode_call_errorhandler( | 
 | 5341 |                     errors, | 
 | 5342 |                     &errorHandler, | 
 | 5343 |                     "utf16", errmsg, | 
 | 5344 |                     &starts, | 
 | 5345 |                     (const char **)&e, | 
 | 5346 |                     &startinpos, | 
 | 5347 |                     &endinpos, | 
 | 5348 |                     &exc, | 
 | 5349 |                     (const char **)&q, | 
 | 5350 |                     &unicode, | 
 | 5351 |                     &outpos, | 
 | 5352 |                     &p)) | 
 | 5353 |                 goto onError; | 
 | 5354 |             /* The remaining input chars are ignored if the callback | 
 | 5355 |                chooses to skip the input */ | 
 | 5356 |         } | 
 | 5357 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 |  | 
 | 5359 |     if (byteorder) | 
 | 5360 |         *byteorder = bo; | 
 | 5361 |  | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5362 |     if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5363 |         *consumed = (const char *)q-starts; | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5364 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5365 |     /* Adjust length */ | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5366 |     if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5367 |         goto onError; | 
 | 5368 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5369 |     Py_XDECREF(errorHandler); | 
 | 5370 |     Py_XDECREF(exc); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5371 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5372 |     if (_PyUnicode_READY_REPLACE(&unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5373 |         Py_DECREF(unicode); | 
 | 5374 |         return NULL; | 
 | 5375 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5376 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5377 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5378 |     return (PyObject *)unicode; | 
 | 5379 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5380 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 |     Py_DECREF(unicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5382 |     Py_XDECREF(errorHandler); | 
 | 5383 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5384 |     return NULL; | 
 | 5385 | } | 
 | 5386 |  | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5387 | #undef FAST_CHAR_MASK | 
 | 5388 | #undef SWAPPED_FAST_CHAR_MASK | 
 | 5389 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5390 | PyObject * | 
 | 5391 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5392 |                       Py_ssize_t size, | 
 | 5393 |                       const char *errors, | 
 | 5394 |                       int byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5395 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5396 |     PyObject *v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5397 |     unsigned char *p; | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5398 |     Py_ssize_t nsize, bytesize; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5399 | #ifdef Py_UNICODE_WIDE | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5400 |     Py_ssize_t i, pairs; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5401 | #else | 
 | 5402 |     const int pairs = 0; | 
 | 5403 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5404 |     /* Offsets from p for storing byte pairs in the right order. */ | 
 | 5405 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN | 
 | 5406 |     int ihi = 1, ilo = 0; | 
 | 5407 | #else | 
 | 5408 |     int ihi = 0, ilo = 1; | 
 | 5409 | #endif | 
 | 5410 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5411 | #define STORECHAR(CH)                           \ | 
 | 5412 |     do {                                        \ | 
 | 5413 |         p[ihi] = ((CH) >> 8) & 0xff;            \ | 
 | 5414 |         p[ilo] = (CH) & 0xff;                   \ | 
 | 5415 |         p += 2;                                 \ | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5416 |     } while(0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 |  | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5418 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5419 |     for (i = pairs = 0; i < size; i++) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5420 |         if (s[i] >= 0x10000) | 
 | 5421 |             pairs++; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5422 | #endif | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5423 |     /* 2 * (size + pairs + (byteorder == 0)) */ | 
 | 5424 |     if (size > PY_SSIZE_T_MAX || | 
 | 5425 |         size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5426 |         return PyErr_NoMemory(); | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5427 |     nsize = size + pairs + (byteorder == 0); | 
 | 5428 |     bytesize = nsize * 2; | 
 | 5429 |     if (bytesize / 2 != nsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5430 |         return PyErr_NoMemory(); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5431 |     v = PyBytes_FromStringAndSize(NULL, bytesize); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5432 |     if (v == NULL) | 
 | 5433 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5434 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5435 |     p = (unsigned char *)PyBytes_AS_STRING(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5436 |     if (byteorder == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5437 |         STORECHAR(0xFEFF); | 
| Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5438 |     if (size == 0) | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5439 |         goto done; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5440 |  | 
 | 5441 |     if (byteorder == -1) { | 
 | 5442 |         /* force LE */ | 
 | 5443 |         ihi = 1; | 
 | 5444 |         ilo = 0; | 
 | 5445 |     } | 
 | 5446 |     else if (byteorder == 1) { | 
 | 5447 |         /* force BE */ | 
 | 5448 |         ihi = 0; | 
 | 5449 |         ilo = 1; | 
 | 5450 |     } | 
 | 5451 |  | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5452 |     while (size-- > 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5453 |         Py_UNICODE ch = *s++; | 
 | 5454 |         Py_UNICODE ch2 = 0; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5455 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5456 |         if (ch >= 0x10000) { | 
 | 5457 |             ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); | 
 | 5458 |             ch  = 0xD800 | ((ch-0x10000) >> 10); | 
 | 5459 |         } | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5460 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5461 |         STORECHAR(ch); | 
 | 5462 |         if (ch2) | 
 | 5463 |             STORECHAR(ch2); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5464 |     } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5465 |  | 
 | 5466 |   done: | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5467 |     return v; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5468 | #undef STORECHAR | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | } | 
 | 5470 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5471 | PyObject * | 
 | 5472 | PyUnicode_AsUTF16String(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | { | 
 | 5474 |     if (!PyUnicode_Check(unicode)) { | 
 | 5475 |         PyErr_BadArgument(); | 
 | 5476 |         return NULL; | 
 | 5477 |     } | 
 | 5478 |     return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5479 |                                  PyUnicode_GET_SIZE(unicode), | 
 | 5480 |                                  NULL, | 
 | 5481 |                                  0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | } | 
 | 5483 |  | 
 | 5484 | /* --- Unicode Escape Codec ----------------------------------------------- */ | 
 | 5485 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5486 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines | 
 | 5487 |    if all the escapes in the string make it still a valid ASCII string. | 
 | 5488 |    Returns -1 if any escapes were found which cause the string to | 
 | 5489 |    pop out of ASCII range.  Otherwise returns the length of the | 
 | 5490 |    required buffer to hold the string. | 
 | 5491 |    */ | 
 | 5492 | Py_ssize_t | 
 | 5493 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) | 
 | 5494 | { | 
 | 5495 |     const unsigned char *p = (const unsigned char *)s; | 
 | 5496 |     const unsigned char *end = p + size; | 
 | 5497 |     Py_ssize_t length = 0; | 
 | 5498 |  | 
 | 5499 |     if (size < 0) | 
 | 5500 |         return -1; | 
 | 5501 |  | 
 | 5502 |     for (; p < end; ++p) { | 
 | 5503 |         if (*p > 127) { | 
 | 5504 |             /* Non-ASCII */ | 
 | 5505 |             return -1; | 
 | 5506 |         } | 
 | 5507 |         else if (*p != '\\') { | 
 | 5508 |             /* Normal character */ | 
 | 5509 |             ++length; | 
 | 5510 |         } | 
 | 5511 |         else { | 
 | 5512 |             /* Backslash-escape, check next char */ | 
 | 5513 |             ++p; | 
 | 5514 |             /* Escape sequence reaches till end of string or | 
 | 5515 |                non-ASCII follow-up. */ | 
 | 5516 |             if (p >= end || *p > 127) | 
 | 5517 |                 return -1; | 
 | 5518 |             switch (*p) { | 
 | 5519 |             case '\n': | 
 | 5520 |                 /* backslash + \n result in zero characters */ | 
 | 5521 |                 break; | 
 | 5522 |             case '\\': case '\'': case '\"': | 
 | 5523 |             case 'b': case 'f': case 't': | 
 | 5524 |             case 'n': case 'r': case 'v': case 'a': | 
 | 5525 |                 ++length; | 
 | 5526 |                 break; | 
 | 5527 |             case '0': case '1': case '2': case '3': | 
 | 5528 |             case '4': case '5': case '6': case '7': | 
 | 5529 |             case 'x': case 'u': case 'U': case 'N': | 
 | 5530 |                 /* these do not guarantee ASCII characters */ | 
 | 5531 |                 return -1; | 
 | 5532 |             default: | 
 | 5533 |                 /* count the backslash + the other character */ | 
 | 5534 |                 length += 2; | 
 | 5535 |             } | 
 | 5536 |         } | 
 | 5537 |     } | 
 | 5538 |     return length; | 
 | 5539 | } | 
 | 5540 |  | 
 | 5541 | /* Similar to PyUnicode_WRITE but either write into wstr field | 
 | 5542 |    or treat string as ASCII. */ | 
 | 5543 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ | 
 | 5544 |     do { \ | 
 | 5545 |         if ((kind) != PyUnicode_WCHAR_KIND) \ | 
 | 5546 |             ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ | 
 | 5547 |         else \ | 
 | 5548 |             ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ | 
 | 5549 |     } while (0) | 
 | 5550 |  | 
 | 5551 | #define WRITE_WSTR(buf, index, value) \ | 
 | 5552 |     assert(kind == PyUnicode_WCHAR_KIND), \ | 
 | 5553 |     ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) | 
 | 5554 |  | 
 | 5555 |  | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5556 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; | 
| Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5557 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5558 | PyObject * | 
 | 5559 | PyUnicode_DecodeUnicodeEscape(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5560 |                               Py_ssize_t size, | 
| Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5561 |                               const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5563 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5564 |     Py_ssize_t startinpos; | 
 | 5565 |     Py_ssize_t endinpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5566 |     int j; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5567 |     PyUnicodeObject *v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5568 |     Py_UNICODE *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5569 |     const char *end; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5570 |     char* message; | 
 | 5571 |     Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5572 |     PyObject *errorHandler = NULL; | 
 | 5573 |     PyObject *exc = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5574 |     Py_ssize_t ascii_length; | 
 | 5575 |     Py_ssize_t i; | 
 | 5576 |     int kind; | 
 | 5577 |     void *data; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5578 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5579 |     ascii_length = length_of_escaped_ascii_string(s, size); | 
 | 5580 |  | 
 | 5581 |     /* After length_of_escaped_ascii_string() there are two alternatives, | 
 | 5582 |        either the string is pure ASCII with named escapes like \n, etc. | 
 | 5583 |        and we determined it's exact size (common case) | 
 | 5584 |        or it contains \x, \u, ... escape sequences.  then we create a | 
 | 5585 |        legacy wchar string and resize it at the end of this function. */ | 
 | 5586 |     if (ascii_length >= 0) { | 
 | 5587 |         v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); | 
 | 5588 |         if (!v) | 
 | 5589 |             goto onError; | 
 | 5590 |         assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); | 
 | 5591 |         kind = PyUnicode_1BYTE_KIND; | 
 | 5592 |         data = PyUnicode_DATA(v); | 
 | 5593 |     } | 
 | 5594 |     else { | 
 | 5595 |         /* Escaped strings will always be longer than the resulting | 
 | 5596 |            Unicode string, so we start with size here and then reduce the | 
 | 5597 |            length after conversion to the true value. | 
 | 5598 |            (but if the error callback returns a long replacement string | 
 | 5599 |            we'll have to allocate more space) */ | 
 | 5600 |         v = _PyUnicode_New(size); | 
 | 5601 |         if (!v) | 
 | 5602 |             goto onError; | 
 | 5603 |         kind = PyUnicode_WCHAR_KIND; | 
 | 5604 |         data = PyUnicode_AS_UNICODE(v); | 
 | 5605 |     } | 
 | 5606 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5607 |     if (size == 0) | 
 | 5608 |         return (PyObject *)v; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5609 |     i = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 |     end = s + size; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5611 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5612 |     while (s < end) { | 
 | 5613 |         unsigned char c; | 
| Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5614 |         Py_UNICODE x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5615 |         int digits; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5616 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5617 |         if (kind == PyUnicode_WCHAR_KIND) { | 
 | 5618 |             assert(i < _PyUnicode_WSTR_LENGTH(v)); | 
 | 5619 |         } | 
 | 5620 |         else { | 
 | 5621 |             /* The only case in which i == ascii_length is a backslash | 
 | 5622 |                followed by a newline. */ | 
 | 5623 |             assert(i <= ascii_length); | 
 | 5624 |         } | 
 | 5625 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5626 |         /* Non-escape characters are interpreted as Unicode ordinals */ | 
 | 5627 |         if (*s != '\\') { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5628 |             WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5629 |             continue; | 
 | 5630 |         } | 
 | 5631 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5632 |         startinpos = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 |         /* \ - Escapes */ | 
 | 5634 |         s++; | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5635 |         c = *s++; | 
 | 5636 |         if (s > end) | 
 | 5637 |             c = '\0'; /* Invalid after \ */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5638 |  | 
 | 5639 |         if (kind == PyUnicode_WCHAR_KIND) { | 
 | 5640 |             assert(i < _PyUnicode_WSTR_LENGTH(v)); | 
 | 5641 |         } | 
 | 5642 |         else { | 
 | 5643 |             /* The only case in which i == ascii_length is a backslash | 
 | 5644 |                followed by a newline. */ | 
 | 5645 |             assert(i < ascii_length || (i == ascii_length && c == '\n')); | 
 | 5646 |         } | 
 | 5647 |  | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5648 |         switch (c) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5649 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5650 |             /* \x escapes */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5651 |         case '\n': break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5652 |         case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; | 
 | 5653 |         case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; | 
 | 5654 |         case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; | 
 | 5655 |         case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; | 
 | 5656 |         /* FF */ | 
 | 5657 |         case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; | 
 | 5658 |         case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; | 
 | 5659 |         case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; | 
 | 5660 |         case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; | 
 | 5661 |         /* VT */ | 
 | 5662 |         case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; | 
 | 5663 |         /* BEL, not classic C */ | 
 | 5664 |         case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5666 |             /* \OOO (octal) escapes */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 |         case '0': case '1': case '2': case '3': | 
 | 5668 |         case '4': case '5': case '6': case '7': | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5669 |             x = s[-1] - '0'; | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5670 |             if (s < end && '0' <= *s && *s <= '7') { | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5671 |                 x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5672 |                 if (s < end && '0' <= *s && *s <= '7') | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5673 |                     x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5674 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5675 |             WRITE_WSTR(data, i++, x); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 |             break; | 
 | 5677 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5678 |             /* hex escapes */ | 
 | 5679 |             /* \xXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5680 |         case 'x': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5681 |             digits = 2; | 
 | 5682 |             message = "truncated \\xXX escape"; | 
 | 5683 |             goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5685 |             /* \uXXXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5686 |         case 'u': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5687 |             digits = 4; | 
 | 5688 |             message = "truncated \\uXXXX escape"; | 
 | 5689 |             goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5691 |             /* \UXXXXXXXX */ | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5692 |         case 'U': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5693 |             digits = 8; | 
 | 5694 |             message = "truncated \\UXXXXXXXX escape"; | 
 | 5695 |         hexescape: | 
 | 5696 |             chr = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5697 |             p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5698 |             if (s+digits>end) { | 
 | 5699 |                 endinpos = size; | 
 | 5700 |                 if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5701 |                         errors, &errorHandler, | 
 | 5702 |                         "unicodeescape", "end of string in escape sequence", | 
 | 5703 |                         &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5704 |                         &v, &i, &p)) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5705 |                     goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5706 |                 data = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5707 |                 goto nextByte; | 
 | 5708 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5709 |             for (j = 0; j < digits; ++j) { | 
 | 5710 |                 c = (unsigned char) s[j]; | 
| David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5711 |                 if (!Py_ISXDIGIT(c)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5712 |                     endinpos = (s+j+1)-starts; | 
 | 5713 |                     p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5714 |                     if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5715 |                             errors, &errorHandler, | 
 | 5716 |                             "unicodeescape", message, | 
 | 5717 |                             &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5718 |                             &v, &i, &p)) | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5719 |                         goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5720 |                     data = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5721 |                     goto nextByte; | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5722 |                 } | 
 | 5723 |                 chr = (chr<<4) & ~0xF; | 
 | 5724 |                 if (c >= '0' && c <= '9') | 
 | 5725 |                     chr += c - '0'; | 
 | 5726 |                 else if (c >= 'a' && c <= 'f') | 
 | 5727 |                     chr += 10 + c - 'a'; | 
 | 5728 |                 else | 
 | 5729 |                     chr += 10 + c - 'A'; | 
 | 5730 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5731 |             s += j; | 
| Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5732 |             if (chr == 0xffffffff && PyErr_Occurred()) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5733 |                 /* _decoding_error will have already written into the | 
 | 5734 |                    target buffer. */ | 
 | 5735 |                 break; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5736 |         store: | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5737 |             /* when we get here, chr is a 32-bit unicode character */ | 
 | 5738 |             if (chr <= 0xffff) | 
 | 5739 |                 /* UCS-2 character */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5740 |                 WRITE_WSTR(data, i++, chr); | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5741 |             else if (chr <= 0x10ffff) { | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5742 |                 /* UCS-4 character. Either store directly, or as | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5743 |                    surrogate pair. */ | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5744 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5745 |                 WRITE_WSTR(data, i++, chr); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5746 | #else | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5747 |                 chr -= 0x10000L; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5748 |                 WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); | 
 | 5749 |                 WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5750 | #endif | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5751 |             } else { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5752 |                 endinpos = s-starts; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5753 |                 p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5754 |                 if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 |                         errors, &errorHandler, | 
 | 5756 |                         "unicodeescape", "illegal Unicode character", | 
 | 5757 |                         &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5758 |                         &v, &i, &p)) | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5759 |                     goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5760 |                 data = PyUnicode_AS_UNICODE(v); | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5761 |             } | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5762 |             break; | 
 | 5763 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5764 |             /* \N{name} */ | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5765 |         case 'N': | 
 | 5766 |             message = "malformed \\N character escape"; | 
 | 5767 |             if (ucnhash_CAPI == NULL) { | 
 | 5768 |                 /* load the unicode data module */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5769 |                 ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( | 
 | 5770 |                                                 PyUnicodeData_CAPSULE_NAME, 1); | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5771 |                 if (ucnhash_CAPI == NULL) | 
 | 5772 |                     goto ucnhashError; | 
 | 5773 |             } | 
 | 5774 |             if (*s == '{') { | 
 | 5775 |                 const char *start = s+1; | 
 | 5776 |                 /* look for the closing brace */ | 
 | 5777 |                 while (*s != '}' && s < end) | 
 | 5778 |                     s++; | 
 | 5779 |                 if (s > start && s < end && *s == '}') { | 
 | 5780 |                     /* found a name.  look it up in the unicode database */ | 
 | 5781 |                     message = "unknown Unicode character name"; | 
 | 5782 |                     s++; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5783 |                     if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), | 
 | 5784 |                                               &chr)) | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5785 |                         goto store; | 
 | 5786 |                 } | 
 | 5787 |             } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5788 |             endinpos = s-starts; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5789 |             p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5790 |             if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5791 |                     errors, &errorHandler, | 
 | 5792 |                     "unicodeescape", message, | 
 | 5793 |                     &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5794 |                     &v, &i, &p)) | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5795 |                 goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5796 |             data = PyUnicode_AS_UNICODE(v); | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5797 |             break; | 
 | 5798 |  | 
 | 5799 |         default: | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5800 |             if (s > end) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5801 |                 assert(kind == PyUnicode_WCHAR_KIND); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5802 |                 message = "\\ at end of string"; | 
 | 5803 |                 s--; | 
 | 5804 |                 endinpos = s-starts; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5805 |                 p = PyUnicode_AS_UNICODE(v) + i; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5806 |                 if (unicode_decode_call_errorhandler( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5807 |                         errors, &errorHandler, | 
 | 5808 |                         "unicodeescape", message, | 
 | 5809 |                         &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5810 |                         &v, &i, &p)) | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5811 |                     goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5812 |                 data = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5813 |             } | 
 | 5814 |             else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5815 |                 WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); | 
 | 5816 |                 WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5817 |             } | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5818 |             break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5819 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5820 |       nextByte: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5821 |         ; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5822 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5823 |     /* Ensure the length prediction worked in case of ASCII strings */ | 
 | 5824 |     assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); | 
 | 5825 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5826 |     if (kind == PyUnicode_WCHAR_KIND) | 
 | 5827 |     { | 
 | 5828 |         if (PyUnicode_Resize((PyObject**)&v, i) < 0) | 
 | 5829 |             goto onError; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5830 |     } | 
| Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5831 |     Py_XDECREF(errorHandler); | 
 | 5832 |     Py_XDECREF(exc); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5833 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5834 |     if (_PyUnicode_READY_REPLACE(&v)) { | 
 | 5835 |         Py_DECREF(v); | 
 | 5836 |         return NULL; | 
 | 5837 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5838 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5839 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5840 |     return (PyObject *)v; | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5841 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5842 |   ucnhashError: | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5843 |     PyErr_SetString( | 
 | 5844 |         PyExc_UnicodeError, | 
 | 5845 |         "\\N escapes not supported (can't load unicodedata module)" | 
 | 5846 |         ); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5847 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5848 |     Py_XDECREF(errorHandler); | 
 | 5849 |     Py_XDECREF(exc); | 
| Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5850 |     return NULL; | 
 | 5851 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5852 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5853 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5854 |     Py_XDECREF(errorHandler); | 
 | 5855 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5856 |     return NULL; | 
 | 5857 | } | 
 | 5858 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5859 | #undef WRITE_ASCII_OR_WSTR | 
 | 5860 | #undef WRITE_WSTR | 
 | 5861 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5862 | /* Return a Unicode-Escape string version of the Unicode object. | 
 | 5863 |  | 
 | 5864 |    If quotes is true, the string is enclosed in u"" or u'' quotes as | 
 | 5865 |    appropriate. | 
 | 5866 |  | 
 | 5867 | */ | 
 | 5868 |  | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5869 | static const char *hexdigits = "0123456789abcdef"; | 
 | 5870 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5871 | PyObject * | 
 | 5872 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5873 |                               Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5874 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5875 |     PyObject *repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 |     char *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5877 |  | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5878 | #ifdef Py_UNICODE_WIDE | 
 | 5879 |     const Py_ssize_t expandsize = 10; | 
 | 5880 | #else | 
 | 5881 |     const Py_ssize_t expandsize = 6; | 
 | 5882 | #endif | 
 | 5883 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5884 |     /* XXX(nnorwitz): rather than over-allocating, it would be | 
 | 5885 |        better to choose a different scheme.  Perhaps scan the | 
 | 5886 |        first N-chars of the string and allocate based on that size. | 
 | 5887 |     */ | 
 | 5888 |     /* Initial allocation is based on the longest-possible unichr | 
 | 5889 |        escape. | 
 | 5890 |  | 
 | 5891 |        In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source | 
 | 5892 |        unichr, so in this case it's the longest unichr escape. In | 
 | 5893 |        narrow (UTF-16) builds this is five chars per source unichr | 
 | 5894 |        since there are two unichrs in the surrogate pair, so in narrow | 
 | 5895 |        (UTF-16) builds it's not the longest unichr escape. | 
 | 5896 |  | 
 | 5897 |        In wide or narrow builds '\uxxxx' is 6 chars per source unichr, | 
 | 5898 |        so in the narrow (UTF-16) build case it's the longest unichr | 
 | 5899 |        escape. | 
 | 5900 |     */ | 
 | 5901 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5902 |     if (size == 0) | 
 | 5903 |         return PyBytes_FromStringAndSize(NULL, 0); | 
 | 5904 |  | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5905 |     if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5906 |         return PyErr_NoMemory(); | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5907 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5908 |     repr = PyBytes_FromStringAndSize(NULL, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5909 |                                      2 | 
 | 5910 |                                      + expandsize*size | 
 | 5911 |                                      + 1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 |     if (repr == NULL) | 
 | 5913 |         return NULL; | 
 | 5914 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5915 |     p = PyBytes_AS_STRING(repr); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5916 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 |     while (size-- > 0) { | 
 | 5918 |         Py_UNICODE ch = *s++; | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5919 |  | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5920 |         /* Escape backslashes */ | 
 | 5921 |         if (ch == '\\') { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 |             *p++ = '\\'; | 
 | 5923 |             *p++ = (char) ch; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5924 |             continue; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5925 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5926 |  | 
| Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5927 | #ifdef Py_UNICODE_WIDE | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5928 |         /* Map 21-bit characters to '\U00xxxxxx' */ | 
 | 5929 |         else if (ch >= 0x10000) { | 
 | 5930 |             *p++ = '\\'; | 
 | 5931 |             *p++ = 'U'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5932 |             *p++ = hexdigits[(ch >> 28) & 0x0000000F]; | 
 | 5933 |             *p++ = hexdigits[(ch >> 24) & 0x0000000F]; | 
 | 5934 |             *p++ = hexdigits[(ch >> 20) & 0x0000000F]; | 
 | 5935 |             *p++ = hexdigits[(ch >> 16) & 0x0000000F]; | 
 | 5936 |             *p++ = hexdigits[(ch >> 12) & 0x0000000F]; | 
 | 5937 |             *p++ = hexdigits[(ch >> 8) & 0x0000000F]; | 
 | 5938 |             *p++ = hexdigits[(ch >> 4) & 0x0000000F]; | 
 | 5939 |             *p++ = hexdigits[ch & 0x0000000F]; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5940 |             continue; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5941 |         } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5942 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5943 |         /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ | 
 | 5944 |         else if (ch >= 0xD800 && ch < 0xDC00) { | 
 | 5945 |             Py_UNICODE ch2; | 
 | 5946 |             Py_UCS4 ucs; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5947 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5948 |             ch2 = *s++; | 
 | 5949 |             size--; | 
| Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5950 |             if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5951 |                 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | 
 | 5952 |                 *p++ = '\\'; | 
 | 5953 |                 *p++ = 'U'; | 
 | 5954 |                 *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; | 
 | 5955 |                 *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; | 
 | 5956 |                 *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; | 
 | 5957 |                 *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; | 
 | 5958 |                 *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; | 
 | 5959 |                 *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; | 
 | 5960 |                 *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; | 
 | 5961 |                 *p++ = hexdigits[ucs & 0x0000000F]; | 
 | 5962 |                 continue; | 
 | 5963 |             } | 
 | 5964 |             /* Fall through: isolated surrogates are copied as-is */ | 
 | 5965 |             s--; | 
 | 5966 |             size++; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5967 |         } | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5968 | #endif | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5969 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 |         /* Map 16-bit characters to '\uxxxx' */ | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5971 |         if (ch >= 256) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 |             *p++ = '\\'; | 
 | 5973 |             *p++ = 'u'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5974 |             *p++ = hexdigits[(ch >> 12) & 0x000F]; | 
 | 5975 |             *p++ = hexdigits[(ch >> 8) & 0x000F]; | 
 | 5976 |             *p++ = hexdigits[(ch >> 4) & 0x000F]; | 
 | 5977 |             *p++ = hexdigits[ch & 0x000F]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5979 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5980 |         /* Map special whitespace to '\t', \n', '\r' */ | 
 | 5981 |         else if (ch == '\t') { | 
 | 5982 |             *p++ = '\\'; | 
 | 5983 |             *p++ = 't'; | 
 | 5984 |         } | 
 | 5985 |         else if (ch == '\n') { | 
 | 5986 |             *p++ = '\\'; | 
 | 5987 |             *p++ = 'n'; | 
 | 5988 |         } | 
 | 5989 |         else if (ch == '\r') { | 
 | 5990 |             *p++ = '\\'; | 
 | 5991 |             *p++ = 'r'; | 
 | 5992 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5993 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5994 |         /* Map non-printable US ASCII to '\xhh' */ | 
| Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5995 |         else if (ch < ' ' || ch >= 0x7F) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5996 |             *p++ = '\\'; | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5997 |             *p++ = 'x'; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5998 |             *p++ = hexdigits[(ch >> 4) & 0x000F]; | 
 | 5999 |             *p++ = hexdigits[ch & 0x000F]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6000 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6001 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6002 |         /* Copy everything else as-is */ | 
 | 6003 |         else | 
 | 6004 |             *p++ = (char) ch; | 
 | 6005 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6006 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6007 |     assert(p - PyBytes_AS_STRING(repr) > 0); | 
 | 6008 |     if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) | 
 | 6009 |         return NULL; | 
 | 6010 |     return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | } | 
 | 6012 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6013 | PyObject * | 
 | 6014 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | { | 
| Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6016 |     PyObject *s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6017 |     if (!PyUnicode_Check(unicode)) { | 
 | 6018 |         PyErr_BadArgument(); | 
 | 6019 |         return NULL; | 
 | 6020 |     } | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6021 |     s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), | 
 | 6022 |                                       PyUnicode_GET_SIZE(unicode)); | 
| Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6023 |     return s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6024 | } | 
 | 6025 |  | 
 | 6026 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ | 
 | 6027 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6028 | PyObject * | 
 | 6029 | PyUnicode_DecodeRawUnicodeEscape(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6030 |                                  Py_ssize_t size, | 
 | 6031 |                                  const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6032 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6033 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6034 |     Py_ssize_t startinpos; | 
 | 6035 |     Py_ssize_t endinpos; | 
 | 6036 |     Py_ssize_t outpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6037 |     PyUnicodeObject *v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6038 |     Py_UNICODE *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 |     const char *end; | 
 | 6040 |     const char *bs; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6041 |     PyObject *errorHandler = NULL; | 
 | 6042 |     PyObject *exc = NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6043 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 |     /* Escaped strings will always be longer than the resulting | 
 | 6045 |        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] | 6046 |        length after conversion to the true value. (But decoding error | 
 | 6047 |        handler might have to resize the string) */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 |     v = _PyUnicode_New(size); | 
 | 6049 |     if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6050 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 |     if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6052 |         return (PyObject *)v; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6053 |     p = PyUnicode_AS_UNICODE(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6054 |     end = s + size; | 
 | 6055 |     while (s < end) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6056 |         unsigned char c; | 
 | 6057 |         Py_UCS4 x; | 
 | 6058 |         int i; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6059 |         int count; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6060 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6061 |         /* Non-escape characters are interpreted as Unicode ordinals */ | 
 | 6062 |         if (*s != '\\') { | 
 | 6063 |             *p++ = (unsigned char)*s++; | 
 | 6064 |             continue; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6065 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6066 |         startinpos = s-starts; | 
 | 6067 |  | 
 | 6068 |         /* \u-escapes are only interpreted iff the number of leading | 
 | 6069 |            backslashes if odd */ | 
 | 6070 |         bs = s; | 
 | 6071 |         for (;s < end;) { | 
 | 6072 |             if (*s != '\\') | 
 | 6073 |                 break; | 
 | 6074 |             *p++ = (unsigned char)*s++; | 
 | 6075 |         } | 
 | 6076 |         if (((s - bs) & 1) == 0 || | 
 | 6077 |             s >= end || | 
 | 6078 |             (*s != 'u' && *s != 'U')) { | 
 | 6079 |             continue; | 
 | 6080 |         } | 
 | 6081 |         p--; | 
 | 6082 |         count = *s=='u' ? 4 : 8; | 
 | 6083 |         s++; | 
 | 6084 |  | 
 | 6085 |         /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ | 
 | 6086 |         outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 6087 |         for (x = 0, i = 0; i < count; ++i, ++s) { | 
 | 6088 |             c = (unsigned char)*s; | 
| David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6089 |             if (!Py_ISXDIGIT(c)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6090 |                 endinpos = s-starts; | 
 | 6091 |                 if (unicode_decode_call_errorhandler( | 
 | 6092 |                         errors, &errorHandler, | 
 | 6093 |                         "rawunicodeescape", "truncated \\uXXXX", | 
 | 6094 |                         &starts, &end, &startinpos, &endinpos, &exc, &s, | 
 | 6095 |                         &v, &outpos, &p)) | 
 | 6096 |                     goto onError; | 
 | 6097 |                 goto nextByte; | 
 | 6098 |             } | 
 | 6099 |             x = (x<<4) & ~0xF; | 
 | 6100 |             if (c >= '0' && c <= '9') | 
 | 6101 |                 x += c - '0'; | 
 | 6102 |             else if (c >= 'a' && c <= 'f') | 
 | 6103 |                 x += 10 + c - 'a'; | 
 | 6104 |             else | 
 | 6105 |                 x += 10 + c - 'A'; | 
 | 6106 |         } | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6107 |         if (x <= 0xffff) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6108 |             /* UCS-2 character */ | 
 | 6109 |             *p++ = (Py_UNICODE) x; | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6110 |         else if (x <= 0x10ffff) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6111 |             /* UCS-4 character. Either store directly, or as | 
 | 6112 |                surrogate pair. */ | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6113 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6114 |             *p++ = (Py_UNICODE) x; | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6115 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6116 |             x -= 0x10000L; | 
 | 6117 |             *p++ = 0xD800 + (Py_UNICODE) (x >> 10); | 
 | 6118 |             *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6119 | #endif | 
 | 6120 |         } else { | 
 | 6121 |             endinpos = s-starts; | 
 | 6122 |             outpos = p-PyUnicode_AS_UNICODE(v); | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6123 |             if (unicode_decode_call_errorhandler( | 
 | 6124 |                     errors, &errorHandler, | 
 | 6125 |                     "rawunicodeescape", "\\Uxxxxxxxx out of range", | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6126 |                     &starts, &end, &startinpos, &endinpos, &exc, &s, | 
 | 6127 |                     &v, &outpos, &p)) | 
 | 6128 |                 goto onError; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6129 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6130 |       nextByte: | 
 | 6131 |         ; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6132 |     } | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6133 |     if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6134 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6135 |     Py_XDECREF(errorHandler); | 
 | 6136 |     Py_XDECREF(exc); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6137 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6138 |     if (_PyUnicode_READY_REPLACE(&v)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6139 |         Py_DECREF(v); | 
 | 6140 |         return NULL; | 
 | 6141 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6142 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6143 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6144 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6145 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6146 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6148 |     Py_XDECREF(errorHandler); | 
 | 6149 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6150 |     return NULL; | 
 | 6151 | } | 
 | 6152 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6153 | PyObject * | 
 | 6154 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6155 |                                  Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6156 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6157 |     PyObject *repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6158 |     char *p; | 
 | 6159 |     char *q; | 
 | 6160 |  | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6161 | #ifdef Py_UNICODE_WIDE | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6162 |     const Py_ssize_t expandsize = 10; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6163 | #else | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6164 |     const Py_ssize_t expandsize = 6; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6165 | #endif | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6166 |  | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6167 |     if (size > PY_SSIZE_T_MAX / expandsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6168 |         return PyErr_NoMemory(); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6169 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6170 |     repr = PyBytes_FromStringAndSize(NULL, expandsize * size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6171 |     if (repr == NULL) | 
 | 6172 |         return NULL; | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6173 |     if (size == 0) | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6174 |         return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6176 |     p = q = PyBytes_AS_STRING(repr); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6177 |     while (size-- > 0) { | 
 | 6178 |         Py_UNICODE ch = *s++; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6179 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6180 |         /* Map 32-bit characters to '\Uxxxxxxxx' */ | 
 | 6181 |         if (ch >= 0x10000) { | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6182 |             *p++ = '\\'; | 
 | 6183 |             *p++ = 'U'; | 
| Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6184 |             *p++ = hexdigits[(ch >> 28) & 0xf]; | 
 | 6185 |             *p++ = hexdigits[(ch >> 24) & 0xf]; | 
 | 6186 |             *p++ = hexdigits[(ch >> 20) & 0xf]; | 
 | 6187 |             *p++ = hexdigits[(ch >> 16) & 0xf]; | 
 | 6188 |             *p++ = hexdigits[(ch >> 12) & 0xf]; | 
 | 6189 |             *p++ = hexdigits[(ch >> 8) & 0xf]; | 
 | 6190 |             *p++ = hexdigits[(ch >> 4) & 0xf]; | 
 | 6191 |             *p++ = hexdigits[ch & 15]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6192 |         } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6193 |         else | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6194 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6195 |             /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ | 
 | 6196 |             if (ch >= 0xD800 && ch < 0xDC00) { | 
 | 6197 |                 Py_UNICODE ch2; | 
 | 6198 |                 Py_UCS4 ucs; | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6199 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6200 |                 ch2 = *s++; | 
 | 6201 |                 size--; | 
| Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6202 |                 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6203 |                     ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | 
 | 6204 |                     *p++ = '\\'; | 
 | 6205 |                     *p++ = 'U'; | 
 | 6206 |                     *p++ = hexdigits[(ucs >> 28) & 0xf]; | 
 | 6207 |                     *p++ = hexdigits[(ucs >> 24) & 0xf]; | 
 | 6208 |                     *p++ = hexdigits[(ucs >> 20) & 0xf]; | 
 | 6209 |                     *p++ = hexdigits[(ucs >> 16) & 0xf]; | 
 | 6210 |                     *p++ = hexdigits[(ucs >> 12) & 0xf]; | 
 | 6211 |                     *p++ = hexdigits[(ucs >> 8) & 0xf]; | 
 | 6212 |                     *p++ = hexdigits[(ucs >> 4) & 0xf]; | 
 | 6213 |                     *p++ = hexdigits[ucs & 0xf]; | 
 | 6214 |                     continue; | 
 | 6215 |                 } | 
 | 6216 |                 /* Fall through: isolated surrogates are copied as-is */ | 
 | 6217 |                 s--; | 
 | 6218 |                 size++; | 
 | 6219 |             } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6220 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6221 |         /* Map 16-bit characters to '\uxxxx' */ | 
 | 6222 |         if (ch >= 256) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 |             *p++ = '\\'; | 
 | 6224 |             *p++ = 'u'; | 
| Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6225 |             *p++ = hexdigits[(ch >> 12) & 0xf]; | 
 | 6226 |             *p++ = hexdigits[(ch >> 8) & 0xf]; | 
 | 6227 |             *p++ = hexdigits[(ch >> 4) & 0xf]; | 
 | 6228 |             *p++ = hexdigits[ch & 15]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6229 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6230 |         /* Copy everything else as-is */ | 
 | 6231 |         else | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 |             *p++ = (char) ch; | 
 | 6233 |     } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6234 |     size = p - q; | 
 | 6235 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6236 |     assert(size > 0); | 
 | 6237 |     if (_PyBytes_Resize(&repr, size) < 0) | 
 | 6238 |         return NULL; | 
 | 6239 |     return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6240 | } | 
 | 6241 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6242 | PyObject * | 
 | 6243 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6244 | { | 
| Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6245 |     PyObject *s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6246 |     if (!PyUnicode_Check(unicode)) { | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6247 |         PyErr_BadArgument(); | 
 | 6248 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6249 |     } | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6250 |     s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), | 
 | 6251 |                                          PyUnicode_GET_SIZE(unicode)); | 
 | 6252 |  | 
| Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6253 |     return s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6254 | } | 
 | 6255 |  | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6256 | /* --- Unicode Internal Codec ------------------------------------------- */ | 
 | 6257 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6258 | PyObject * | 
 | 6259 | _PyUnicode_DecodeUnicodeInternal(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6260 |                                  Py_ssize_t size, | 
 | 6261 |                                  const char *errors) | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6262 | { | 
 | 6263 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6264 |     Py_ssize_t startinpos; | 
 | 6265 |     Py_ssize_t endinpos; | 
 | 6266 |     Py_ssize_t outpos; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6267 |     PyUnicodeObject *v; | 
 | 6268 |     Py_UNICODE *p; | 
 | 6269 |     const char *end; | 
 | 6270 |     const char *reason; | 
 | 6271 |     PyObject *errorHandler = NULL; | 
 | 6272 |     PyObject *exc = NULL; | 
 | 6273 |  | 
| Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6274 | #ifdef Py_UNICODE_WIDE | 
 | 6275 |     Py_UNICODE unimax = PyUnicode_GetMax(); | 
 | 6276 | #endif | 
 | 6277 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6278 |     /* XXX overflow detection missing */ | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6279 |     v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); | 
 | 6280 |     if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6281 |         goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6282 |     /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH | 
 | 6283 |        as string was created with the old API. */ | 
 | 6284 |     if (PyUnicode_GET_SIZE(v) == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 |         return (PyObject *)v; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6286 |     p = PyUnicode_AS_UNICODE(v); | 
 | 6287 |     end = s + size; | 
 | 6288 |  | 
 | 6289 |     while (s < end) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6290 |         memcpy(p, s, sizeof(Py_UNICODE)); | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6291 |         /* We have to sanity check the raw data, otherwise doom looms for | 
 | 6292 |            some malformed UCS-4 data. */ | 
 | 6293 |         if ( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6294 | #ifdef Py_UNICODE_WIDE | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6295 |             *p > unimax || *p < 0 || | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | #endif | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6297 |             end-s < Py_UNICODE_SIZE | 
 | 6298 |             ) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6299 |         { | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6300 |             startinpos = s - starts; | 
 | 6301 |             if (end-s < Py_UNICODE_SIZE) { | 
 | 6302 |                 endinpos = end-starts; | 
 | 6303 |                 reason = "truncated input"; | 
 | 6304 |             } | 
 | 6305 |             else { | 
 | 6306 |                 endinpos = s - starts + Py_UNICODE_SIZE; | 
 | 6307 |                 reason = "illegal code point (> 0x10FFFF)"; | 
 | 6308 |             } | 
 | 6309 |             outpos = p - PyUnicode_AS_UNICODE(v); | 
 | 6310 |             if (unicode_decode_call_errorhandler( | 
 | 6311 |                     errors, &errorHandler, | 
 | 6312 |                     "unicode_internal", reason, | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6313 |                     &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6314 |                     &v, &outpos, &p)) { | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6315 |                 goto onError; | 
 | 6316 |             } | 
 | 6317 |         } | 
 | 6318 |         else { | 
 | 6319 |             p++; | 
 | 6320 |             s += Py_UNICODE_SIZE; | 
 | 6321 |         } | 
 | 6322 |     } | 
 | 6323 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6324 |     if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6325 |         goto onError; | 
 | 6326 |     Py_XDECREF(errorHandler); | 
 | 6327 |     Py_XDECREF(exc); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6328 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6329 |     if (_PyUnicode_READY_REPLACE(&v)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6330 |         Py_DECREF(v); | 
 | 6331 |         return NULL; | 
 | 6332 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6333 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6334 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6335 |     return (PyObject *)v; | 
 | 6336 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6337 |   onError: | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6338 |     Py_XDECREF(v); | 
 | 6339 |     Py_XDECREF(errorHandler); | 
 | 6340 |     Py_XDECREF(exc); | 
 | 6341 |     return NULL; | 
 | 6342 | } | 
 | 6343 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6344 | /* --- Latin-1 Codec ------------------------------------------------------ */ | 
 | 6345 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6346 | PyObject * | 
 | 6347 | PyUnicode_DecodeLatin1(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6348 |                        Py_ssize_t size, | 
 | 6349 |                        const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6350 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6351 |     /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6352 |     return _PyUnicode_FromUCS1((unsigned char*)s, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | } | 
 | 6354 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6355 | /* create or adjust a UnicodeEncodeError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6356 | static void | 
 | 6357 | make_encode_exception(PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6358 |                       const char *encoding, | 
 | 6359 |                       const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 6360 |                       Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 6361 |                       const char *reason) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6362 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6363 |     if (*exceptionObject == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6364 |         *exceptionObject = PyUnicodeEncodeError_Create( | 
 | 6365 |             encoding, unicode, size, startpos, endpos, reason); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6366 |     } | 
 | 6367 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6368 |         if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) | 
 | 6369 |             goto onError; | 
 | 6370 |         if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) | 
 | 6371 |             goto onError; | 
 | 6372 |         if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) | 
 | 6373 |             goto onError; | 
 | 6374 |         return; | 
 | 6375 |       onError: | 
 | 6376 |         Py_DECREF(*exceptionObject); | 
 | 6377 |         *exceptionObject = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6378 |     } | 
 | 6379 | } | 
 | 6380 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6381 | /* raises a UnicodeEncodeError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6382 | static void | 
 | 6383 | raise_encode_exception(PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6384 |                        const char *encoding, | 
 | 6385 |                        const Py_UNICODE *unicode, Py_ssize_t size, | 
 | 6386 |                        Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 6387 |                        const char *reason) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6388 | { | 
 | 6389 |     make_encode_exception(exceptionObject, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6390 |                           encoding, unicode, size, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6391 |     if (*exceptionObject != NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6392 |         PyCodec_StrictErrors(*exceptionObject); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6393 | } | 
 | 6394 |  | 
 | 6395 | /* error handling callback helper: | 
 | 6396 |    build arguments, call the callback and check the arguments, | 
 | 6397 |    put the result into newpos and return the replacement string, which | 
 | 6398 |    has to be freed by the caller */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6399 | static PyObject * | 
 | 6400 | unicode_encode_call_errorhandler(const char *errors, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6401 |                                  PyObject **errorHandler, | 
 | 6402 |                                  const char *encoding, const char *reason, | 
 | 6403 |                                  const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | 
 | 6404 |                                  Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 6405 |                                  Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6406 | { | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6407 |     static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6408 |  | 
 | 6409 |     PyObject *restuple; | 
 | 6410 |     PyObject *resunicode; | 
 | 6411 |  | 
 | 6412 |     if (*errorHandler == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6413 |         *errorHandler = PyCodec_LookupError(errors); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6414 |         if (*errorHandler == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6415 |             return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6416 |     } | 
 | 6417 |  | 
 | 6418 |     make_encode_exception(exceptionObject, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6419 |                           encoding, unicode, size, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6420 |     if (*exceptionObject == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6421 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6422 |  | 
 | 6423 |     restuple = PyObject_CallFunctionObjArgs( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6424 |         *errorHandler, *exceptionObject, NULL); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6425 |     if (restuple == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6426 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6427 |     if (!PyTuple_Check(restuple)) { | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6428 |         PyErr_SetString(PyExc_TypeError, &argparse[3]); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6429 |         Py_DECREF(restuple); | 
 | 6430 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6431 |     } | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6432 |     if (!PyArg_ParseTuple(restuple, argparse, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6433 |                           &resunicode, newpos)) { | 
 | 6434 |         Py_DECREF(restuple); | 
 | 6435 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6436 |     } | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6437 |     if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { | 
 | 6438 |         PyErr_SetString(PyExc_TypeError, &argparse[3]); | 
 | 6439 |         Py_DECREF(restuple); | 
 | 6440 |         return NULL; | 
 | 6441 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6442 |     if (*newpos<0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6443 |         *newpos = size+*newpos; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6444 |     if (*newpos<0 || *newpos>size) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6445 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); | 
 | 6446 |         Py_DECREF(restuple); | 
 | 6447 |         return NULL; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6448 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6449 |     Py_INCREF(resunicode); | 
 | 6450 |     Py_DECREF(restuple); | 
 | 6451 |     return resunicode; | 
 | 6452 | } | 
 | 6453 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6454 | static PyObject * | 
 | 6455 | unicode_encode_ucs1(const Py_UNICODE *p, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6456 |                     Py_ssize_t size, | 
 | 6457 |                     const char *errors, | 
 | 6458 |                     int limit) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6459 | { | 
 | 6460 |     /* output object */ | 
 | 6461 |     PyObject *res; | 
 | 6462 |     /* pointers to the beginning and end+1 of input */ | 
 | 6463 |     const Py_UNICODE *startp = p; | 
 | 6464 |     const Py_UNICODE *endp = p + size; | 
 | 6465 |     /* pointer to the beginning of the unencodable characters */ | 
 | 6466 |     /* const Py_UNICODE *badp = NULL; */ | 
 | 6467 |     /* pointer into the output */ | 
 | 6468 |     char *str; | 
 | 6469 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6470 |     Py_ssize_t ressize; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6471 |     const char *encoding = (limit == 256) ? "latin-1" : "ascii"; | 
 | 6472 |     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] | 6473 |     PyObject *errorHandler = NULL; | 
 | 6474 |     PyObject *exc = NULL; | 
 | 6475 |     /* the following variable is used for caching string comparisons | 
 | 6476 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ | 
 | 6477 |     int known_errorHandler = -1; | 
 | 6478 |  | 
 | 6479 |     /* allocate enough for a simple encoding without | 
 | 6480 |        replacements, if we need more, we'll resize */ | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6481 |     if (size == 0) | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6482 |         return PyBytes_FromStringAndSize(NULL, 0); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6483 |     res = PyBytes_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6484 |     if (res == NULL) | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6485 |         return NULL; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6486 |     str = PyBytes_AS_STRING(res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6487 |     ressize = size; | 
 | 6488 |  | 
 | 6489 |     while (p<endp) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6490 |         Py_UNICODE c = *p; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6491 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6492 |         /* can we encode this? */ | 
 | 6493 |         if (c<limit) { | 
 | 6494 |             /* no overflow check, because we know that the space is enough */ | 
 | 6495 |             *str++ = (char)c; | 
 | 6496 |             ++p; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6497 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6498 |         else { | 
 | 6499 |             Py_ssize_t unicodepos = p-startp; | 
 | 6500 |             Py_ssize_t requiredsize; | 
 | 6501 |             PyObject *repunicode; | 
 | 6502 |             Py_ssize_t repsize; | 
 | 6503 |             Py_ssize_t newpos; | 
 | 6504 |             Py_ssize_t respos; | 
 | 6505 |             Py_UNICODE *uni2; | 
 | 6506 |             /* startpos for collecting unencodable chars */ | 
 | 6507 |             const Py_UNICODE *collstart = p; | 
 | 6508 |             const Py_UNICODE *collend = p; | 
 | 6509 |             /* find all unecodable characters */ | 
 | 6510 |             while ((collend < endp) && ((*collend)>=limit)) | 
 | 6511 |                 ++collend; | 
 | 6512 |             /* cache callback name lookup (if not done yet, i.e. it's the first error) */ | 
 | 6513 |             if (known_errorHandler==-1) { | 
 | 6514 |                 if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 6515 |                     known_errorHandler = 1; | 
 | 6516 |                 else if (!strcmp(errors, "replace")) | 
 | 6517 |                     known_errorHandler = 2; | 
 | 6518 |                 else if (!strcmp(errors, "ignore")) | 
 | 6519 |                     known_errorHandler = 3; | 
 | 6520 |                 else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 6521 |                     known_errorHandler = 4; | 
 | 6522 |                 else | 
 | 6523 |                     known_errorHandler = 0; | 
 | 6524 |             } | 
 | 6525 |             switch (known_errorHandler) { | 
 | 6526 |             case 1: /* strict */ | 
 | 6527 |                 raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); | 
 | 6528 |                 goto onError; | 
 | 6529 |             case 2: /* replace */ | 
 | 6530 |                 while (collstart++<collend) | 
 | 6531 |                     *str++ = '?'; /* fall through */ | 
 | 6532 |             case 3: /* ignore */ | 
 | 6533 |                 p = collend; | 
 | 6534 |                 break; | 
 | 6535 |             case 4: /* xmlcharrefreplace */ | 
 | 6536 |                 respos = str - PyBytes_AS_STRING(res); | 
 | 6537 |                 /* determine replacement size (temporarily (mis)uses p) */ | 
 | 6538 |                 for (p = collstart, repsize = 0; p < collend; ++p) { | 
 | 6539 |                     if (*p<10) | 
 | 6540 |                         repsize += 2+1+1; | 
 | 6541 |                     else if (*p<100) | 
 | 6542 |                         repsize += 2+2+1; | 
 | 6543 |                     else if (*p<1000) | 
 | 6544 |                         repsize += 2+3+1; | 
 | 6545 |                     else if (*p<10000) | 
 | 6546 |                         repsize += 2+4+1; | 
| Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6547 | #ifndef Py_UNICODE_WIDE | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6548 |                     else | 
 | 6549 |                         repsize += 2+5+1; | 
| Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6550 | #else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6551 |                     else if (*p<100000) | 
 | 6552 |                         repsize += 2+5+1; | 
 | 6553 |                     else if (*p<1000000) | 
 | 6554 |                         repsize += 2+6+1; | 
 | 6555 |                     else | 
 | 6556 |                         repsize += 2+7+1; | 
| Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6557 | #endif | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6558 |                 } | 
 | 6559 |                 requiredsize = respos+repsize+(endp-collend); | 
 | 6560 |                 if (requiredsize > ressize) { | 
 | 6561 |                     if (requiredsize<2*ressize) | 
 | 6562 |                         requiredsize = 2*ressize; | 
 | 6563 |                     if (_PyBytes_Resize(&res, requiredsize)) | 
 | 6564 |                         goto onError; | 
 | 6565 |                     str = PyBytes_AS_STRING(res) + respos; | 
 | 6566 |                     ressize = requiredsize; | 
 | 6567 |                 } | 
 | 6568 |                 /* generate replacement (temporarily (mis)uses p) */ | 
 | 6569 |                 for (p = collstart; p < collend; ++p) { | 
 | 6570 |                     str += sprintf(str, "&#%d;", (int)*p); | 
 | 6571 |                 } | 
 | 6572 |                 p = collend; | 
 | 6573 |                 break; | 
 | 6574 |             default: | 
 | 6575 |                 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | 
 | 6576 |                                                               encoding, reason, startp, size, &exc, | 
 | 6577 |                                                               collstart-startp, collend-startp, &newpos); | 
 | 6578 |                 if (repunicode == NULL) | 
 | 6579 |                     goto onError; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6580 |                 if (PyBytes_Check(repunicode)) { | 
 | 6581 |                     /* Directly copy bytes result to output. */ | 
 | 6582 |                     repsize = PyBytes_Size(repunicode); | 
 | 6583 |                     if (repsize > 1) { | 
 | 6584 |                         /* Make room for all additional bytes. */ | 
| Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6585 |                         respos = str - PyBytes_AS_STRING(res); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6586 |                         if (_PyBytes_Resize(&res, ressize+repsize-1)) { | 
 | 6587 |                             Py_DECREF(repunicode); | 
 | 6588 |                             goto onError; | 
 | 6589 |                         } | 
| Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6590 |                         str = PyBytes_AS_STRING(res) + respos; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6591 |                         ressize += repsize-1; | 
 | 6592 |                     } | 
 | 6593 |                     memcpy(str, PyBytes_AsString(repunicode), repsize); | 
 | 6594 |                     str += repsize; | 
 | 6595 |                     p = startp + newpos; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6596 |                     Py_DECREF(repunicode); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6597 |                     break; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6598 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6599 |                 /* need more space? (at least enough for what we | 
 | 6600 |                    have+the replacement+the rest of the string, so | 
 | 6601 |                    we won't have to check space for encodable characters) */ | 
 | 6602 |                 respos = str - PyBytes_AS_STRING(res); | 
 | 6603 |                 repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 6604 |                 requiredsize = respos+repsize+(endp-collend); | 
 | 6605 |                 if (requiredsize > ressize) { | 
 | 6606 |                     if (requiredsize<2*ressize) | 
 | 6607 |                         requiredsize = 2*ressize; | 
 | 6608 |                     if (_PyBytes_Resize(&res, requiredsize)) { | 
 | 6609 |                         Py_DECREF(repunicode); | 
 | 6610 |                         goto onError; | 
 | 6611 |                     } | 
 | 6612 |                     str = PyBytes_AS_STRING(res) + respos; | 
 | 6613 |                     ressize = requiredsize; | 
 | 6614 |                 } | 
 | 6615 |                 /* check if there is anything unencodable in the replacement | 
 | 6616 |                    and copy it to the output */ | 
 | 6617 |                 for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { | 
 | 6618 |                     c = *uni2; | 
 | 6619 |                     if (c >= limit) { | 
 | 6620 |                         raise_encode_exception(&exc, encoding, startp, size, | 
 | 6621 |                                                unicodepos, unicodepos+1, reason); | 
 | 6622 |                         Py_DECREF(repunicode); | 
 | 6623 |                         goto onError; | 
 | 6624 |                     } | 
 | 6625 |                     *str = (char)c; | 
 | 6626 |                 } | 
 | 6627 |                 p = startp + newpos; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6628 |                 Py_DECREF(repunicode); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6629 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6630 |         } | 
 | 6631 |     } | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6632 |     /* Resize if we allocated to much */ | 
 | 6633 |     size = str - PyBytes_AS_STRING(res); | 
 | 6634 |     if (size < ressize) { /* If this falls res will be NULL */ | 
| Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6635 |         assert(size >= 0); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6636 |         if (_PyBytes_Resize(&res, size) < 0) | 
 | 6637 |             goto onError; | 
 | 6638 |     } | 
 | 6639 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6640 |     Py_XDECREF(errorHandler); | 
 | 6641 |     Py_XDECREF(exc); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6642 |     return res; | 
 | 6643 |  | 
 | 6644 |   onError: | 
 | 6645 |     Py_XDECREF(res); | 
 | 6646 |     Py_XDECREF(errorHandler); | 
 | 6647 |     Py_XDECREF(exc); | 
 | 6648 |     return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6649 | } | 
 | 6650 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6651 | PyObject * | 
 | 6652 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6653 |                        Py_ssize_t size, | 
 | 6654 |                        const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6656 |     return unicode_encode_ucs1(p, size, errors, 256); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | } | 
 | 6658 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6659 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6660 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6661 | { | 
 | 6662 |     if (!PyUnicode_Check(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6663 |         PyErr_BadArgument(); | 
 | 6664 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6665 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6666 |     if (PyUnicode_READY(unicode) == -1) | 
 | 6667 |         return NULL; | 
 | 6668 |     /* Fast path: if it is a one-byte string, construct | 
 | 6669 |        bytes object directly. */ | 
 | 6670 |     if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) | 
 | 6671 |         return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), | 
 | 6672 |                                          PyUnicode_GET_LENGTH(unicode)); | 
 | 6673 |     /* Non-Latin-1 characters present. Defer to above function to | 
 | 6674 |        raise the exception. */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6675 |     return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6676 |                                   PyUnicode_GET_SIZE(unicode), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6677 |                                   errors); | 
 | 6678 | } | 
 | 6679 |  | 
 | 6680 | PyObject* | 
 | 6681 | PyUnicode_AsLatin1String(PyObject *unicode) | 
 | 6682 | { | 
 | 6683 |     return _PyUnicode_AsLatin1String(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | } | 
 | 6685 |  | 
 | 6686 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ | 
 | 6687 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6688 | PyObject * | 
 | 6689 | PyUnicode_DecodeASCII(const char *s, | 
 | 6690 |                       Py_ssize_t size, | 
 | 6691 |                       const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6693 |     const char *starts = s; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 |     PyUnicodeObject *v; | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6695 |     Py_UNICODE *u; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6696 |     Py_ssize_t startinpos; | 
 | 6697 |     Py_ssize_t endinpos; | 
 | 6698 |     Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6699 |     const char *e; | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6700 |     int has_error; | 
 | 6701 |     const unsigned char *p = (const unsigned char *)s; | 
 | 6702 |     const unsigned char *end = p + size; | 
 | 6703 |     const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6704 |     PyObject *errorHandler = NULL; | 
 | 6705 |     PyObject *exc = NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6706 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 |     /* ASCII is equivalent to the first 128 ordinals in Unicode. */ | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6708 |     if (size == 1 && (unsigned char)s[0] < 128) | 
 | 6709 |         return get_latin1_char((unsigned char)s[0]); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6710 |  | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6711 |     has_error = 0; | 
 | 6712 |     while (p < end && !has_error) { | 
 | 6713 |         /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for | 
 | 6714 |            an explanation. */ | 
 | 6715 |         if (!((size_t) p & LONG_PTR_MASK)) { | 
 | 6716 |             /* Help register allocation */ | 
 | 6717 |             register const unsigned char *_p = p; | 
 | 6718 |             while (_p < aligned_end) { | 
 | 6719 |                 unsigned long value = *(unsigned long *) _p; | 
 | 6720 |                 if (value & ASCII_CHAR_MASK) { | 
 | 6721 |                     has_error = 1; | 
 | 6722 |                     break; | 
 | 6723 |                 } | 
 | 6724 |                 _p += SIZEOF_LONG; | 
 | 6725 |             } | 
 | 6726 |             if (_p == end) | 
 | 6727 |                 break; | 
 | 6728 |             if (has_error) | 
 | 6729 |                 break; | 
 | 6730 |             p = _p; | 
 | 6731 |         } | 
 | 6732 |         if (*p & 0x80) { | 
 | 6733 |             has_error = 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6734 |             break; | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6735 |         } | 
 | 6736 |         else { | 
 | 6737 |             ++p; | 
 | 6738 |         } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6739 |     } | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6740 |     if (!has_error) | 
 | 6741 |         return unicode_fromascii((const unsigned char *)s, size); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6742 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 |     v = _PyUnicode_New(size); | 
 | 6744 |     if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6745 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6746 |     if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6747 |         return (PyObject *)v; | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6748 |     u = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6749 |     e = s + size; | 
 | 6750 |     while (s < e) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6751 |         register unsigned char c = (unsigned char)*s; | 
 | 6752 |         if (c < 128) { | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6753 |             *u++ = c; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6754 |             ++s; | 
 | 6755 |         } | 
 | 6756 |         else { | 
 | 6757 |             startinpos = s-starts; | 
 | 6758 |             endinpos = startinpos + 1; | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6759 |             outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6760 |             if (unicode_decode_call_errorhandler( | 
 | 6761 |                     errors, &errorHandler, | 
 | 6762 |                     "ascii", "ordinal not in range(128)", | 
 | 6763 |                     &starts, &e, &startinpos, &endinpos, &exc, &s, | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6764 |                     &v, &outpos, &u)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6765 |                 goto onError; | 
 | 6766 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6767 |     } | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6768 |     if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) | 
 | 6769 |         if (PyUnicode_Resize((PyObject**)&v, u - PyUnicode_AS_UNICODE(v)) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6770 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6771 |     Py_XDECREF(errorHandler); | 
 | 6772 |     Py_XDECREF(exc); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6773 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6774 |     if (_PyUnicode_READY_REPLACE(&v)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6775 |         Py_DECREF(v); | 
 | 6776 |         return NULL; | 
 | 6777 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6778 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6779 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6781 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6782 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 |     Py_XDECREF(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6784 |     Py_XDECREF(errorHandler); | 
 | 6785 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6786 |     return NULL; | 
 | 6787 | } | 
 | 6788 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6789 | PyObject * | 
 | 6790 | PyUnicode_EncodeASCII(const Py_UNICODE *p, | 
 | 6791 |                       Py_ssize_t size, | 
 | 6792 |                       const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6793 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6794 |     return unicode_encode_ucs1(p, size, errors, 128); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | } | 
 | 6796 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6797 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6798 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6799 | { | 
 | 6800 |     if (!PyUnicode_Check(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6801 |         PyErr_BadArgument(); | 
 | 6802 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6803 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6804 |     if (PyUnicode_READY(unicode) == -1) | 
 | 6805 |         return NULL; | 
 | 6806 |     /* Fast path: if it is an ASCII-only string, construct bytes object | 
 | 6807 |        directly. Else defer to above function to raise the exception. */ | 
 | 6808 |     if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) | 
 | 6809 |         return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), | 
 | 6810 |                                          PyUnicode_GET_LENGTH(unicode)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 |     return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6812 |                                  PyUnicode_GET_SIZE(unicode), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6813 |                                  errors); | 
 | 6814 | } | 
 | 6815 |  | 
 | 6816 | PyObject * | 
 | 6817 | PyUnicode_AsASCIIString(PyObject *unicode) | 
 | 6818 | { | 
 | 6819 |     return _PyUnicode_AsASCIIString(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6820 | } | 
 | 6821 |  | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6822 | #ifdef HAVE_MBCS | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6823 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6824 | /* --- MBCS codecs for Windows -------------------------------------------- */ | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6825 |  | 
| Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6826 | #if SIZEOF_INT < SIZEOF_SIZE_T | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6827 | #define NEED_RETRY | 
 | 6828 | #endif | 
 | 6829 |  | 
 | 6830 | /* XXX This code is limited to "true" double-byte encodings, as | 
 | 6831 |    a) it assumes an incomplete character consists of a single byte, and | 
 | 6832 |    b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6833 |    encodings, see IsDBCSLeadByteEx documentation. */ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6834 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6835 | static int | 
 | 6836 | is_dbcs_lead_byte(const char *s, int offset) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6837 | { | 
 | 6838 |     const char *curr = s + offset; | 
 | 6839 |  | 
 | 6840 |     if (IsDBCSLeadByte(*curr)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6841 |         const char *prev = CharPrev(s, curr); | 
 | 6842 |         return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6843 |     } | 
 | 6844 |     return 0; | 
 | 6845 | } | 
 | 6846 |  | 
 | 6847 | /* | 
 | 6848 |  * Decode MBCS string into unicode object. If 'final' is set, converts | 
 | 6849 |  * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. | 
 | 6850 |  */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6851 | static int | 
 | 6852 | decode_mbcs(PyUnicodeObject **v, | 
 | 6853 |             const char *s, /* MBCS string */ | 
 | 6854 |             int size, /* sizeof MBCS string */ | 
 | 6855 |             int final, | 
 | 6856 |             const char *errors) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6857 | { | 
 | 6858 |     Py_UNICODE *p; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6859 |     Py_ssize_t n; | 
 | 6860 |     DWORD usize; | 
 | 6861 |     DWORD flags; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6862 |  | 
 | 6863 |     assert(size >= 0); | 
 | 6864 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6865 |     /* check and handle 'errors' arg */ | 
 | 6866 |     if (errors==NULL || strcmp(errors, "strict")==0) | 
 | 6867 |         flags = MB_ERR_INVALID_CHARS; | 
 | 6868 |     else if (strcmp(errors, "ignore")==0) | 
 | 6869 |         flags = 0; | 
 | 6870 |     else { | 
 | 6871 |         PyErr_Format(PyExc_ValueError, | 
 | 6872 |                      "mbcs encoding does not support errors='%s'", | 
 | 6873 |                      errors); | 
 | 6874 |         return -1; | 
 | 6875 |     } | 
 | 6876 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6877 |     /* Skip trailing lead-byte unless 'final' is set */ | 
 | 6878 |     if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6879 |         --size; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6880 |  | 
 | 6881 |     /* First get the size of the result */ | 
 | 6882 |     if (size > 0) { | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6883 |         usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); | 
 | 6884 |         if (usize==0) | 
 | 6885 |             goto mbcs_decode_error; | 
 | 6886 |     } else | 
 | 6887 |         usize = 0; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6888 |  | 
 | 6889 |     if (*v == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6890 |         /* Create unicode object */ | 
 | 6891 |         *v = _PyUnicode_New(usize); | 
 | 6892 |         if (*v == NULL) | 
 | 6893 |             return -1; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6894 |         n = 0; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6895 |     } | 
 | 6896 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6897 |         /* Extend unicode object */ | 
 | 6898 |         n = PyUnicode_GET_SIZE(*v); | 
| Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6899 |         if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6900 |             return -1; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6901 |     } | 
 | 6902 |  | 
 | 6903 |     /* Do the conversion */ | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6904 |     if (usize > 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6905 |         p = PyUnicode_AS_UNICODE(*v) + n; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6906 |         if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { | 
 | 6907 |             goto mbcs_decode_error; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6908 |         } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6909 |     } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6910 |     return size; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6911 |  | 
 | 6912 | mbcs_decode_error: | 
 | 6913 |     /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then | 
 | 6914 |        we raise a UnicodeDecodeError - else it is a 'generic' | 
 | 6915 |        windows error | 
 | 6916 |      */ | 
 | 6917 |     if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { | 
 | 6918 |         /* Ideally, we should get reason from FormatMessage - this | 
 | 6919 |            is the Windows 2000 English version of the message | 
 | 6920 |         */ | 
 | 6921 |         PyObject *exc = NULL; | 
 | 6922 |         const char *reason = "No mapping for the Unicode character exists " | 
 | 6923 |                              "in the target multi-byte code page."; | 
 | 6924 |         make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); | 
 | 6925 |         if (exc != NULL) { | 
 | 6926 |             PyCodec_StrictErrors(exc); | 
 | 6927 |             Py_DECREF(exc); | 
 | 6928 |         } | 
 | 6929 |     } else { | 
 | 6930 |         PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 6931 |     } | 
 | 6932 |     return -1; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6933 | } | 
 | 6934 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6935 | PyObject * | 
 | 6936 | PyUnicode_DecodeMBCSStateful(const char *s, | 
 | 6937 |                              Py_ssize_t size, | 
 | 6938 |                              const char *errors, | 
 | 6939 |                              Py_ssize_t *consumed) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6940 | { | 
 | 6941 |     PyUnicodeObject *v = NULL; | 
 | 6942 |     int done; | 
 | 6943 |  | 
 | 6944 |     if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6945 |         *consumed = 0; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6946 |  | 
 | 6947 | #ifdef NEED_RETRY | 
 | 6948 |   retry: | 
 | 6949 |     if (size > INT_MAX) | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6950 |         done = decode_mbcs(&v, s, INT_MAX, 0, errors); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6951 |     else | 
 | 6952 | #endif | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6953 |         done = decode_mbcs(&v, s, (int)size, !consumed, errors); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6954 |  | 
 | 6955 |     if (done < 0) { | 
 | 6956 |         Py_XDECREF(v); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6957 |         return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6958 |     } | 
 | 6959 |  | 
 | 6960 |     if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6961 |         *consumed += done; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6962 |  | 
 | 6963 | #ifdef NEED_RETRY | 
 | 6964 |     if (size > INT_MAX) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6965 |         s += done; | 
 | 6966 |         size -= done; | 
 | 6967 |         goto retry; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6968 |     } | 
 | 6969 | #endif | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6970 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6971 |     if (_PyUnicode_READY_REPLACE(&v)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6972 |         Py_DECREF(v); | 
 | 6973 |         return NULL; | 
 | 6974 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6975 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6976 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6977 |     return (PyObject *)v; | 
 | 6978 | } | 
 | 6979 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6980 | PyObject * | 
 | 6981 | PyUnicode_DecodeMBCS(const char *s, | 
 | 6982 |                      Py_ssize_t size, | 
 | 6983 |                      const char *errors) | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6984 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6985 |     return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); | 
 | 6986 | } | 
 | 6987 |  | 
 | 6988 | /* | 
 | 6989 |  * Convert unicode into string object (MBCS). | 
 | 6990 |  * Returns 0 if succeed, -1 otherwise. | 
 | 6991 |  */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6992 | static int | 
 | 6993 | encode_mbcs(PyObject **repr, | 
 | 6994 |             const Py_UNICODE *p, /* unicode */ | 
 | 6995 |             int size, /* size of unicode */ | 
 | 6996 |             const char* errors) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6997 | { | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6998 |     BOOL usedDefaultChar = FALSE; | 
 | 6999 |     BOOL *pusedDefaultChar; | 
 | 7000 |     int mbcssize; | 
 | 7001 |     Py_ssize_t n; | 
 | 7002 |     PyObject *exc = NULL; | 
 | 7003 |     DWORD flags; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7004 |  | 
 | 7005 |     assert(size >= 0); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7006 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7007 |     /* check and handle 'errors' arg */ | 
 | 7008 |     if (errors==NULL || strcmp(errors, "strict")==0) { | 
 | 7009 |         flags = WC_NO_BEST_FIT_CHARS; | 
 | 7010 |         pusedDefaultChar = &usedDefaultChar; | 
 | 7011 |     } else if (strcmp(errors, "replace")==0) { | 
 | 7012 |         flags = 0; | 
 | 7013 |         pusedDefaultChar = NULL; | 
 | 7014 |     } else { | 
 | 7015 |          PyErr_Format(PyExc_ValueError, | 
 | 7016 |                       "mbcs encoding does not support errors='%s'", | 
 | 7017 |                       errors); | 
 | 7018 |          return -1; | 
 | 7019 |     } | 
 | 7020 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7021 |     /* First get the size of the result */ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7022 |     if (size > 0) { | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7023 |         mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, | 
 | 7024 |                                        NULL, pusedDefaultChar); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7025 |         if (mbcssize == 0) { | 
 | 7026 |             PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 7027 |             return -1; | 
 | 7028 |         } | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7029 |         /* If we used a default char, then we failed! */ | 
 | 7030 |         if (pusedDefaultChar && *pusedDefaultChar) | 
 | 7031 |             goto mbcs_encode_error; | 
 | 7032 |     } else { | 
 | 7033 |         mbcssize = 0; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7034 |     } | 
 | 7035 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7036 |     if (*repr == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7037 |         /* Create string object */ | 
 | 7038 |         *repr = PyBytes_FromStringAndSize(NULL, mbcssize); | 
 | 7039 |         if (*repr == NULL) | 
 | 7040 |             return -1; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7041 |         n = 0; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7042 |     } | 
 | 7043 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7044 |         /* Extend string object */ | 
 | 7045 |         n = PyBytes_Size(*repr); | 
 | 7046 |         if (_PyBytes_Resize(repr, n + mbcssize) < 0) | 
 | 7047 |             return -1; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7048 |     } | 
 | 7049 |  | 
 | 7050 |     /* Do the conversion */ | 
 | 7051 |     if (size > 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7052 |         char *s = PyBytes_AS_STRING(*repr) + n; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7053 |         if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, | 
 | 7054 |                                      NULL, pusedDefaultChar)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7055 |             PyErr_SetFromWindowsErrWithFilename(0, NULL); | 
 | 7056 |             return -1; | 
 | 7057 |         } | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7058 |         if (pusedDefaultChar && *pusedDefaultChar) | 
 | 7059 |             goto mbcs_encode_error; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7060 |     } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7061 |     return 0; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7062 |  | 
 | 7063 | mbcs_encode_error: | 
 | 7064 |     raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); | 
 | 7065 |     Py_XDECREF(exc); | 
 | 7066 |     return -1; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7067 | } | 
 | 7068 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7069 | PyObject * | 
 | 7070 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, | 
 | 7071 |                      Py_ssize_t size, | 
 | 7072 |                      const char *errors) | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7073 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7074 |     PyObject *repr = NULL; | 
 | 7075 |     int ret; | 
| Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7076 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7077 | #ifdef NEED_RETRY | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7078 |   retry: | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7079 |     if (size > INT_MAX) | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7080 |         ret = encode_mbcs(&repr, p, INT_MAX, errors); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7081 |     else | 
 | 7082 | #endif | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7083 |         ret = encode_mbcs(&repr, p, (int)size, errors); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7084 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7085 |     if (ret < 0) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7086 |         Py_XDECREF(repr); | 
 | 7087 |         return NULL; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7088 |     } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7089 |  | 
 | 7090 | #ifdef NEED_RETRY | 
 | 7091 |     if (size > INT_MAX) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7092 |         p += INT_MAX; | 
 | 7093 |         size -= INT_MAX; | 
 | 7094 |         goto retry; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7095 |     } | 
 | 7096 | #endif | 
 | 7097 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7098 |     return repr; | 
 | 7099 | } | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7100 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7101 | PyObject * | 
 | 7102 | PyUnicode_AsMBCSString(PyObject *unicode) | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7103 | { | 
 | 7104 |     if (!PyUnicode_Check(unicode)) { | 
 | 7105 |         PyErr_BadArgument(); | 
 | 7106 |         return NULL; | 
 | 7107 |     } | 
 | 7108 |     return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7109 |                                 PyUnicode_GET_SIZE(unicode), | 
 | 7110 |                                 NULL); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7111 | } | 
 | 7112 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7113 | #undef NEED_RETRY | 
 | 7114 |  | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7115 | #endif /* HAVE_MBCS */ | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7116 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7117 | /* --- Character Mapping Codec -------------------------------------------- */ | 
 | 7118 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7119 | PyObject * | 
 | 7120 | PyUnicode_DecodeCharmap(const char *s, | 
 | 7121 |                         Py_ssize_t size, | 
 | 7122 |                         PyObject *mapping, | 
 | 7123 |                         const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7124 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7125 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7126 |     Py_ssize_t startinpos; | 
 | 7127 |     Py_ssize_t endinpos; | 
 | 7128 |     Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7129 |     const char *e; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 |     PyUnicodeObject *v; | 
 | 7131 |     Py_UNICODE *p; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7132 |     Py_ssize_t extrachars = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7133 |     PyObject *errorHandler = NULL; | 
 | 7134 |     PyObject *exc = NULL; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7135 |     Py_UNICODE *mapstring = NULL; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7136 |     Py_ssize_t maplen = 0; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7137 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7138 |     /* Default to Latin-1 */ | 
 | 7139 |     if (mapping == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7140 |         return PyUnicode_DecodeLatin1(s, size, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7141 |  | 
 | 7142 |     v = _PyUnicode_New(size); | 
 | 7143 |     if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7144 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7145 |     if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7146 |         return (PyObject *)v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7147 |     p = PyUnicode_AS_UNICODE(v); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7148 |     e = s + size; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7149 |     if (PyUnicode_CheckExact(mapping)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7150 |         mapstring = PyUnicode_AS_UNICODE(mapping); | 
 | 7151 |         maplen = PyUnicode_GET_SIZE(mapping); | 
 | 7152 |         while (s < e) { | 
 | 7153 |             unsigned char ch = *s; | 
 | 7154 |             Py_UNICODE x = 0xfffe; /* illegal value */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7155 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7156 |             if (ch < maplen) | 
 | 7157 |                 x = mapstring[ch]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7158 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7159 |             if (x == 0xfffe) { | 
 | 7160 |                 /* undefined mapping */ | 
 | 7161 |                 outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 7162 |                 startinpos = s-starts; | 
 | 7163 |                 endinpos = startinpos+1; | 
 | 7164 |                 if (unicode_decode_call_errorhandler( | 
 | 7165 |                         errors, &errorHandler, | 
 | 7166 |                         "charmap", "character maps to <undefined>", | 
 | 7167 |                         &starts, &e, &startinpos, &endinpos, &exc, &s, | 
 | 7168 |                         &v, &outpos, &p)) { | 
 | 7169 |                     goto onError; | 
 | 7170 |                 } | 
 | 7171 |                 continue; | 
 | 7172 |             } | 
 | 7173 |             *p++ = x; | 
 | 7174 |             ++s; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7175 |         } | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7176 |     } | 
 | 7177 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7178 |         while (s < e) { | 
 | 7179 |             unsigned char ch = *s; | 
 | 7180 |             PyObject *w, *x; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7181 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7182 |             /* Get mapping (char ordinal -> integer, Unicode char or None) */ | 
 | 7183 |             w = PyLong_FromLong((long)ch); | 
 | 7184 |             if (w == NULL) | 
 | 7185 |                 goto onError; | 
 | 7186 |             x = PyObject_GetItem(mapping, w); | 
 | 7187 |             Py_DECREF(w); | 
 | 7188 |             if (x == NULL) { | 
 | 7189 |                 if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 7190 |                     /* No mapping found means: mapping is undefined. */ | 
 | 7191 |                     PyErr_Clear(); | 
 | 7192 |                     x = Py_None; | 
 | 7193 |                     Py_INCREF(x); | 
 | 7194 |                 } else | 
 | 7195 |                     goto onError; | 
 | 7196 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7197 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7198 |             /* Apply mapping */ | 
 | 7199 |             if (PyLong_Check(x)) { | 
 | 7200 |                 long value = PyLong_AS_LONG(x); | 
 | 7201 |                 if (value < 0 || value > 65535) { | 
 | 7202 |                     PyErr_SetString(PyExc_TypeError, | 
 | 7203 |                                     "character mapping must be in range(65536)"); | 
 | 7204 |                     Py_DECREF(x); | 
 | 7205 |                     goto onError; | 
 | 7206 |                 } | 
 | 7207 |                 *p++ = (Py_UNICODE)value; | 
 | 7208 |             } | 
 | 7209 |             else if (x == Py_None) { | 
 | 7210 |                 /* undefined mapping */ | 
 | 7211 |                 outpos = p-PyUnicode_AS_UNICODE(v); | 
 | 7212 |                 startinpos = s-starts; | 
 | 7213 |                 endinpos = startinpos+1; | 
 | 7214 |                 if (unicode_decode_call_errorhandler( | 
 | 7215 |                         errors, &errorHandler, | 
 | 7216 |                         "charmap", "character maps to <undefined>", | 
 | 7217 |                         &starts, &e, &startinpos, &endinpos, &exc, &s, | 
 | 7218 |                         &v, &outpos, &p)) { | 
 | 7219 |                     Py_DECREF(x); | 
 | 7220 |                     goto onError; | 
 | 7221 |                 } | 
 | 7222 |                 Py_DECREF(x); | 
 | 7223 |                 continue; | 
 | 7224 |             } | 
 | 7225 |             else if (PyUnicode_Check(x)) { | 
 | 7226 |                 Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7227 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7228 |                 if (targetsize == 1) | 
 | 7229 |                     /* 1-1 mapping */ | 
 | 7230 |                     *p++ = *PyUnicode_AS_UNICODE(x); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7231 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7232 |                 else if (targetsize > 1) { | 
 | 7233 |                     /* 1-n mapping */ | 
 | 7234 |                     if (targetsize > extrachars) { | 
 | 7235 |                         /* resize first */ | 
 | 7236 |                         Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); | 
 | 7237 |                         Py_ssize_t needed = (targetsize - extrachars) + \ | 
 | 7238 |                             (targetsize << 2); | 
 | 7239 |                         extrachars += needed; | 
 | 7240 |                         /* XXX overflow detection missing */ | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7241 |                         if (PyUnicode_Resize((PyObject**)&v, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7242 |                                               PyUnicode_GET_SIZE(v) + needed) < 0) { | 
 | 7243 |                             Py_DECREF(x); | 
 | 7244 |                             goto onError; | 
 | 7245 |                         } | 
 | 7246 |                         p = PyUnicode_AS_UNICODE(v) + oldpos; | 
 | 7247 |                     } | 
 | 7248 |                     Py_UNICODE_COPY(p, | 
 | 7249 |                                     PyUnicode_AS_UNICODE(x), | 
 | 7250 |                                     targetsize); | 
 | 7251 |                     p += targetsize; | 
 | 7252 |                     extrachars -= targetsize; | 
 | 7253 |                 } | 
 | 7254 |                 /* 1-0 mapping: skip the character */ | 
 | 7255 |             } | 
 | 7256 |             else { | 
 | 7257 |                 /* wrong return value */ | 
 | 7258 |                 PyErr_SetString(PyExc_TypeError, | 
 | 7259 |                                 "character mapping must return integer, None or str"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7260 |                 Py_DECREF(x); | 
 | 7261 |                 goto onError; | 
 | 7262 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7263 |             Py_DECREF(x); | 
 | 7264 |             ++s; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7265 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7266 |     } | 
 | 7267 |     if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7268 |         if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7269 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7270 |     Py_XDECREF(errorHandler); | 
 | 7271 |     Py_XDECREF(exc); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7272 | #ifndef DONT_MAKE_RESULT_READY | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7273 |     if (_PyUnicode_READY_REPLACE(&v)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7274 |         Py_DECREF(v); | 
 | 7275 |         return NULL; | 
 | 7276 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7277 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7278 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7279 |     return (PyObject *)v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7280 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7281 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7282 |     Py_XDECREF(errorHandler); | 
 | 7283 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7284 |     Py_XDECREF(v); | 
 | 7285 |     return NULL; | 
 | 7286 | } | 
 | 7287 |  | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7288 | /* Charmap encoding: the lookup table */ | 
 | 7289 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7290 | struct encoding_map { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7291 |     PyObject_HEAD | 
 | 7292 |     unsigned char level1[32]; | 
 | 7293 |     int count2, count3; | 
 | 7294 |     unsigned char level23[1]; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7295 | }; | 
 | 7296 |  | 
 | 7297 | static PyObject* | 
 | 7298 | encoding_map_size(PyObject *obj, PyObject* args) | 
 | 7299 | { | 
 | 7300 |     struct encoding_map *map = (struct encoding_map*)obj; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7301 |     return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7302 |                            128*map->count3); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7303 | } | 
 | 7304 |  | 
 | 7305 | static PyMethodDef encoding_map_methods[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7306 |     {"size", encoding_map_size, METH_NOARGS, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7307 |      PyDoc_STR("Return the size (in bytes) of this object") }, | 
 | 7308 |     { 0 } | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7309 | }; | 
 | 7310 |  | 
 | 7311 | static void | 
 | 7312 | encoding_map_dealloc(PyObject* o) | 
 | 7313 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7314 |     PyObject_FREE(o); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7315 | } | 
 | 7316 |  | 
 | 7317 | static PyTypeObject EncodingMapType = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7318 |     PyVarObject_HEAD_INIT(NULL, 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7319 |     "EncodingMap",          /*tp_name*/ | 
 | 7320 |     sizeof(struct encoding_map),   /*tp_basicsize*/ | 
 | 7321 |     0,                      /*tp_itemsize*/ | 
 | 7322 |     /* methods */ | 
 | 7323 |     encoding_map_dealloc,   /*tp_dealloc*/ | 
 | 7324 |     0,                      /*tp_print*/ | 
 | 7325 |     0,                      /*tp_getattr*/ | 
 | 7326 |     0,                      /*tp_setattr*/ | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7327 |     0,                      /*tp_reserved*/ | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7328 |     0,                      /*tp_repr*/ | 
 | 7329 |     0,                      /*tp_as_number*/ | 
 | 7330 |     0,                      /*tp_as_sequence*/ | 
 | 7331 |     0,                      /*tp_as_mapping*/ | 
 | 7332 |     0,                      /*tp_hash*/ | 
 | 7333 |     0,                      /*tp_call*/ | 
 | 7334 |     0,                      /*tp_str*/ | 
 | 7335 |     0,                      /*tp_getattro*/ | 
 | 7336 |     0,                      /*tp_setattro*/ | 
 | 7337 |     0,                      /*tp_as_buffer*/ | 
 | 7338 |     Py_TPFLAGS_DEFAULT,     /*tp_flags*/ | 
 | 7339 |     0,                      /*tp_doc*/ | 
 | 7340 |     0,                      /*tp_traverse*/ | 
 | 7341 |     0,                      /*tp_clear*/ | 
 | 7342 |     0,                      /*tp_richcompare*/ | 
 | 7343 |     0,                      /*tp_weaklistoffset*/ | 
 | 7344 |     0,                      /*tp_iter*/ | 
 | 7345 |     0,                      /*tp_iternext*/ | 
 | 7346 |     encoding_map_methods,   /*tp_methods*/ | 
 | 7347 |     0,                      /*tp_members*/ | 
 | 7348 |     0,                      /*tp_getset*/ | 
 | 7349 |     0,                      /*tp_base*/ | 
 | 7350 |     0,                      /*tp_dict*/ | 
 | 7351 |     0,                      /*tp_descr_get*/ | 
 | 7352 |     0,                      /*tp_descr_set*/ | 
 | 7353 |     0,                      /*tp_dictoffset*/ | 
 | 7354 |     0,                      /*tp_init*/ | 
 | 7355 |     0,                      /*tp_alloc*/ | 
 | 7356 |     0,                      /*tp_new*/ | 
 | 7357 |     0,                      /*tp_free*/ | 
 | 7358 |     0,                      /*tp_is_gc*/ | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7359 | }; | 
 | 7360 |  | 
 | 7361 | PyObject* | 
 | 7362 | PyUnicode_BuildEncodingMap(PyObject* string) | 
 | 7363 | { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7364 |     PyObject *result; | 
 | 7365 |     struct encoding_map *mresult; | 
 | 7366 |     int i; | 
 | 7367 |     int need_dict = 0; | 
 | 7368 |     unsigned char level1[32]; | 
 | 7369 |     unsigned char level2[512]; | 
 | 7370 |     unsigned char *mlevel1, *mlevel2, *mlevel3; | 
 | 7371 |     int count2 = 0, count3 = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7372 |     int kind; | 
 | 7373 |     void *data; | 
 | 7374 |     Py_UCS4 ch; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7375 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7376 |     if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7377 |         PyErr_BadArgument(); | 
 | 7378 |         return NULL; | 
 | 7379 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7380 |     kind = PyUnicode_KIND(string); | 
 | 7381 |     data = PyUnicode_DATA(string); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7382 |     memset(level1, 0xFF, sizeof level1); | 
 | 7383 |     memset(level2, 0xFF, sizeof level2); | 
 | 7384 |  | 
 | 7385 |     /* If there isn't a one-to-one mapping of NULL to \0, | 
 | 7386 |        or if there are non-BMP characters, we need to use | 
 | 7387 |        a mapping dictionary. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7388 |     if (PyUnicode_READ(kind, data, 0) != 0) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7389 |         need_dict = 1; | 
 | 7390 |     for (i = 1; i < 256; i++) { | 
 | 7391 |         int l1, l2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7392 |         ch = PyUnicode_READ(kind, data, i); | 
 | 7393 |         if (ch == 0 || ch > 0xFFFF) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7394 |             need_dict = 1; | 
 | 7395 |             break; | 
 | 7396 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7397 |         if (ch == 0xFFFE) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7398 |             /* unmapped character */ | 
 | 7399 |             continue; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7400 |         l1 = ch >> 11; | 
 | 7401 |         l2 = ch >> 7; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7402 |         if (level1[l1] == 0xFF) | 
 | 7403 |             level1[l1] = count2++; | 
 | 7404 |         if (level2[l2] == 0xFF) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7405 |             level2[l2] = count3++; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7406 |     } | 
 | 7407 |  | 
 | 7408 |     if (count2 >= 0xFF || count3 >= 0xFF) | 
 | 7409 |         need_dict = 1; | 
 | 7410 |  | 
 | 7411 |     if (need_dict) { | 
 | 7412 |         PyObject *result = PyDict_New(); | 
 | 7413 |         PyObject *key, *value; | 
 | 7414 |         if (!result) | 
 | 7415 |             return NULL; | 
 | 7416 |         for (i = 0; i < 256; i++) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7417 |             key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7418 |             value = PyLong_FromLong(i); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7419 |             if (!key || !value) | 
 | 7420 |                 goto failed1; | 
 | 7421 |             if (PyDict_SetItem(result, key, value) == -1) | 
 | 7422 |                 goto failed1; | 
 | 7423 |             Py_DECREF(key); | 
 | 7424 |             Py_DECREF(value); | 
 | 7425 |         } | 
 | 7426 |         return result; | 
 | 7427 |       failed1: | 
 | 7428 |         Py_XDECREF(key); | 
 | 7429 |         Py_XDECREF(value); | 
 | 7430 |         Py_DECREF(result); | 
 | 7431 |         return NULL; | 
 | 7432 |     } | 
 | 7433 |  | 
 | 7434 |     /* Create a three-level trie */ | 
 | 7435 |     result = PyObject_MALLOC(sizeof(struct encoding_map) + | 
 | 7436 |                              16*count2 + 128*count3 - 1); | 
 | 7437 |     if (!result) | 
 | 7438 |         return PyErr_NoMemory(); | 
 | 7439 |     PyObject_Init(result, &EncodingMapType); | 
 | 7440 |     mresult = (struct encoding_map*)result; | 
 | 7441 |     mresult->count2 = count2; | 
 | 7442 |     mresult->count3 = count3; | 
 | 7443 |     mlevel1 = mresult->level1; | 
 | 7444 |     mlevel2 = mresult->level23; | 
 | 7445 |     mlevel3 = mresult->level23 + 16*count2; | 
 | 7446 |     memcpy(mlevel1, level1, 32); | 
 | 7447 |     memset(mlevel2, 0xFF, 16*count2); | 
 | 7448 |     memset(mlevel3, 0, 128*count3); | 
 | 7449 |     count3 = 0; | 
 | 7450 |     for (i = 1; i < 256; i++) { | 
 | 7451 |         int o1, o2, o3, i2, i3; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7452 |         if (PyUnicode_READ(kind, data, i) == 0xFFFE) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7453 |             /* unmapped character */ | 
 | 7454 |             continue; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7455 |         o1 = PyUnicode_READ(kind, data, i)>>11; | 
 | 7456 |         o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7457 |         i2 = 16*mlevel1[o1] + o2; | 
 | 7458 |         if (mlevel2[i2] == 0xFF) | 
 | 7459 |             mlevel2[i2] = count3++; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7460 |         o3 = PyUnicode_READ(kind, data, i) & 0x7F; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7461 |         i3 = 128*mlevel2[i2] + o3; | 
 | 7462 |         mlevel3[i3] = i; | 
 | 7463 |     } | 
 | 7464 |     return result; | 
 | 7465 | } | 
 | 7466 |  | 
 | 7467 | static int | 
 | 7468 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) | 
 | 7469 | { | 
 | 7470 |     struct encoding_map *map = (struct encoding_map*)mapping; | 
 | 7471 |     int l1 = c>>11; | 
 | 7472 |     int l2 = (c>>7) & 0xF; | 
 | 7473 |     int l3 = c & 0x7F; | 
 | 7474 |     int i; | 
 | 7475 |  | 
 | 7476 | #ifdef Py_UNICODE_WIDE | 
 | 7477 |     if (c > 0xFFFF) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7478 |         return -1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7479 |     } | 
 | 7480 | #endif | 
 | 7481 |     if (c == 0) | 
 | 7482 |         return 0; | 
 | 7483 |     /* level 1*/ | 
 | 7484 |     i = map->level1[l1]; | 
 | 7485 |     if (i == 0xFF) { | 
 | 7486 |         return -1; | 
 | 7487 |     } | 
 | 7488 |     /* level 2*/ | 
 | 7489 |     i = map->level23[16*i+l2]; | 
 | 7490 |     if (i == 0xFF) { | 
 | 7491 |         return -1; | 
 | 7492 |     } | 
 | 7493 |     /* level 3 */ | 
 | 7494 |     i = map->level23[16*map->count2 + 128*i + l3]; | 
 | 7495 |     if (i == 0) { | 
 | 7496 |         return -1; | 
 | 7497 |     } | 
 | 7498 |     return i; | 
 | 7499 | } | 
 | 7500 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7501 | /* Lookup the character ch in the mapping. If the character | 
 | 7502 |    can't be found, Py_None is returned (or NULL, if another | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7503 |    error occurred). */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7504 | static PyObject * | 
 | 7505 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7507 |     PyObject *w = PyLong_FromLong((long)c); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7508 |     PyObject *x; | 
 | 7509 |  | 
 | 7510 |     if (w == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7511 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7512 |     x = PyObject_GetItem(mapping, w); | 
 | 7513 |     Py_DECREF(w); | 
 | 7514 |     if (x == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7515 |         if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 7516 |             /* No mapping found means: mapping is undefined. */ | 
 | 7517 |             PyErr_Clear(); | 
 | 7518 |             x = Py_None; | 
 | 7519 |             Py_INCREF(x); | 
 | 7520 |             return x; | 
 | 7521 |         } else | 
 | 7522 |             return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7523 |     } | 
| Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7524 |     else if (x == Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7525 |         return x; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7526 |     else if (PyLong_Check(x)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7527 |         long value = PyLong_AS_LONG(x); | 
 | 7528 |         if (value < 0 || value > 255) { | 
 | 7529 |             PyErr_SetString(PyExc_TypeError, | 
 | 7530 |                             "character mapping must be in range(256)"); | 
 | 7531 |             Py_DECREF(x); | 
 | 7532 |             return NULL; | 
 | 7533 |         } | 
 | 7534 |         return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7535 |     } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7536 |     else if (PyBytes_Check(x)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 |         return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7538 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7539 |         /* wrong return value */ | 
 | 7540 |         PyErr_Format(PyExc_TypeError, | 
 | 7541 |                      "character mapping must return integer, bytes or None, not %.400s", | 
 | 7542 |                      x->ob_type->tp_name); | 
 | 7543 |         Py_DECREF(x); | 
 | 7544 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7545 |     } | 
 | 7546 | } | 
 | 7547 |  | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7548 | static int | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7549 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7550 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7551 |     Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); | 
 | 7552 |     /* exponentially overallocate to minimize reallocations */ | 
 | 7553 |     if (requiredsize < 2*outsize) | 
 | 7554 |         requiredsize = 2*outsize; | 
 | 7555 |     if (_PyBytes_Resize(outobj, requiredsize)) | 
 | 7556 |         return -1; | 
 | 7557 |     return 0; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7558 | } | 
 | 7559 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7560 | typedef enum charmapencode_result { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7561 |     enc_SUCCESS, enc_FAILED, enc_EXCEPTION | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7562 | } charmapencode_result; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7563 | /* lookup the character, put the result in the output string and adjust | 
| Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 7564 |    various state variables. Resize the output bytes object if not enough | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7565 |    space is available. Return a new reference to the object that | 
 | 7566 |    was put in the output buffer, or Py_None, if the mapping was undefined | 
 | 7567 |    (in which case no character was written) or NULL, if a | 
| Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7568 |    reallocation error occurred. The caller must decref the result */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7569 | static charmapencode_result | 
 | 7570 | charmapencode_output(Py_UNICODE c, PyObject *mapping, | 
 | 7571 |                      PyObject **outobj, Py_ssize_t *outpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7572 | { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7573 |     PyObject *rep; | 
 | 7574 |     char *outstart; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7575 |     Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7576 |  | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7577 |     if (Py_TYPE(mapping) == &EncodingMapType) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7578 |         int res = encoding_map_lookup(c, mapping); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7579 |         Py_ssize_t requiredsize = *outpos+1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7580 |         if (res == -1) | 
 | 7581 |             return enc_FAILED; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7582 |         if (outsize<requiredsize) | 
 | 7583 |             if (charmapencode_resize(outobj, outpos, requiredsize)) | 
 | 7584 |                 return enc_EXCEPTION; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7585 |         outstart = PyBytes_AS_STRING(*outobj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7586 |         outstart[(*outpos)++] = (char)res; | 
 | 7587 |         return enc_SUCCESS; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7588 |     } | 
 | 7589 |  | 
 | 7590 |     rep = charmapencode_lookup(c, mapping); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7591 |     if (rep==NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7592 |         return enc_EXCEPTION; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7593 |     else if (rep==Py_None) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7594 |         Py_DECREF(rep); | 
 | 7595 |         return enc_FAILED; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7596 |     } else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7597 |         if (PyLong_Check(rep)) { | 
 | 7598 |             Py_ssize_t requiredsize = *outpos+1; | 
 | 7599 |             if (outsize<requiredsize) | 
 | 7600 |                 if (charmapencode_resize(outobj, outpos, requiredsize)) { | 
 | 7601 |                     Py_DECREF(rep); | 
 | 7602 |                     return enc_EXCEPTION; | 
 | 7603 |                 } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7604 |             outstart = PyBytes_AS_STRING(*outobj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7605 |             outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7606 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7607 |         else { | 
 | 7608 |             const char *repchars = PyBytes_AS_STRING(rep); | 
 | 7609 |             Py_ssize_t repsize = PyBytes_GET_SIZE(rep); | 
 | 7610 |             Py_ssize_t requiredsize = *outpos+repsize; | 
 | 7611 |             if (outsize<requiredsize) | 
 | 7612 |                 if (charmapencode_resize(outobj, outpos, requiredsize)) { | 
 | 7613 |                     Py_DECREF(rep); | 
 | 7614 |                     return enc_EXCEPTION; | 
 | 7615 |                 } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7616 |             outstart = PyBytes_AS_STRING(*outobj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7617 |             memcpy(outstart + *outpos, repchars, repsize); | 
 | 7618 |             *outpos += repsize; | 
 | 7619 |         } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7620 |     } | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7621 |     Py_DECREF(rep); | 
 | 7622 |     return enc_SUCCESS; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7623 | } | 
 | 7624 |  | 
 | 7625 | /* handle an error in PyUnicode_EncodeCharmap | 
 | 7626 |    Return 0 on success, -1 on error */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7627 | static int | 
 | 7628 | charmap_encoding_error( | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7629 |     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] | 7630 |     PyObject **exceptionObject, | 
| Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7631 |     int *known_errorHandler, PyObject **errorHandler, const char *errors, | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7632 |     PyObject **res, Py_ssize_t *respos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7633 | { | 
 | 7634 |     PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7635 |     Py_ssize_t repsize; | 
 | 7636 |     Py_ssize_t newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7637 |     Py_UNICODE *uni2; | 
 | 7638 |     /* startpos for collecting unencodable chars */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7639 |     Py_ssize_t collstartpos = *inpos; | 
 | 7640 |     Py_ssize_t collendpos = *inpos+1; | 
 | 7641 |     Py_ssize_t collpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7642 |     char *encoding = "charmap"; | 
 | 7643 |     char *reason = "character maps to <undefined>"; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7644 |     charmapencode_result x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7645 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7646 |     /* find all unencodable characters */ | 
 | 7647 |     while (collendpos < size) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7648 |         PyObject *rep; | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7649 |         if (Py_TYPE(mapping) == &EncodingMapType) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7650 |             int res = encoding_map_lookup(p[collendpos], mapping); | 
 | 7651 |             if (res != -1) | 
 | 7652 |                 break; | 
 | 7653 |             ++collendpos; | 
 | 7654 |             continue; | 
 | 7655 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7656 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7657 |         rep = charmapencode_lookup(p[collendpos], mapping); | 
 | 7658 |         if (rep==NULL) | 
 | 7659 |             return -1; | 
 | 7660 |         else if (rep!=Py_None) { | 
 | 7661 |             Py_DECREF(rep); | 
 | 7662 |             break; | 
 | 7663 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7664 |         Py_DECREF(rep); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7665 |         ++collendpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7666 |     } | 
 | 7667 |     /* cache callback name lookup | 
 | 7668 |      * (if not done yet, i.e. it's the first error) */ | 
 | 7669 |     if (*known_errorHandler==-1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7670 |         if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 7671 |             *known_errorHandler = 1; | 
 | 7672 |         else if (!strcmp(errors, "replace")) | 
 | 7673 |             *known_errorHandler = 2; | 
 | 7674 |         else if (!strcmp(errors, "ignore")) | 
 | 7675 |             *known_errorHandler = 3; | 
 | 7676 |         else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 7677 |             *known_errorHandler = 4; | 
 | 7678 |         else | 
 | 7679 |             *known_errorHandler = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7680 |     } | 
 | 7681 |     switch (*known_errorHandler) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7682 |     case 1: /* strict */ | 
 | 7683 |         raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 7684 |         return -1; | 
 | 7685 |     case 2: /* replace */ | 
 | 7686 |         for (collpos = collstartpos; collpos<collendpos; ++collpos) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7687 |             x = charmapencode_output('?', mapping, res, respos); | 
 | 7688 |             if (x==enc_EXCEPTION) { | 
 | 7689 |                 return -1; | 
 | 7690 |             } | 
 | 7691 |             else if (x==enc_FAILED) { | 
 | 7692 |                 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 7693 |                 return -1; | 
 | 7694 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7695 |         } | 
 | 7696 |         /* fall through */ | 
 | 7697 |     case 3: /* ignore */ | 
 | 7698 |         *inpos = collendpos; | 
 | 7699 |         break; | 
 | 7700 |     case 4: /* xmlcharrefreplace */ | 
 | 7701 |         /* generate replacement (temporarily (mis)uses p) */ | 
 | 7702 |         for (collpos = collstartpos; collpos < collendpos; ++collpos) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7703 |             char buffer[2+29+1+1]; | 
 | 7704 |             char *cp; | 
 | 7705 |             sprintf(buffer, "&#%d;", (int)p[collpos]); | 
 | 7706 |             for (cp = buffer; *cp; ++cp) { | 
 | 7707 |                 x = charmapencode_output(*cp, mapping, res, respos); | 
 | 7708 |                 if (x==enc_EXCEPTION) | 
 | 7709 |                     return -1; | 
 | 7710 |                 else if (x==enc_FAILED) { | 
 | 7711 |                     raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 7712 |                     return -1; | 
 | 7713 |                 } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7714 |             } | 
 | 7715 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7716 |         *inpos = collendpos; | 
 | 7717 |         break; | 
 | 7718 |     default: | 
 | 7719 |         repunicode = unicode_encode_call_errorhandler(errors, errorHandler, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7720 |                                                       encoding, reason, p, size, exceptionObject, | 
 | 7721 |                                                       collstartpos, collendpos, &newpos); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7722 |         if (repunicode == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7723 |             return -1; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7724 |         if (PyBytes_Check(repunicode)) { | 
 | 7725 |             /* Directly copy bytes result to output. */ | 
 | 7726 |             Py_ssize_t outsize = PyBytes_Size(*res); | 
 | 7727 |             Py_ssize_t requiredsize; | 
 | 7728 |             repsize = PyBytes_Size(repunicode); | 
 | 7729 |             requiredsize = *respos + repsize; | 
 | 7730 |             if (requiredsize > outsize) | 
 | 7731 |                 /* Make room for all additional bytes. */ | 
 | 7732 |                 if (charmapencode_resize(res, respos, requiredsize)) { | 
 | 7733 |                     Py_DECREF(repunicode); | 
 | 7734 |                     return -1; | 
 | 7735 |                 } | 
 | 7736 |             memcpy(PyBytes_AsString(*res) + *respos, | 
 | 7737 |                    PyBytes_AsString(repunicode),  repsize); | 
 | 7738 |             *respos += repsize; | 
 | 7739 |             *inpos = newpos; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7740 |             Py_DECREF(repunicode); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7741 |             break; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7742 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7743 |         /* generate replacement  */ | 
 | 7744 |         repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 7745 |         for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7746 |             x = charmapencode_output(*uni2, mapping, res, respos); | 
 | 7747 |             if (x==enc_EXCEPTION) { | 
 | 7748 |                 return -1; | 
 | 7749 |             } | 
 | 7750 |             else if (x==enc_FAILED) { | 
 | 7751 |                 Py_DECREF(repunicode); | 
 | 7752 |                 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | 
 | 7753 |                 return -1; | 
 | 7754 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7755 |         } | 
 | 7756 |         *inpos = newpos; | 
 | 7757 |         Py_DECREF(repunicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7758 |     } | 
 | 7759 |     return 0; | 
 | 7760 | } | 
 | 7761 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7762 | PyObject * | 
 | 7763 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, | 
 | 7764 |                         Py_ssize_t size, | 
 | 7765 |                         PyObject *mapping, | 
 | 7766 |                         const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7767 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7768 |     /* output object */ | 
 | 7769 |     PyObject *res = NULL; | 
 | 7770 |     /* current input position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7771 |     Py_ssize_t inpos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7772 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7773 |     Py_ssize_t respos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7774 |     PyObject *errorHandler = NULL; | 
 | 7775 |     PyObject *exc = NULL; | 
 | 7776 |     /* the following variable is used for caching string comparisons | 
 | 7777 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
 | 7778 |      * 3=ignore, 4=xmlcharrefreplace */ | 
 | 7779 |     int known_errorHandler = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 |  | 
 | 7781 |     /* Default to Latin-1 */ | 
 | 7782 |     if (mapping == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7783 |         return PyUnicode_EncodeLatin1(p, size, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7784 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7785 |     /* allocate enough for a simple encoding without | 
 | 7786 |        replacements, if we need more, we'll resize */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7787 |     res = PyBytes_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7788 |     if (res == NULL) | 
 | 7789 |         goto onError; | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7790 |     if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 |         return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7792 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7793 |     while (inpos<size) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7794 |         /* try to encode it */ | 
 | 7795 |         charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); | 
 | 7796 |         if (x==enc_EXCEPTION) /* error */ | 
 | 7797 |             goto onError; | 
 | 7798 |         if (x==enc_FAILED) { /* unencodable character */ | 
 | 7799 |             if (charmap_encoding_error(p, size, &inpos, mapping, | 
 | 7800 |                                        &exc, | 
 | 7801 |                                        &known_errorHandler, &errorHandler, errors, | 
 | 7802 |                                        &res, &respos)) { | 
 | 7803 |                 goto onError; | 
 | 7804 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7805 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7806 |         else | 
 | 7807 |             /* done with this character => adjust input position */ | 
 | 7808 |             ++inpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7809 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7810 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7811 |     /* Resize if we allocated to much */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7812 |     if (respos<PyBytes_GET_SIZE(res)) | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7813 |         if (_PyBytes_Resize(&res, respos) < 0) | 
 | 7814 |             goto onError; | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7815 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7816 |     Py_XDECREF(exc); | 
 | 7817 |     Py_XDECREF(errorHandler); | 
 | 7818 |     return res; | 
 | 7819 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7820 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7821 |     Py_XDECREF(res); | 
 | 7822 |     Py_XDECREF(exc); | 
 | 7823 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7824 |     return NULL; | 
 | 7825 | } | 
 | 7826 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7827 | PyObject * | 
 | 7828 | PyUnicode_AsCharmapString(PyObject *unicode, | 
 | 7829 |                           PyObject *mapping) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7830 | { | 
 | 7831 |     if (!PyUnicode_Check(unicode) || mapping == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7832 |         PyErr_BadArgument(); | 
 | 7833 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 |     } | 
 | 7835 |     return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7836 |                                    PyUnicode_GET_SIZE(unicode), | 
 | 7837 |                                    mapping, | 
 | 7838 |                                    NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | } | 
 | 7840 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7841 | /* create or adjust a UnicodeTranslateError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7842 | static void | 
 | 7843 | make_translate_exception(PyObject **exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7844 |                          PyObject *unicode, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7845 |                          Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 7846 |                          const char *reason) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7847 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7848 |     if (*exceptionObject == NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7849 |         *exceptionObject = _PyUnicodeTranslateError_Create( | 
 | 7850 |             unicode, startpos, endpos, reason); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7851 |     } | 
 | 7852 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7853 |         if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) | 
 | 7854 |             goto onError; | 
 | 7855 |         if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) | 
 | 7856 |             goto onError; | 
 | 7857 |         if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) | 
 | 7858 |             goto onError; | 
 | 7859 |         return; | 
 | 7860 |       onError: | 
 | 7861 |         Py_DECREF(*exceptionObject); | 
 | 7862 |         *exceptionObject = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7863 |     } | 
 | 7864 | } | 
 | 7865 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7866 | /* raises a UnicodeTranslateError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7867 | static void | 
 | 7868 | raise_translate_exception(PyObject **exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7869 |                           PyObject *unicode, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7870 |                           Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 7871 |                           const char *reason) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7872 | { | 
 | 7873 |     make_translate_exception(exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7874 |                              unicode, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7875 |     if (*exceptionObject != NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7876 |         PyCodec_StrictErrors(*exceptionObject); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7877 | } | 
 | 7878 |  | 
 | 7879 | /* error handling callback helper: | 
 | 7880 |    build arguments, call the callback and check the arguments, | 
 | 7881 |    put the result into newpos and return the replacement string, which | 
 | 7882 |    has to be freed by the caller */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7883 | static PyObject * | 
 | 7884 | unicode_translate_call_errorhandler(const char *errors, | 
 | 7885 |                                     PyObject **errorHandler, | 
 | 7886 |                                     const char *reason, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7887 |                                     PyObject *unicode, PyObject **exceptionObject, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7888 |                                     Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 7889 |                                     Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7890 | { | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7891 |     static char *argparse = "O!n;translating error handler must return (str, int) tuple"; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7892 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7893 |     Py_ssize_t i_newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7894 |     PyObject *restuple; | 
 | 7895 |     PyObject *resunicode; | 
 | 7896 |  | 
 | 7897 |     if (*errorHandler == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7898 |         *errorHandler = PyCodec_LookupError(errors); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7899 |         if (*errorHandler == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7900 |             return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7901 |     } | 
 | 7902 |  | 
 | 7903 |     make_translate_exception(exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7904 |                              unicode, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7905 |     if (*exceptionObject == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7906 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7907 |  | 
 | 7908 |     restuple = PyObject_CallFunctionObjArgs( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7909 |         *errorHandler, *exceptionObject, NULL); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7910 |     if (restuple == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7911 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7912 |     if (!PyTuple_Check(restuple)) { | 
| Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7913 |         PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7914 |         Py_DECREF(restuple); | 
 | 7915 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7916 |     } | 
 | 7917 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7918 |                           &resunicode, &i_newpos)) { | 
 | 7919 |         Py_DECREF(restuple); | 
 | 7920 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7921 |     } | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7922 |     if (i_newpos<0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7923 |         *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7924 |     else | 
 | 7925 |         *newpos = i_newpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7926 |     if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7927 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); | 
 | 7928 |         Py_DECREF(restuple); | 
 | 7929 |         return NULL; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7930 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7931 |     Py_INCREF(resunicode); | 
 | 7932 |     Py_DECREF(restuple); | 
 | 7933 |     return resunicode; | 
 | 7934 | } | 
 | 7935 |  | 
 | 7936 | /* Lookup the character ch in the mapping and put the result in result, | 
 | 7937 |    which must be decrefed by the caller. | 
 | 7938 |    Return 0 on success, -1 on error */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7939 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7940 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7941 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7942 |     PyObject *w = PyLong_FromLong((long)c); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7943 |     PyObject *x; | 
 | 7944 |  | 
 | 7945 |     if (w == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7946 |         return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7947 |     x = PyObject_GetItem(mapping, w); | 
 | 7948 |     Py_DECREF(w); | 
 | 7949 |     if (x == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7950 |         if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 7951 |             /* No mapping found means: use 1:1 mapping. */ | 
 | 7952 |             PyErr_Clear(); | 
 | 7953 |             *result = NULL; | 
 | 7954 |             return 0; | 
 | 7955 |         } else | 
 | 7956 |             return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7957 |     } | 
 | 7958 |     else if (x == Py_None) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7959 |         *result = x; | 
 | 7960 |         return 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7961 |     } | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7962 |     else if (PyLong_Check(x)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7963 |         long value = PyLong_AS_LONG(x); | 
 | 7964 |         long max = PyUnicode_GetMax(); | 
 | 7965 |         if (value < 0 || value > max) { | 
 | 7966 |             PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7967 |                          "character mapping must be in range(0x%x)", max+1); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7968 |             Py_DECREF(x); | 
 | 7969 |             return -1; | 
 | 7970 |         } | 
 | 7971 |         *result = x; | 
 | 7972 |         return 0; | 
 | 7973 |     } | 
 | 7974 |     else if (PyUnicode_Check(x)) { | 
 | 7975 |         *result = x; | 
 | 7976 |         return 0; | 
 | 7977 |     } | 
 | 7978 |     else { | 
 | 7979 |         /* wrong return value */ | 
 | 7980 |         PyErr_SetString(PyExc_TypeError, | 
 | 7981 |                         "character mapping must return integer, None or str"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7982 |         Py_DECREF(x); | 
 | 7983 |         return -1; | 
 | 7984 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7985 | } | 
 | 7986 | /* ensure that *outobj is at least requiredsize characters long, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7987 |    if not reallocate and adjust various state variables. | 
 | 7988 |    Return 0 on success, -1 on error */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7989 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7990 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7991 |                                Py_ssize_t requiredsize) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7992 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7993 |     Py_ssize_t oldsize = *psize; | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7994 |     if (requiredsize > oldsize) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7995 |         /* exponentially overallocate to minimize reallocations */ | 
 | 7996 |         if (requiredsize < 2 * oldsize) | 
 | 7997 |             requiredsize = 2 * oldsize; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7998 |         *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); | 
 | 7999 |         if (*outobj == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8000 |             return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8001 |         *psize = requiredsize; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8002 |     } | 
 | 8003 |     return 0; | 
 | 8004 | } | 
 | 8005 | /* lookup the character, put the result in the output string and adjust | 
 | 8006 |    various state variables. Return a new reference to the object that | 
 | 8007 |    was put in the output buffer in *result, or Py_None, if the mapping was | 
 | 8008 |    undefined (in which case no character was written). | 
 | 8009 |    The called must decref result. | 
 | 8010 |    Return 0 on success, -1 on error. */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8011 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8012 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, | 
 | 8013 |                         PyObject *mapping, Py_UCS4 **output, | 
 | 8014 |                         Py_ssize_t *osize, Py_ssize_t *opos, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8015 |                         PyObject **res) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8016 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8017 |     Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); | 
 | 8018 |     if (charmaptranslate_lookup(curinp, mapping, res)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8019 |         return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8020 |     if (*res==NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8021 |         /* not found => default to 1:1 mapping */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8022 |         (*output)[(*opos)++] = curinp; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8023 |     } | 
 | 8024 |     else if (*res==Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8025 |         ; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8026 |     else if (PyLong_Check(*res)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8027 |         /* no overflow check, because we know that the space is enough */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8028 |         (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8029 |     } | 
 | 8030 |     else if (PyUnicode_Check(*res)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8031 |         Py_ssize_t repsize; | 
 | 8032 |         if (PyUnicode_READY(*res) == -1) | 
 | 8033 |             return -1; | 
 | 8034 |         repsize = PyUnicode_GET_LENGTH(*res); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8035 |         if (repsize==1) { | 
 | 8036 |             /* no overflow check, because we know that the space is enough */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8037 |             (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8038 |         } | 
 | 8039 |         else if (repsize!=0) { | 
 | 8040 |             /* more than one character */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8041 |             Py_ssize_t requiredsize = *opos + | 
 | 8042 |                 (PyUnicode_GET_LENGTH(input) - ipos) + | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8043 |                 repsize - 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8044 |             Py_ssize_t i; | 
 | 8045 |             if (charmaptranslate_makespace(output, osize, requiredsize)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8046 |                 return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8047 |             for(i = 0; i < repsize; i++) | 
 | 8048 |                 (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8049 |         } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8050 |     } | 
 | 8051 |     else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8052 |         return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8053 |     return 0; | 
 | 8054 | } | 
 | 8055 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8056 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8057 | _PyUnicode_TranslateCharmap(PyObject *input, | 
 | 8058 |                             PyObject *mapping, | 
 | 8059 |                             const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8060 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8061 |     /* input object */ | 
 | 8062 |     char *idata; | 
 | 8063 |     Py_ssize_t size, i; | 
 | 8064 |     int kind; | 
 | 8065 |     /* output buffer */ | 
 | 8066 |     Py_UCS4 *output = NULL; | 
 | 8067 |     Py_ssize_t osize; | 
 | 8068 |     PyObject *res; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8069 |     /* current output position */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8070 |     Py_ssize_t opos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8071 |     char *reason = "character maps to <undefined>"; | 
 | 8072 |     PyObject *errorHandler = NULL; | 
 | 8073 |     PyObject *exc = NULL; | 
 | 8074 |     /* the following variable is used for caching string comparisons | 
 | 8075 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
 | 8076 |      * 3=ignore, 4=xmlcharrefreplace */ | 
 | 8077 |     int known_errorHandler = -1; | 
 | 8078 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8079 |     if (mapping == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8080 |         PyErr_BadArgument(); | 
 | 8081 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8082 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8083 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8084 |     if (PyUnicode_READY(input) == -1) | 
 | 8085 |         return NULL; | 
 | 8086 |     idata = (char*)PyUnicode_DATA(input); | 
 | 8087 |     kind = PyUnicode_KIND(input); | 
 | 8088 |     size = PyUnicode_GET_LENGTH(input); | 
 | 8089 |     i = 0; | 
 | 8090 |  | 
 | 8091 |     if (size == 0) { | 
 | 8092 |         Py_INCREF(input); | 
 | 8093 |         return input; | 
 | 8094 |     } | 
 | 8095 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8096 |     /* allocate enough for a simple 1:1 translation without | 
 | 8097 |        replacements, if we need more, we'll resize */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8098 |     osize = size; | 
 | 8099 |     output = PyMem_Malloc(osize * sizeof(Py_UCS4)); | 
 | 8100 |     opos = 0; | 
 | 8101 |     if (output == NULL) { | 
 | 8102 |         PyErr_NoMemory(); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8103 |         goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8104 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8105 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8106 |     while (i<size) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8107 |         /* try to encode it */ | 
 | 8108 |         PyObject *x = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8109 |         if (charmaptranslate_output(input, i, mapping, | 
 | 8110 |                                     &output, &osize, &opos, &x)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8111 |             Py_XDECREF(x); | 
 | 8112 |             goto onError; | 
 | 8113 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8114 |         Py_XDECREF(x); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8115 |         if (x!=Py_None) /* it worked => adjust input pointer */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8116 |             ++i; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8117 |         else { /* untranslatable character */ | 
 | 8118 |             PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
 | 8119 |             Py_ssize_t repsize; | 
 | 8120 |             Py_ssize_t newpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8121 |             Py_ssize_t uni2; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 |             /* startpos for collecting untranslatable chars */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8123 |             Py_ssize_t collstart = i; | 
 | 8124 |             Py_ssize_t collend = i+1; | 
 | 8125 |             Py_ssize_t coll; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 |             /* find all untranslatable characters */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8128 |             while (collend < size) { | 
 | 8129 |                 if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8130 |                     goto onError; | 
 | 8131 |                 Py_XDECREF(x); | 
 | 8132 |                 if (x!=Py_None) | 
 | 8133 |                     break; | 
 | 8134 |                 ++collend; | 
 | 8135 |             } | 
 | 8136 |             /* cache callback name lookup | 
 | 8137 |              * (if not done yet, i.e. it's the first error) */ | 
 | 8138 |             if (known_errorHandler==-1) { | 
 | 8139 |                 if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 8140 |                     known_errorHandler = 1; | 
 | 8141 |                 else if (!strcmp(errors, "replace")) | 
 | 8142 |                     known_errorHandler = 2; | 
 | 8143 |                 else if (!strcmp(errors, "ignore")) | 
 | 8144 |                     known_errorHandler = 3; | 
 | 8145 |                 else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 8146 |                     known_errorHandler = 4; | 
 | 8147 |                 else | 
 | 8148 |                     known_errorHandler = 0; | 
 | 8149 |             } | 
 | 8150 |             switch (known_errorHandler) { | 
 | 8151 |             case 1: /* strict */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8152 |                 raise_translate_exception(&exc, input, collstart, | 
 | 8153 |                                           collend, reason); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8154 |                 goto onError; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8155 |             case 2: /* replace */ | 
 | 8156 |                 /* No need to check for space, this is a 1:1 replacement */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8157 |                 for (coll = collstart; coll<collend; coll++) | 
 | 8158 |                     output[opos++] = '?'; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8159 |                 /* fall through */ | 
 | 8160 |             case 3: /* ignore */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8161 |                 i = collend; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8162 |                 break; | 
 | 8163 |             case 4: /* xmlcharrefreplace */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8164 |                 /* generate replacement (temporarily (mis)uses i) */ | 
 | 8165 |                 for (i = collstart; i < collend; ++i) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8166 |                     char buffer[2+29+1+1]; | 
 | 8167 |                     char *cp; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8168 |                     sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); | 
 | 8169 |                     if (charmaptranslate_makespace(&output, &osize, | 
 | 8170 |                                                    opos+strlen(buffer)+(size-collend))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8171 |                         goto onError; | 
 | 8172 |                     for (cp = buffer; *cp; ++cp) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8173 |                         output[opos++] = *cp; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8175 |                 i = collend; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8176 |                 break; | 
 | 8177 |             default: | 
 | 8178 |                 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8179 |                                                                  reason, input, &exc, | 
 | 8180 |                                                                  collstart, collend, &newpos); | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8181 |                 if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8182 |                     goto onError; | 
 | 8183 |                 /* generate replacement  */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8184 |                 repsize = PyUnicode_GET_LENGTH(repunicode); | 
 | 8185 |                 if (charmaptranslate_makespace(&output, &osize, | 
 | 8186 |                                                opos+repsize+(size-collend))) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8187 |                     Py_DECREF(repunicode); | 
 | 8188 |                     goto onError; | 
 | 8189 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8190 |                 for (uni2 = 0; repsize-->0; ++uni2) | 
 | 8191 |                     output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); | 
 | 8192 |                 i = newpos; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8193 |                 Py_DECREF(repunicode); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8194 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8195 |         } | 
 | 8196 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8197 |     res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); | 
 | 8198 |     if (!res) | 
 | 8199 |         goto onError; | 
 | 8200 |     PyMem_Free(output); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8201 |     Py_XDECREF(exc); | 
 | 8202 |     Py_XDECREF(errorHandler); | 
 | 8203 |     return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8204 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8205 |   onError: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8206 |     PyMem_Free(output); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8207 |     Py_XDECREF(exc); | 
 | 8208 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8209 |     return NULL; | 
 | 8210 | } | 
 | 8211 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8212 | /* Deprecated. Use PyUnicode_Translate instead. */ | 
 | 8213 | PyObject * | 
 | 8214 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, | 
 | 8215 |                            Py_ssize_t size, | 
 | 8216 |                            PyObject *mapping, | 
 | 8217 |                            const char *errors) | 
 | 8218 | { | 
 | 8219 |     PyObject *unicode = PyUnicode_FromUnicode(p, size); | 
 | 8220 |     if (!unicode) | 
 | 8221 |         return NULL; | 
 | 8222 |     return _PyUnicode_TranslateCharmap(unicode, mapping, errors); | 
 | 8223 | } | 
 | 8224 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8225 | PyObject * | 
 | 8226 | PyUnicode_Translate(PyObject *str, | 
 | 8227 |                     PyObject *mapping, | 
 | 8228 |                     const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8229 | { | 
 | 8230 |     PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8231 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8232 |     str = PyUnicode_FromObject(str); | 
 | 8233 |     if (str == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 |         goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8235 |     result = _PyUnicode_TranslateCharmap(str, mapping, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8236 |     Py_DECREF(str); | 
 | 8237 |     return result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8238 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8239 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8240 |     Py_XDECREF(str); | 
 | 8241 |     return NULL; | 
 | 8242 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8243 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8244 | static Py_UCS4 | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8245 | fix_decimal_and_space_to_ascii(PyObject *self) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8246 | { | 
 | 8247 |     /* No need to call PyUnicode_READY(self) because this function is only | 
 | 8248 |        called as a callback from fixup() which does it already. */ | 
 | 8249 |     const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
 | 8250 |     const int kind = PyUnicode_KIND(self); | 
 | 8251 |     void *data = PyUnicode_DATA(self); | 
 | 8252 |     Py_UCS4 maxchar = 0, ch, fixed; | 
 | 8253 |     Py_ssize_t i; | 
 | 8254 |  | 
 | 8255 |     for (i = 0; i < len; ++i) { | 
 | 8256 |         ch = PyUnicode_READ(kind, data, i); | 
 | 8257 |         fixed = 0; | 
 | 8258 |         if (ch > 127) { | 
 | 8259 |             if (Py_UNICODE_ISSPACE(ch)) | 
 | 8260 |                 fixed = ' '; | 
 | 8261 |             else { | 
 | 8262 |                 const int decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 8263 |                 if (decimal >= 0) | 
 | 8264 |                     fixed = '0' + decimal; | 
 | 8265 |             } | 
 | 8266 |             if (fixed != 0) { | 
 | 8267 |                 if (fixed > maxchar) | 
 | 8268 |                     maxchar = fixed; | 
 | 8269 |                 PyUnicode_WRITE(kind, data, i, fixed); | 
 | 8270 |             } | 
 | 8271 |             else if (ch > maxchar) | 
 | 8272 |                 maxchar = ch; | 
 | 8273 |         } | 
 | 8274 |         else if (ch > maxchar) | 
 | 8275 |             maxchar = ch; | 
 | 8276 |     } | 
 | 8277 |  | 
 | 8278 |     return maxchar; | 
 | 8279 | } | 
 | 8280 |  | 
 | 8281 | PyObject * | 
 | 8282 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) | 
 | 8283 | { | 
 | 8284 |     if (!PyUnicode_Check(unicode)) { | 
 | 8285 |         PyErr_BadInternalCall(); | 
 | 8286 |         return NULL; | 
 | 8287 |     } | 
 | 8288 |     if (PyUnicode_READY(unicode) == -1) | 
 | 8289 |         return NULL; | 
 | 8290 |     if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { | 
 | 8291 |         /* If the string is already ASCII, just return the same string */ | 
 | 8292 |         Py_INCREF(unicode); | 
 | 8293 |         return unicode; | 
 | 8294 |     } | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8295 |     return fixup(unicode, fix_decimal_and_space_to_ascii); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8296 | } | 
 | 8297 |  | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8298 | PyObject * | 
 | 8299 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, | 
 | 8300 |                                   Py_ssize_t length) | 
 | 8301 | { | 
 | 8302 |     PyObject *result; | 
 | 8303 |     Py_UNICODE *p; /* write pointer into result */ | 
 | 8304 |     Py_ssize_t i; | 
 | 8305 |     /* Copy to a new string */ | 
 | 8306 |     result = (PyObject *)_PyUnicode_New(length); | 
 | 8307 |     Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); | 
 | 8308 |     if (result == NULL) | 
 | 8309 |         return result; | 
 | 8310 |     p = PyUnicode_AS_UNICODE(result); | 
 | 8311 |     /* Iterate over code points */ | 
 | 8312 |     for (i = 0; i < length; i++) { | 
 | 8313 |         Py_UNICODE ch =s[i]; | 
 | 8314 |         if (ch > 127) { | 
 | 8315 |             int decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 8316 |             if (decimal >= 0) | 
 | 8317 |                 p[i] = '0' + decimal; | 
 | 8318 |         } | 
 | 8319 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8320 | #ifndef DONT_MAKE_RESULT_READY | 
 | 8321 |     if (_PyUnicode_READY_REPLACE(&result)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8322 |         Py_DECREF(result); | 
 | 8323 |         return NULL; | 
 | 8324 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8325 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8326 |     assert(_PyUnicode_CheckConsistency(result, 1)); | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8327 |     return result; | 
 | 8328 | } | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8329 | /* --- Decimal Encoder ---------------------------------------------------- */ | 
 | 8330 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8331 | int | 
 | 8332 | PyUnicode_EncodeDecimal(Py_UNICODE *s, | 
 | 8333 |                         Py_ssize_t length, | 
 | 8334 |                         char *output, | 
 | 8335 |                         const char *errors) | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8336 | { | 
 | 8337 |     Py_UNICODE *p, *end; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8338 |     PyObject *errorHandler = NULL; | 
 | 8339 |     PyObject *exc = NULL; | 
 | 8340 |     const char *encoding = "decimal"; | 
 | 8341 |     const char *reason = "invalid decimal Unicode string"; | 
 | 8342 |     /* the following variable is used for caching string comparisons | 
 | 8343 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ | 
 | 8344 |     int known_errorHandler = -1; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8345 |  | 
 | 8346 |     if (output == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8347 |         PyErr_BadArgument(); | 
 | 8348 |         return -1; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8349 |     } | 
 | 8350 |  | 
 | 8351 |     p = s; | 
 | 8352 |     end = s + length; | 
 | 8353 |     while (p < end) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8354 |         register Py_UNICODE ch = *p; | 
 | 8355 |         int decimal; | 
 | 8356 |         PyObject *repunicode; | 
 | 8357 |         Py_ssize_t repsize; | 
 | 8358 |         Py_ssize_t newpos; | 
 | 8359 |         Py_UNICODE *uni2; | 
 | 8360 |         Py_UNICODE *collstart; | 
 | 8361 |         Py_UNICODE *collend; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8362 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8363 |         if (Py_UNICODE_ISSPACE(ch)) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8364 |             *output++ = ' '; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8365 |             ++p; | 
 | 8366 |             continue; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8367 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8368 |         decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 8369 |         if (decimal >= 0) { | 
 | 8370 |             *output++ = '0' + decimal; | 
 | 8371 |             ++p; | 
 | 8372 |             continue; | 
 | 8373 |         } | 
 | 8374 |         if (0 < ch && ch < 256) { | 
 | 8375 |             *output++ = (char)ch; | 
 | 8376 |             ++p; | 
 | 8377 |             continue; | 
 | 8378 |         } | 
 | 8379 |         /* All other characters are considered unencodable */ | 
 | 8380 |         collstart = p; | 
 | 8381 |         collend = p+1; | 
 | 8382 |         while (collend < end) { | 
 | 8383 |             if ((0 < *collend && *collend < 256) || | 
 | 8384 |                 !Py_UNICODE_ISSPACE(*collend) || | 
 | 8385 |                 Py_UNICODE_TODECIMAL(*collend)) | 
 | 8386 |                 break; | 
 | 8387 |         } | 
 | 8388 |         /* cache callback name lookup | 
 | 8389 |          * (if not done yet, i.e. it's the first error) */ | 
 | 8390 |         if (known_errorHandler==-1) { | 
 | 8391 |             if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 8392 |                 known_errorHandler = 1; | 
 | 8393 |             else if (!strcmp(errors, "replace")) | 
 | 8394 |                 known_errorHandler = 2; | 
 | 8395 |             else if (!strcmp(errors, "ignore")) | 
 | 8396 |                 known_errorHandler = 3; | 
 | 8397 |             else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 8398 |                 known_errorHandler = 4; | 
 | 8399 |             else | 
 | 8400 |                 known_errorHandler = 0; | 
 | 8401 |         } | 
 | 8402 |         switch (known_errorHandler) { | 
 | 8403 |         case 1: /* strict */ | 
 | 8404 |             raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); | 
 | 8405 |             goto onError; | 
 | 8406 |         case 2: /* replace */ | 
 | 8407 |             for (p = collstart; p < collend; ++p) | 
 | 8408 |                 *output++ = '?'; | 
 | 8409 |             /* fall through */ | 
 | 8410 |         case 3: /* ignore */ | 
 | 8411 |             p = collend; | 
 | 8412 |             break; | 
 | 8413 |         case 4: /* xmlcharrefreplace */ | 
 | 8414 |             /* generate replacement (temporarily (mis)uses p) */ | 
 | 8415 |             for (p = collstart; p < collend; ++p) | 
 | 8416 |                 output += sprintf(output, "&#%d;", (int)*p); | 
 | 8417 |             p = collend; | 
 | 8418 |             break; | 
 | 8419 |         default: | 
 | 8420 |             repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | 
 | 8421 |                                                           encoding, reason, s, length, &exc, | 
 | 8422 |                                                           collstart-s, collend-s, &newpos); | 
 | 8423 |             if (repunicode == NULL) | 
 | 8424 |                 goto onError; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8425 |             if (!PyUnicode_Check(repunicode)) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8426 |                 /* Byte results not supported, since they have no decimal property. */ | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8427 |                 PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); | 
 | 8428 |                 Py_DECREF(repunicode); | 
 | 8429 |                 goto onError; | 
 | 8430 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8431 |             /* generate replacement  */ | 
 | 8432 |             repsize = PyUnicode_GET_SIZE(repunicode); | 
 | 8433 |             for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | 
 | 8434 |                 Py_UNICODE ch = *uni2; | 
 | 8435 |                 if (Py_UNICODE_ISSPACE(ch)) | 
 | 8436 |                     *output++ = ' '; | 
 | 8437 |                 else { | 
 | 8438 |                     decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 8439 |                     if (decimal >= 0) | 
 | 8440 |                         *output++ = '0' + decimal; | 
 | 8441 |                     else if (0 < ch && ch < 256) | 
 | 8442 |                         *output++ = (char)ch; | 
 | 8443 |                     else { | 
 | 8444 |                         Py_DECREF(repunicode); | 
 | 8445 |                         raise_encode_exception(&exc, encoding, | 
 | 8446 |                                                s, length, collstart-s, collend-s, reason); | 
 | 8447 |                         goto onError; | 
 | 8448 |                     } | 
 | 8449 |                 } | 
 | 8450 |             } | 
 | 8451 |             p = s + newpos; | 
 | 8452 |             Py_DECREF(repunicode); | 
 | 8453 |         } | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8454 |     } | 
 | 8455 |     /* 0-terminate the output string */ | 
 | 8456 |     *output++ = '\0'; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8457 |     Py_XDECREF(exc); | 
 | 8458 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8459 |     return 0; | 
 | 8460 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8461 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8462 |     Py_XDECREF(exc); | 
 | 8463 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8464 |     return -1; | 
 | 8465 | } | 
 | 8466 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | /* --- Helpers ------------------------------------------------------------ */ | 
 | 8468 |  | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8469 | #include "stringlib/asciilib.h" | 
 | 8470 | #include "stringlib/fastsearch.h" | 
 | 8471 | #include "stringlib/partition.h" | 
 | 8472 | #include "stringlib/split.h" | 
 | 8473 | #include "stringlib/count.h" | 
 | 8474 | #include "stringlib/find.h" | 
 | 8475 | #include "stringlib/localeutil.h" | 
 | 8476 | #include "stringlib/undef.h" | 
 | 8477 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8478 | #include "stringlib/ucs1lib.h" | 
 | 8479 | #include "stringlib/fastsearch.h" | 
 | 8480 | #include "stringlib/partition.h" | 
 | 8481 | #include "stringlib/split.h" | 
 | 8482 | #include "stringlib/count.h" | 
 | 8483 | #include "stringlib/find.h" | 
 | 8484 | #include "stringlib/localeutil.h" | 
 | 8485 | #include "stringlib/undef.h" | 
 | 8486 |  | 
 | 8487 | #include "stringlib/ucs2lib.h" | 
 | 8488 | #include "stringlib/fastsearch.h" | 
 | 8489 | #include "stringlib/partition.h" | 
 | 8490 | #include "stringlib/split.h" | 
 | 8491 | #include "stringlib/count.h" | 
 | 8492 | #include "stringlib/find.h" | 
 | 8493 | #include "stringlib/localeutil.h" | 
 | 8494 | #include "stringlib/undef.h" | 
 | 8495 |  | 
 | 8496 | #include "stringlib/ucs4lib.h" | 
 | 8497 | #include "stringlib/fastsearch.h" | 
 | 8498 | #include "stringlib/partition.h" | 
 | 8499 | #include "stringlib/split.h" | 
 | 8500 | #include "stringlib/count.h" | 
 | 8501 | #include "stringlib/find.h" | 
 | 8502 | #include "stringlib/localeutil.h" | 
 | 8503 | #include "stringlib/undef.h" | 
 | 8504 |  | 
 | 8505 | static Py_ssize_t | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8506 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ascii)(const Py_UCS1*, Py_ssize_t, | 
 | 8507 |                                   const Py_UCS1*, Py_ssize_t, | 
 | 8508 |                                   Py_ssize_t, Py_ssize_t), | 
 | 8509 |                Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8510 |                                   const Py_UCS1*, Py_ssize_t, | 
 | 8511 |                                   Py_ssize_t, Py_ssize_t), | 
 | 8512 |                Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, | 
 | 8513 |                                   const Py_UCS2*, Py_ssize_t, | 
 | 8514 |                                   Py_ssize_t, Py_ssize_t), | 
 | 8515 |                Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, | 
 | 8516 |                                   const Py_UCS4*, Py_ssize_t, | 
 | 8517 |                                   Py_ssize_t, Py_ssize_t), | 
 | 8518 |                PyObject* s1, PyObject* s2, | 
 | 8519 |                Py_ssize_t start, | 
 | 8520 |                Py_ssize_t end) | 
 | 8521 | { | 
 | 8522 |     int kind1, kind2, kind; | 
 | 8523 |     void *buf1, *buf2; | 
 | 8524 |     Py_ssize_t len1, len2, result; | 
 | 8525 |  | 
 | 8526 |     kind1 = PyUnicode_KIND(s1); | 
 | 8527 |     kind2 = PyUnicode_KIND(s2); | 
 | 8528 |     kind = kind1 > kind2 ? kind1 : kind2; | 
 | 8529 |     buf1 = PyUnicode_DATA(s1); | 
 | 8530 |     buf2 = PyUnicode_DATA(s2); | 
 | 8531 |     if (kind1 != kind) | 
 | 8532 |         buf1 = _PyUnicode_AsKind(s1, kind); | 
 | 8533 |     if (!buf1) | 
 | 8534 |         return -2; | 
 | 8535 |     if (kind2 != kind) | 
 | 8536 |         buf2 = _PyUnicode_AsKind(s2, kind); | 
 | 8537 |     if (!buf2) { | 
 | 8538 |         if (kind1 != kind) PyMem_Free(buf1); | 
 | 8539 |         return -2; | 
 | 8540 |     } | 
 | 8541 |     len1 = PyUnicode_GET_LENGTH(s1); | 
 | 8542 |     len2 = PyUnicode_GET_LENGTH(s2); | 
 | 8543 |  | 
 | 8544 |     switch(kind) { | 
 | 8545 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8546 |         if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) | 
 | 8547 |             result = ascii(buf1, len1, buf2, len2, start, end); | 
 | 8548 |         else | 
 | 8549 |             result = ucs1(buf1, len1, buf2, len2, start, end); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8550 |         break; | 
 | 8551 |     case PyUnicode_2BYTE_KIND: | 
 | 8552 |         result = ucs2(buf1, len1, buf2, len2, start, end); | 
 | 8553 |         break; | 
 | 8554 |     case PyUnicode_4BYTE_KIND: | 
 | 8555 |         result = ucs4(buf1, len1, buf2, len2, start, end); | 
 | 8556 |         break; | 
 | 8557 |     default: | 
 | 8558 |         assert(0); result = -2; | 
 | 8559 |     } | 
 | 8560 |  | 
 | 8561 |     if (kind1 != kind) | 
 | 8562 |         PyMem_Free(buf1); | 
 | 8563 |     if (kind2 != kind) | 
 | 8564 |         PyMem_Free(buf2); | 
 | 8565 |  | 
 | 8566 |     return result; | 
 | 8567 | } | 
 | 8568 |  | 
 | 8569 | Py_ssize_t | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8570 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8571 |                                    Py_ssize_t n_buffer, | 
 | 8572 |                                    void *digits, Py_ssize_t n_digits, | 
 | 8573 |                                    Py_ssize_t min_width, | 
 | 8574 |                                    const char *grouping, | 
 | 8575 |                                    const char *thousands_sep) | 
 | 8576 | { | 
 | 8577 |     switch(kind) { | 
 | 8578 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8579 |         if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) | 
 | 8580 |             return _PyUnicode_ascii_InsertThousandsGrouping( | 
 | 8581 |                 (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, | 
 | 8582 |                 min_width, grouping, thousands_sep); | 
 | 8583 |         else | 
 | 8584 |             return _PyUnicode_ucs1_InsertThousandsGrouping( | 
 | 8585 |                 (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, | 
 | 8586 |                 min_width, grouping, thousands_sep); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8587 |     case PyUnicode_2BYTE_KIND: | 
 | 8588 |         return _PyUnicode_ucs2_InsertThousandsGrouping( | 
 | 8589 |             (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, | 
 | 8590 |             min_width, grouping, thousands_sep); | 
 | 8591 |     case PyUnicode_4BYTE_KIND: | 
 | 8592 |         return _PyUnicode_ucs4_InsertThousandsGrouping( | 
 | 8593 |             (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, | 
 | 8594 |             min_width, grouping, thousands_sep); | 
 | 8595 |     } | 
 | 8596 |     assert(0); | 
 | 8597 |     return -1; | 
 | 8598 | } | 
 | 8599 |  | 
 | 8600 |  | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8601 | #include "stringlib/unicodedefs.h" | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8602 | #include "stringlib/fastsearch.h" | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8603 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8604 | #include "stringlib/count.h" | 
 | 8605 | #include "stringlib/find.h" | 
| Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8606 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8607 | /* helper macro to fixup start/end slice values */ | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8608 | #define ADJUST_INDICES(start, end, len)         \ | 
 | 8609 |     if (end > len)                              \ | 
 | 8610 |         end = len;                              \ | 
 | 8611 |     else if (end < 0) {                         \ | 
 | 8612 |         end += len;                             \ | 
 | 8613 |         if (end < 0)                            \ | 
 | 8614 |             end = 0;                            \ | 
 | 8615 |     }                                           \ | 
 | 8616 |     if (start < 0) {                            \ | 
 | 8617 |         start += len;                           \ | 
 | 8618 |         if (start < 0)                          \ | 
 | 8619 |             start = 0;                          \ | 
 | 8620 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8621 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8622 | Py_ssize_t | 
 | 8623 | PyUnicode_Count(PyObject *str, | 
 | 8624 |                 PyObject *substr, | 
 | 8625 |                 Py_ssize_t start, | 
 | 8626 |                 Py_ssize_t end) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8627 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8628 |     Py_ssize_t result; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8629 |     PyUnicodeObject* str_obj; | 
 | 8630 |     PyUnicodeObject* sub_obj; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8631 |     int kind1, kind2, kind; | 
 | 8632 |     void *buf1 = NULL, *buf2 = NULL; | 
 | 8633 |     Py_ssize_t len1, len2; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8634 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8635 |     str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8636 |     if (!str_obj || PyUnicode_READY(str_obj) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8637 |         return -1; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8638 |     sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8639 |     if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8640 |         Py_DECREF(str_obj); | 
 | 8641 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8642 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8643 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8644 |     kind1 = PyUnicode_KIND(str_obj); | 
 | 8645 |     kind2 = PyUnicode_KIND(sub_obj); | 
 | 8646 |     kind = kind1 > kind2 ? kind1 : kind2; | 
 | 8647 |     buf1 = PyUnicode_DATA(str_obj); | 
 | 8648 |     if (kind1 != kind) | 
 | 8649 |         buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); | 
 | 8650 |     if (!buf1) | 
 | 8651 |         goto onError; | 
 | 8652 |     buf2 = PyUnicode_DATA(sub_obj); | 
 | 8653 |     if (kind2 != kind) | 
 | 8654 |         buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); | 
 | 8655 |     if (!buf2) | 
 | 8656 |         goto onError; | 
 | 8657 |     len1 = PyUnicode_GET_LENGTH(str_obj); | 
 | 8658 |     len2 = PyUnicode_GET_LENGTH(sub_obj); | 
 | 8659 |  | 
 | 8660 |     ADJUST_INDICES(start, end, len1); | 
 | 8661 |     switch(kind) { | 
 | 8662 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8663 |         if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) | 
 | 8664 |             result = asciilib_count( | 
 | 8665 |                 ((Py_UCS1*)buf1) + start, end - start, | 
 | 8666 |                 buf2, len2, PY_SSIZE_T_MAX | 
 | 8667 |                 ); | 
 | 8668 |         else | 
 | 8669 |             result = ucs1lib_count( | 
 | 8670 |                 ((Py_UCS1*)buf1) + start, end - start, | 
 | 8671 |                 buf2, len2, PY_SSIZE_T_MAX | 
 | 8672 |                 ); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8673 |         break; | 
 | 8674 |     case PyUnicode_2BYTE_KIND: | 
 | 8675 |         result = ucs2lib_count( | 
 | 8676 |             ((Py_UCS2*)buf1) + start, end - start, | 
 | 8677 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 8678 |             ); | 
 | 8679 |         break; | 
 | 8680 |     case PyUnicode_4BYTE_KIND: | 
 | 8681 |         result = ucs4lib_count( | 
 | 8682 |             ((Py_UCS4*)buf1) + start, end - start, | 
 | 8683 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 8684 |             ); | 
 | 8685 |         break; | 
 | 8686 |     default: | 
 | 8687 |         assert(0); result = 0; | 
 | 8688 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8689 |  | 
 | 8690 |     Py_DECREF(sub_obj); | 
 | 8691 |     Py_DECREF(str_obj); | 
 | 8692 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8693 |     if (kind1 != kind) | 
 | 8694 |         PyMem_Free(buf1); | 
 | 8695 |     if (kind2 != kind) | 
 | 8696 |         PyMem_Free(buf2); | 
 | 8697 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8698 |     return result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8699 |   onError: | 
 | 8700 |     Py_DECREF(sub_obj); | 
 | 8701 |     Py_DECREF(str_obj); | 
 | 8702 |     if (kind1 != kind && buf1) | 
 | 8703 |         PyMem_Free(buf1); | 
 | 8704 |     if (kind2 != kind && buf2) | 
 | 8705 |         PyMem_Free(buf2); | 
 | 8706 |     return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8707 | } | 
 | 8708 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8709 | Py_ssize_t | 
 | 8710 | PyUnicode_Find(PyObject *str, | 
 | 8711 |                PyObject *sub, | 
 | 8712 |                Py_ssize_t start, | 
 | 8713 |                Py_ssize_t end, | 
 | 8714 |                int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8715 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8716 |     Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8717 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8718 |     str = PyUnicode_FromObject(str); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8719 |     if (!str || PyUnicode_READY(str) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8720 |         return -2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8721 |     sub = PyUnicode_FromObject(sub); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8722 |     if (!sub || PyUnicode_READY(sub) == -1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8723 |         Py_DECREF(str); | 
 | 8724 |         return -2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8725 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8726 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8727 |     if (direction > 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8728 |         result = any_find_slice( | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8729 |             asciilib_find_slice, ucs1lib_find_slice, | 
 | 8730 |             ucs2lib_find_slice, ucs4lib_find_slice, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8731 |             str, sub, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8732 |             ); | 
 | 8733 |     else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8734 |         result = any_find_slice( | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8735 |             asciilib_find_slice, ucs1lib_rfind_slice, | 
 | 8736 |             ucs2lib_rfind_slice, ucs4lib_rfind_slice, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8737 |             str, sub, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8738 |             ); | 
 | 8739 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8740 |     Py_DECREF(str); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8741 |     Py_DECREF(sub); | 
 | 8742 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8743 |     return result; | 
 | 8744 | } | 
 | 8745 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | Py_ssize_t | 
 | 8747 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, | 
 | 8748 |                    Py_ssize_t start, Py_ssize_t end, | 
 | 8749 |                    int direction) | 
 | 8750 | { | 
 | 8751 |     char *result; | 
 | 8752 |     int kind; | 
 | 8753 |     if (PyUnicode_READY(str) == -1) | 
 | 8754 |         return -2; | 
| Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8755 |     if (start < 0 || end < 0) { | 
 | 8756 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 8757 |         return -2; | 
 | 8758 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8759 |     if (end > PyUnicode_GET_LENGTH(str)) | 
 | 8760 |         end = PyUnicode_GET_LENGTH(str); | 
 | 8761 |     kind = PyUnicode_KIND(str); | 
 | 8762 |     result = findchar(PyUnicode_1BYTE_DATA(str) | 
 | 8763 |                       + PyUnicode_KIND_SIZE(kind, start), | 
 | 8764 |                       kind, | 
 | 8765 |                       end-start, ch, direction); | 
 | 8766 |     if (!result) | 
 | 8767 |         return -1; | 
 | 8768 |     return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); | 
 | 8769 | } | 
 | 8770 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8771 | static int | 
 | 8772 | tailmatch(PyUnicodeObject *self, | 
 | 8773 |           PyUnicodeObject *substring, | 
 | 8774 |           Py_ssize_t start, | 
 | 8775 |           Py_ssize_t end, | 
 | 8776 |           int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8777 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8778 |     int kind_self; | 
 | 8779 |     int kind_sub; | 
 | 8780 |     void *data_self; | 
 | 8781 |     void *data_sub; | 
 | 8782 |     Py_ssize_t offset; | 
 | 8783 |     Py_ssize_t i; | 
 | 8784 |     Py_ssize_t end_sub; | 
 | 8785 |  | 
 | 8786 |     if (PyUnicode_READY(self) == -1 || | 
 | 8787 |         PyUnicode_READY(substring) == -1) | 
 | 8788 |         return 0; | 
 | 8789 |  | 
 | 8790 |     if (PyUnicode_GET_LENGTH(substring) == 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8791 |         return 1; | 
 | 8792 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8793 |     ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); | 
 | 8794 |     end -= PyUnicode_GET_LENGTH(substring); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8795 |     if (end < start) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8796 |         return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8797 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8798 |     kind_self = PyUnicode_KIND(self); | 
 | 8799 |     data_self = PyUnicode_DATA(self); | 
 | 8800 |     kind_sub = PyUnicode_KIND(substring); | 
 | 8801 |     data_sub = PyUnicode_DATA(substring); | 
 | 8802 |     end_sub = PyUnicode_GET_LENGTH(substring) - 1; | 
 | 8803 |  | 
 | 8804 |     if (direction > 0) | 
 | 8805 |         offset = end; | 
 | 8806 |     else | 
 | 8807 |         offset = start; | 
 | 8808 |  | 
 | 8809 |     if (PyUnicode_READ(kind_self, data_self, offset) == | 
 | 8810 |         PyUnicode_READ(kind_sub, data_sub, 0) && | 
 | 8811 |         PyUnicode_READ(kind_self, data_self, offset + end_sub) == | 
 | 8812 |         PyUnicode_READ(kind_sub, data_sub, end_sub)) { | 
 | 8813 |         /* If both are of the same kind, memcmp is sufficient */ | 
 | 8814 |         if (kind_self == kind_sub) { | 
 | 8815 |             return ! memcmp((char *)data_self + | 
 | 8816 |                                 (offset * PyUnicode_CHARACTER_SIZE(substring)), | 
 | 8817 |                             data_sub, | 
 | 8818 |                             PyUnicode_GET_LENGTH(substring) * | 
 | 8819 |                                 PyUnicode_CHARACTER_SIZE(substring)); | 
 | 8820 |         } | 
 | 8821 |         /* otherwise we have to compare each character by first accesing it */ | 
 | 8822 |         else { | 
 | 8823 |             /* We do not need to compare 0 and len(substring)-1 because | 
 | 8824 |                the if statement above ensured already that they are equal | 
 | 8825 |                when we end up here. */ | 
 | 8826 |             // TODO: honor direction and do a forward or backwards search | 
 | 8827 |             for (i = 1; i < end_sub; ++i) { | 
 | 8828 |                 if (PyUnicode_READ(kind_self, data_self, offset + i) != | 
 | 8829 |                     PyUnicode_READ(kind_sub, data_sub, i)) | 
 | 8830 |                     return 0; | 
 | 8831 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8832 |             return 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8833 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8834 |     } | 
 | 8835 |  | 
 | 8836 |     return 0; | 
 | 8837 | } | 
 | 8838 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8839 | Py_ssize_t | 
 | 8840 | PyUnicode_Tailmatch(PyObject *str, | 
 | 8841 |                     PyObject *substr, | 
 | 8842 |                     Py_ssize_t start, | 
 | 8843 |                     Py_ssize_t end, | 
 | 8844 |                     int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8845 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8846 |     Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8847 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8848 |     str = PyUnicode_FromObject(str); | 
 | 8849 |     if (str == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8850 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8851 |     substr = PyUnicode_FromObject(substr); | 
 | 8852 |     if (substr == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8853 |         Py_DECREF(str); | 
 | 8854 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8855 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8856 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8857 |     result = tailmatch((PyUnicodeObject *)str, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8858 |                        (PyUnicodeObject *)substr, | 
 | 8859 |                        start, end, direction); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8860 |     Py_DECREF(str); | 
 | 8861 |     Py_DECREF(substr); | 
 | 8862 |     return result; | 
 | 8863 | } | 
 | 8864 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8865 | /* Apply fixfct filter to the Unicode object self and return a | 
 | 8866 |    reference to the modified object */ | 
 | 8867 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8868 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8869 | fixup(PyObject *self, | 
 | 8870 |       Py_UCS4 (*fixfct)(PyObject *s)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8871 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8872 |     PyObject *u; | 
 | 8873 |     Py_UCS4 maxchar_old, maxchar_new = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8874 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8875 |     if (PyUnicode_READY(self) == -1) | 
 | 8876 |         return NULL; | 
 | 8877 |     maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); | 
 | 8878 |     u = PyUnicode_New(PyUnicode_GET_LENGTH(self), | 
 | 8879 |                       maxchar_old); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8880 |     if (u == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8881 |         return NULL; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8882 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8883 |     Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), | 
 | 8884 |               PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8885 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8886 |     /* fix functions return the new maximum character in a string, | 
 | 8887 |        if the kind of the resulting unicode object does not change, | 
 | 8888 |        everything is fine.  Otherwise we need to change the string kind | 
 | 8889 |        and re-run the fix function. */ | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8890 |     maxchar_new = fixfct(u); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8891 |     if (maxchar_new == 0) | 
 | 8892 |         /* do nothing, keep maxchar_new at 0 which means no changes. */; | 
 | 8893 |     else if (maxchar_new <= 127) | 
 | 8894 |         maxchar_new = 127; | 
 | 8895 |     else if (maxchar_new <= 255) | 
 | 8896 |         maxchar_new = 255; | 
 | 8897 |     else if (maxchar_new <= 65535) | 
 | 8898 |         maxchar_new = 65535; | 
 | 8899 |     else | 
 | 8900 |         maxchar_new = 1114111; /* 0x10ffff */ | 
 | 8901 |  | 
 | 8902 |     if (!maxchar_new && PyUnicode_CheckExact(self)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8903 |         /* fixfct should return TRUE if it modified the buffer. If | 
 | 8904 |            FALSE, return a reference to the original buffer instead | 
 | 8905 |            (to save space, not time) */ | 
 | 8906 |         Py_INCREF(self); | 
 | 8907 |         Py_DECREF(u); | 
 | 8908 |         return (PyObject*) self; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8909 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8910 |     else if (maxchar_new == maxchar_old) { | 
 | 8911 |         return u; | 
 | 8912 |     } | 
 | 8913 |     else { | 
 | 8914 |         /* In case the maximum character changed, we need to | 
 | 8915 |            convert the string to the new category. */ | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8916 |         PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8917 |         if (v == NULL) { | 
 | 8918 |             Py_DECREF(u); | 
 | 8919 |             return NULL; | 
 | 8920 |         } | 
 | 8921 |         if (maxchar_new > maxchar_old) { | 
 | 8922 |             /* If the maxchar increased so that the kind changed, not all | 
 | 8923 |                characters are representable anymore and we need to fix the | 
 | 8924 |                string again. This only happens in very few cases. */ | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8925 |             copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8926 |             maxchar_old = fixfct(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8927 |             assert(maxchar_old > 0 && maxchar_old <= maxchar_new); | 
 | 8928 |         } | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8929 |         else { | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8930 |             copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8931 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8932 |  | 
 | 8933 |         Py_DECREF(u); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8934 |         assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8935 |         return v; | 
 | 8936 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8937 | } | 
 | 8938 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8939 | static Py_UCS4 | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8940 | fixupper(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8941 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8942 |     /* No need to call PyUnicode_READY(self) because this function is only | 
 | 8943 |        called as a callback from fixup() which does it already. */ | 
 | 8944 |     const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
 | 8945 |     const int kind = PyUnicode_KIND(self); | 
 | 8946 |     void *data = PyUnicode_DATA(self); | 
 | 8947 |     int touched = 0; | 
 | 8948 |     Py_UCS4 maxchar = 0; | 
 | 8949 |     Py_ssize_t i; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8950 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8951 |     for (i = 0; i < len; ++i) { | 
 | 8952 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 8953 |         const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); | 
 | 8954 |         if (up != ch) { | 
 | 8955 |             if (up > maxchar) | 
 | 8956 |                 maxchar = up; | 
 | 8957 |             PyUnicode_WRITE(kind, data, i, up); | 
 | 8958 |             touched = 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8959 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8960 |         else if (ch > maxchar) | 
 | 8961 |             maxchar = ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8962 |     } | 
 | 8963 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8964 |     if (touched) | 
 | 8965 |         return maxchar; | 
 | 8966 |     else | 
 | 8967 |         return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8968 | } | 
 | 8969 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8970 | static Py_UCS4 | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8971 | fixlower(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8972 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8973 |     /* No need to call PyUnicode_READY(self) because fixup() which does it. */ | 
 | 8974 |     const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
 | 8975 |     const int kind = PyUnicode_KIND(self); | 
 | 8976 |     void *data = PyUnicode_DATA(self); | 
 | 8977 |     int touched = 0; | 
 | 8978 |     Py_UCS4 maxchar = 0; | 
 | 8979 |     Py_ssize_t i; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8980 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8981 |     for(i = 0; i < len; ++i) { | 
 | 8982 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 8983 |         const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); | 
 | 8984 |         if (lo != ch) { | 
 | 8985 |             if (lo > maxchar) | 
 | 8986 |                 maxchar = lo; | 
 | 8987 |             PyUnicode_WRITE(kind, data, i, lo); | 
 | 8988 |             touched = 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8989 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8990 |         else if (ch > maxchar) | 
 | 8991 |             maxchar = ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8992 |     } | 
 | 8993 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8994 |     if (touched) | 
 | 8995 |         return maxchar; | 
 | 8996 |     else | 
 | 8997 |         return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8998 | } | 
 | 8999 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9000 | static Py_UCS4 | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9001 | fixswapcase(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9003 |     /* No need to call PyUnicode_READY(self) because fixup() which does it. */ | 
 | 9004 |     const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
 | 9005 |     const int kind = PyUnicode_KIND(self); | 
 | 9006 |     void *data = PyUnicode_DATA(self); | 
 | 9007 |     int touched = 0; | 
 | 9008 |     Py_UCS4 maxchar = 0; | 
 | 9009 |     Py_ssize_t i; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9010 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9011 |     for(i = 0; i < len; ++i) { | 
 | 9012 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 9013 |         Py_UCS4 nu = 0; | 
 | 9014 |  | 
 | 9015 |         if (Py_UNICODE_ISUPPER(ch)) | 
 | 9016 |             nu = Py_UNICODE_TOLOWER(ch); | 
 | 9017 |         else if (Py_UNICODE_ISLOWER(ch)) | 
 | 9018 |             nu = Py_UNICODE_TOUPPER(ch); | 
 | 9019 |  | 
 | 9020 |         if (nu != 0) { | 
 | 9021 |             if (nu > maxchar) | 
 | 9022 |                 maxchar = nu; | 
 | 9023 |             PyUnicode_WRITE(kind, data, i, nu); | 
 | 9024 |             touched = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9025 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9026 |         else if (ch > maxchar) | 
 | 9027 |             maxchar = ch; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 |     } | 
 | 9029 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9030 |     if (touched) | 
 | 9031 |         return maxchar; | 
 | 9032 |     else | 
 | 9033 |         return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 | } | 
 | 9035 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9036 | static Py_UCS4 | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9037 | fixcapitalize(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9038 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9039 |     /* No need to call PyUnicode_READY(self) because fixup() which does it. */ | 
 | 9040 |     const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
 | 9041 |     const int kind = PyUnicode_KIND(self); | 
 | 9042 |     void *data = PyUnicode_DATA(self); | 
 | 9043 |     int touched = 0; | 
 | 9044 |     Py_UCS4 maxchar = 0; | 
 | 9045 |     Py_ssize_t i = 0; | 
 | 9046 |     Py_UCS4 ch; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9047 |  | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9048 |     if (len == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9049 |         return 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9050 |  | 
 | 9051 |     ch = PyUnicode_READ(kind, data, i); | 
 | 9052 |     if (!Py_UNICODE_ISUPPER(ch)) { | 
 | 9053 |         maxchar = Py_UNICODE_TOUPPER(ch); | 
 | 9054 |         PyUnicode_WRITE(kind, data, i, maxchar); | 
 | 9055 |         touched = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9057 |     ++i; | 
 | 9058 |     for(; i < len; ++i) { | 
 | 9059 |         ch = PyUnicode_READ(kind, data, i); | 
 | 9060 |         if (!Py_UNICODE_ISLOWER(ch)) { | 
 | 9061 |             const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); | 
 | 9062 |             if (lo > maxchar) | 
 | 9063 |                 maxchar = lo; | 
 | 9064 |             PyUnicode_WRITE(kind, data, i, lo); | 
 | 9065 |             touched = 1; | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9066 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9067 |         else if (ch > maxchar) | 
 | 9068 |             maxchar = ch; | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9069 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9070 |  | 
 | 9071 |     if (touched) | 
 | 9072 |         return maxchar; | 
 | 9073 |     else | 
 | 9074 |         return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9075 | } | 
 | 9076 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9077 | static Py_UCS4 | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9078 | fixtitle(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9079 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9080 |     /* No need to call PyUnicode_READY(self) because fixup() which does it. */ | 
 | 9081 |     const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
 | 9082 |     const int kind = PyUnicode_KIND(self); | 
 | 9083 |     void *data = PyUnicode_DATA(self); | 
 | 9084 |     Py_UCS4 maxchar = 0; | 
 | 9085 |     Py_ssize_t i = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9086 |     int previous_is_cased; | 
 | 9087 |  | 
 | 9088 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9089 |     if (len == 1) { | 
 | 9090 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 9091 |         const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); | 
 | 9092 |         if (ti != ch) { | 
 | 9093 |             PyUnicode_WRITE(kind, data, i, ti); | 
 | 9094 |             return ti; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9095 |         } | 
 | 9096 |         else | 
 | 9097 |             return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9098 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9099 |     previous_is_cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9100 |     for(; i < len; ++i) { | 
 | 9101 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 9102 |         Py_UCS4 nu; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9103 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9104 |         if (previous_is_cased) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9105 |             nu = Py_UNICODE_TOLOWER(ch); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9106 |         else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9107 |             nu = Py_UNICODE_TOTITLE(ch); | 
 | 9108 |  | 
 | 9109 |         if (nu > maxchar) | 
 | 9110 |             maxchar = nu; | 
 | 9111 |         PyUnicode_WRITE(kind, data, i, nu); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9112 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9113 |         if (Py_UNICODE_ISLOWER(ch) || | 
 | 9114 |             Py_UNICODE_ISUPPER(ch) || | 
 | 9115 |             Py_UNICODE_ISTITLE(ch)) | 
 | 9116 |             previous_is_cased = 1; | 
 | 9117 |         else | 
 | 9118 |             previous_is_cased = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9119 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9120 |     return maxchar; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9121 | } | 
 | 9122 |  | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9123 | PyObject * | 
 | 9124 | PyUnicode_Join(PyObject *separator, PyObject *seq) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9125 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9126 |     PyObject *sep = NULL; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9127 |     Py_ssize_t seplen; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9128 |     PyObject *res = NULL; /* the result */ | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9129 |     PyObject *fseq;          /* PySequence_Fast(seq) */ | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9130 |     Py_ssize_t seqlen;       /* len(fseq) -- number of items in sequence */ | 
 | 9131 |     PyObject **items; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9132 |     PyObject *item; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9133 |     Py_ssize_t sz, i, res_offset; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9134 |     Py_UCS4 maxchar; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9135 |     Py_UCS4 item_maxchar; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9136 |     int use_memcpy; | 
 | 9137 |     unsigned char *res_data = NULL, *sep_data = NULL; | 
 | 9138 |     PyObject *last_obj; | 
 | 9139 |     unsigned int kind = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9140 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9141 |     fseq = PySequence_Fast(seq, ""); | 
 | 9142 |     if (fseq == NULL) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9143 |         return NULL; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9144 |     } | 
 | 9145 |  | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9146 |     /* NOTE: the following code can't call back into Python code, | 
 | 9147 |      * so we are sure that fseq won't be mutated. | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9148 |      */ | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9149 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9150 |     seqlen = PySequence_Fast_GET_SIZE(fseq); | 
 | 9151 |     /* If empty sequence, return u"". */ | 
 | 9152 |     if (seqlen == 0) { | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9153 |         Py_DECREF(fseq); | 
 | 9154 |         Py_INCREF(unicode_empty); | 
 | 9155 |         res = unicode_empty; | 
 | 9156 |         return res; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9157 |     } | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9158 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9159 |     /* If singleton sequence with an exact Unicode, return that. */ | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9160 |     last_obj = NULL; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9161 |     items = PySequence_Fast_ITEMS(fseq); | 
| Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9162 |     if (seqlen == 1) { | 
 | 9163 |         if (PyUnicode_CheckExact(items[0])) { | 
 | 9164 |             res = items[0]; | 
 | 9165 |             Py_INCREF(res); | 
 | 9166 |             Py_DECREF(fseq); | 
 | 9167 |             return res; | 
 | 9168 |         } | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9169 |         seplen = 0; | 
| Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9170 |         maxchar = 0; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9171 |     } | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9172 |     else { | 
| Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9173 |         /* Set up sep and seplen */ | 
 | 9174 |         if (separator == NULL) { | 
 | 9175 |             /* fall back to a blank space separator */ | 
 | 9176 |             sep = PyUnicode_FromOrdinal(' '); | 
 | 9177 |             if (!sep) | 
 | 9178 |                 goto onError; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9179 |             seplen = 1; | 
| Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9180 |             maxchar = 32; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9181 |         } | 
| Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9182 |         else { | 
 | 9183 |             if (!PyUnicode_Check(separator)) { | 
 | 9184 |                 PyErr_Format(PyExc_TypeError, | 
 | 9185 |                              "separator: expected str instance," | 
 | 9186 |                              " %.80s found", | 
 | 9187 |                              Py_TYPE(separator)->tp_name); | 
 | 9188 |                 goto onError; | 
 | 9189 |             } | 
 | 9190 |             if (PyUnicode_READY(separator)) | 
 | 9191 |                 goto onError; | 
 | 9192 |             sep = separator; | 
 | 9193 |             seplen = PyUnicode_GET_LENGTH(separator); | 
 | 9194 |             maxchar = PyUnicode_MAX_CHAR_VALUE(separator); | 
 | 9195 |             /* inc refcount to keep this code path symmetric with the | 
 | 9196 |                above case of a blank separator */ | 
 | 9197 |             Py_INCREF(sep); | 
 | 9198 |         } | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9199 |         last_obj = sep; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9200 |     } | 
 | 9201 |  | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9202 |     /* There are at least two things to join, or else we have a subclass | 
 | 9203 |      * of str in the sequence. | 
 | 9204 |      * Do a pre-pass to figure out the total amount of space we'll | 
 | 9205 |      * need (sz), and see whether all argument are strings. | 
 | 9206 |      */ | 
 | 9207 |     sz = 0; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9208 | #ifdef Py_DEBUG | 
 | 9209 |     use_memcpy = 0; | 
 | 9210 | #else | 
 | 9211 |     use_memcpy = 1; | 
 | 9212 | #endif | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9213 |     for (i = 0; i < seqlen; i++) { | 
 | 9214 |         const Py_ssize_t old_sz = sz; | 
 | 9215 |         item = items[i]; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9216 |         if (!PyUnicode_Check(item)) { | 
 | 9217 |             PyErr_Format(PyExc_TypeError, | 
 | 9218 |                          "sequence item %zd: expected str instance," | 
 | 9219 |                          " %.80s found", | 
 | 9220 |                          i, Py_TYPE(item)->tp_name); | 
 | 9221 |             goto onError; | 
 | 9222 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9223 |         if (PyUnicode_READY(item) == -1) | 
 | 9224 |             goto onError; | 
 | 9225 |         sz += PyUnicode_GET_LENGTH(item); | 
 | 9226 |         item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); | 
| Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9227 |         maxchar = Py_MAX(maxchar, item_maxchar); | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9228 |         if (i != 0) | 
 | 9229 |             sz += seplen; | 
 | 9230 |         if (sz < old_sz || sz > PY_SSIZE_T_MAX) { | 
 | 9231 |             PyErr_SetString(PyExc_OverflowError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9232 |                             "join() result is too long for a Python string"); | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9233 |             goto onError; | 
 | 9234 |         } | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9235 |         if (use_memcpy && last_obj != NULL) { | 
 | 9236 |             if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) | 
 | 9237 |                 use_memcpy = 0; | 
 | 9238 |         } | 
 | 9239 |         last_obj = item; | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9240 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9241 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9242 |     res = PyUnicode_New(sz, maxchar); | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9243 |     if (res == NULL) | 
 | 9244 |         goto onError; | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9245 |  | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9246 |     /* Catenate everything. */ | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9247 | #ifdef Py_DEBUG | 
 | 9248 |     use_memcpy = 0; | 
 | 9249 | #else | 
 | 9250 |     if (use_memcpy) { | 
 | 9251 |         res_data = PyUnicode_1BYTE_DATA(res); | 
 | 9252 |         kind = PyUnicode_KIND(res); | 
 | 9253 |         if (seplen != 0) | 
 | 9254 |             sep_data = PyUnicode_1BYTE_DATA(sep); | 
 | 9255 |     } | 
 | 9256 | #endif | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9257 |     for (i = 0, res_offset = 0; i < seqlen; ++i) { | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9258 |         Py_ssize_t itemlen; | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9259 |         item = items[i]; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9260 |         /* Copy item, and maybe the separator. */ | 
| Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9261 |         if (i && seplen != 0) { | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9262 |             if (use_memcpy) { | 
 | 9263 |                 Py_MEMCPY(res_data, | 
 | 9264 |                           sep_data, | 
 | 9265 |                           PyUnicode_KIND_SIZE(kind, seplen)); | 
 | 9266 |                 res_data += PyUnicode_KIND_SIZE(kind, seplen); | 
 | 9267 |             } | 
 | 9268 |             else { | 
 | 9269 |                 copy_characters(res, res_offset, sep, 0, seplen); | 
 | 9270 |                 res_offset += seplen; | 
 | 9271 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9272 |         } | 
| Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9273 |         itemlen = PyUnicode_GET_LENGTH(item); | 
 | 9274 |         if (itemlen != 0) { | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9275 |             if (use_memcpy) { | 
 | 9276 |                 Py_MEMCPY(res_data, | 
 | 9277 |                           PyUnicode_DATA(item), | 
 | 9278 |                           PyUnicode_KIND_SIZE(kind, itemlen)); | 
 | 9279 |                 res_data += PyUnicode_KIND_SIZE(kind, itemlen); | 
 | 9280 |             } | 
 | 9281 |             else { | 
 | 9282 |                 copy_characters(res, res_offset, item, 0, itemlen); | 
 | 9283 |                 res_offset += itemlen; | 
 | 9284 |             } | 
| Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9285 |         } | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9286 |     } | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame^] | 9287 |     if (use_memcpy) | 
 | 9288 |         assert(res_data == PyUnicode_1BYTE_DATA(res) | 
 | 9289 |                            + PyUnicode_KIND_SIZE(kind, PyUnicode_GET_LENGTH(res))); | 
 | 9290 |     else | 
 | 9291 |         assert(res_offset == PyUnicode_GET_LENGTH(res)); | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9292 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9293 |     Py_DECREF(fseq); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9294 |     Py_XDECREF(sep); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9295 |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9296 |     return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9297 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9298 |   onError: | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9299 |     Py_DECREF(fseq); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9300 |     Py_XDECREF(sep); | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9301 |     Py_XDECREF(res); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9302 |     return NULL; | 
 | 9303 | } | 
 | 9304 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9305 | #define FILL(kind, data, value, start, length) \ | 
 | 9306 |     do { \ | 
 | 9307 |         Py_ssize_t i_ = 0; \ | 
 | 9308 |         assert(kind != PyUnicode_WCHAR_KIND); \ | 
 | 9309 |         switch ((kind)) { \ | 
 | 9310 |         case PyUnicode_1BYTE_KIND: { \ | 
 | 9311 |             unsigned char * to_ = (unsigned char *)((data)) + (start); \ | 
 | 9312 |             memset(to_, (unsigned char)value, length); \ | 
 | 9313 |             break; \ | 
 | 9314 |         } \ | 
 | 9315 |         case PyUnicode_2BYTE_KIND: { \ | 
 | 9316 |             Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ | 
 | 9317 |             for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ | 
 | 9318 |             break; \ | 
 | 9319 |         } \ | 
 | 9320 |         default: { \ | 
 | 9321 |             Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ | 
 | 9322 |             for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ | 
 | 9323 |             break; \ | 
 | 9324 |         } \ | 
 | 9325 |         } \ | 
 | 9326 |     } while (0) | 
 | 9327 |  | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9328 | static PyObject * | 
 | 9329 | pad(PyObject *self, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9330 |     Py_ssize_t left, | 
 | 9331 |     Py_ssize_t right, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9332 |     Py_UCS4 fill) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9333 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9334 |     PyObject *u; | 
 | 9335 |     Py_UCS4 maxchar; | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9336 |     int kind; | 
 | 9337 |     void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9338 |  | 
 | 9339 |     if (left < 0) | 
 | 9340 |         left = 0; | 
 | 9341 |     if (right < 0) | 
 | 9342 |         right = 0; | 
 | 9343 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9344 |     if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9345 |         Py_INCREF(self); | 
 | 9346 |         return self; | 
 | 9347 |     } | 
 | 9348 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9349 |     if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || | 
 | 9350 |         right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9351 |         PyErr_SetString(PyExc_OverflowError, "padded string is too long"); | 
 | 9352 |         return NULL; | 
 | 9353 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9354 |     maxchar = PyUnicode_MAX_CHAR_VALUE(self); | 
 | 9355 |     if (fill > maxchar) | 
 | 9356 |         maxchar = fill; | 
 | 9357 |     u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9358 |     if (!u) | 
 | 9359 |         return NULL; | 
 | 9360 |  | 
 | 9361 |     kind = PyUnicode_KIND(u); | 
 | 9362 |     data = PyUnicode_DATA(u); | 
 | 9363 |     if (left) | 
 | 9364 |         FILL(kind, data, fill, 0, left); | 
 | 9365 |     if (right) | 
 | 9366 |         FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9367 |     copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9368 |     assert(_PyUnicode_CheckConsistency(u, 1)); | 
 | 9369 |     return u; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9370 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9371 | #undef FILL | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9372 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9373 | PyObject * | 
 | 9374 | PyUnicode_Splitlines(PyObject *string, int keepends) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9375 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9376 |     PyObject *list; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9377 |  | 
 | 9378 |     string = PyUnicode_FromObject(string); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9379 |     if (string == NULL || PyUnicode_READY(string) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9380 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9381 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9382 |     switch(PyUnicode_KIND(string)) { | 
 | 9383 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9384 |         if (PyUnicode_IS_ASCII(string)) | 
 | 9385 |             list = asciilib_splitlines( | 
 | 9386 |                 (PyObject*) string, PyUnicode_1BYTE_DATA(string), | 
 | 9387 |                 PyUnicode_GET_LENGTH(string), keepends); | 
 | 9388 |         else | 
 | 9389 |             list = ucs1lib_splitlines( | 
 | 9390 |                 (PyObject*) string, PyUnicode_1BYTE_DATA(string), | 
 | 9391 |                 PyUnicode_GET_LENGTH(string), keepends); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9392 |         break; | 
 | 9393 |     case PyUnicode_2BYTE_KIND: | 
 | 9394 |         list = ucs2lib_splitlines( | 
 | 9395 |             (PyObject*) string, PyUnicode_2BYTE_DATA(string), | 
 | 9396 |             PyUnicode_GET_LENGTH(string), keepends); | 
 | 9397 |         break; | 
 | 9398 |     case PyUnicode_4BYTE_KIND: | 
 | 9399 |         list = ucs4lib_splitlines( | 
 | 9400 |             (PyObject*) string, PyUnicode_4BYTE_DATA(string), | 
 | 9401 |             PyUnicode_GET_LENGTH(string), keepends); | 
 | 9402 |         break; | 
 | 9403 |     default: | 
 | 9404 |         assert(0); | 
 | 9405 |         list = 0; | 
 | 9406 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9407 |     Py_DECREF(string); | 
 | 9408 |     return list; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9409 | } | 
 | 9410 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9411 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9412 | split(PyObject *self, | 
 | 9413 |       PyObject *substring, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9414 |       Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9415 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9416 |     int kind1, kind2, kind; | 
 | 9417 |     void *buf1, *buf2; | 
 | 9418 |     Py_ssize_t len1, len2; | 
 | 9419 |     PyObject* out; | 
 | 9420 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9421 |     if (maxcount < 0) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9422 |         maxcount = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9423 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9424 |     if (PyUnicode_READY(self) == -1) | 
 | 9425 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9426 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9427 |     if (substring == NULL) | 
 | 9428 |         switch(PyUnicode_KIND(self)) { | 
 | 9429 |         case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9430 |             if (PyUnicode_IS_ASCII(self)) | 
 | 9431 |                 return asciilib_split_whitespace( | 
 | 9432 |                     (PyObject*) self,  PyUnicode_1BYTE_DATA(self), | 
 | 9433 |                     PyUnicode_GET_LENGTH(self), maxcount | 
 | 9434 |                     ); | 
 | 9435 |             else | 
 | 9436 |                 return ucs1lib_split_whitespace( | 
 | 9437 |                     (PyObject*) self,  PyUnicode_1BYTE_DATA(self), | 
 | 9438 |                     PyUnicode_GET_LENGTH(self), maxcount | 
 | 9439 |                     ); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9440 |         case PyUnicode_2BYTE_KIND: | 
 | 9441 |             return ucs2lib_split_whitespace( | 
 | 9442 |                 (PyObject*) self,  PyUnicode_2BYTE_DATA(self), | 
 | 9443 |                 PyUnicode_GET_LENGTH(self), maxcount | 
 | 9444 |                 ); | 
 | 9445 |         case PyUnicode_4BYTE_KIND: | 
 | 9446 |             return ucs4lib_split_whitespace( | 
 | 9447 |                 (PyObject*) self,  PyUnicode_4BYTE_DATA(self), | 
 | 9448 |                 PyUnicode_GET_LENGTH(self), maxcount | 
 | 9449 |                 ); | 
 | 9450 |         default: | 
 | 9451 |             assert(0); | 
 | 9452 |             return NULL; | 
 | 9453 |         } | 
 | 9454 |  | 
 | 9455 |     if (PyUnicode_READY(substring) == -1) | 
 | 9456 |         return NULL; | 
 | 9457 |  | 
 | 9458 |     kind1 = PyUnicode_KIND(self); | 
 | 9459 |     kind2 = PyUnicode_KIND(substring); | 
 | 9460 |     kind = kind1 > kind2 ? kind1 : kind2; | 
 | 9461 |     buf1 = PyUnicode_DATA(self); | 
 | 9462 |     buf2 = PyUnicode_DATA(substring); | 
 | 9463 |     if (kind1 != kind) | 
 | 9464 |         buf1 = _PyUnicode_AsKind((PyObject*)self, kind); | 
 | 9465 |     if (!buf1) | 
 | 9466 |         return NULL; | 
 | 9467 |     if (kind2 != kind) | 
 | 9468 |         buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); | 
 | 9469 |     if (!buf2) { | 
 | 9470 |         if (kind1 != kind) PyMem_Free(buf1); | 
 | 9471 |         return NULL; | 
 | 9472 |     } | 
 | 9473 |     len1 = PyUnicode_GET_LENGTH(self); | 
 | 9474 |     len2 = PyUnicode_GET_LENGTH(substring); | 
 | 9475 |  | 
 | 9476 |     switch(kind) { | 
 | 9477 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9478 |         if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) | 
 | 9479 |             out = asciilib_split( | 
 | 9480 |                 (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
 | 9481 |         else | 
 | 9482 |             out = ucs1lib_split( | 
 | 9483 |                 (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9484 |         break; | 
 | 9485 |     case PyUnicode_2BYTE_KIND: | 
 | 9486 |         out = ucs2lib_split( | 
 | 9487 |             (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
 | 9488 |         break; | 
 | 9489 |     case PyUnicode_4BYTE_KIND: | 
 | 9490 |         out = ucs4lib_split( | 
 | 9491 |             (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
 | 9492 |         break; | 
 | 9493 |     default: | 
 | 9494 |         out = NULL; | 
 | 9495 |     } | 
 | 9496 |     if (kind1 != kind) | 
 | 9497 |         PyMem_Free(buf1); | 
 | 9498 |     if (kind2 != kind) | 
 | 9499 |         PyMem_Free(buf2); | 
 | 9500 |     return out; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9501 | } | 
 | 9502 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9503 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9504 | rsplit(PyObject *self, | 
 | 9505 |        PyObject *substring, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9506 |        Py_ssize_t maxcount) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9507 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9508 |     int kind1, kind2, kind; | 
 | 9509 |     void *buf1, *buf2; | 
 | 9510 |     Py_ssize_t len1, len2; | 
 | 9511 |     PyObject* out; | 
 | 9512 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9513 |     if (maxcount < 0) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9514 |         maxcount = PY_SSIZE_T_MAX; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9515 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9516 |     if (PyUnicode_READY(self) == -1) | 
 | 9517 |         return NULL; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9518 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9519 |     if (substring == NULL) | 
 | 9520 |         switch(PyUnicode_KIND(self)) { | 
 | 9521 |         case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9522 |             if (PyUnicode_IS_ASCII(self)) | 
 | 9523 |                 return asciilib_rsplit_whitespace( | 
 | 9524 |                     (PyObject*) self,  PyUnicode_1BYTE_DATA(self), | 
 | 9525 |                     PyUnicode_GET_LENGTH(self), maxcount | 
 | 9526 |                     ); | 
 | 9527 |             else | 
 | 9528 |                 return ucs1lib_rsplit_whitespace( | 
 | 9529 |                     (PyObject*) self,  PyUnicode_1BYTE_DATA(self), | 
 | 9530 |                     PyUnicode_GET_LENGTH(self), maxcount | 
 | 9531 |                     ); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9532 |         case PyUnicode_2BYTE_KIND: | 
 | 9533 |             return ucs2lib_rsplit_whitespace( | 
 | 9534 |                 (PyObject*) self,  PyUnicode_2BYTE_DATA(self), | 
 | 9535 |                 PyUnicode_GET_LENGTH(self), maxcount | 
 | 9536 |                 ); | 
 | 9537 |         case PyUnicode_4BYTE_KIND: | 
 | 9538 |             return ucs4lib_rsplit_whitespace( | 
 | 9539 |                 (PyObject*) self,  PyUnicode_4BYTE_DATA(self), | 
 | 9540 |                 PyUnicode_GET_LENGTH(self), maxcount | 
 | 9541 |                 ); | 
 | 9542 |         default: | 
 | 9543 |             assert(0); | 
 | 9544 |             return NULL; | 
 | 9545 |         } | 
 | 9546 |  | 
 | 9547 |     if (PyUnicode_READY(substring) == -1) | 
 | 9548 |         return NULL; | 
 | 9549 |  | 
 | 9550 |     kind1 = PyUnicode_KIND(self); | 
 | 9551 |     kind2 = PyUnicode_KIND(substring); | 
 | 9552 |     kind = kind1 > kind2 ? kind1 : kind2; | 
 | 9553 |     buf1 = PyUnicode_DATA(self); | 
 | 9554 |     buf2 = PyUnicode_DATA(substring); | 
 | 9555 |     if (kind1 != kind) | 
 | 9556 |         buf1 = _PyUnicode_AsKind((PyObject*)self, kind); | 
 | 9557 |     if (!buf1) | 
 | 9558 |         return NULL; | 
 | 9559 |     if (kind2 != kind) | 
 | 9560 |         buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); | 
 | 9561 |     if (!buf2) { | 
 | 9562 |         if (kind1 != kind) PyMem_Free(buf1); | 
 | 9563 |         return NULL; | 
 | 9564 |     } | 
 | 9565 |     len1 = PyUnicode_GET_LENGTH(self); | 
 | 9566 |     len2 = PyUnicode_GET_LENGTH(substring); | 
 | 9567 |  | 
 | 9568 |     switch(kind) { | 
 | 9569 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9570 |         if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) | 
 | 9571 |             out = asciilib_rsplit( | 
 | 9572 |                 (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
 | 9573 |         else | 
 | 9574 |             out = ucs1lib_rsplit( | 
 | 9575 |                 (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9576 |         break; | 
 | 9577 |     case PyUnicode_2BYTE_KIND: | 
 | 9578 |         out = ucs2lib_rsplit( | 
 | 9579 |             (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
 | 9580 |         break; | 
 | 9581 |     case PyUnicode_4BYTE_KIND: | 
 | 9582 |         out = ucs4lib_rsplit( | 
 | 9583 |             (PyObject*) self,  buf1, len1, buf2, len2, maxcount); | 
 | 9584 |         break; | 
 | 9585 |     default: | 
 | 9586 |         out = NULL; | 
 | 9587 |     } | 
 | 9588 |     if (kind1 != kind) | 
 | 9589 |         PyMem_Free(buf1); | 
 | 9590 |     if (kind2 != kind) | 
 | 9591 |         PyMem_Free(buf2); | 
 | 9592 |     return out; | 
 | 9593 | } | 
 | 9594 |  | 
 | 9595 | static Py_ssize_t | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9596 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, | 
 | 9597 |             PyObject *str2, void *buf2, Py_ssize_t len2, Py_ssize_t offset) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9598 | { | 
 | 9599 |     switch(kind) { | 
 | 9600 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9601 |         if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) | 
 | 9602 |             return asciilib_find(buf1, len1, buf2, len2, offset); | 
 | 9603 |         else | 
 | 9604 |             return ucs1lib_find(buf1, len1, buf2, len2, offset); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9605 |     case PyUnicode_2BYTE_KIND: | 
 | 9606 |         return ucs2lib_find(buf1, len1, buf2, len2, offset); | 
 | 9607 |     case PyUnicode_4BYTE_KIND: | 
 | 9608 |         return ucs4lib_find(buf1, len1, buf2, len2, offset); | 
 | 9609 |     } | 
 | 9610 |     assert(0); | 
 | 9611 |     return -1; | 
 | 9612 | } | 
 | 9613 |  | 
 | 9614 | static Py_ssize_t | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9615 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, | 
 | 9616 |              PyObject *str1, void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9617 | { | 
 | 9618 |         switch(kind) { | 
 | 9619 |         case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9620 |             if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) | 
 | 9621 |                 return asciilib_count(sbuf, slen, buf1, len1, maxcount); | 
 | 9622 |             else | 
 | 9623 |                 return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9624 |         case PyUnicode_2BYTE_KIND: | 
 | 9625 |             return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); | 
 | 9626 |         case PyUnicode_4BYTE_KIND: | 
 | 9627 |             return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); | 
 | 9628 |         } | 
 | 9629 |         assert(0); | 
 | 9630 |         return 0; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9631 | } | 
 | 9632 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9633 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9634 | replace(PyObject *self, PyObject *str1, | 
 | 9635 |         PyObject *str2, Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9636 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9637 |     PyObject *u; | 
 | 9638 |     char *sbuf = PyUnicode_DATA(self); | 
 | 9639 |     char *buf1 = PyUnicode_DATA(str1); | 
 | 9640 |     char *buf2 = PyUnicode_DATA(str2); | 
 | 9641 |     int srelease = 0, release1 = 0, release2 = 0; | 
 | 9642 |     int skind = PyUnicode_KIND(self); | 
 | 9643 |     int kind1 = PyUnicode_KIND(str1); | 
 | 9644 |     int kind2 = PyUnicode_KIND(str2); | 
 | 9645 |     Py_ssize_t slen = PyUnicode_GET_LENGTH(self); | 
 | 9646 |     Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); | 
 | 9647 |     Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9648 |  | 
 | 9649 |     if (maxcount < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9650 |         maxcount = PY_SSIZE_T_MAX; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9651 |     else if (maxcount == 0 || slen == 0) | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9652 |         goto nothing; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9653 |  | 
| Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9654 |     if (str1 == str2) | 
 | 9655 |         goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9656 |     if (skind < kind1) | 
 | 9657 |         /* substring too wide to be present */ | 
 | 9658 |         goto nothing; | 
 | 9659 |  | 
 | 9660 |     if (len1 == len2) { | 
| Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9661 |         Py_ssize_t i; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9662 |         /* same length */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9663 |         if (len1 == 0) | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9664 |             goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9665 |         if (len1 == 1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9666 |             /* replace characters */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9667 |             Py_UCS4 u1, u2, maxchar; | 
 | 9668 |             int mayshrink, rkind; | 
 | 9669 |             u1 = PyUnicode_READ_CHAR(str1, 0); | 
 | 9670 |             if (!findchar(sbuf, PyUnicode_KIND(self), | 
 | 9671 |                           slen, u1, 1)) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9672 |                 goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9673 |             u2 = PyUnicode_READ_CHAR(str2, 0); | 
 | 9674 |             maxchar = PyUnicode_MAX_CHAR_VALUE(self); | 
 | 9675 |             /* Replacing u1 with u2 may cause a maxchar reduction in the | 
 | 9676 |                result string. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9677 |             if (u2 > maxchar) { | 
 | 9678 |                 maxchar = u2; | 
 | 9679 |                 mayshrink = 0; | 
 | 9680 |             } | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 9681 |             else | 
 | 9682 |                 mayshrink = maxchar > 127; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9683 |             u = PyUnicode_New(slen, maxchar); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9684 |             if (!u) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9685 |                 goto error; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9686 |             copy_characters(u, 0, self, 0, slen); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9687 |             rkind = PyUnicode_KIND(u); | 
 | 9688 |             for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) | 
 | 9689 |                 if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9690 |                     if (--maxcount < 0) | 
 | 9691 |                         break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9692 |                     PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9693 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9694 |             if (mayshrink) { | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9695 |                 unicode_adjust_maxchar(&u); | 
 | 9696 |                 if (u == NULL) | 
 | 9697 |                     goto error; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9698 |             } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9699 |         } else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9700 |             int rkind = skind; | 
 | 9701 |             char *res; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9702 |             PyObject *rstr; | 
 | 9703 |             Py_UCS4 maxchar; | 
 | 9704 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9705 |             if (kind1 < rkind) { | 
 | 9706 |                 /* widen substring */ | 
 | 9707 |                 buf1 = _PyUnicode_AsKind(str1, rkind); | 
 | 9708 |                 if (!buf1) goto error; | 
 | 9709 |                 release1 = 1; | 
 | 9710 |             } | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9711 |             i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9712 |             if (i < 0) | 
 | 9713 |                 goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9714 |             if (rkind > kind2) { | 
 | 9715 |                 /* widen replacement */ | 
 | 9716 |                 buf2 = _PyUnicode_AsKind(str2, rkind); | 
 | 9717 |                 if (!buf2) goto error; | 
 | 9718 |                 release2 = 1; | 
 | 9719 |             } | 
 | 9720 |             else if (rkind < kind2) { | 
 | 9721 |                 /* widen self and buf1 */ | 
 | 9722 |                 rkind = kind2; | 
 | 9723 |                 if (release1) PyMem_Free(buf1); | 
 | 9724 |                 sbuf = _PyUnicode_AsKind(self, rkind); | 
 | 9725 |                 if (!sbuf) goto error; | 
 | 9726 |                 srelease = 1; | 
 | 9727 |                 buf1 = _PyUnicode_AsKind(str1, rkind); | 
 | 9728 |                 if (!buf1) goto error; | 
 | 9729 |                 release1 = 1; | 
 | 9730 |             } | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9731 |             maxchar = PyUnicode_MAX_CHAR_VALUE(self); | 
 | 9732 |             maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); | 
 | 9733 |             rstr = PyUnicode_New(slen, maxchar); | 
 | 9734 |             if (!rstr) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9735 |                 goto error; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9736 |             res = PyUnicode_DATA(rstr); | 
 | 9737 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9738 |             memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9739 |             /* change everything in-place, starting with this one */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9740 |             memcpy(res + PyUnicode_KIND_SIZE(rkind, i), | 
 | 9741 |                    buf2, | 
 | 9742 |                    PyUnicode_KIND_SIZE(rkind, len2)); | 
 | 9743 |             i += len1; | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9744 |  | 
 | 9745 |             while ( --maxcount > 0) { | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9746 |                 i = anylib_find(rkind, self, | 
 | 9747 |                                 sbuf+PyUnicode_KIND_SIZE(rkind, i), slen-i, | 
 | 9748 |                                 str1, buf1, len1, i); | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9749 |                 if (i == -1) | 
 | 9750 |                     break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9751 |                 memcpy(res + PyUnicode_KIND_SIZE(rkind, i), | 
 | 9752 |                        buf2, | 
 | 9753 |                        PyUnicode_KIND_SIZE(rkind, len2)); | 
 | 9754 |                 i += len1; | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9755 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 |  | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9757 |             u = rstr; | 
 | 9758 |             unicode_adjust_maxchar(&u); | 
 | 9759 |             if (!u) | 
 | 9760 |                 goto error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9761 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9762 |     } else { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9763 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9764 |         Py_ssize_t n, i, j, ires; | 
 | 9765 |         Py_ssize_t product, new_size; | 
 | 9766 |         int rkind = skind; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9767 |         PyObject *rstr; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9768 |         char *res; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9769 |         Py_UCS4 maxchar; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9770 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9771 |         if (kind1 < rkind) { | 
 | 9772 |             buf1 = _PyUnicode_AsKind(str1, rkind); | 
 | 9773 |             if (!buf1) goto error; | 
 | 9774 |             release1 = 1; | 
 | 9775 |         } | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9776 |         n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9777 |         if (n == 0) | 
 | 9778 |             goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9779 |         if (kind2 < rkind) { | 
 | 9780 |             buf2 = _PyUnicode_AsKind(str2, rkind); | 
 | 9781 |             if (!buf2) goto error; | 
 | 9782 |             release2 = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9783 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9784 |         else if (kind2 > rkind) { | 
 | 9785 |             rkind = kind2; | 
 | 9786 |             sbuf = _PyUnicode_AsKind(self, rkind); | 
 | 9787 |             if (!sbuf) goto error; | 
 | 9788 |             srelease = 1; | 
 | 9789 |             if (release1) PyMem_Free(buf1); | 
 | 9790 |             buf1 = _PyUnicode_AsKind(str1, rkind); | 
 | 9791 |             if (!buf1) goto error; | 
 | 9792 |             release1 = 1; | 
 | 9793 |         } | 
 | 9794 |         /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - | 
 | 9795 |            PyUnicode_GET_LENGTH(str1))); */ | 
 | 9796 |         product = n * (len2-len1); | 
 | 9797 |         if ((product / (len2-len1)) != n) { | 
 | 9798 |                 PyErr_SetString(PyExc_OverflowError, | 
 | 9799 |                                 "replace string is too long"); | 
 | 9800 |                 goto error; | 
 | 9801 |         } | 
 | 9802 |         new_size = slen + product; | 
 | 9803 |         if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { | 
 | 9804 |             PyErr_SetString(PyExc_OverflowError, | 
 | 9805 |                             "replace string is too long"); | 
 | 9806 |             goto error; | 
 | 9807 |         } | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9808 |         maxchar = PyUnicode_MAX_CHAR_VALUE(self); | 
 | 9809 |         maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); | 
 | 9810 |         rstr = PyUnicode_New(new_size, maxchar); | 
 | 9811 |         if (!rstr) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9812 |             goto error; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9813 |         res = PyUnicode_DATA(rstr); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9814 |         ires = i = 0; | 
 | 9815 |         if (len1 > 0) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9816 |             while (n-- > 0) { | 
 | 9817 |                 /* look for next match */ | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9818 |                 j = anylib_find(rkind, self, | 
 | 9819 |                                 sbuf + PyUnicode_KIND_SIZE(rkind, i), slen-i, | 
 | 9820 |                                 str1, buf1, len1, i); | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9821 |                 if (j == -1) | 
 | 9822 |                     break; | 
 | 9823 |                 else if (j > i) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9824 |                     /* copy unchanged part [i:j] */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9825 |                     memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
 | 9826 |                            sbuf + PyUnicode_KIND_SIZE(rkind, i), | 
 | 9827 |                            PyUnicode_KIND_SIZE(rkind, j-i)); | 
 | 9828 |                     ires += j - i; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9829 |                 } | 
 | 9830 |                 /* copy substitution string */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9831 |                 if (len2 > 0) { | 
 | 9832 |                     memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
 | 9833 |                            buf2, | 
 | 9834 |                            PyUnicode_KIND_SIZE(rkind, len2)); | 
 | 9835 |                     ires += len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9836 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9837 |                 i = j + len1; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9838 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9839 |             if (i < slen) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9840 |                 /* copy tail [i:] */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9841 |                 memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
 | 9842 |                        sbuf + PyUnicode_KIND_SIZE(rkind, i), | 
 | 9843 |                        PyUnicode_KIND_SIZE(rkind, slen-i)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9844 |         } else { | 
 | 9845 |             /* interleave */ | 
 | 9846 |             while (n > 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9847 |                 memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
 | 9848 |                        buf2, | 
 | 9849 |                        PyUnicode_KIND_SIZE(rkind, len2)); | 
 | 9850 |                 ires += len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9851 |                 if (--n <= 0) | 
 | 9852 |                     break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9853 |                 memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
 | 9854 |                        sbuf + PyUnicode_KIND_SIZE(rkind, i), | 
 | 9855 |                        PyUnicode_KIND_SIZE(rkind, 1)); | 
 | 9856 |                 ires++; | 
 | 9857 |                 i++; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9858 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9859 |             memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), | 
 | 9860 |                    sbuf + PyUnicode_KIND_SIZE(rkind, i), | 
 | 9861 |                    PyUnicode_KIND_SIZE(rkind, slen-i)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9862 |         } | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9863 |         u = rstr; | 
 | 9864 |         unicode_adjust_maxchar(&u); | 
 | 9865 |         if (u == NULL) | 
 | 9866 |             goto error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9867 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9868 |     if (srelease) | 
 | 9869 |         PyMem_FREE(sbuf); | 
 | 9870 |     if (release1) | 
 | 9871 |         PyMem_FREE(buf1); | 
 | 9872 |     if (release2) | 
 | 9873 |         PyMem_FREE(buf2); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9874 |     assert(_PyUnicode_CheckConsistency(u, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9875 |     return u; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9876 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9877 |   nothing: | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9878 |     /* nothing to replace; return original string (when possible) */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9879 |     if (srelease) | 
 | 9880 |         PyMem_FREE(sbuf); | 
 | 9881 |     if (release1) | 
 | 9882 |         PyMem_FREE(buf1); | 
 | 9883 |     if (release2) | 
 | 9884 |         PyMem_FREE(buf2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9885 |     if (PyUnicode_CheckExact(self)) { | 
 | 9886 |         Py_INCREF(self); | 
 | 9887 |         return (PyObject *) self; | 
 | 9888 |     } | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9889 |     return PyUnicode_Copy(self); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9890 |   error: | 
 | 9891 |     if (srelease && sbuf) | 
 | 9892 |         PyMem_FREE(sbuf); | 
 | 9893 |     if (release1 && buf1) | 
 | 9894 |         PyMem_FREE(buf1); | 
 | 9895 |     if (release2 && buf2) | 
 | 9896 |         PyMem_FREE(buf2); | 
 | 9897 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9898 | } | 
 | 9899 |  | 
 | 9900 | /* --- Unicode Object Methods --------------------------------------------- */ | 
 | 9901 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9902 | PyDoc_STRVAR(title__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9903 |              "S.title() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9904 | \n\ | 
 | 9905 | 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] | 9906 | characters, all remaining cased characters have lower case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9907 |  | 
 | 9908 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9909 | unicode_title(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9910 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9911 |     return fixup(self, fixtitle); | 
 | 9912 | } | 
 | 9913 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9914 | PyDoc_STRVAR(capitalize__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9915 |              "S.capitalize() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9916 | \n\ | 
 | 9917 | Return a capitalized version of S, i.e. make the first character\n\ | 
| Senthil Kumaran | e51ee8a | 2010-07-05 12:00:56 +0000 | [diff] [blame] | 9918 | have upper case and the rest lower case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9919 |  | 
 | 9920 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9921 | unicode_capitalize(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9922 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9923 |     return fixup(self, fixcapitalize); | 
 | 9924 | } | 
 | 9925 |  | 
 | 9926 | #if 0 | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9927 | PyDoc_STRVAR(capwords__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9928 |              "S.capwords() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9929 | \n\ | 
 | 9930 | 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] | 9931 | normalized whitespace (all whitespace strings are replaced by ' ')."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9932 |  | 
 | 9933 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9934 | unicode_capwords(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9935 | { | 
 | 9936 |     PyObject *list; | 
 | 9937 |     PyObject *item; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9938 |     Py_ssize_t i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9939 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 |     /* Split into words */ | 
 | 9941 |     list = split(self, NULL, -1); | 
 | 9942 |     if (!list) | 
 | 9943 |         return NULL; | 
 | 9944 |  | 
 | 9945 |     /* Capitalize each word */ | 
 | 9946 |     for (i = 0; i < PyList_GET_SIZE(list); i++) { | 
 | 9947 |         item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9948 |                      fixcapitalize); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9949 |         if (item == NULL) | 
 | 9950 |             goto onError; | 
 | 9951 |         Py_DECREF(PyList_GET_ITEM(list, i)); | 
 | 9952 |         PyList_SET_ITEM(list, i, item); | 
 | 9953 |     } | 
 | 9954 |  | 
 | 9955 |     /* Join the words to form a new string */ | 
 | 9956 |     item = PyUnicode_Join(NULL, list); | 
 | 9957 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9958 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9959 |     Py_DECREF(list); | 
 | 9960 |     return (PyObject *)item; | 
 | 9961 | } | 
 | 9962 | #endif | 
 | 9963 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9964 | /* Argument converter.  Coerces to a single unicode character */ | 
 | 9965 |  | 
 | 9966 | static int | 
 | 9967 | convert_uc(PyObject *obj, void *addr) | 
 | 9968 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9969 |     Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9970 |     PyObject *uniobj; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9971 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9972 |     uniobj = PyUnicode_FromObject(obj); | 
 | 9973 |     if (uniobj == NULL) { | 
 | 9974 |         PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9975 |                         "The fill character cannot be converted to Unicode"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9976 |         return 0; | 
 | 9977 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9978 |     if (PyUnicode_GET_LENGTH(uniobj) != 1) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9979 |         PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9980 |                         "The fill character must be exactly one character long"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9981 |         Py_DECREF(uniobj); | 
 | 9982 |         return 0; | 
 | 9983 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9984 |     *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9985 |     Py_DECREF(uniobj); | 
 | 9986 |     return 1; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9987 | } | 
 | 9988 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9989 | PyDoc_STRVAR(center__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9990 |              "S.center(width[, fillchar]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9991 | \n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9992 | Return S centered in a string of length width. Padding is\n\ | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9993 | done using the specified fill character (default is a space)"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9994 |  | 
 | 9995 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9996 | unicode_center(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9997 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9998 |     Py_ssize_t marg, left; | 
 | 9999 |     Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10000 |     Py_UCS4 fillchar = ' '; | 
 | 10001 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10002 |     if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10003 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10004 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10005 |     if (PyUnicode_READY(self) == -1) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10006 |         return NULL; | 
 | 10007 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10008 |     if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10009 |         Py_INCREF(self); | 
 | 10010 |         return (PyObject*) self; | 
 | 10011 |     } | 
 | 10012 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10013 |     marg = width - _PyUnicode_LENGTH(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10014 |     left = marg / 2 + (marg & width & 1); | 
 | 10015 |  | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10016 |     return pad(self, left, marg - left, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10017 | } | 
 | 10018 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10019 | #if 0 | 
 | 10020 |  | 
 | 10021 | /* This code should go into some future Unicode collation support | 
 | 10022 |    module. The basic comparison should compare ordinals on a naive | 
| Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 10023 |    basis (this is what Java does and thus Jython too). */ | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10024 |  | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10025 | /* speedy UTF-16 code point order comparison */ | 
 | 10026 | /* gleaned from: */ | 
 | 10027 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ | 
 | 10028 |  | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 10029 | static short utf16Fixup[32] = | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10030 | { | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10031 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10032 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 10033 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 10034 |     0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10035 | }; | 
 | 10036 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10037 | static int | 
 | 10038 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) | 
 | 10039 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10040 |     Py_ssize_t len1, len2; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10041 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10042 |     Py_UNICODE *s1 = str1->str; | 
 | 10043 |     Py_UNICODE *s2 = str2->str; | 
 | 10044 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10045 |     len1 = str1->_base._base.length; | 
 | 10046 |     len2 = str2->_base._base.length; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10047 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10048 |     while (len1 > 0 && len2 > 0) { | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10049 |         Py_UNICODE c1, c2; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10050 |  | 
 | 10051 |         c1 = *s1++; | 
 | 10052 |         c2 = *s2++; | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10053 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10054 |         if (c1 > (1<<11) * 26) | 
 | 10055 |             c1 += utf16Fixup[c1>>11]; | 
 | 10056 |         if (c2 > (1<<11) * 26) | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10057 |             c2 += utf16Fixup[c2>>11]; | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10058 |         /* now c1 and c2 are in UTF-32-compatible order */ | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10059 |  | 
 | 10060 |         if (c1 != c2) | 
 | 10061 |             return (c1 < c2) ? -1 : 1; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10062 |  | 
| Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 10063 |         len1--; len2--; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10064 |     } | 
 | 10065 |  | 
 | 10066 |     return (len1 < len2) ? -1 : (len1 != len2); | 
 | 10067 | } | 
 | 10068 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10069 | #else | 
 | 10070 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | /* This function assumes that str1 and str2 are readied by the caller. */ | 
 | 10072 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10073 | static int | 
 | 10074 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) | 
 | 10075 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10076 |     int kind1, kind2; | 
 | 10077 |     void *data1, *data2; | 
 | 10078 |     Py_ssize_t len1, len2, i; | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10079 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 |     kind1 = PyUnicode_KIND(str1); | 
 | 10081 |     kind2 = PyUnicode_KIND(str2); | 
 | 10082 |     data1 = PyUnicode_DATA(str1); | 
 | 10083 |     data2 = PyUnicode_DATA(str2); | 
 | 10084 |     len1 = PyUnicode_GET_LENGTH(str1); | 
 | 10085 |     len2 = PyUnicode_GET_LENGTH(str2); | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10086 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10087 |     for (i = 0; i < len1 && i < len2; ++i) { | 
 | 10088 |         Py_UCS4 c1, c2; | 
 | 10089 |         c1 = PyUnicode_READ(kind1, data1, i); | 
 | 10090 |         c2 = PyUnicode_READ(kind2, data2, i); | 
| Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10091 |  | 
 | 10092 |         if (c1 != c2) | 
 | 10093 |             return (c1 < c2) ? -1 : 1; | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10094 |     } | 
 | 10095 |  | 
 | 10096 |     return (len1 < len2) ? -1 : (len1 != len2); | 
 | 10097 | } | 
 | 10098 |  | 
 | 10099 | #endif | 
 | 10100 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10101 | int | 
 | 10102 | PyUnicode_Compare(PyObject *left, PyObject *right) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10103 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10104 |     if (PyUnicode_Check(left) && PyUnicode_Check(right)) { | 
 | 10105 |         if (PyUnicode_READY(left) == -1 || | 
 | 10106 |             PyUnicode_READY(right) == -1) | 
 | 10107 |             return -1; | 
| Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10108 |         return unicode_compare((PyUnicodeObject *)left, | 
 | 10109 |                                (PyUnicodeObject *)right); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10110 |     } | 
| Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10111 |     PyErr_Format(PyExc_TypeError, | 
 | 10112 |                  "Can't compare %.100s and %.100s", | 
 | 10113 |                  left->ob_type->tp_name, | 
 | 10114 |                  right->ob_type->tp_name); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10115 |     return -1; | 
 | 10116 | } | 
 | 10117 |  | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10118 | int | 
 | 10119 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) | 
 | 10120 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10121 |     Py_ssize_t i; | 
 | 10122 |     int kind; | 
 | 10123 |     void *data; | 
 | 10124 |     Py_UCS4 chr; | 
 | 10125 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10126 |     assert(_PyUnicode_CHECK(uni)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10127 |     if (PyUnicode_READY(uni) == -1) | 
 | 10128 |         return -1; | 
 | 10129 |     kind = PyUnicode_KIND(uni); | 
 | 10130 |     data = PyUnicode_DATA(uni); | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10131 |     /* Compare Unicode string and source character set string */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10132 |     for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) | 
 | 10133 |         if (chr != str[i]) | 
 | 10134 |             return (chr < (unsigned char)(str[i])) ? -1 : 1; | 
| Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10135 |     /* This check keeps Python strings that end in '\0' from comparing equal | 
 | 10136 |      to C strings identical up to that point. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10137 |     if (PyUnicode_GET_LENGTH(uni) != i || chr) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10138 |         return 1; /* uni is longer */ | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10139 |     if (str[i]) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10140 |         return -1; /* str is longer */ | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10141 |     return 0; | 
 | 10142 | } | 
 | 10143 |  | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10144 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10145 | #define TEST_COND(cond)                         \ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10146 |     ((cond) ? Py_True : Py_False) | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10147 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10148 | PyObject * | 
 | 10149 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10150 | { | 
 | 10151 |     int result; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10152 |  | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10153 |     if (PyUnicode_Check(left) && PyUnicode_Check(right)) { | 
 | 10154 |         PyObject *v; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10155 |         if (PyUnicode_READY(left) == -1 || | 
 | 10156 |             PyUnicode_READY(right) == -1) | 
 | 10157 |             return NULL; | 
 | 10158 |         if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || | 
 | 10159 |             PyUnicode_KIND(left) != PyUnicode_KIND(right)) { | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10160 |             if (op == Py_EQ) { | 
 | 10161 |                 Py_INCREF(Py_False); | 
 | 10162 |                 return Py_False; | 
 | 10163 |             } | 
 | 10164 |             if (op == Py_NE) { | 
 | 10165 |                 Py_INCREF(Py_True); | 
 | 10166 |                 return Py_True; | 
 | 10167 |             } | 
 | 10168 |         } | 
 | 10169 |         if (left == right) | 
 | 10170 |             result = 0; | 
 | 10171 |         else | 
 | 10172 |             result = unicode_compare((PyUnicodeObject *)left, | 
 | 10173 |                                      (PyUnicodeObject *)right); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10174 |  | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10175 |         /* Convert the return value to a Boolean */ | 
 | 10176 |         switch (op) { | 
 | 10177 |         case Py_EQ: | 
 | 10178 |             v = TEST_COND(result == 0); | 
 | 10179 |             break; | 
 | 10180 |         case Py_NE: | 
 | 10181 |             v = TEST_COND(result != 0); | 
 | 10182 |             break; | 
 | 10183 |         case Py_LE: | 
 | 10184 |             v = TEST_COND(result <= 0); | 
 | 10185 |             break; | 
 | 10186 |         case Py_GE: | 
 | 10187 |             v = TEST_COND(result >= 0); | 
 | 10188 |             break; | 
 | 10189 |         case Py_LT: | 
 | 10190 |             v = TEST_COND(result == -1); | 
 | 10191 |             break; | 
 | 10192 |         case Py_GT: | 
 | 10193 |             v = TEST_COND(result == 1); | 
 | 10194 |             break; | 
 | 10195 |         default: | 
 | 10196 |             PyErr_BadArgument(); | 
 | 10197 |             return NULL; | 
 | 10198 |         } | 
 | 10199 |         Py_INCREF(v); | 
 | 10200 |         return v; | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10201 |     } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10202 |  | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10203 |     Py_RETURN_NOTIMPLEMENTED; | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10204 | } | 
 | 10205 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10206 | int | 
 | 10207 | PyUnicode_Contains(PyObject *container, PyObject *element) | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10208 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10209 |     PyObject *str, *sub; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10210 |     int kind1, kind2, kind; | 
 | 10211 |     void *buf1, *buf2; | 
 | 10212 |     Py_ssize_t len1, len2; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10213 |     int result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10214 |  | 
 | 10215 |     /* Coerce the two arguments */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10216 |     sub = PyUnicode_FromObject(element); | 
 | 10217 |     if (!sub) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10218 |         PyErr_Format(PyExc_TypeError, | 
 | 10219 |                      "'in <string>' requires string as left operand, not %s", | 
 | 10220 |                      element->ob_type->tp_name); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10221 |         return -1; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10222 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10223 |     if (PyUnicode_READY(sub) == -1) | 
 | 10224 |         return -1; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10225 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10226 |     str = PyUnicode_FromObject(container); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10227 |     if (!str || PyUnicode_READY(str) == -1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10228 |         Py_DECREF(sub); | 
 | 10229 |         return -1; | 
 | 10230 |     } | 
 | 10231 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10232 |     kind1 = PyUnicode_KIND(str); | 
 | 10233 |     kind2 = PyUnicode_KIND(sub); | 
 | 10234 |     kind = kind1 > kind2 ? kind1 : kind2; | 
 | 10235 |     buf1 = PyUnicode_DATA(str); | 
 | 10236 |     buf2 = PyUnicode_DATA(sub); | 
 | 10237 |     if (kind1 != kind) | 
 | 10238 |         buf1 = _PyUnicode_AsKind((PyObject*)str, kind); | 
 | 10239 |     if (!buf1) { | 
 | 10240 |         Py_DECREF(sub); | 
 | 10241 |         return -1; | 
 | 10242 |     } | 
 | 10243 |     if (kind2 != kind) | 
 | 10244 |         buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); | 
 | 10245 |     if (!buf2) { | 
 | 10246 |         Py_DECREF(sub); | 
 | 10247 |         if (kind1 != kind) PyMem_Free(buf1); | 
 | 10248 |         return -1; | 
 | 10249 |     } | 
 | 10250 |     len1 = PyUnicode_GET_LENGTH(str); | 
 | 10251 |     len2 = PyUnicode_GET_LENGTH(sub); | 
 | 10252 |  | 
 | 10253 |     switch(kind) { | 
 | 10254 |     case PyUnicode_1BYTE_KIND: | 
 | 10255 |         result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; | 
 | 10256 |         break; | 
 | 10257 |     case PyUnicode_2BYTE_KIND: | 
 | 10258 |         result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; | 
 | 10259 |         break; | 
 | 10260 |     case PyUnicode_4BYTE_KIND: | 
 | 10261 |         result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; | 
 | 10262 |         break; | 
 | 10263 |     default: | 
 | 10264 |         result = -1; | 
 | 10265 |         assert(0); | 
 | 10266 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10267 |  | 
 | 10268 |     Py_DECREF(str); | 
 | 10269 |     Py_DECREF(sub); | 
 | 10270 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10271 |     if (kind1 != kind) | 
 | 10272 |         PyMem_Free(buf1); | 
 | 10273 |     if (kind2 != kind) | 
 | 10274 |         PyMem_Free(buf2); | 
 | 10275 |  | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10276 |     return result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10277 | } | 
 | 10278 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10279 | /* Concat to string or Unicode object giving a new Unicode object. */ | 
 | 10280 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10281 | PyObject * | 
 | 10282 | PyUnicode_Concat(PyObject *left, PyObject *right) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10283 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10284 |     PyObject *u = NULL, *v = NULL, *w; | 
 | 10285 |     Py_UCS4 maxchar; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10286 |  | 
 | 10287 |     /* Coerce the two arguments */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10288 |     u = PyUnicode_FromObject(left); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10289 |     if (u == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10290 |         goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10291 |     v = PyUnicode_FromObject(right); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10292 |     if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10293 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10294 |  | 
 | 10295 |     /* Shortcuts */ | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10296 |     if (v == unicode_empty) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10297 |         Py_DECREF(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10298 |         return u; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10299 |     } | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10300 |     if (u == unicode_empty) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10301 |         Py_DECREF(u); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10302 |         return v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10303 |     } | 
 | 10304 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 |     maxchar = PyUnicode_MAX_CHAR_VALUE(u); | 
| Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 10306 |     maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10307 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10308 |     /* Concat the two Unicode strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10309 |     w = PyUnicode_New( | 
 | 10310 |         PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), | 
 | 10311 |         maxchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10312 |     if (w == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10313 |         goto onError; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10314 |     copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); | 
 | 10315 |     copy_characters(w, PyUnicode_GET_LENGTH(u), v, 0, PyUnicode_GET_LENGTH(v)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10316 |     Py_DECREF(u); | 
 | 10317 |     Py_DECREF(v); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10318 |     assert(_PyUnicode_CheckConsistency(w, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10319 |     return w; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10320 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10321 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10322 |     Py_XDECREF(u); | 
 | 10323 |     Py_XDECREF(v); | 
 | 10324 |     return NULL; | 
 | 10325 | } | 
 | 10326 |  | 
| Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10327 | static void | 
 | 10328 | unicode_append_inplace(PyObject **p_left, PyObject *right) | 
 | 10329 | { | 
 | 10330 |     Py_ssize_t left_len, right_len, new_len; | 
| Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10331 |  | 
 | 10332 |     assert(PyUnicode_IS_READY(*p_left)); | 
 | 10333 |     assert(PyUnicode_IS_READY(right)); | 
 | 10334 |  | 
 | 10335 |     left_len = PyUnicode_GET_LENGTH(*p_left); | 
 | 10336 |     right_len = PyUnicode_GET_LENGTH(right); | 
 | 10337 |     if (left_len > PY_SSIZE_T_MAX - right_len) { | 
 | 10338 |         PyErr_SetString(PyExc_OverflowError, | 
 | 10339 |                         "strings are too large to concat"); | 
 | 10340 |         goto error; | 
 | 10341 |     } | 
 | 10342 |     new_len = left_len + right_len; | 
 | 10343 |  | 
 | 10344 |     /* Now we own the last reference to 'left', so we can resize it | 
 | 10345 |      * in-place. | 
 | 10346 |      */ | 
 | 10347 |     if (unicode_resize(p_left, new_len) != 0) { | 
 | 10348 |         /* XXX if _PyUnicode_Resize() fails, 'left' has been | 
 | 10349 |          * deallocated so it cannot be put back into | 
 | 10350 |          * 'variable'.  The MemoryError is raised when there | 
 | 10351 |          * is no value in 'variable', which might (very | 
 | 10352 |          * remotely) be a cause of incompatibilities. | 
 | 10353 |          */ | 
 | 10354 |         goto error; | 
 | 10355 |     } | 
 | 10356 |     /* copy 'right' into the newly allocated area of 'left' */ | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10357 |     copy_characters(*p_left, left_len, right, 0, right_len); | 
 | 10358 |     _PyUnicode_DIRTY(*p_left); | 
| Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10359 |     return; | 
 | 10360 |  | 
 | 10361 | error: | 
 | 10362 |     Py_DECREF(*p_left); | 
 | 10363 |     *p_left = NULL; | 
 | 10364 | } | 
 | 10365 |  | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10366 | void | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10367 | PyUnicode_Append(PyObject **p_left, PyObject *right) | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10368 | { | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10369 |     PyObject *left, *res; | 
 | 10370 |  | 
 | 10371 |     if (p_left == NULL) { | 
 | 10372 |         if (!PyErr_Occurred()) | 
 | 10373 |             PyErr_BadInternalCall(); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10374 |         return; | 
 | 10375 |     } | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10376 |     left = *p_left; | 
 | 10377 |     if (right == NULL || !PyUnicode_Check(left)) { | 
 | 10378 |         if (!PyErr_Occurred()) | 
 | 10379 |             PyErr_BadInternalCall(); | 
 | 10380 |         goto error; | 
 | 10381 |     } | 
 | 10382 |  | 
| Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10383 |     if (PyUnicode_READY(left)) | 
 | 10384 |         goto error; | 
 | 10385 |     if (PyUnicode_READY(right)) | 
 | 10386 |         goto error; | 
 | 10387 |  | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10388 |     if (PyUnicode_CheckExact(left) && left != unicode_empty | 
 | 10389 |         && PyUnicode_CheckExact(right) && right != unicode_empty | 
 | 10390 |         && unicode_resizable(left) | 
 | 10391 |         && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) | 
 | 10392 |             || _PyUnicode_WSTR(left) != NULL)) | 
 | 10393 |     { | 
| Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10394 |         /* Don't resize for ascii += latin1. Convert ascii to latin1 requires | 
 | 10395 |            to change the structure size, but characters are stored just after | 
| Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10396 |            the structure, and so it requires to move all characters which is | 
| Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10397 |            not so different than duplicating the string. */ | 
 | 10398 |         if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10399 |         { | 
| Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10400 |             unicode_append_inplace(p_left, right); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10401 |             if (p_left != NULL) | 
 | 10402 |                 assert(_PyUnicode_CheckConsistency(*p_left, 1)); | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10403 |             return; | 
 | 10404 |         } | 
 | 10405 |     } | 
 | 10406 |  | 
 | 10407 |     res = PyUnicode_Concat(left, right); | 
 | 10408 |     if (res == NULL) | 
 | 10409 |         goto error; | 
 | 10410 |     Py_DECREF(left); | 
 | 10411 |     *p_left = res; | 
 | 10412 |     return; | 
 | 10413 |  | 
 | 10414 | error: | 
 | 10415 |     Py_DECREF(*p_left); | 
 | 10416 |     *p_left = NULL; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10417 | } | 
 | 10418 |  | 
 | 10419 | void | 
 | 10420 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) | 
 | 10421 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10422 |     PyUnicode_Append(pleft, right); | 
 | 10423 |     Py_XDECREF(right); | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10424 | } | 
 | 10425 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10426 | PyDoc_STRVAR(count__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10427 |              "S.count(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10428 | \n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10429 | Return the number of non-overlapping occurrences of substring sub in\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10430 | 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] | 10431 | interpreted as in slice notation."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10432 |  | 
 | 10433 | static PyObject * | 
 | 10434 | unicode_count(PyUnicodeObject *self, PyObject *args) | 
 | 10435 | { | 
 | 10436 |     PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10437 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10438 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10439 |     PyObject *result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10440 |     int kind1, kind2, kind; | 
 | 10441 |     void *buf1, *buf2; | 
 | 10442 |     Py_ssize_t len1, len2, iresult; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10443 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10444 |     if (!stringlib_parse_args_finds_unicode("count", args, &substring, | 
 | 10445 |                                             &start, &end)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10446 |         return NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10447 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10448 |     kind1 = PyUnicode_KIND(self); | 
 | 10449 |     kind2 = PyUnicode_KIND(substring); | 
 | 10450 |     kind = kind1 > kind2 ? kind1 : kind2; | 
 | 10451 |     buf1 = PyUnicode_DATA(self); | 
 | 10452 |     buf2 = PyUnicode_DATA(substring); | 
 | 10453 |     if (kind1 != kind) | 
 | 10454 |         buf1 = _PyUnicode_AsKind((PyObject*)self, kind); | 
 | 10455 |     if (!buf1) { | 
 | 10456 |         Py_DECREF(substring); | 
 | 10457 |         return NULL; | 
 | 10458 |     } | 
 | 10459 |     if (kind2 != kind) | 
 | 10460 |         buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); | 
 | 10461 |     if (!buf2) { | 
 | 10462 |         Py_DECREF(substring); | 
 | 10463 |         if (kind1 != kind) PyMem_Free(buf1); | 
 | 10464 |         return NULL; | 
 | 10465 |     } | 
 | 10466 |     len1 = PyUnicode_GET_LENGTH(self); | 
 | 10467 |     len2 = PyUnicode_GET_LENGTH(substring); | 
 | 10468 |  | 
 | 10469 |     ADJUST_INDICES(start, end, len1); | 
 | 10470 |     switch(kind) { | 
 | 10471 |     case PyUnicode_1BYTE_KIND: | 
 | 10472 |         iresult = ucs1lib_count( | 
 | 10473 |             ((Py_UCS1*)buf1) + start, end - start, | 
 | 10474 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 10475 |             ); | 
 | 10476 |         break; | 
 | 10477 |     case PyUnicode_2BYTE_KIND: | 
 | 10478 |         iresult = ucs2lib_count( | 
 | 10479 |             ((Py_UCS2*)buf1) + start, end - start, | 
 | 10480 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 10481 |             ); | 
 | 10482 |         break; | 
 | 10483 |     case PyUnicode_4BYTE_KIND: | 
 | 10484 |         iresult = ucs4lib_count( | 
 | 10485 |             ((Py_UCS4*)buf1) + start, end - start, | 
 | 10486 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 10487 |             ); | 
 | 10488 |         break; | 
 | 10489 |     default: | 
 | 10490 |         assert(0); iresult = 0; | 
 | 10491 |     } | 
 | 10492 |  | 
 | 10493 |     result = PyLong_FromSsize_t(iresult); | 
 | 10494 |  | 
 | 10495 |     if (kind1 != kind) | 
 | 10496 |         PyMem_Free(buf1); | 
 | 10497 |     if (kind2 != kind) | 
 | 10498 |         PyMem_Free(buf2); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10499 |  | 
 | 10500 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10501 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10502 |     return result; | 
 | 10503 | } | 
 | 10504 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10505 | PyDoc_STRVAR(encode__doc__, | 
| Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10506 |              "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10507 | \n\ | 
| Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10508 | Encode S using the codec registered for encoding. Default encoding\n\ | 
 | 10509 | is 'utf-8'. errors may be given to set a different error\n\ | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 10510 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10511 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ | 
 | 10512 | 'xmlcharrefreplace' as well as any other name registered with\n\ | 
 | 10513 | codecs.register_error that can handle UnicodeEncodeErrors."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10514 |  | 
 | 10515 | static PyObject * | 
| Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10516 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10517 | { | 
| Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10518 |     static char *kwlist[] = {"encoding", "errors", 0}; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10519 |     char *encoding = NULL; | 
 | 10520 |     char *errors = NULL; | 
| Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10521 |  | 
| Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10522 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", | 
 | 10523 |                                      kwlist, &encoding, &errors)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10524 |         return NULL; | 
| Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10525 |     return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10526 | } | 
 | 10527 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10528 | PyDoc_STRVAR(expandtabs__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10529 |              "S.expandtabs([tabsize]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10530 | \n\ | 
 | 10531 | 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] | 10532 | 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] | 10533 |  | 
 | 10534 | static PyObject* | 
 | 10535 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) | 
 | 10536 | { | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10537 |     Py_ssize_t i, j, line_pos, src_len, incr; | 
 | 10538 |     Py_UCS4 ch; | 
 | 10539 |     PyObject *u; | 
 | 10540 |     void *src_data, *dest_data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10541 |     int tabsize = 8; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10542 |     int kind; | 
| Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10543 |     int found; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10544 |  | 
 | 10545 |     if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10546 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10547 |  | 
| Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10548 |     if (PyUnicode_READY(self) == -1) | 
 | 10549 |         return NULL; | 
 | 10550 |  | 
| Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10551 |     /* First pass: determine size of output string */ | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10552 |     src_len = PyUnicode_GET_LENGTH(self); | 
 | 10553 |     i = j = line_pos = 0; | 
 | 10554 |     kind = PyUnicode_KIND(self); | 
 | 10555 |     src_data = PyUnicode_DATA(self); | 
| Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10556 |     found = 0; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10557 |     for (; i < src_len; i++) { | 
 | 10558 |         ch = PyUnicode_READ(kind, src_data, i); | 
 | 10559 |         if (ch == '\t') { | 
| Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10560 |             found = 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10561 |             if (tabsize > 0) { | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10562 |                 incr = tabsize - (line_pos % tabsize); /* cannot overflow */ | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10563 |                 if (j > PY_SSIZE_T_MAX - incr) | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10564 |                     goto overflow; | 
 | 10565 |                 line_pos += incr; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10566 |                 j += incr; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10567 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10568 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10569 |         else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10570 |             if (j > PY_SSIZE_T_MAX - 1) | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10571 |                 goto overflow; | 
 | 10572 |             line_pos++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10573 |             j++; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10574 |             if (ch == '\n' || ch == '\r') | 
 | 10575 |                 line_pos = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10576 |         } | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10577 |     } | 
| Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10578 |     if (!found && PyUnicode_CheckExact(self)) { | 
 | 10579 |         Py_INCREF((PyObject *) self); | 
 | 10580 |         return (PyObject *) self; | 
 | 10581 |     } | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10582 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10583 |     /* Second pass: create output string and fill it */ | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10584 |     u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10585 |     if (!u) | 
 | 10586 |         return NULL; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10587 |     dest_data = PyUnicode_DATA(u); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10588 |  | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10589 |     i = j = line_pos = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10590 |  | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10591 |     for (; i < src_len; i++) { | 
 | 10592 |         ch = PyUnicode_READ(kind, src_data, i); | 
 | 10593 |         if (ch == '\t') { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10594 |             if (tabsize > 0) { | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10595 |                 incr = tabsize - (line_pos % tabsize); | 
 | 10596 |                 line_pos += incr; | 
 | 10597 |                 while (incr--) { | 
 | 10598 |                     PyUnicode_WRITE(kind, dest_data, j, ' '); | 
 | 10599 |                     j++; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10600 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10601 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10602 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10603 |         else { | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10604 |             line_pos++; | 
 | 10605 |             PyUnicode_WRITE(kind, dest_data, j, ch); | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10606 |             j++; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10607 |             if (ch == '\n' || ch == '\r') | 
 | 10608 |                 line_pos = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10609 |         } | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10610 |     } | 
 | 10611 |     assert (j == PyUnicode_GET_LENGTH(u)); | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10612 | #ifndef DONT_MAKE_RESULT_READY | 
 | 10613 |     if (_PyUnicode_READY_REPLACE(&u)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10614 |         Py_DECREF(u); | 
 | 10615 |         return NULL; | 
 | 10616 |     } | 
| Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10617 | #endif | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10618 |     assert(_PyUnicode_CheckConsistency(u, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10619 |     return (PyObject*) u; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10620 |  | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10621 |   overflow: | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10622 |     PyErr_SetString(PyExc_OverflowError, "new string is too long"); | 
 | 10623 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10624 | } | 
 | 10625 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10626 | PyDoc_STRVAR(find__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10627 |              "S.find(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10628 | \n\ | 
 | 10629 | Return the lowest index in S where substring sub is found,\n\ | 
| Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10630 | such that sub is contained within S[start:end].  Optional\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10631 | arguments start and end are interpreted as in slice notation.\n\ | 
 | 10632 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10633 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10634 |  | 
 | 10635 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10636 | unicode_find(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10637 | { | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10638 |     PyUnicodeObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10639 |     Py_ssize_t start; | 
 | 10640 |     Py_ssize_t end; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10641 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10642 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10643 |     if (!stringlib_parse_args_finds_unicode("find", args, &substring, | 
 | 10644 |                                             &start, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10645 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10646 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10647 |     if (PyUnicode_READY(self) == -1) | 
 | 10648 |         return NULL; | 
 | 10649 |     if (PyUnicode_READY(substring) == -1) | 
 | 10650 |         return NULL; | 
 | 10651 |  | 
 | 10652 |     result = any_find_slice( | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10653 |         asciilib_find_slice, ucs1lib_find_slice, | 
 | 10654 |         ucs2lib_find_slice, ucs4lib_find_slice, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10655 |         self, (PyObject*)substring, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10656 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10657 |  | 
 | 10658 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10659 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10660 |     if (result == -2) | 
 | 10661 |         return NULL; | 
 | 10662 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10663 |     return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10664 | } | 
 | 10665 |  | 
 | 10666 | static PyObject * | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10667 | unicode_getitem(PyObject *self, Py_ssize_t index) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10668 | { | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10669 |     Py_UCS4 ch = PyUnicode_ReadChar(self, index); | 
 | 10670 |     if (ch == (Py_UCS4)-1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10671 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10672 |     return PyUnicode_FromOrdinal(ch); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10673 | } | 
 | 10674 |  | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10675 | /* Believe it or not, this produces the same value for ASCII strings | 
| Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10676 |    as bytes_hash(). */ | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10677 | static Py_hash_t | 
| Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10678 | unicode_hash(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10679 | { | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10680 |     Py_ssize_t len; | 
| Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10681 |     Py_uhash_t x; | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10682 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10683 |     if (_PyUnicode_HASH(self) != -1) | 
 | 10684 |         return _PyUnicode_HASH(self); | 
 | 10685 |     if (PyUnicode_READY(self) == -1) | 
 | 10686 |         return -1; | 
 | 10687 |     len = PyUnicode_GET_LENGTH(self); | 
 | 10688 |  | 
 | 10689 |     /* The hash function as a macro, gets expanded three times below. */ | 
 | 10690 | #define HASH(P) \ | 
 | 10691 |     x = (Py_uhash_t)*P << 7; \ | 
 | 10692 |     while (--len >= 0) \ | 
 | 10693 |         x = (1000003*x) ^ (Py_uhash_t)*P++; | 
 | 10694 |  | 
 | 10695 |     switch (PyUnicode_KIND(self)) { | 
 | 10696 |     case PyUnicode_1BYTE_KIND: { | 
 | 10697 |         const unsigned char *c = PyUnicode_1BYTE_DATA(self); | 
 | 10698 |         HASH(c); | 
 | 10699 |         break; | 
 | 10700 |     } | 
 | 10701 |     case PyUnicode_2BYTE_KIND: { | 
 | 10702 |         const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); | 
 | 10703 |         HASH(s); | 
 | 10704 |         break; | 
 | 10705 |     } | 
 | 10706 |     default: { | 
 | 10707 |         Py_UCS4 *l; | 
 | 10708 |         assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && | 
 | 10709 |                "Impossible switch case in unicode_hash"); | 
 | 10710 |         l = PyUnicode_4BYTE_DATA(self); | 
 | 10711 |         HASH(l); | 
 | 10712 |         break; | 
 | 10713 |     } | 
 | 10714 |     } | 
 | 10715 |     x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); | 
 | 10716 |  | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10717 |     if (x == -1) | 
 | 10718 |         x = -2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10719 |     _PyUnicode_HASH(self) = x; | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10720 |     return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10721 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10722 | #undef HASH | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10723 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10724 | PyDoc_STRVAR(index__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10725 |              "S.index(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10726 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10727 | 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] | 10728 |  | 
 | 10729 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10730 | unicode_index(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10731 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10732 |     Py_ssize_t result; | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10733 |     PyUnicodeObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10734 |     Py_ssize_t start; | 
 | 10735 |     Py_ssize_t end; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10736 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10737 |     if (!stringlib_parse_args_finds_unicode("index", args, &substring, | 
 | 10738 |                                             &start, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10739 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10740 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10741 |     if (PyUnicode_READY(self) == -1) | 
 | 10742 |         return NULL; | 
 | 10743 |     if (PyUnicode_READY(substring) == -1) | 
 | 10744 |         return NULL; | 
 | 10745 |  | 
 | 10746 |     result = any_find_slice( | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10747 |         asciilib_find_slice, ucs1lib_find_slice, | 
 | 10748 |         ucs2lib_find_slice, ucs4lib_find_slice, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10749 |         self, (PyObject*)substring, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10750 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10751 |  | 
 | 10752 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10753 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10754 |     if (result == -2) | 
 | 10755 |         return NULL; | 
 | 10756 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10757 |     if (result < 0) { | 
 | 10758 |         PyErr_SetString(PyExc_ValueError, "substring not found"); | 
 | 10759 |         return NULL; | 
 | 10760 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10761 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10762 |     return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10763 | } | 
 | 10764 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10765 | PyDoc_STRVAR(islower__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10766 |              "S.islower() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10768 | 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] | 10769 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10770 |  | 
 | 10771 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10772 | unicode_islower(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10773 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10774 |     Py_ssize_t i, length; | 
 | 10775 |     int kind; | 
 | 10776 |     void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10777 |     int cased; | 
 | 10778 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10779 |     if (PyUnicode_READY(self) == -1) | 
 | 10780 |         return NULL; | 
 | 10781 |     length = PyUnicode_GET_LENGTH(self); | 
 | 10782 |     kind = PyUnicode_KIND(self); | 
 | 10783 |     data = PyUnicode_DATA(self); | 
 | 10784 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10785 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10786 |     if (length == 1) | 
 | 10787 |         return PyBool_FromLong( | 
 | 10788 |             Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10789 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10790 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10791 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10792 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10793 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10794 |     cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10795 |     for (i = 0; i < length; i++) { | 
 | 10796 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10797 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10798 |         if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) | 
 | 10799 |             return PyBool_FromLong(0); | 
 | 10800 |         else if (!cased && Py_UNICODE_ISLOWER(ch)) | 
 | 10801 |             cased = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10802 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10803 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10804 | } | 
 | 10805 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10806 | PyDoc_STRVAR(isupper__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10807 |              "S.isupper() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10808 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10809 | 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] | 10810 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10811 |  | 
 | 10812 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10813 | unicode_isupper(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10814 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10815 |     Py_ssize_t i, length; | 
 | 10816 |     int kind; | 
 | 10817 |     void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10818 |     int cased; | 
 | 10819 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10820 |     if (PyUnicode_READY(self) == -1) | 
 | 10821 |         return NULL; | 
 | 10822 |     length = PyUnicode_GET_LENGTH(self); | 
 | 10823 |     kind = PyUnicode_KIND(self); | 
 | 10824 |     data = PyUnicode_DATA(self); | 
 | 10825 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10826 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10827 |     if (length == 1) | 
 | 10828 |         return PyBool_FromLong( | 
 | 10829 |             Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10830 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10831 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10832 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10833 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10834 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10835 |     cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10836 |     for (i = 0; i < length; i++) { | 
 | 10837 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10838 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10839 |         if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) | 
 | 10840 |             return PyBool_FromLong(0); | 
 | 10841 |         else if (!cased && Py_UNICODE_ISUPPER(ch)) | 
 | 10842 |             cased = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10843 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10844 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10845 | } | 
 | 10846 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10847 | PyDoc_STRVAR(istitle__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10848 |              "S.istitle() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10849 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10850 | Return True if S is a titlecased string and there is at least one\n\ | 
 | 10851 | character in S, i.e. upper- and titlecase characters may only\n\ | 
 | 10852 | follow uncased characters and lowercase characters only cased ones.\n\ | 
 | 10853 | Return False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10854 |  | 
 | 10855 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10856 | unicode_istitle(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10857 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10858 |     Py_ssize_t i, length; | 
 | 10859 |     int kind; | 
 | 10860 |     void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10861 |     int cased, previous_is_cased; | 
 | 10862 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10863 |     if (PyUnicode_READY(self) == -1) | 
 | 10864 |         return NULL; | 
 | 10865 |     length = PyUnicode_GET_LENGTH(self); | 
 | 10866 |     kind = PyUnicode_KIND(self); | 
 | 10867 |     data = PyUnicode_DATA(self); | 
 | 10868 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10869 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10870 |     if (length == 1) { | 
 | 10871 |         Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
 | 10872 |         return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || | 
 | 10873 |                                (Py_UNICODE_ISUPPER(ch) != 0)); | 
 | 10874 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10875 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10876 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10877 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10878 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10879 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10880 |     cased = 0; | 
 | 10881 |     previous_is_cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10882 |     for (i = 0; i < length; i++) { | 
 | 10883 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10884 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10885 |         if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { | 
 | 10886 |             if (previous_is_cased) | 
 | 10887 |                 return PyBool_FromLong(0); | 
 | 10888 |             previous_is_cased = 1; | 
 | 10889 |             cased = 1; | 
 | 10890 |         } | 
 | 10891 |         else if (Py_UNICODE_ISLOWER(ch)) { | 
 | 10892 |             if (!previous_is_cased) | 
 | 10893 |                 return PyBool_FromLong(0); | 
 | 10894 |             previous_is_cased = 1; | 
 | 10895 |             cased = 1; | 
 | 10896 |         } | 
 | 10897 |         else | 
 | 10898 |             previous_is_cased = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10899 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10900 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10901 | } | 
 | 10902 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10903 | PyDoc_STRVAR(isspace__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10904 |              "S.isspace() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10905 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10906 | Return True if all characters in S are whitespace\n\ | 
 | 10907 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10908 |  | 
 | 10909 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10910 | unicode_isspace(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10911 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10912 |     Py_ssize_t i, length; | 
 | 10913 |     int kind; | 
 | 10914 |     void *data; | 
 | 10915 |  | 
 | 10916 |     if (PyUnicode_READY(self) == -1) | 
 | 10917 |         return NULL; | 
 | 10918 |     length = PyUnicode_GET_LENGTH(self); | 
 | 10919 |     kind = PyUnicode_KIND(self); | 
 | 10920 |     data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10921 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10922 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10923 |     if (length == 1) | 
 | 10924 |         return PyBool_FromLong( | 
 | 10925 |             Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10926 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10927 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10928 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10929 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10930 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10931 |     for (i = 0; i < length; i++) { | 
 | 10932 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10933 |         if (!Py_UNICODE_ISSPACE(ch)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10934 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10935 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10936 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10937 | } | 
 | 10938 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10939 | PyDoc_STRVAR(isalpha__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10940 |              "S.isalpha() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10941 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10942 | Return True if all characters in S are alphabetic\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10943 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10944 |  | 
 | 10945 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10946 | unicode_isalpha(PyUnicodeObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10947 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10948 |     Py_ssize_t i, length; | 
 | 10949 |     int kind; | 
 | 10950 |     void *data; | 
 | 10951 |  | 
 | 10952 |     if (PyUnicode_READY(self) == -1) | 
 | 10953 |         return NULL; | 
 | 10954 |     length = PyUnicode_GET_LENGTH(self); | 
 | 10955 |     kind = PyUnicode_KIND(self); | 
 | 10956 |     data = PyUnicode_DATA(self); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10957 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10958 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10959 |     if (length == 1) | 
 | 10960 |         return PyBool_FromLong( | 
 | 10961 |             Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10962 |  | 
 | 10963 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10964 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10965 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10966 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10967 |     for (i = 0; i < length; i++) { | 
 | 10968 |         if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10969 |             return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10970 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10971 |     return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10972 | } | 
 | 10973 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10974 | PyDoc_STRVAR(isalnum__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10975 |              "S.isalnum() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10976 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10977 | Return True if all characters in S are alphanumeric\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10978 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10979 |  | 
 | 10980 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10981 | unicode_isalnum(PyUnicodeObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10982 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10983 |     int kind; | 
 | 10984 |     void *data; | 
 | 10985 |     Py_ssize_t len, i; | 
 | 10986 |  | 
 | 10987 |     if (PyUnicode_READY(self) == -1) | 
 | 10988 |         return NULL; | 
 | 10989 |  | 
 | 10990 |     kind = PyUnicode_KIND(self); | 
 | 10991 |     data = PyUnicode_DATA(self); | 
 | 10992 |     len = PyUnicode_GET_LENGTH(self); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10993 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10994 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10995 |     if (len == 1) { | 
 | 10996 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
 | 10997 |         return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); | 
 | 10998 |     } | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10999 |  | 
 | 11000 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11001 |     if (len == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11002 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11003 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11004 |     for (i = 0; i < len; i++) { | 
 | 11005 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11006 |         if (!Py_UNICODE_ISALNUM(ch)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11007 |             return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11008 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11009 |     return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11010 | } | 
 | 11011 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11012 | PyDoc_STRVAR(isdecimal__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11013 |              "S.isdecimal() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11014 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11015 | 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] | 11016 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 |  | 
 | 11018 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11019 | unicode_isdecimal(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11020 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11021 |     Py_ssize_t i, length; | 
 | 11022 |     int kind; | 
 | 11023 |     void *data; | 
 | 11024 |  | 
 | 11025 |     if (PyUnicode_READY(self) == -1) | 
 | 11026 |         return NULL; | 
 | 11027 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11028 |     kind = PyUnicode_KIND(self); | 
 | 11029 |     data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11030 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11031 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11032 |     if (length == 1) | 
 | 11033 |         return PyBool_FromLong( | 
 | 11034 |             Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11035 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11036 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11037 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11038 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11039 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11040 |     for (i = 0; i < length; i++) { | 
 | 11041 |         if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11042 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11044 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11045 | } | 
 | 11046 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11047 | PyDoc_STRVAR(isdigit__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11048 |              "S.isdigit() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11049 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11050 | Return True if all characters in S are digits\n\ | 
 | 11051 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11052 |  | 
 | 11053 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11054 | unicode_isdigit(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11055 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11056 |     Py_ssize_t i, length; | 
 | 11057 |     int kind; | 
 | 11058 |     void *data; | 
 | 11059 |  | 
 | 11060 |     if (PyUnicode_READY(self) == -1) | 
 | 11061 |         return NULL; | 
 | 11062 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11063 |     kind = PyUnicode_KIND(self); | 
 | 11064 |     data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11065 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11066 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11067 |     if (length == 1) { | 
 | 11068 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
 | 11069 |         return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); | 
 | 11070 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11071 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11072 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11073 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11074 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11075 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11076 |     for (i = 0; i < length; i++) { | 
 | 11077 |         if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11078 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11079 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11080 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11081 | } | 
 | 11082 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11083 | PyDoc_STRVAR(isnumeric__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11084 |              "S.isnumeric() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11085 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11086 | 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] | 11087 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11088 |  | 
 | 11089 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11090 | unicode_isnumeric(PyUnicodeObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11091 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11092 |     Py_ssize_t i, length; | 
 | 11093 |     int kind; | 
 | 11094 |     void *data; | 
 | 11095 |  | 
 | 11096 |     if (PyUnicode_READY(self) == -1) | 
 | 11097 |         return NULL; | 
 | 11098 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11099 |     kind = PyUnicode_KIND(self); | 
 | 11100 |     data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11101 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11102 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11103 |     if (length == 1) | 
 | 11104 |         return PyBool_FromLong( | 
 | 11105 |             Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11106 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11107 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11108 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11109 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11110 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11111 |     for (i = 0; i < length; i++) { | 
 | 11112 |         if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11113 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11114 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11115 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11116 | } | 
 | 11117 |  | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11118 | int | 
 | 11119 | PyUnicode_IsIdentifier(PyObject *self) | 
 | 11120 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11121 |     int kind; | 
 | 11122 |     void *data; | 
 | 11123 |     Py_ssize_t i; | 
| Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11124 |     Py_UCS4 first; | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11125 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11126 |     if (PyUnicode_READY(self) == -1) { | 
 | 11127 |         Py_FatalError("identifier not ready"); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11128 |         return 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11129 |     } | 
 | 11130 |  | 
 | 11131 |     /* Special case for empty strings */ | 
 | 11132 |     if (PyUnicode_GET_LENGTH(self) == 0) | 
 | 11133 |         return 0; | 
 | 11134 |     kind = PyUnicode_KIND(self); | 
 | 11135 |     data = PyUnicode_DATA(self); | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11136 |  | 
 | 11137 |     /* PEP 3131 says that the first character must be in | 
 | 11138 |        XID_Start and subsequent characters in XID_Continue, | 
 | 11139 |        and for the ASCII range, the 2.x rules apply (i.e | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11140 |        start with letters and underscore, continue with | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11141 |        letters, digits, underscore). However, given the current | 
 | 11142 |        definition of XID_Start and XID_Continue, it is sufficient | 
 | 11143 |        to check just for these, except that _ must be allowed | 
 | 11144 |        as starting an identifier.  */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11145 |     first = PyUnicode_READ(kind, data, 0); | 
| Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11146 |     if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11147 |         return 0; | 
 | 11148 |  | 
| Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11149 |     for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11150 |         if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11151 |             return 0; | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11152 |     return 1; | 
 | 11153 | } | 
 | 11154 |  | 
 | 11155 | PyDoc_STRVAR(isidentifier__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11156 |              "S.isidentifier() -> bool\n\ | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11157 | \n\ | 
 | 11158 | Return True if S is a valid identifier according\n\ | 
 | 11159 | to the language definition."); | 
 | 11160 |  | 
 | 11161 | static PyObject* | 
 | 11162 | unicode_isidentifier(PyObject *self) | 
 | 11163 | { | 
 | 11164 |     return PyBool_FromLong(PyUnicode_IsIdentifier(self)); | 
 | 11165 | } | 
 | 11166 |  | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11167 | PyDoc_STRVAR(isprintable__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11168 |              "S.isprintable() -> bool\n\ | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11169 | \n\ | 
 | 11170 | Return True if all characters in S are considered\n\ | 
 | 11171 | printable in repr() or S is empty, False otherwise."); | 
 | 11172 |  | 
 | 11173 | static PyObject* | 
 | 11174 | unicode_isprintable(PyObject *self) | 
 | 11175 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11176 |     Py_ssize_t i, length; | 
 | 11177 |     int kind; | 
 | 11178 |     void *data; | 
 | 11179 |  | 
 | 11180 |     if (PyUnicode_READY(self) == -1) | 
 | 11181 |         return NULL; | 
 | 11182 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11183 |     kind = PyUnicode_KIND(self); | 
 | 11184 |     data = PyUnicode_DATA(self); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11185 |  | 
 | 11186 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11187 |     if (length == 1) | 
 | 11188 |         return PyBool_FromLong( | 
 | 11189 |             Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11190 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11191 |     for (i = 0; i < length; i++) { | 
 | 11192 |         if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11193 |             Py_RETURN_FALSE; | 
 | 11194 |         } | 
 | 11195 |     } | 
 | 11196 |     Py_RETURN_TRUE; | 
 | 11197 | } | 
 | 11198 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11199 | PyDoc_STRVAR(join__doc__, | 
| Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11200 |              "S.join(iterable) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11201 | \n\ | 
 | 11202 | Return a string which is the concatenation of the strings in the\n\ | 
| Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11203 | iterable.  The separator between elements is S."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11204 |  | 
 | 11205 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11206 | unicode_join(PyObject *self, PyObject *data) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | { | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11208 |     return PyUnicode_Join(self, data); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11209 | } | 
 | 11210 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11211 | static Py_ssize_t | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | unicode_length(PyUnicodeObject *self) | 
 | 11213 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11214 |     if (PyUnicode_READY(self) == -1) | 
 | 11215 |         return -1; | 
 | 11216 |     return PyUnicode_GET_LENGTH(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11217 | } | 
 | 11218 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11219 | PyDoc_STRVAR(ljust__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11220 |              "S.ljust(width[, fillchar]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | \n\ | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11222 | 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] | 11223 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11224 |  | 
 | 11225 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11226 | unicode_ljust(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11227 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11228 |     Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11229 |     Py_UCS4 fillchar = ' '; | 
 | 11230 |  | 
 | 11231 |     if (PyUnicode_READY(self) == -1) | 
 | 11232 |         return NULL; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11233 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11234 |     if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11235 |         return NULL; | 
 | 11236 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11237 |     if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 |         Py_INCREF(self); | 
 | 11239 |         return (PyObject*) self; | 
 | 11240 |     } | 
 | 11241 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11242 |     return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11243 | } | 
 | 11244 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11245 | PyDoc_STRVAR(lower__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11246 |              "S.lower() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11248 | Return a copy of the string S converted to lowercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 |  | 
 | 11250 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11251 | unicode_lower(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11252 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 |     return fixup(self, fixlower); | 
 | 11254 | } | 
 | 11255 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11256 | #define LEFTSTRIP 0 | 
 | 11257 | #define RIGHTSTRIP 1 | 
 | 11258 | #define BOTHSTRIP 2 | 
 | 11259 |  | 
 | 11260 | /* Arrays indexed by above */ | 
 | 11261 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; | 
 | 11262 |  | 
 | 11263 | #define STRIPNAME(i) (stripformat[i]+3) | 
 | 11264 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11265 | /* externally visible for str.strip(unicode) */ | 
 | 11266 | PyObject * | 
 | 11267 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) | 
 | 11268 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11269 |     void *data; | 
 | 11270 |     int kind; | 
 | 11271 |     Py_ssize_t i, j, len; | 
 | 11272 |     BLOOM_MASK sepmask; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11273 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11274 |     if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) | 
 | 11275 |         return NULL; | 
 | 11276 |  | 
 | 11277 |     kind = PyUnicode_KIND(self); | 
 | 11278 |     data = PyUnicode_DATA(self); | 
 | 11279 |     len = PyUnicode_GET_LENGTH(self); | 
 | 11280 |     sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), | 
 | 11281 |                               PyUnicode_DATA(sepobj), | 
 | 11282 |                               PyUnicode_GET_LENGTH(sepobj)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11283 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11284 |     i = 0; | 
 | 11285 |     if (striptype != RIGHTSTRIP) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11286 |         while (i < len && | 
 | 11287 |                BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11288 |             i++; | 
 | 11289 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11290 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11291 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11292 |     j = len; | 
 | 11293 |     if (striptype != LEFTSTRIP) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11294 |         do { | 
 | 11295 |             j--; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11296 |         } while (j >= i && | 
 | 11297 |                  BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11298 |         j++; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11299 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11300 |  | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11301 |     return PyUnicode_Substring((PyObject*)self, i, j); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11302 | } | 
 | 11303 |  | 
 | 11304 | PyObject* | 
 | 11305 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) | 
 | 11306 | { | 
 | 11307 |     unsigned char *data; | 
 | 11308 |     int kind; | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11309 |     Py_ssize_t length; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11310 |  | 
| Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11311 |     if (PyUnicode_READY(self) == -1) | 
 | 11312 |         return NULL; | 
 | 11313 |  | 
 | 11314 |     end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); | 
 | 11315 |  | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11316 |     if (start == 0 && end == PyUnicode_GET_LENGTH(self)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11317 |     { | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11318 |         if (PyUnicode_CheckExact(self)) { | 
 | 11319 |             Py_INCREF(self); | 
 | 11320 |             return self; | 
 | 11321 |         } | 
 | 11322 |         else | 
 | 11323 |             return PyUnicode_Copy(self); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11324 |     } | 
 | 11325 |  | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11326 |     length = end - start; | 
 | 11327 |     if (length == 1) | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11328 |         return unicode_getitem(self, start); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11329 |  | 
| Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11330 |     if (start < 0 || end < 0) { | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11331 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 11332 |         return NULL; | 
 | 11333 |     } | 
 | 11334 |  | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11335 |     if (PyUnicode_IS_ASCII(self)) { | 
 | 11336 |         kind = PyUnicode_KIND(self); | 
 | 11337 |         data = PyUnicode_1BYTE_DATA(self); | 
 | 11338 |         return unicode_fromascii(data + start, length); | 
 | 11339 |     } | 
 | 11340 |     else { | 
 | 11341 |         kind = PyUnicode_KIND(self); | 
 | 11342 |         data = PyUnicode_1BYTE_DATA(self); | 
 | 11343 |         return PyUnicode_FromKindAndData(kind, | 
 | 11344 |                                          data + PyUnicode_KIND_SIZE(kind, start), | 
 | 11345 |                                          length); | 
 | 11346 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11347 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11348 |  | 
 | 11349 | static PyObject * | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11350 | do_strip(PyUnicodeObject *self, int striptype) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11351 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11352 |     int kind; | 
 | 11353 |     void *data; | 
 | 11354 |     Py_ssize_t len, i, j; | 
 | 11355 |  | 
 | 11356 |     if (PyUnicode_READY(self) == -1) | 
 | 11357 |         return NULL; | 
 | 11358 |  | 
 | 11359 |     kind = PyUnicode_KIND(self); | 
 | 11360 |     data = PyUnicode_DATA(self); | 
 | 11361 |     len = PyUnicode_GET_LENGTH(self); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11362 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11363 |     i = 0; | 
 | 11364 |     if (striptype != RIGHTSTRIP) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11365 |         while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11366 |             i++; | 
 | 11367 |         } | 
 | 11368 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11369 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11370 |     j = len; | 
 | 11371 |     if (striptype != LEFTSTRIP) { | 
 | 11372 |         do { | 
 | 11373 |             j--; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11374 |         } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11375 |         j++; | 
 | 11376 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11377 |  | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11378 |     return PyUnicode_Substring((PyObject*)self, i, j); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11379 | } | 
 | 11380 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11381 |  | 
 | 11382 | static PyObject * | 
 | 11383 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) | 
 | 11384 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11385 |     PyObject *sep = NULL; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11386 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11387 |     if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) | 
 | 11388 |         return NULL; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11389 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11390 |     if (sep != NULL && sep != Py_None) { | 
 | 11391 |         if (PyUnicode_Check(sep)) | 
 | 11392 |             return _PyUnicode_XStrip(self, striptype, sep); | 
 | 11393 |         else { | 
 | 11394 |             PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11395 |                          "%s arg must be None or str", | 
 | 11396 |                          STRIPNAME(striptype)); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11397 |             return NULL; | 
 | 11398 |         } | 
 | 11399 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11400 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11401 |     return do_strip(self, striptype); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11402 | } | 
 | 11403 |  | 
 | 11404 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11405 | PyDoc_STRVAR(strip__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11406 |              "S.strip([chars]) -> str\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11407 | \n\ | 
 | 11408 | Return a copy of the string S with leading and trailing\n\ | 
 | 11409 | whitespace removed.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11410 | If chars is given and not None, remove characters in chars instead."); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11411 |  | 
 | 11412 | static PyObject * | 
 | 11413 | unicode_strip(PyUnicodeObject *self, PyObject *args) | 
 | 11414 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11415 |     if (PyTuple_GET_SIZE(args) == 0) | 
 | 11416 |         return do_strip(self, BOTHSTRIP); /* Common case */ | 
 | 11417 |     else | 
 | 11418 |         return do_argstrip(self, BOTHSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11419 | } | 
 | 11420 |  | 
 | 11421 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11422 | PyDoc_STRVAR(lstrip__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11423 |              "S.lstrip([chars]) -> str\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11424 | \n\ | 
 | 11425 | Return a copy of the string S with leading whitespace removed.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11426 | If chars is given and not None, remove characters in chars instead."); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11427 |  | 
 | 11428 | static PyObject * | 
 | 11429 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) | 
 | 11430 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11431 |     if (PyTuple_GET_SIZE(args) == 0) | 
 | 11432 |         return do_strip(self, LEFTSTRIP); /* Common case */ | 
 | 11433 |     else | 
 | 11434 |         return do_argstrip(self, LEFTSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11435 | } | 
 | 11436 |  | 
 | 11437 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11438 | PyDoc_STRVAR(rstrip__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11439 |              "S.rstrip([chars]) -> str\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11440 | \n\ | 
 | 11441 | Return a copy of the string S with trailing whitespace removed.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11442 | If chars is given and not None, remove characters in chars instead."); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11443 |  | 
 | 11444 | static PyObject * | 
 | 11445 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) | 
 | 11446 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11447 |     if (PyTuple_GET_SIZE(args) == 0) | 
 | 11448 |         return do_strip(self, RIGHTSTRIP); /* Common case */ | 
 | 11449 |     else | 
 | 11450 |         return do_argstrip(self, RIGHTSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11451 | } | 
 | 11452 |  | 
 | 11453 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | static PyObject* | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11455 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | { | 
 | 11457 |     PyUnicodeObject *u; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 |     Py_ssize_t nchars, n; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11459 |  | 
| Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11460 |     if (len < 1) { | 
 | 11461 |         Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11462 |         return unicode_empty; | 
| Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11463 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11464 |  | 
| Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11465 |     if (len == 1 && PyUnicode_CheckExact(str)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11466 |         /* no repeat, return original string */ | 
 | 11467 |         Py_INCREF(str); | 
 | 11468 |         return (PyObject*) str; | 
 | 11469 |     } | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11470 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11471 |     if (PyUnicode_READY(str) == -1) | 
 | 11472 |         return NULL; | 
 | 11473 |  | 
| Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11474 |     if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11475 |         PyErr_SetString(PyExc_OverflowError, | 
 | 11476 |                         "repeated string is too long"); | 
 | 11477 |         return NULL; | 
 | 11478 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11479 |     nchars = len * PyUnicode_GET_LENGTH(str); | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11480 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11481 |     u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11482 |     if (!u) | 
 | 11483 |         return NULL; | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11484 |     assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11485 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11486 |     if (PyUnicode_GET_LENGTH(str) == 1) { | 
 | 11487 |         const int kind = PyUnicode_KIND(str); | 
 | 11488 |         const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); | 
 | 11489 |         void *to = PyUnicode_DATA(u); | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11490 |         if (kind == PyUnicode_1BYTE_KIND) | 
 | 11491 |             memset(to, (unsigned char)fill_char, len); | 
 | 11492 |         else { | 
 | 11493 |             for (n = 0; n < len; ++n) | 
 | 11494 |                 PyUnicode_WRITE(kind, to, n, fill_char); | 
 | 11495 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11496 |     } | 
 | 11497 |     else { | 
 | 11498 |         /* number of characters copied this far */ | 
 | 11499 |         Py_ssize_t done = PyUnicode_GET_LENGTH(str); | 
 | 11500 |         const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); | 
 | 11501 |         char *to = (char *) PyUnicode_DATA(u); | 
 | 11502 |         Py_MEMCPY(to, PyUnicode_DATA(str), | 
 | 11503 |                   PyUnicode_GET_LENGTH(str) * char_size); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11504 |         while (done < nchars) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 |             n = (done <= nchars-done) ? done : nchars-done; | 
 | 11506 |             Py_MEMCPY(to + (done * char_size), to, n * char_size); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11507 |             done += n; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11508 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11509 |     } | 
 | 11510 |  | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11511 |     assert(_PyUnicode_CheckConsistency(u, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11512 |     return (PyObject*) u; | 
 | 11513 | } | 
 | 11514 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11515 | PyObject * | 
 | 11516 | PyUnicode_Replace(PyObject *obj, | 
 | 11517 |                   PyObject *subobj, | 
 | 11518 |                   PyObject *replobj, | 
 | 11519 |                   Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | { | 
 | 11521 |     PyObject *self; | 
 | 11522 |     PyObject *str1; | 
 | 11523 |     PyObject *str2; | 
 | 11524 |     PyObject *result; | 
 | 11525 |  | 
 | 11526 |     self = PyUnicode_FromObject(obj); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11527 |     if (self == NULL || PyUnicode_READY(self) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11528 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11529 |     str1 = PyUnicode_FromObject(subobj); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11530 |     if (str1 == NULL || PyUnicode_READY(str1) == -1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11531 |         Py_DECREF(self); | 
 | 11532 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 |     } | 
 | 11534 |     str2 = PyUnicode_FromObject(replobj); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11535 |     if (str2 == NULL || PyUnicode_READY(str2)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11536 |         Py_DECREF(self); | 
 | 11537 |         Py_DECREF(str1); | 
 | 11538 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11540 |     result = replace(self, str1, str2, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11541 |     Py_DECREF(self); | 
 | 11542 |     Py_DECREF(str1); | 
 | 11543 |     Py_DECREF(str2); | 
 | 11544 |     return result; | 
 | 11545 | } | 
 | 11546 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11547 | PyDoc_STRVAR(replace__doc__, | 
| Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11548 |              "S.replace(old, new[, count]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11549 | \n\ | 
 | 11550 | Return a copy of S with all occurrences of substring\n\ | 
| Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11551 | old replaced by new.  If the optional argument count is\n\ | 
 | 11552 | given, only the first count occurrences are replaced."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11553 |  | 
 | 11554 | static PyObject* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11555 | unicode_replace(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11556 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11557 |     PyObject *str1; | 
 | 11558 |     PyObject *str2; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11559 |     Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11560 |     PyObject *result; | 
 | 11561 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11562 |     if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11563 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11564 |     if (!PyUnicode_READY(self) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11565 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11566 |     str1 = PyUnicode_FromObject(str1); | 
 | 11567 |     if (str1 == NULL || PyUnicode_READY(str1) == -1) | 
 | 11568 |         return NULL; | 
 | 11569 |     str2 = PyUnicode_FromObject(str2); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11570 |     if (str2 == NULL || PyUnicode_READY(str2) == -1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11571 |         Py_DECREF(str1); | 
 | 11572 |         return NULL; | 
| Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11573 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11574 |  | 
 | 11575 |     result = replace(self, str1, str2, maxcount); | 
 | 11576 |  | 
 | 11577 |     Py_DECREF(str1); | 
 | 11578 |     Py_DECREF(str2); | 
 | 11579 |     return result; | 
 | 11580 | } | 
 | 11581 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11582 | static PyObject * | 
 | 11583 | unicode_repr(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11584 | { | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11585 |     PyObject *repr; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11586 |     Py_ssize_t isize; | 
 | 11587 |     Py_ssize_t osize, squote, dquote, i, o; | 
 | 11588 |     Py_UCS4 max, quote; | 
 | 11589 |     int ikind, okind; | 
 | 11590 |     void *idata, *odata; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11591 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11592 |     if (PyUnicode_READY(unicode) == -1) | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11593 |         return NULL; | 
 | 11594 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11595 |     isize = PyUnicode_GET_LENGTH(unicode); | 
 | 11596 |     idata = PyUnicode_DATA(unicode); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11597 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11598 |     /* Compute length of output, quote characters, and | 
 | 11599 |        maximum character */ | 
 | 11600 |     osize = 2; /* quotes */ | 
 | 11601 |     max = 127; | 
 | 11602 |     squote = dquote = 0; | 
 | 11603 |     ikind = PyUnicode_KIND(unicode); | 
 | 11604 |     for (i = 0; i < isize; i++) { | 
 | 11605 |         Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); | 
 | 11606 |         switch (ch) { | 
 | 11607 |         case '\'': squote++; osize++; break; | 
 | 11608 |         case '"':  dquote++; osize++; break; | 
 | 11609 |         case '\\': case '\t': case '\r': case '\n': | 
 | 11610 |             osize += 2; break; | 
 | 11611 |         default: | 
 | 11612 |             /* Fast-path ASCII */ | 
 | 11613 |             if (ch < ' ' || ch == 0x7f) | 
 | 11614 |                 osize += 4; /* \xHH */ | 
 | 11615 |             else if (ch < 0x7f) | 
 | 11616 |                 osize++; | 
 | 11617 |             else if (Py_UNICODE_ISPRINTABLE(ch)) { | 
 | 11618 |                 osize++; | 
 | 11619 |                 max = ch > max ? ch : max; | 
 | 11620 |             } | 
 | 11621 |             else if (ch < 0x100) | 
 | 11622 |                 osize += 4; /* \xHH */ | 
 | 11623 |             else if (ch < 0x10000) | 
 | 11624 |                 osize += 6; /* \uHHHH */ | 
 | 11625 |             else | 
 | 11626 |                 osize += 10; /* \uHHHHHHHH */ | 
 | 11627 |         } | 
 | 11628 |     } | 
 | 11629 |  | 
 | 11630 |     quote = '\''; | 
 | 11631 |     if (squote) { | 
 | 11632 |         if (dquote) | 
 | 11633 |             /* Both squote and dquote present. Use squote, | 
 | 11634 |                and escape them */ | 
 | 11635 |             osize += squote; | 
 | 11636 |         else | 
 | 11637 |             quote = '"'; | 
 | 11638 |     } | 
 | 11639 |  | 
 | 11640 |     repr = PyUnicode_New(osize, max); | 
 | 11641 |     if (repr == NULL) | 
 | 11642 |         return NULL; | 
 | 11643 |     okind = PyUnicode_KIND(repr); | 
 | 11644 |     odata = PyUnicode_DATA(repr); | 
 | 11645 |  | 
 | 11646 |     PyUnicode_WRITE(okind, odata, 0, quote); | 
 | 11647 |     PyUnicode_WRITE(okind, odata, osize-1, quote); | 
 | 11648 |  | 
 | 11649 |     for (i = 0, o = 1; i < isize; i++) { | 
 | 11650 |         Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11651 |  | 
 | 11652 |         /* Escape quotes and backslashes */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11653 |         if ((ch == quote) || (ch == '\\')) { | 
 | 11654 |             PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 11655 |             PyUnicode_WRITE(okind, odata, o++, ch); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11656 |             continue; | 
 | 11657 |         } | 
 | 11658 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11659 |         /* Map special whitespace to '\t', \n', '\r' */ | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11660 |         if (ch == '\t') { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11661 |             PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 11662 |             PyUnicode_WRITE(okind, odata, o++, 't'); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11663 |         } | 
 | 11664 |         else if (ch == '\n') { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11665 |             PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 11666 |             PyUnicode_WRITE(okind, odata, o++, 'n'); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11667 |         } | 
 | 11668 |         else if (ch == '\r') { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11669 |             PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 11670 |             PyUnicode_WRITE(okind, odata, o++, 'r'); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11671 |         } | 
 | 11672 |  | 
 | 11673 |         /* Map non-printable US ASCII to '\xhh' */ | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11674 |         else if (ch < ' ' || ch == 0x7F) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11675 |             PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 11676 |             PyUnicode_WRITE(okind, odata, o++, 'x'); | 
 | 11677 |             PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); | 
 | 11678 |             PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11679 |         } | 
 | 11680 |  | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11681 |         /* Copy ASCII characters as-is */ | 
 | 11682 |         else if (ch < 0x7F) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11683 |             PyUnicode_WRITE(okind, odata, o++, ch); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11684 |         } | 
 | 11685 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11686 |         /* Non-ASCII characters */ | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11687 |         else { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11688 |             /* Map Unicode whitespace and control characters | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11689 |                (categories Z* and C* except ASCII space) | 
 | 11690 |             */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11691 |             if (!Py_UNICODE_ISPRINTABLE(ch)) { | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11692 |                 /* Map 8-bit characters to '\xhh' */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11693 |                 if (ch <= 0xff) { | 
 | 11694 |                     PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 11695 |                     PyUnicode_WRITE(okind, odata, o++, 'x'); | 
 | 11696 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); | 
 | 11697 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11698 |                 } | 
 | 11699 |                 /* Map 21-bit characters to '\U00xxxxxx' */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11700 |                 else if (ch >= 0x10000) { | 
 | 11701 |                     PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 11702 |                     PyUnicode_WRITE(okind, odata, o++, 'U'); | 
 | 11703 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); | 
 | 11704 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); | 
 | 11705 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); | 
 | 11706 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); | 
 | 11707 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); | 
 | 11708 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); | 
 | 11709 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); | 
 | 11710 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11711 |                 } | 
 | 11712 |                 /* Map 16-bit characters to '\uxxxx' */ | 
 | 11713 |                 else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11714 |                     PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 11715 |                     PyUnicode_WRITE(okind, odata, o++, 'u'); | 
 | 11716 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); | 
 | 11717 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); | 
 | 11718 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); | 
 | 11719 |                     PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11720 |                 } | 
 | 11721 |             } | 
 | 11722 |             /* Copy characters as-is */ | 
 | 11723 |             else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11724 |                 PyUnicode_WRITE(okind, odata, o++, ch); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11725 |             } | 
 | 11726 |         } | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11727 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11728 |     /* Closing quote already added at the beginning */ | 
| Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 11729 |     assert(_PyUnicode_CheckConsistency(repr, 1)); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11730 |     return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11731 | } | 
 | 11732 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11733 | PyDoc_STRVAR(rfind__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11734 |              "S.rfind(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11735 | \n\ | 
 | 11736 | Return the highest index in S where substring sub is found,\n\ | 
| Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11737 | such that sub is contained within S[start:end].  Optional\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11738 | arguments start and end are interpreted as in slice notation.\n\ | 
 | 11739 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11740 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11741 |  | 
 | 11742 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11743 | unicode_rfind(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11744 | { | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11745 |     PyUnicodeObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11746 |     Py_ssize_t start; | 
 | 11747 |     Py_ssize_t end; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11748 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11749 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11750 |     if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, | 
 | 11751 |                                             &start, &end)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11752 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11753 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11754 |     if (PyUnicode_READY(self) == -1) | 
 | 11755 |         return NULL; | 
 | 11756 |     if (PyUnicode_READY(substring) == -1) | 
 | 11757 |         return NULL; | 
 | 11758 |  | 
 | 11759 |     result = any_find_slice( | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11760 |         asciilib_rfind_slice, ucs1lib_rfind_slice, | 
 | 11761 |         ucs2lib_rfind_slice, ucs4lib_rfind_slice, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11762 |         self, (PyObject*)substring, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11763 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11764 |  | 
 | 11765 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11766 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11767 |     if (result == -2) | 
 | 11768 |         return NULL; | 
 | 11769 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11770 |     return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | } | 
 | 11772 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11773 | PyDoc_STRVAR(rindex__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11774 |              "S.rindex(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11775 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11776 | 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] | 11777 |  | 
 | 11778 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11779 | unicode_rindex(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11780 | { | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11781 |     PyUnicodeObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11782 |     Py_ssize_t start; | 
 | 11783 |     Py_ssize_t end; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11784 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11785 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11786 |     if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, | 
 | 11787 |                                             &start, &end)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11788 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11789 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11790 |     if (PyUnicode_READY(self) == -1) | 
 | 11791 |         return NULL; | 
 | 11792 |     if (PyUnicode_READY(substring) == -1) | 
 | 11793 |         return NULL; | 
 | 11794 |  | 
 | 11795 |     result = any_find_slice( | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11796 |         asciilib_rfind_slice, ucs1lib_rfind_slice, | 
 | 11797 |         ucs2lib_rfind_slice, ucs4lib_rfind_slice, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11798 |         self, (PyObject*)substring, start, end | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11799 |         ); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11800 |  | 
 | 11801 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11802 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11803 |     if (result == -2) | 
 | 11804 |         return NULL; | 
 | 11805 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11806 |     if (result < 0) { | 
 | 11807 |         PyErr_SetString(PyExc_ValueError, "substring not found"); | 
 | 11808 |         return NULL; | 
 | 11809 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11810 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11811 |     return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11812 | } | 
 | 11813 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11814 | PyDoc_STRVAR(rjust__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11815 |              "S.rjust(width[, fillchar]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | \n\ | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11817 | Return S right-justified in a string of length width. Padding is\n\ | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11818 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 |  | 
 | 11820 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11821 | unicode_rjust(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11822 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11823 |     Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11824 |     Py_UCS4 fillchar = ' '; | 
 | 11825 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11826 |     if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11827 |         return NULL; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11828 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11829 |     if (PyUnicode_READY(self) == -1) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11830 |         return NULL; | 
 | 11831 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11832 |     if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11833 |         Py_INCREF(self); | 
 | 11834 |         return (PyObject*) self; | 
 | 11835 |     } | 
 | 11836 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11837 |     return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11838 | } | 
 | 11839 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11840 | PyObject * | 
 | 11841 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11842 | { | 
 | 11843 |     PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11844 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11845 |     s = PyUnicode_FromObject(s); | 
 | 11846 |     if (s == NULL) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11847 |         return NULL; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11848 |     if (sep != NULL) { | 
 | 11849 |         sep = PyUnicode_FromObject(sep); | 
 | 11850 |         if (sep == NULL) { | 
 | 11851 |             Py_DECREF(s); | 
 | 11852 |             return NULL; | 
 | 11853 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11854 |     } | 
 | 11855 |  | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11856 |     result = split(s, sep, maxsplit); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11857 |  | 
 | 11858 |     Py_DECREF(s); | 
 | 11859 |     Py_XDECREF(sep); | 
 | 11860 |     return result; | 
 | 11861 | } | 
 | 11862 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11863 | PyDoc_STRVAR(split__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11864 |              "S.split([sep[, maxsplit]]) -> list of strings\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11865 | \n\ | 
 | 11866 | Return a list of the words in S, using sep as the\n\ | 
 | 11867 | delimiter string.  If maxsplit is given, at most maxsplit\n\ | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11868 | splits are done. If sep is not specified or is None, any\n\ | 
| Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 11869 | whitespace string is a separator and empty strings are\n\ | 
 | 11870 | removed from the result."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11871 |  | 
 | 11872 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11873 | unicode_split(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11874 | { | 
 | 11875 |     PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11876 |     Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11877 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11878 |     if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11879 |         return NULL; | 
 | 11880 |  | 
 | 11881 |     if (substring == Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11882 |         return split(self, NULL, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11883 |     else if (PyUnicode_Check(substring)) | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11884 |         return split(self, substring, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11885 |     else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11886 |         return PyUnicode_Split((PyObject *)self, substring, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11887 | } | 
 | 11888 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11889 | PyObject * | 
 | 11890 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) | 
 | 11891 | { | 
 | 11892 |     PyObject* str_obj; | 
 | 11893 |     PyObject* sep_obj; | 
 | 11894 |     PyObject* out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11895 |     int kind1, kind2, kind; | 
 | 11896 |     void *buf1 = NULL, *buf2 = NULL; | 
 | 11897 |     Py_ssize_t len1, len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11898 |  | 
 | 11899 |     str_obj = PyUnicode_FromObject(str_in); | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11900 |     if (!str_obj || PyUnicode_READY(str_obj) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11901 |         return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11902 |     sep_obj = PyUnicode_FromObject(sep_in); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11903 |     if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11904 |         Py_DECREF(str_obj); | 
 | 11905 |         return NULL; | 
 | 11906 |     } | 
 | 11907 |  | 
| Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11908 |     kind1 = PyUnicode_KIND(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11909 |     kind2 = PyUnicode_KIND(sep_obj); | 
| Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11910 |     kind = Py_MAX(kind1, kind2); | 
 | 11911 |     buf1 = PyUnicode_DATA(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 |     if (kind1 != kind) | 
| Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11913 |         buf1 = _PyUnicode_AsKind(str_obj, kind); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11914 |     if (!buf1) | 
 | 11915 |         goto onError; | 
 | 11916 |     buf2 = PyUnicode_DATA(sep_obj); | 
 | 11917 |     if (kind2 != kind) | 
 | 11918 |         buf2 = _PyUnicode_AsKind(sep_obj, kind); | 
 | 11919 |     if (!buf2) | 
 | 11920 |         goto onError; | 
 | 11921 |     len1 = PyUnicode_GET_LENGTH(str_obj); | 
 | 11922 |     len2 = PyUnicode_GET_LENGTH(sep_obj); | 
 | 11923 |  | 
| Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11924 |     switch(PyUnicode_KIND(str_obj)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11925 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11926 |         if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) | 
 | 11927 |             out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 11928 |         else | 
 | 11929 |             out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11930 |         break; | 
 | 11931 |     case PyUnicode_2BYTE_KIND: | 
 | 11932 |         out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 11933 |         break; | 
 | 11934 |     case PyUnicode_4BYTE_KIND: | 
 | 11935 |         out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 11936 |         break; | 
 | 11937 |     default: | 
 | 11938 |         assert(0); | 
 | 11939 |         out = 0; | 
 | 11940 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11941 |  | 
 | 11942 |     Py_DECREF(sep_obj); | 
 | 11943 |     Py_DECREF(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11944 |     if (kind1 != kind) | 
 | 11945 |         PyMem_Free(buf1); | 
 | 11946 |     if (kind2 != kind) | 
 | 11947 |         PyMem_Free(buf2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11948 |  | 
 | 11949 |     return out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11950 |   onError: | 
 | 11951 |     Py_DECREF(sep_obj); | 
 | 11952 |     Py_DECREF(str_obj); | 
 | 11953 |     if (kind1 != kind && buf1) | 
 | 11954 |         PyMem_Free(buf1); | 
 | 11955 |     if (kind2 != kind && buf2) | 
 | 11956 |         PyMem_Free(buf2); | 
 | 11957 |     return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11958 | } | 
 | 11959 |  | 
 | 11960 |  | 
 | 11961 | PyObject * | 
 | 11962 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) | 
 | 11963 | { | 
 | 11964 |     PyObject* str_obj; | 
 | 11965 |     PyObject* sep_obj; | 
 | 11966 |     PyObject* out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11967 |     int kind1, kind2, kind; | 
 | 11968 |     void *buf1 = NULL, *buf2 = NULL; | 
 | 11969 |     Py_ssize_t len1, len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11970 |  | 
 | 11971 |     str_obj = PyUnicode_FromObject(str_in); | 
 | 11972 |     if (!str_obj) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11973 |         return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11974 |     sep_obj = PyUnicode_FromObject(sep_in); | 
 | 11975 |     if (!sep_obj) { | 
 | 11976 |         Py_DECREF(str_obj); | 
 | 11977 |         return NULL; | 
 | 11978 |     } | 
 | 11979 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11980 |     kind1 = PyUnicode_KIND(str_in); | 
 | 11981 |     kind2 = PyUnicode_KIND(sep_obj); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11982 |     kind = Py_MAX(kind1, kind2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11983 |     buf1 = PyUnicode_DATA(str_in); | 
 | 11984 |     if (kind1 != kind) | 
 | 11985 |         buf1 = _PyUnicode_AsKind(str_in, kind); | 
 | 11986 |     if (!buf1) | 
 | 11987 |         goto onError; | 
 | 11988 |     buf2 = PyUnicode_DATA(sep_obj); | 
 | 11989 |     if (kind2 != kind) | 
 | 11990 |         buf2 = _PyUnicode_AsKind(sep_obj, kind); | 
 | 11991 |     if (!buf2) | 
 | 11992 |         goto onError; | 
 | 11993 |     len1 = PyUnicode_GET_LENGTH(str_obj); | 
 | 11994 |     len2 = PyUnicode_GET_LENGTH(sep_obj); | 
 | 11995 |  | 
 | 11996 |     switch(PyUnicode_KIND(str_in)) { | 
 | 11997 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11998 |         if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) | 
 | 11999 |             out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 12000 |         else | 
 | 12001 |             out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12002 |         break; | 
 | 12003 |     case PyUnicode_2BYTE_KIND: | 
 | 12004 |         out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 12005 |         break; | 
 | 12006 |     case PyUnicode_4BYTE_KIND: | 
 | 12007 |         out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 12008 |         break; | 
 | 12009 |     default: | 
 | 12010 |         assert(0); | 
 | 12011 |         out = 0; | 
 | 12012 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12013 |  | 
 | 12014 |     Py_DECREF(sep_obj); | 
 | 12015 |     Py_DECREF(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12016 |     if (kind1 != kind) | 
 | 12017 |         PyMem_Free(buf1); | 
 | 12018 |     if (kind2 != kind) | 
 | 12019 |         PyMem_Free(buf2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12020 |  | 
 | 12021 |     return out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12022 |   onError: | 
 | 12023 |     Py_DECREF(sep_obj); | 
 | 12024 |     Py_DECREF(str_obj); | 
 | 12025 |     if (kind1 != kind && buf1) | 
 | 12026 |         PyMem_Free(buf1); | 
 | 12027 |     if (kind2 != kind && buf2) | 
 | 12028 |         PyMem_Free(buf2); | 
 | 12029 |     return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12030 | } | 
 | 12031 |  | 
 | 12032 | PyDoc_STRVAR(partition__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12033 |              "S.partition(sep) -> (head, sep, tail)\n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12034 | \n\ | 
| Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12035 | Search for the separator sep in S, and return the part before it,\n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12036 | the separator itself, and the part after it.  If the separator is not\n\ | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12037 | found, return S and two empty strings."); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12038 |  | 
 | 12039 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12040 | unicode_partition(PyObject *self, PyObject *separator) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12041 | { | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12042 |     return PyUnicode_Partition(self, separator); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12043 | } | 
 | 12044 |  | 
 | 12045 | PyDoc_STRVAR(rpartition__doc__, | 
| Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12046 |              "S.rpartition(sep) -> (head, sep, tail)\n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12047 | \n\ | 
| Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12048 | Search for the separator sep in S, starting at the end of S, and return\n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12049 | the part before it, the separator itself, and the part after it.  If the\n\ | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12050 | separator is not found, return two empty strings and S."); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12051 |  | 
 | 12052 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12053 | unicode_rpartition(PyObject *self, PyObject *separator) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12054 | { | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12055 |     return PyUnicode_RPartition(self, separator); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12056 | } | 
 | 12057 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12058 | PyObject * | 
 | 12059 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12060 | { | 
 | 12061 |     PyObject *result; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12062 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12063 |     s = PyUnicode_FromObject(s); | 
 | 12064 |     if (s == NULL) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12065 |         return NULL; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12066 |     if (sep != NULL) { | 
 | 12067 |         sep = PyUnicode_FromObject(sep); | 
 | 12068 |         if (sep == NULL) { | 
 | 12069 |             Py_DECREF(s); | 
 | 12070 |             return NULL; | 
 | 12071 |         } | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12072 |     } | 
 | 12073 |  | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12074 |     result = rsplit(s, sep, maxsplit); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12075 |  | 
 | 12076 |     Py_DECREF(s); | 
 | 12077 |     Py_XDECREF(sep); | 
 | 12078 |     return result; | 
 | 12079 | } | 
 | 12080 |  | 
 | 12081 | PyDoc_STRVAR(rsplit__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12082 |              "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12083 | \n\ | 
 | 12084 | Return a list of the words in S, using sep as the\n\ | 
 | 12085 | delimiter string, starting at the end of the string and\n\ | 
 | 12086 | working to the front.  If maxsplit is given, at most maxsplit\n\ | 
 | 12087 | splits are done. If sep is not specified, any whitespace string\n\ | 
 | 12088 | is a separator."); | 
 | 12089 |  | 
 | 12090 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12091 | unicode_rsplit(PyObject *self, PyObject *args) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12092 | { | 
 | 12093 |     PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12094 |     Py_ssize_t maxcount = -1; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12095 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12096 |     if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12097 |         return NULL; | 
 | 12098 |  | 
 | 12099 |     if (substring == Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12100 |         return rsplit(self, NULL, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12101 |     else if (PyUnicode_Check(substring)) | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12102 |         return rsplit(self, substring, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12103 |     else | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12104 |         return PyUnicode_RSplit(self, substring, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12105 | } | 
 | 12106 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12107 | PyDoc_STRVAR(splitlines__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12108 |              "S.splitlines([keepends]) -> list of strings\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12109 | \n\ | 
 | 12110 | 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] | 12111 | 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] | 12112 | is given and true."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12113 |  | 
 | 12114 | static PyObject* | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12115 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12116 | { | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12117 |     static char *kwlist[] = {"keepends", 0}; | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12118 |     int keepends = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12119 |  | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12120 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", | 
 | 12121 |                                      kwlist, &keepends)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12122 |         return NULL; | 
 | 12123 |  | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12124 |     return PyUnicode_Splitlines((PyObject *)self, keepends); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12125 | } | 
 | 12126 |  | 
 | 12127 | static | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12128 | PyObject *unicode_str(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12129 | { | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12130 |     if (PyUnicode_CheckExact(self)) { | 
 | 12131 |         Py_INCREF(self); | 
 | 12132 |         return self; | 
 | 12133 |     } else | 
 | 12134 |         /* Subtype -- return genuine unicode string with the same value. */ | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12135 |         return PyUnicode_Copy(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12136 | } | 
 | 12137 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12138 | PyDoc_STRVAR(swapcase__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12139 |              "S.swapcase() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12140 | \n\ | 
 | 12141 | 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] | 12142 | and vice versa."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12143 |  | 
 | 12144 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12145 | unicode_swapcase(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12146 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12147 |     return fixup(self, fixswapcase); | 
 | 12148 | } | 
 | 12149 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12150 | PyDoc_STRVAR(maketrans__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12151 |              "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12152 | \n\ | 
 | 12153 | Return a translation table usable for str.translate().\n\ | 
 | 12154 | If there is only one argument, it must be a dictionary mapping Unicode\n\ | 
 | 12155 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12156 | Character keys will be then converted to ordinals.\n\ | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12157 | If there are two arguments, they must be strings of equal length, and\n\ | 
 | 12158 | in the resulting dictionary, each character in x will be mapped to the\n\ | 
 | 12159 | character at the same position in y. If there is a third argument, it\n\ | 
 | 12160 | must be a string, whose characters will be mapped to None in the result."); | 
 | 12161 |  | 
 | 12162 | static PyObject* | 
 | 12163 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) | 
 | 12164 | { | 
 | 12165 |     PyObject *x, *y = NULL, *z = NULL; | 
 | 12166 |     PyObject *new = NULL, *key, *value; | 
 | 12167 |     Py_ssize_t i = 0; | 
 | 12168 |     int res; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12169 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12170 |     if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) | 
 | 12171 |         return NULL; | 
 | 12172 |     new = PyDict_New(); | 
 | 12173 |     if (!new) | 
 | 12174 |         return NULL; | 
 | 12175 |     if (y != NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12176 |         int x_kind, y_kind, z_kind; | 
 | 12177 |         void *x_data, *y_data, *z_data; | 
 | 12178 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12179 |         /* x must be a string too, of equal length */ | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12180 |         if (!PyUnicode_Check(x)) { | 
 | 12181 |             PyErr_SetString(PyExc_TypeError, "first maketrans argument must " | 
 | 12182 |                             "be a string if there is a second argument"); | 
 | 12183 |             goto err; | 
 | 12184 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12185 |         if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12186 |             PyErr_SetString(PyExc_ValueError, "the first two maketrans " | 
 | 12187 |                             "arguments must have equal length"); | 
 | 12188 |             goto err; | 
 | 12189 |         } | 
 | 12190 |         /* create entries for translating chars in x to those in y */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12191 |         x_kind = PyUnicode_KIND(x); | 
 | 12192 |         y_kind = PyUnicode_KIND(y); | 
 | 12193 |         x_data = PyUnicode_DATA(x); | 
 | 12194 |         y_data = PyUnicode_DATA(y); | 
 | 12195 |         for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { | 
 | 12196 |             key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); | 
 | 12197 |             value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12198 |             if (!key || !value) | 
 | 12199 |                 goto err; | 
 | 12200 |             res = PyDict_SetItem(new, key, value); | 
 | 12201 |             Py_DECREF(key); | 
 | 12202 |             Py_DECREF(value); | 
 | 12203 |             if (res < 0) | 
 | 12204 |                 goto err; | 
 | 12205 |         } | 
 | 12206 |         /* create entries for deleting chars in z */ | 
 | 12207 |         if (z != NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12208 |             z_kind = PyUnicode_KIND(z); | 
 | 12209 |             z_data = PyUnicode_DATA(z); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12210 |             for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12211 |                 key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12212 |                 if (!key) | 
 | 12213 |                     goto err; | 
 | 12214 |                 res = PyDict_SetItem(new, key, Py_None); | 
 | 12215 |                 Py_DECREF(key); | 
 | 12216 |                 if (res < 0) | 
 | 12217 |                     goto err; | 
 | 12218 |             } | 
 | 12219 |         } | 
 | 12220 |     } else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12221 |         int kind; | 
 | 12222 |         void *data; | 
 | 12223 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12224 |         /* x must be a dict */ | 
| Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12225 |         if (!PyDict_CheckExact(x)) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12226 |             PyErr_SetString(PyExc_TypeError, "if you give only one argument " | 
 | 12227 |                             "to maketrans it must be a dict"); | 
 | 12228 |             goto err; | 
 | 12229 |         } | 
 | 12230 |         /* copy entries into the new dict, converting string keys to int keys */ | 
 | 12231 |         while (PyDict_Next(x, &i, &key, &value)) { | 
 | 12232 |             if (PyUnicode_Check(key)) { | 
 | 12233 |                 /* convert string keys to integer keys */ | 
 | 12234 |                 PyObject *newkey; | 
 | 12235 |                 if (PyUnicode_GET_SIZE(key) != 1) { | 
 | 12236 |                     PyErr_SetString(PyExc_ValueError, "string keys in translate " | 
 | 12237 |                                     "table must be of length 1"); | 
 | 12238 |                     goto err; | 
 | 12239 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12240 |                 kind = PyUnicode_KIND(key); | 
 | 12241 |                 data = PyUnicode_DATA(key); | 
 | 12242 |                 newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12243 |                 if (!newkey) | 
 | 12244 |                     goto err; | 
 | 12245 |                 res = PyDict_SetItem(new, newkey, value); | 
 | 12246 |                 Py_DECREF(newkey); | 
 | 12247 |                 if (res < 0) | 
 | 12248 |                     goto err; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12249 |             } else if (PyLong_Check(key)) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12250 |                 /* just keep integer keys */ | 
 | 12251 |                 if (PyDict_SetItem(new, key, value) < 0) | 
 | 12252 |                     goto err; | 
 | 12253 |             } else { | 
 | 12254 |                 PyErr_SetString(PyExc_TypeError, "keys in translate table must " | 
 | 12255 |                                 "be strings or integers"); | 
 | 12256 |                 goto err; | 
 | 12257 |             } | 
 | 12258 |         } | 
 | 12259 |     } | 
 | 12260 |     return new; | 
 | 12261 |   err: | 
 | 12262 |     Py_DECREF(new); | 
 | 12263 |     return NULL; | 
 | 12264 | } | 
 | 12265 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12266 | PyDoc_STRVAR(translate__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12267 |              "S.translate(table) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | \n\ | 
 | 12269 | Return a copy of the string S, where all characters have been mapped\n\ | 
 | 12270 | through the given translation table, which must be a mapping of\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12271 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ | 
| Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12272 | Unmapped characters are left untouched. Characters mapped to None\n\ | 
 | 12273 | are deleted."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12274 |  | 
 | 12275 | static PyObject* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12276 | unicode_translate(PyObject *self, PyObject *table) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12277 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12278 |     return _PyUnicode_TranslateCharmap(self, table, "ignore"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12279 | } | 
 | 12280 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12281 | PyDoc_STRVAR(upper__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12282 |              "S.upper() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12284 | Return a copy of S converted to uppercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 |  | 
 | 12286 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12287 | unicode_upper(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12288 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12289 |     return fixup(self, fixupper); | 
 | 12290 | } | 
 | 12291 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12292 | PyDoc_STRVAR(zfill__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12293 |              "S.zfill(width) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12294 | \n\ | 
| Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12295 | Pad a numeric string S with zeros on the left, to fill a field\n\ | 
 | 12296 | of the specified width. The string S is never truncated."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12297 |  | 
 | 12298 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12299 | unicode_zfill(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12300 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12301 |     Py_ssize_t fill; | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12302 |     PyObject *u; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12303 |     Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12304 |     int kind; | 
 | 12305 |     void *data; | 
 | 12306 |     Py_UCS4 chr; | 
 | 12307 |  | 
 | 12308 |     if (PyUnicode_READY(self) == -1) | 
 | 12309 |         return NULL; | 
 | 12310 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12311 |     if (!PyArg_ParseTuple(args, "n:zfill", &width)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12312 |         return NULL; | 
 | 12313 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12314 |     if (PyUnicode_GET_LENGTH(self) >= width) { | 
| Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12315 |         if (PyUnicode_CheckExact(self)) { | 
 | 12316 |             Py_INCREF(self); | 
 | 12317 |             return (PyObject*) self; | 
 | 12318 |         } | 
 | 12319 |         else | 
| Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 12320 |             return PyUnicode_Copy((PyObject*)self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12321 |     } | 
 | 12322 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12323 |     fill = width - _PyUnicode_LENGTH(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12324 |  | 
 | 12325 |     u = pad(self, fill, 0, '0'); | 
 | 12326 |  | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12327 |     if (u == NULL) | 
 | 12328 |         return NULL; | 
 | 12329 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12330 |     kind = PyUnicode_KIND(u); | 
 | 12331 |     data = PyUnicode_DATA(u); | 
 | 12332 |     chr = PyUnicode_READ(kind, data, fill); | 
 | 12333 |  | 
 | 12334 |     if (chr == '+' || chr == '-') { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12335 |         /* move sign to beginning of string */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12336 |         PyUnicode_WRITE(kind, data, 0, chr); | 
 | 12337 |         PyUnicode_WRITE(kind, data, fill, '0'); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12338 |     } | 
 | 12339 |  | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12340 |     assert(_PyUnicode_CheckConsistency(u, 1)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12341 |     return (PyObject*) u; | 
 | 12342 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12343 |  | 
 | 12344 | #if 0 | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12345 | static PyObject * | 
 | 12346 | unicode__decimal2ascii(PyObject *self) | 
 | 12347 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12348 |     return PyUnicode_TransformDecimalAndSpaceToASCII(self); | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12349 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12350 | #endif | 
 | 12351 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12352 | PyDoc_STRVAR(startswith__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12353 |              "S.startswith(prefix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12354 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12355 | Return True if S starts with the specified prefix, False otherwise.\n\ | 
 | 12356 | With optional start, test S beginning at that position.\n\ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12357 | With optional end, stop comparing S at that position.\n\ | 
 | 12358 | prefix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12359 |  | 
 | 12360 | static PyObject * | 
 | 12361 | unicode_startswith(PyUnicodeObject *self, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12362 |                    PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12363 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12364 |     PyObject *subobj; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12365 |     PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12366 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12367 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12368 |     int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12369 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12370 |     if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12371 |         return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12372 |     if (PyTuple_Check(subobj)) { | 
 | 12373 |         Py_ssize_t i; | 
 | 12374 |         for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
 | 12375 |             substring = (PyUnicodeObject *)PyUnicode_FromObject( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12376 |                 PyTuple_GET_ITEM(subobj, i)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12377 |             if (substring == NULL) | 
 | 12378 |                 return NULL; | 
 | 12379 |             result = tailmatch(self, substring, start, end, -1); | 
 | 12380 |             Py_DECREF(substring); | 
 | 12381 |             if (result) { | 
 | 12382 |                 Py_RETURN_TRUE; | 
 | 12383 |             } | 
 | 12384 |         } | 
 | 12385 |         /* nothing matched */ | 
 | 12386 |         Py_RETURN_FALSE; | 
 | 12387 |     } | 
 | 12388 |     substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12389 |     if (substring == NULL) { | 
 | 12390 |         if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 12391 |             PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " | 
 | 12392 |                          "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12393 |         return NULL; | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12394 |     } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12395 |     result = tailmatch(self, substring, start, end, -1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12396 |     Py_DECREF(substring); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12397 |     return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12398 | } | 
 | 12399 |  | 
 | 12400 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12401 | PyDoc_STRVAR(endswith__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12402 |              "S.endswith(suffix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12403 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12404 | Return True if S ends with the specified suffix, False otherwise.\n\ | 
 | 12405 | With optional start, test S beginning at that position.\n\ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12406 | With optional end, stop comparing S at that position.\n\ | 
 | 12407 | suffix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12408 |  | 
 | 12409 | static PyObject * | 
 | 12410 | unicode_endswith(PyUnicodeObject *self, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12411 |                  PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12412 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12413 |     PyObject *subobj; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12414 |     PyUnicodeObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12415 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12416 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12417 |     int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12418 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12419 |     if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12420 |         return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12421 |     if (PyTuple_Check(subobj)) { | 
 | 12422 |         Py_ssize_t i; | 
 | 12423 |         for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
 | 12424 |             substring = (PyUnicodeObject *)PyUnicode_FromObject( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12425 |                 PyTuple_GET_ITEM(subobj, i)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12426 |             if (substring == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12427 |                 return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12428 |             result = tailmatch(self, substring, start, end, +1); | 
 | 12429 |             Py_DECREF(substring); | 
 | 12430 |             if (result) { | 
 | 12431 |                 Py_RETURN_TRUE; | 
 | 12432 |             } | 
 | 12433 |         } | 
 | 12434 |         Py_RETURN_FALSE; | 
 | 12435 |     } | 
 | 12436 |     substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12437 |     if (substring == NULL) { | 
 | 12438 |         if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 12439 |             PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " | 
 | 12440 |                          "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12441 |         return NULL; | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12442 |     } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12443 |     result = tailmatch(self, substring, start, end, +1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12444 |     Py_DECREF(substring); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12445 |     return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12446 | } | 
 | 12447 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12448 | #include "stringlib/unicode_format.h" | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12449 |  | 
 | 12450 | PyDoc_STRVAR(format__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12451 |              "S.format(*args, **kwargs) -> str\n\ | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12452 | \n\ | 
| Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12453 | Return a formatted version of S, using substitutions from args and kwargs.\n\ | 
 | 12454 | The substitutions are identified by braces ('{' and '}')."); | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12455 |  | 
| Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12456 | PyDoc_STRVAR(format_map__doc__, | 
 | 12457 |              "S.format_map(mapping) -> str\n\ | 
 | 12458 | \n\ | 
| Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12459 | Return a formatted version of S, using substitutions from mapping.\n\ | 
 | 12460 | The substitutions are identified by braces ('{' and '}')."); | 
| Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12461 |  | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12462 | static PyObject * | 
 | 12463 | unicode__format__(PyObject* self, PyObject* args) | 
 | 12464 | { | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12465 |     PyObject *format_spec, *out; | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12466 |  | 
 | 12467 |     if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) | 
 | 12468 |         return NULL; | 
 | 12469 |  | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12470 |     out = _PyUnicode_FormatAdvanced(self, format_spec, 0, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12471 |                                      PyUnicode_GET_LENGTH(format_spec)); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12472 |     return out; | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12473 | } | 
 | 12474 |  | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12475 | PyDoc_STRVAR(p_format__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12476 |              "S.__format__(format_spec) -> str\n\ | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12477 | \n\ | 
| Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12478 | Return a formatted version of S as described by format_spec."); | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12479 |  | 
 | 12480 | static PyObject * | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12481 | unicode__sizeof__(PyUnicodeObject *v) | 
 | 12482 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12483 |     Py_ssize_t size; | 
 | 12484 |  | 
 | 12485 |     /* If it's a compact object, account for base structure + | 
 | 12486 |        character data. */ | 
 | 12487 |     if (PyUnicode_IS_COMPACT_ASCII(v)) | 
 | 12488 |         size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; | 
 | 12489 |     else if (PyUnicode_IS_COMPACT(v)) | 
 | 12490 |         size = sizeof(PyCompactUnicodeObject) + | 
 | 12491 |             (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); | 
 | 12492 |     else { | 
 | 12493 |         /* If it is a two-block object, account for base object, and | 
 | 12494 |            for character block if present. */ | 
 | 12495 |         size = sizeof(PyUnicodeObject); | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12496 |         if (_PyUnicode_DATA_ANY(v)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12497 |             size += (PyUnicode_GET_LENGTH(v) + 1) * | 
 | 12498 |                 PyUnicode_CHARACTER_SIZE(v); | 
 | 12499 |     } | 
 | 12500 |     /* If the wstr pointer is present, account for it unless it is shared | 
| Victor Stinner | a3be613 | 2011-10-03 02:16:37 +0200 | [diff] [blame] | 12501 |        with the data pointer. Check if the data is not shared. */ | 
| Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12502 |     if (_PyUnicode_HAS_WSTR_MEMORY(v)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12503 |         size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12504 |     if (_PyUnicode_HAS_UTF8_MEMORY(v)) | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12505 |         size += PyUnicode_UTF8_LENGTH(v) + 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12506 |  | 
 | 12507 |     return PyLong_FromSsize_t(size); | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12508 | } | 
 | 12509 |  | 
 | 12510 | PyDoc_STRVAR(sizeof__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12511 |              "S.__sizeof__() -> size of S in memory, in bytes"); | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12512 |  | 
 | 12513 | static PyObject * | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12514 | unicode_getnewargs(PyObject *v) | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12515 | { | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12516 |     PyObject *copy = PyUnicode_Copy(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12517 |     if (!copy) | 
 | 12518 |         return NULL; | 
 | 12519 |     return Py_BuildValue("(N)", copy); | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12520 | } | 
 | 12521 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12522 | static PyMethodDef unicode_methods[] = { | 
 | 12523 |  | 
 | 12524 |     /* Order is according to common usage: often used methods should | 
 | 12525 |        appear first, since lookup is done sequentially. */ | 
 | 12526 |  | 
| Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12527 |     {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12528 |     {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, | 
 | 12529 |     {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12530 |     {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12531 |     {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, | 
 | 12532 |     {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, | 
 | 12533 |     {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, | 
 | 12534 |     {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, | 
 | 12535 |     {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, | 
 | 12536 |     {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, | 
 | 12537 |     {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12538 |     {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12539 |     {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, | 
 | 12540 |     {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, | 
 | 12541 |     {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12542 |     {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12543 |     {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, | 
 | 12544 |     {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, | 
 | 12545 |     {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12546 |     {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12547 |     {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12548 |     {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12549 |     {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12550 |     {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, | 
 | 12551 |     {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, | 
 | 12552 |     {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, | 
 | 12553 |     {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, | 
 | 12554 |     {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, | 
 | 12555 |     {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, | 
 | 12556 |     {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, | 
 | 12557 |     {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, | 
 | 12558 |     {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, | 
 | 12559 |     {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, | 
 | 12560 |     {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, | 
 | 12561 |     {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, | 
 | 12562 |     {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, | 
 | 12563 |     {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12564 |     {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12565 |     {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12566 |     {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, | 
| Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12567 |     {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, | 
| Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12568 |     {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12569 |     {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12570 |     {"maketrans", (PyCFunction) unicode_maketrans, | 
 | 12571 |      METH_VARARGS | METH_STATIC, maketrans__doc__}, | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12572 |     {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12573 | #if 0 | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12574 |     {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12575 | #endif | 
 | 12576 |  | 
 | 12577 | #if 0 | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12578 |     /* These methods are just used for debugging the implementation. */ | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12579 |     {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12580 | #endif | 
 | 12581 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12582 |     {"__getnewargs__",  (PyCFunction)unicode_getnewargs, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12583 |     {NULL, NULL} | 
 | 12584 | }; | 
 | 12585 |  | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12586 | static PyObject * | 
 | 12587 | unicode_mod(PyObject *v, PyObject *w) | 
 | 12588 | { | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12589 |     if (!PyUnicode_Check(v)) | 
 | 12590 |         Py_RETURN_NOTIMPLEMENTED; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12591 |     return PyUnicode_Format(v, w); | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12592 | } | 
 | 12593 |  | 
 | 12594 | static PyNumberMethods unicode_as_number = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12595 |     0,              /*nb_add*/ | 
 | 12596 |     0,              /*nb_subtract*/ | 
 | 12597 |     0,              /*nb_multiply*/ | 
 | 12598 |     unicode_mod,            /*nb_remainder*/ | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12599 | }; | 
 | 12600 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12601 | static PySequenceMethods unicode_as_sequence = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12602 |     (lenfunc) unicode_length,       /* sq_length */ | 
 | 12603 |     PyUnicode_Concat,           /* sq_concat */ | 
 | 12604 |     (ssizeargfunc) unicode_repeat,  /* sq_repeat */ | 
 | 12605 |     (ssizeargfunc) unicode_getitem,     /* sq_item */ | 
 | 12606 |     0,                  /* sq_slice */ | 
 | 12607 |     0,                  /* sq_ass_item */ | 
 | 12608 |     0,                  /* sq_ass_slice */ | 
 | 12609 |     PyUnicode_Contains,         /* sq_contains */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12610 | }; | 
 | 12611 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12612 | static PyObject* | 
 | 12613 | unicode_subscript(PyUnicodeObject* self, PyObject* item) | 
 | 12614 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12615 |     if (PyUnicode_READY(self) == -1) | 
 | 12616 |         return NULL; | 
 | 12617 |  | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12618 |     if (PyIndex_Check(item)) { | 
 | 12619 |         Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12620 |         if (i == -1 && PyErr_Occurred()) | 
 | 12621 |             return NULL; | 
 | 12622 |         if (i < 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12623 |             i += PyUnicode_GET_LENGTH(self); | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12624 |         return unicode_getitem((PyObject*)self, i); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12625 |     } else if (PySlice_Check(item)) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12626 |         Py_ssize_t start, stop, step, slicelength, cur, i; | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12627 |         PyObject *result; | 
 | 12628 |         void *src_data, *dest_data; | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12629 |         int src_kind, dest_kind; | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12630 |         Py_UCS4 ch, max_char, kind_limit; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12631 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12632 |         if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12633 |                                  &start, &stop, &step, &slicelength) < 0) { | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12634 |             return NULL; | 
 | 12635 |         } | 
 | 12636 |  | 
 | 12637 |         if (slicelength <= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12638 |             return PyUnicode_New(0, 0); | 
 | 12639 |         } else if (start == 0 && step == 1 && | 
 | 12640 |                    slicelength == PyUnicode_GET_LENGTH(self) && | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12641 |                    PyUnicode_CheckExact(self)) { | 
 | 12642 |             Py_INCREF(self); | 
 | 12643 |             return (PyObject *)self; | 
 | 12644 |         } else if (step == 1) { | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12645 |             return PyUnicode_Substring((PyObject*)self, | 
 | 12646 |                                        start, start + slicelength); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12647 |         } | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12648 |         /* General case */ | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12649 |         max_char = 0; | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12650 |         src_kind = PyUnicode_KIND(self); | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12651 |         kind_limit = kind_maxchar_limit(src_kind); | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12652 |         src_data = PyUnicode_DATA(self); | 
 | 12653 |         for (cur = start, i = 0; i < slicelength; cur += step, i++) { | 
 | 12654 |             ch = PyUnicode_READ(src_kind, src_data, cur); | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12655 |             if (ch > max_char) { | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12656 |                 max_char = ch; | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12657 |                 if (max_char >= kind_limit) | 
 | 12658 |                     break; | 
 | 12659 |             } | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12660 |         } | 
 | 12661 |         result = PyUnicode_New(slicelength, max_char); | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12662 |         if (result == NULL) | 
 | 12663 |             return NULL; | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12664 |         dest_kind = PyUnicode_KIND(result); | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12665 |         dest_data = PyUnicode_DATA(result); | 
 | 12666 |  | 
 | 12667 |         for (cur = start, i = 0; i < slicelength; cur += step, i++) { | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12668 |             Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); | 
 | 12669 |             PyUnicode_WRITE(dest_kind, dest_data, i, ch); | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12670 |         } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12671 |         assert(_PyUnicode_CheckConsistency(result, 1)); | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12672 |         return result; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12673 |     } else { | 
 | 12674 |         PyErr_SetString(PyExc_TypeError, "string indices must be integers"); | 
 | 12675 |         return NULL; | 
 | 12676 |     } | 
 | 12677 | } | 
 | 12678 |  | 
 | 12679 | static PyMappingMethods unicode_as_mapping = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12680 |     (lenfunc)unicode_length,        /* mp_length */ | 
 | 12681 |     (binaryfunc)unicode_subscript,  /* mp_subscript */ | 
 | 12682 |     (objobjargproc)0,           /* mp_ass_subscript */ | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12683 | }; | 
 | 12684 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12686 | /* Helpers for PyUnicode_Format() */ | 
 | 12687 |  | 
 | 12688 | static PyObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12689 | 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] | 12690 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12691 |     Py_ssize_t argidx = *p_argidx; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12692 |     if (argidx < arglen) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12693 |         (*p_argidx)++; | 
 | 12694 |         if (arglen < 0) | 
 | 12695 |             return args; | 
 | 12696 |         else | 
 | 12697 |             return PyTuple_GetItem(args, argidx); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12698 |     } | 
 | 12699 |     PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12700 |                     "not enough arguments for format string"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12701 |     return NULL; | 
 | 12702 | } | 
 | 12703 |  | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12704 | /* Returns a new reference to a PyUnicode object, or NULL on failure. */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12705 |  | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12706 | static PyObject * | 
 | 12707 | formatfloat(PyObject *v, int flags, int prec, int type) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12708 | { | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12709 |     char *p; | 
 | 12710 |     PyObject *result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12711 |     double x; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12712 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12713 |     x = PyFloat_AsDouble(v); | 
 | 12714 |     if (x == -1.0 && PyErr_Occurred()) | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12715 |         return NULL; | 
 | 12716 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12717 |     if (prec < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12718 |         prec = 6; | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12719 |  | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12720 |     p = PyOS_double_to_string(x, type, prec, | 
 | 12721 |                               (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12722 |     if (p == NULL) | 
 | 12723 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12724 |     result = PyUnicode_DecodeASCII(p, strlen(p), NULL); | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12725 |     PyMem_Free(p); | 
 | 12726 |     return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12727 | } | 
 | 12728 |  | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12729 | static PyObject* | 
 | 12730 | formatlong(PyObject *val, int flags, int prec, int type) | 
 | 12731 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12732 |     char *buf; | 
 | 12733 |     int len; | 
 | 12734 |     PyObject *str; /* temporary string object. */ | 
 | 12735 |     PyObject *result; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12736 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12737 |     str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); | 
 | 12738 |     if (!str) | 
 | 12739 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12740 |     result = PyUnicode_DecodeASCII(buf, len, NULL); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12741 |     Py_DECREF(str); | 
 | 12742 |     return result; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12743 | } | 
 | 12744 |  | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12745 | static Py_UCS4 | 
 | 12746 | formatchar(PyObject *v) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12747 | { | 
| Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12748 |     /* presume that the buffer is at least 3 characters long */ | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12749 |     if (PyUnicode_Check(v)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12750 |         if (PyUnicode_GET_LENGTH(v) == 1) { | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12751 |             return PyUnicode_READ_CHAR(v, 0); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12752 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12753 |         goto onError; | 
 | 12754 |     } | 
 | 12755 |     else { | 
 | 12756 |         /* Integer input truncated to a character */ | 
 | 12757 |         long x; | 
 | 12758 |         x = PyLong_AsLong(v); | 
 | 12759 |         if (x == -1 && PyErr_Occurred()) | 
 | 12760 |             goto onError; | 
 | 12761 |  | 
 | 12762 |         if (x < 0 || x > 0x10ffff) { | 
 | 12763 |             PyErr_SetString(PyExc_OverflowError, | 
 | 12764 |                             "%c arg not in range(0x110000)"); | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12765 |             return (Py_UCS4) -1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12766 |         } | 
 | 12767 |  | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12768 |         return (Py_UCS4) x; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12769 |     } | 
| Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12770 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12771 |   onError: | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12772 |     PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12773 |                     "%c requires int or char"); | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12774 |     return (Py_UCS4) -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12775 | } | 
 | 12776 |  | 
| Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 12777 | static int | 
 | 12778 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) | 
 | 12779 | { | 
 | 12780 |     int r; | 
 | 12781 |     assert(count > 0); | 
 | 12782 |     assert(PyUnicode_Check(obj)); | 
 | 12783 |     if (count > 5) { | 
 | 12784 |         PyObject *repeated = unicode_repeat((PyUnicodeObject *) obj, count); | 
 | 12785 |         if (repeated == NULL) | 
 | 12786 |             return -1; | 
 | 12787 |         r = _PyAccu_Accumulate(acc, repeated); | 
 | 12788 |         Py_DECREF(repeated); | 
 | 12789 |         return r; | 
 | 12790 |     } | 
 | 12791 |     else { | 
 | 12792 |         do { | 
 | 12793 |             if (_PyAccu_Accumulate(acc, obj)) | 
 | 12794 |                 return -1; | 
 | 12795 |         } while (--count); | 
 | 12796 |         return 0; | 
 | 12797 |     } | 
 | 12798 | } | 
 | 12799 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12800 | PyObject * | 
 | 12801 | PyUnicode_Format(PyObject *format, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12802 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12803 |     void *fmt; | 
 | 12804 |     int fmtkind; | 
 | 12805 |     PyObject *result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12806 |     int kind; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12807 |     int r; | 
 | 12808 |     Py_ssize_t fmtcnt, fmtpos, arglen, argidx; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12809 |     int args_owned = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12810 |     PyObject *dict = NULL; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12811 |     PyObject *temp = NULL; | 
 | 12812 |     PyObject *second = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12813 |     PyUnicodeObject *uformat; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12814 |     _PyAccu acc; | 
 | 12815 |     static PyObject *plus, *minus, *blank, *zero, *percent; | 
 | 12816 |  | 
 | 12817 |     if (!plus && !(plus = get_latin1_char('+'))) | 
 | 12818 |         return NULL; | 
 | 12819 |     if (!minus && !(minus = get_latin1_char('-'))) | 
 | 12820 |         return NULL; | 
 | 12821 |     if (!blank && !(blank = get_latin1_char(' '))) | 
 | 12822 |         return NULL; | 
 | 12823 |     if (!zero && !(zero = get_latin1_char('0'))) | 
 | 12824 |         return NULL; | 
 | 12825 |     if (!percent && !(percent = get_latin1_char('%'))) | 
 | 12826 |         return NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12827 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12828 |     if (format == NULL || args == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12829 |         PyErr_BadInternalCall(); | 
 | 12830 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12831 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12832 |     uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); | 
 | 12833 |     if (uformat == NULL || PyUnicode_READY(uformat) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12834 |         return NULL; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12835 |     if (_PyAccu_Init(&acc)) | 
 | 12836 |         goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12837 |     fmt = PyUnicode_DATA(uformat); | 
 | 12838 |     fmtkind = PyUnicode_KIND(uformat); | 
 | 12839 |     fmtcnt = PyUnicode_GET_LENGTH(uformat); | 
 | 12840 |     fmtpos = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12841 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12842 |     if (PyTuple_Check(args)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12843 |         arglen = PyTuple_Size(args); | 
 | 12844 |         argidx = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12845 |     } | 
 | 12846 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12847 |         arglen = -1; | 
 | 12848 |         argidx = -2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12849 |     } | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12850 |     if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && | 
| Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12851 |         !PyUnicode_Check(args)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12852 |         dict = args; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12853 |  | 
 | 12854 |     while (--fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12855 |         if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12856 |             PyObject *nonfmt; | 
 | 12857 |             Py_ssize_t nonfmtpos; | 
 | 12858 |             nonfmtpos = fmtpos++; | 
 | 12859 |             while (fmtcnt >= 0 && | 
 | 12860 |                    PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { | 
 | 12861 |                 fmtpos++; | 
 | 12862 |                 fmtcnt--; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12863 |             } | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12864 |             nonfmt = PyUnicode_Substring((PyObject *) uformat, nonfmtpos, fmtpos); | 
 | 12865 |             if (nonfmt == NULL) | 
 | 12866 |                 goto onError; | 
 | 12867 |             r = _PyAccu_Accumulate(&acc, nonfmt); | 
 | 12868 |             Py_DECREF(nonfmt); | 
 | 12869 |             if (r) | 
 | 12870 |                 goto onError; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12871 |         } | 
 | 12872 |         else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12873 |             /* Got a format specifier */ | 
 | 12874 |             int flags = 0; | 
 | 12875 |             Py_ssize_t width = -1; | 
 | 12876 |             int prec = -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12877 |             Py_UCS4 c = '\0'; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12878 |             Py_UCS4 fill, sign; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12879 |             int isnumok; | 
 | 12880 |             PyObject *v = NULL; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12881 |             void *pbuf = NULL; | 
 | 12882 |             Py_ssize_t pindex, len; | 
 | 12883 |             PyObject *signobj = NULL, *fillobj = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12884 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12885 |             fmtpos++; | 
 | 12886 |             if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { | 
 | 12887 |                 Py_ssize_t keystart; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12888 |                 Py_ssize_t keylen; | 
 | 12889 |                 PyObject *key; | 
 | 12890 |                 int pcount = 1; | 
| Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12891 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12892 |                 if (dict == NULL) { | 
 | 12893 |                     PyErr_SetString(PyExc_TypeError, | 
 | 12894 |                                     "format requires a mapping"); | 
 | 12895 |                     goto onError; | 
 | 12896 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12897 |                 ++fmtpos; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12898 |                 --fmtcnt; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12899 |                 keystart = fmtpos; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12900 |                 /* Skip over balanced parentheses */ | 
 | 12901 |                 while (pcount > 0 && --fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12902 |                     if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12903 |                         --pcount; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12904 |                     else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12905 |                         ++pcount; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12906 |                     fmtpos++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12907 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12908 |                 keylen = fmtpos - keystart - 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12909 |                 if (fmtcnt < 0 || pcount > 0) { | 
 | 12910 |                     PyErr_SetString(PyExc_ValueError, | 
 | 12911 |                                     "incomplete format key"); | 
 | 12912 |                     goto onError; | 
 | 12913 |                 } | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12914 |                 key = PyUnicode_Substring((PyObject*)uformat, | 
 | 12915 |                                           keystart, keystart + keylen); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12916 |                 if (key == NULL) | 
 | 12917 |                     goto onError; | 
 | 12918 |                 if (args_owned) { | 
 | 12919 |                     Py_DECREF(args); | 
 | 12920 |                     args_owned = 0; | 
 | 12921 |                 } | 
 | 12922 |                 args = PyObject_GetItem(dict, key); | 
 | 12923 |                 Py_DECREF(key); | 
 | 12924 |                 if (args == NULL) { | 
 | 12925 |                     goto onError; | 
 | 12926 |                 } | 
 | 12927 |                 args_owned = 1; | 
 | 12928 |                 arglen = -1; | 
 | 12929 |                 argidx = -2; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12930 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12931 |             while (--fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12932 |                 switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12933 |                 case '-': flags |= F_LJUST; continue; | 
 | 12934 |                 case '+': flags |= F_SIGN; continue; | 
 | 12935 |                 case ' ': flags |= F_BLANK; continue; | 
 | 12936 |                 case '#': flags |= F_ALT; continue; | 
 | 12937 |                 case '0': flags |= F_ZERO; continue; | 
 | 12938 |                 } | 
 | 12939 |                 break; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12940 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12941 |             if (c == '*') { | 
 | 12942 |                 v = getnextarg(args, arglen, &argidx); | 
 | 12943 |                 if (v == NULL) | 
 | 12944 |                     goto onError; | 
 | 12945 |                 if (!PyLong_Check(v)) { | 
 | 12946 |                     PyErr_SetString(PyExc_TypeError, | 
 | 12947 |                                     "* wants int"); | 
 | 12948 |                     goto onError; | 
 | 12949 |                 } | 
 | 12950 |                 width = PyLong_AsLong(v); | 
 | 12951 |                 if (width == -1 && PyErr_Occurred()) | 
 | 12952 |                     goto onError; | 
 | 12953 |                 if (width < 0) { | 
 | 12954 |                     flags |= F_LJUST; | 
 | 12955 |                     width = -width; | 
 | 12956 |                 } | 
 | 12957 |                 if (--fmtcnt >= 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12958 |                     c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12959 |             } | 
 | 12960 |             else if (c >= '0' && c <= '9') { | 
 | 12961 |                 width = c - '0'; | 
 | 12962 |                 while (--fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12963 |                     c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12964 |                     if (c < '0' || c > '9') | 
 | 12965 |                         break; | 
 | 12966 |                     if ((width*10) / 10 != width) { | 
 | 12967 |                         PyErr_SetString(PyExc_ValueError, | 
 | 12968 |                                         "width too big"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12969 |                         goto onError; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12970 |                     } | 
 | 12971 |                     width = width*10 + (c - '0'); | 
 | 12972 |                 } | 
 | 12973 |             } | 
 | 12974 |             if (c == '.') { | 
 | 12975 |                 prec = 0; | 
 | 12976 |                 if (--fmtcnt >= 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12977 |                     c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12978 |                 if (c == '*') { | 
 | 12979 |                     v = getnextarg(args, arglen, &argidx); | 
 | 12980 |                     if (v == NULL) | 
 | 12981 |                         goto onError; | 
 | 12982 |                     if (!PyLong_Check(v)) { | 
 | 12983 |                         PyErr_SetString(PyExc_TypeError, | 
 | 12984 |                                         "* wants int"); | 
 | 12985 |                         goto onError; | 
 | 12986 |                     } | 
 | 12987 |                     prec = PyLong_AsLong(v); | 
 | 12988 |                     if (prec == -1 && PyErr_Occurred()) | 
 | 12989 |                         goto onError; | 
 | 12990 |                     if (prec < 0) | 
 | 12991 |                         prec = 0; | 
 | 12992 |                     if (--fmtcnt >= 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12993 |                         c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12994 |                 } | 
 | 12995 |                 else if (c >= '0' && c <= '9') { | 
 | 12996 |                     prec = c - '0'; | 
 | 12997 |                     while (--fmtcnt >= 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12998 |                         c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12999 |                         if (c < '0' || c > '9') | 
 | 13000 |                             break; | 
 | 13001 |                         if ((prec*10) / 10 != prec) { | 
 | 13002 |                             PyErr_SetString(PyExc_ValueError, | 
 | 13003 |                                             "prec too big"); | 
 | 13004 |                             goto onError; | 
 | 13005 |                         } | 
 | 13006 |                         prec = prec*10 + (c - '0'); | 
 | 13007 |                     } | 
 | 13008 |                 } | 
 | 13009 |             } /* prec */ | 
 | 13010 |             if (fmtcnt >= 0) { | 
 | 13011 |                 if (c == 'h' || c == 'l' || c == 'L') { | 
 | 13012 |                     if (--fmtcnt >= 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13013 |                         c = PyUnicode_READ(fmtkind, fmt, fmtpos++); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13014 |                 } | 
 | 13015 |             } | 
 | 13016 |             if (fmtcnt < 0) { | 
 | 13017 |                 PyErr_SetString(PyExc_ValueError, | 
 | 13018 |                                 "incomplete format"); | 
 | 13019 |                 goto onError; | 
 | 13020 |             } | 
 | 13021 |             if (c != '%') { | 
 | 13022 |                 v = getnextarg(args, arglen, &argidx); | 
 | 13023 |                 if (v == NULL) | 
 | 13024 |                     goto onError; | 
 | 13025 |             } | 
 | 13026 |             sign = 0; | 
 | 13027 |             fill = ' '; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13028 |             fillobj = blank; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13029 |             switch (c) { | 
 | 13030 |  | 
 | 13031 |             case '%': | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13032 |                 _PyAccu_Accumulate(&acc, percent); | 
 | 13033 |                 continue; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13034 |  | 
 | 13035 |             case 's': | 
 | 13036 |             case 'r': | 
 | 13037 |             case 'a': | 
| Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13038 |                 if (PyUnicode_CheckExact(v) && c == 's') { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13039 |                     temp = v; | 
 | 13040 |                     Py_INCREF(temp); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13041 |                 } | 
 | 13042 |                 else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13043 |                     if (c == 's') | 
 | 13044 |                         temp = PyObject_Str(v); | 
 | 13045 |                     else if (c == 'r') | 
 | 13046 |                         temp = PyObject_Repr(v); | 
 | 13047 |                     else | 
 | 13048 |                         temp = PyObject_ASCII(v); | 
 | 13049 |                     if (temp == NULL) | 
 | 13050 |                         goto onError; | 
 | 13051 |                     if (PyUnicode_Check(temp)) | 
 | 13052 |                         /* nothing to do */; | 
 | 13053 |                     else { | 
 | 13054 |                         Py_DECREF(temp); | 
 | 13055 |                         PyErr_SetString(PyExc_TypeError, | 
 | 13056 |                                         "%s argument has non-string str()"); | 
 | 13057 |                         goto onError; | 
 | 13058 |                     } | 
 | 13059 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13060 |                 if (PyUnicode_READY(temp) == -1) { | 
 | 13061 |                     Py_CLEAR(temp); | 
 | 13062 |                     goto onError; | 
 | 13063 |                 } | 
 | 13064 |                 pbuf = PyUnicode_DATA(temp); | 
 | 13065 |                 kind = PyUnicode_KIND(temp); | 
 | 13066 |                 len = PyUnicode_GET_LENGTH(temp); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13067 |                 if (prec >= 0 && len > prec) | 
 | 13068 |                     len = prec; | 
 | 13069 |                 break; | 
 | 13070 |  | 
 | 13071 |             case 'i': | 
 | 13072 |             case 'd': | 
 | 13073 |             case 'u': | 
 | 13074 |             case 'o': | 
 | 13075 |             case 'x': | 
 | 13076 |             case 'X': | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13077 |                 isnumok = 0; | 
 | 13078 |                 if (PyNumber_Check(v)) { | 
 | 13079 |                     PyObject *iobj=NULL; | 
 | 13080 |  | 
 | 13081 |                     if (PyLong_Check(v)) { | 
 | 13082 |                         iobj = v; | 
 | 13083 |                         Py_INCREF(iobj); | 
 | 13084 |                     } | 
 | 13085 |                     else { | 
 | 13086 |                         iobj = PyNumber_Long(v); | 
 | 13087 |                     } | 
 | 13088 |                     if (iobj!=NULL) { | 
 | 13089 |                         if (PyLong_Check(iobj)) { | 
 | 13090 |                             isnumok = 1; | 
| Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13091 |                             temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13092 |                             Py_DECREF(iobj); | 
 | 13093 |                             if (!temp) | 
 | 13094 |                                 goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13095 |                             if (PyUnicode_READY(temp) == -1) { | 
 | 13096 |                                 Py_CLEAR(temp); | 
 | 13097 |                                 goto onError; | 
 | 13098 |                             } | 
 | 13099 |                             pbuf = PyUnicode_DATA(temp); | 
 | 13100 |                             kind = PyUnicode_KIND(temp); | 
 | 13101 |                             len = PyUnicode_GET_LENGTH(temp); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13102 |                             sign = 1; | 
 | 13103 |                         } | 
 | 13104 |                         else { | 
 | 13105 |                             Py_DECREF(iobj); | 
 | 13106 |                         } | 
 | 13107 |                     } | 
 | 13108 |                 } | 
 | 13109 |                 if (!isnumok) { | 
 | 13110 |                     PyErr_Format(PyExc_TypeError, | 
 | 13111 |                                  "%%%c format: a number is required, " | 
 | 13112 |                                  "not %.200s", (char)c, Py_TYPE(v)->tp_name); | 
 | 13113 |                     goto onError; | 
 | 13114 |                 } | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13115 |                 if (flags & F_ZERO) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13116 |                     fill = '0'; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13117 |                     fillobj = zero; | 
 | 13118 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13119 |                 break; | 
 | 13120 |  | 
 | 13121 |             case 'e': | 
 | 13122 |             case 'E': | 
 | 13123 |             case 'f': | 
 | 13124 |             case 'F': | 
 | 13125 |             case 'g': | 
 | 13126 |             case 'G': | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13127 |                 temp = formatfloat(v, flags, prec, c); | 
 | 13128 |                 if (!temp) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13129 |                     goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13130 |                 if (PyUnicode_READY(temp) == -1) { | 
 | 13131 |                     Py_CLEAR(temp); | 
 | 13132 |                     goto onError; | 
 | 13133 |                 } | 
 | 13134 |                 pbuf = PyUnicode_DATA(temp); | 
 | 13135 |                 kind = PyUnicode_KIND(temp); | 
 | 13136 |                 len = PyUnicode_GET_LENGTH(temp); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13137 |                 sign = 1; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13138 |                 if (flags & F_ZERO) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13139 |                     fill = '0'; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13140 |                     fillobj = zero; | 
 | 13141 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13142 |                 break; | 
 | 13143 |  | 
 | 13144 |             case 'c': | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13145 |             { | 
 | 13146 |                 Py_UCS4 ch = formatchar(v); | 
 | 13147 |                 if (ch == (Py_UCS4) -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13148 |                     goto onError; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13149 |                 temp = _PyUnicode_FromUCS4(&ch, 1); | 
 | 13150 |                 if (temp == NULL) | 
 | 13151 |                     goto onError; | 
 | 13152 |                 pbuf = PyUnicode_DATA(temp); | 
 | 13153 |                 kind = PyUnicode_KIND(temp); | 
 | 13154 |                 len = PyUnicode_GET_LENGTH(temp); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13155 |                 break; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13156 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13157 |  | 
 | 13158 |             default: | 
 | 13159 |                 PyErr_Format(PyExc_ValueError, | 
 | 13160 |                              "unsupported format character '%c' (0x%x) " | 
 | 13161 |                              "at index %zd", | 
 | 13162 |                              (31<=c && c<=126) ? (char)c : '?', | 
 | 13163 |                              (int)c, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13164 |                              fmtpos - 1); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13165 |                 goto onError; | 
 | 13166 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13167 |             /* pbuf is initialized here. */ | 
 | 13168 |             pindex = 0; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13169 |             if (sign) { | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13170 |                 if (PyUnicode_READ(kind, pbuf, pindex) == '-') { | 
 | 13171 |                     signobj = minus; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13172 |                     len--; | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13173 |                     pindex++; | 
 | 13174 |                 } | 
 | 13175 |                 else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { | 
 | 13176 |                     signobj = plus; | 
 | 13177 |                     len--; | 
 | 13178 |                     pindex++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13179 |                 } | 
 | 13180 |                 else if (flags & F_SIGN) | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13181 |                     signobj = plus; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13182 |                 else if (flags & F_BLANK) | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13183 |                     signobj = blank; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13184 |                 else | 
 | 13185 |                     sign = 0; | 
 | 13186 |             } | 
 | 13187 |             if (width < len) | 
 | 13188 |                 width = len; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13189 |             if (sign) { | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13190 |                 if (fill != ' ') { | 
 | 13191 |                     assert(signobj != NULL); | 
 | 13192 |                     if (_PyAccu_Accumulate(&acc, signobj)) | 
 | 13193 |                         goto onError; | 
 | 13194 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13195 |                 if (width > len) | 
 | 13196 |                     width--; | 
 | 13197 |             } | 
 | 13198 |             if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13199 |                 assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13200 |                 assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13201 |                 if (fill != ' ') { | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13202 |                     second = get_latin1_char( | 
 | 13203 |                         PyUnicode_READ(kind, pbuf, pindex + 1)); | 
 | 13204 |                     pindex += 2; | 
 | 13205 |                     if (second == NULL || | 
 | 13206 |                         _PyAccu_Accumulate(&acc, zero) || | 
 | 13207 |                         _PyAccu_Accumulate(&acc, second)) | 
 | 13208 |                         goto onError; | 
 | 13209 |                     Py_CLEAR(second); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13210 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13211 |                 width -= 2; | 
 | 13212 |                 if (width < 0) | 
 | 13213 |                     width = 0; | 
 | 13214 |                 len -= 2; | 
 | 13215 |             } | 
 | 13216 |             if (width > len && !(flags & F_LJUST)) { | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13217 |                 assert(fillobj != NULL); | 
| Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13218 |                 if (repeat_accumulate(&acc, fillobj, width - len)) | 
 | 13219 |                     goto onError; | 
 | 13220 |                 width = len; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13221 |             } | 
 | 13222 |             if (fill == ' ') { | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13223 |                 if (sign) { | 
 | 13224 |                     assert(signobj != NULL); | 
 | 13225 |                     if (_PyAccu_Accumulate(&acc, signobj)) | 
 | 13226 |                         goto onError; | 
 | 13227 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13228 |                 if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13229 |                     assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); | 
 | 13230 |                     assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13231 |                     second = get_latin1_char( | 
 | 13232 |                         PyUnicode_READ(kind, pbuf, pindex + 1)); | 
 | 13233 |                     pindex += 2; | 
 | 13234 |                     if (second == NULL || | 
 | 13235 |                         _PyAccu_Accumulate(&acc, zero) || | 
 | 13236 |                         _PyAccu_Accumulate(&acc, second)) | 
 | 13237 |                         goto onError; | 
 | 13238 |                     Py_CLEAR(second); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13239 |                 } | 
 | 13240 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13241 |             /* Copy all characters, preserving len */ | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13242 |             if (temp != NULL) { | 
 | 13243 |                 assert(pbuf == PyUnicode_DATA(temp)); | 
 | 13244 |                 v = PyUnicode_Substring(temp, pindex, pindex + len); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13245 |             } | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13246 |             else { | 
 | 13247 |                 const char *p = (const char *) pbuf; | 
 | 13248 |                 assert(pbuf != NULL); | 
 | 13249 |                 p = p + PyUnicode_KIND_SIZE(kind, pindex); | 
 | 13250 |                 v = PyUnicode_FromKindAndData(kind, p, len); | 
 | 13251 |             } | 
 | 13252 |             if (v == NULL) | 
 | 13253 |                 goto onError; | 
 | 13254 |             r = _PyAccu_Accumulate(&acc, v); | 
 | 13255 |             Py_DECREF(v); | 
 | 13256 |             if (r) | 
 | 13257 |                 goto onError; | 
| Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13258 |             if (width > len && repeat_accumulate(&acc, blank, width - len)) | 
 | 13259 |                 goto onError; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13260 |             if (dict && (argidx < arglen) && c != '%') { | 
 | 13261 |                 PyErr_SetString(PyExc_TypeError, | 
 | 13262 |                                 "not all arguments converted during string formatting"); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13263 |                 goto onError; | 
 | 13264 |             } | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13265 |             Py_CLEAR(temp); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13266 |         } /* '%' */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13267 |     } /* until end */ | 
 | 13268 |     if (argidx < arglen && !dict) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13269 |         PyErr_SetString(PyExc_TypeError, | 
 | 13270 |                         "not all arguments converted during string formatting"); | 
 | 13271 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13272 |     } | 
 | 13273 |  | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13274 |     result = _PyAccu_Finish(&acc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13275 |     if (args_owned) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13276 |         Py_DECREF(args); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13277 |     } | 
 | 13278 |     Py_DECREF(uformat); | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13279 |     Py_XDECREF(temp); | 
 | 13280 |     Py_XDECREF(second); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13281 |     return (PyObject *)result; | 
 | 13282 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13283 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13284 |     Py_DECREF(uformat); | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13285 |     Py_XDECREF(temp); | 
 | 13286 |     Py_XDECREF(second); | 
 | 13287 |     _PyAccu_Destroy(&acc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13288 |     if (args_owned) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13289 |         Py_DECREF(args); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13290 |     } | 
 | 13291 |     return NULL; | 
 | 13292 | } | 
 | 13293 |  | 
| Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13294 | static PyObject * | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13295 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | 
 | 13296 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13297 | static PyObject * | 
 | 13298 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 13299 | { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13300 |     PyObject *x = NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13301 |     static char *kwlist[] = {"object", "encoding", "errors", 0}; | 
 | 13302 |     char *encoding = NULL; | 
 | 13303 |     char *errors = NULL; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13304 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13305 |     if (type != &PyUnicode_Type) | 
 | 13306 |         return unicode_subtype_new(type, args, kwds); | 
 | 13307 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13308 |                                      kwlist, &x, &encoding, &errors)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13309 |         return NULL; | 
 | 13310 |     if (x == NULL) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13311 |         return (PyObject *)PyUnicode_New(0, 0); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13312 |     if (encoding == NULL && errors == NULL) | 
 | 13313 |         return PyObject_Str(x); | 
 | 13314 |     else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13315 |         return PyUnicode_FromEncodedObject(x, encoding, errors); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13316 | } | 
 | 13317 |  | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13318 | static PyObject * | 
 | 13319 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 13320 | { | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13321 |     PyUnicodeObject *unicode, *self; | 
 | 13322 |     Py_ssize_t length, char_size; | 
 | 13323 |     int share_wstr, share_utf8; | 
 | 13324 |     unsigned int kind; | 
 | 13325 |     void *data; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13326 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13327 |     assert(PyType_IsSubtype(type, &PyUnicode_Type)); | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13328 |  | 
 | 13329 |     unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); | 
 | 13330 |     if (unicode == NULL) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13331 |         return NULL; | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13332 |     assert(_PyUnicode_CHECK(unicode)); | 
| Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13333 |     if (PyUnicode_READY(unicode)) | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13334 |         return NULL; | 
 | 13335 |  | 
 | 13336 |     self = (PyUnicodeObject *) type->tp_alloc(type, 0); | 
 | 13337 |     if (self == NULL) { | 
 | 13338 |         Py_DECREF(unicode); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13339 |         return NULL; | 
 | 13340 |     } | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13341 |     kind = PyUnicode_KIND(unicode); | 
 | 13342 |     length = PyUnicode_GET_LENGTH(unicode); | 
 | 13343 |  | 
 | 13344 |     _PyUnicode_LENGTH(self) = length; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13345 | #ifdef Py_DEBUG | 
 | 13346 |     _PyUnicode_HASH(self) = -1; | 
 | 13347 | #else | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13348 |     _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13349 | #endif | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13350 |     _PyUnicode_STATE(self).interned = 0; | 
 | 13351 |     _PyUnicode_STATE(self).kind = kind; | 
 | 13352 |     _PyUnicode_STATE(self).compact = 0; | 
| Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13353 |     _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13354 |     _PyUnicode_STATE(self).ready = 1; | 
 | 13355 |     _PyUnicode_WSTR(self) = NULL; | 
 | 13356 |     _PyUnicode_UTF8_LENGTH(self) = 0; | 
 | 13357 |     _PyUnicode_UTF8(self) = NULL; | 
 | 13358 |     _PyUnicode_WSTR_LENGTH(self) = 0; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13359 |     _PyUnicode_DATA_ANY(self) = NULL; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13360 |  | 
 | 13361 |     share_utf8 = 0; | 
 | 13362 |     share_wstr = 0; | 
 | 13363 |     if (kind == PyUnicode_1BYTE_KIND) { | 
 | 13364 |         char_size = 1; | 
 | 13365 |         if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) | 
 | 13366 |             share_utf8 = 1; | 
 | 13367 |     } | 
 | 13368 |     else if (kind == PyUnicode_2BYTE_KIND) { | 
 | 13369 |         char_size = 2; | 
 | 13370 |         if (sizeof(wchar_t) == 2) | 
 | 13371 |             share_wstr = 1; | 
 | 13372 |     } | 
 | 13373 |     else { | 
 | 13374 |         assert(kind == PyUnicode_4BYTE_KIND); | 
 | 13375 |         char_size = 4; | 
 | 13376 |         if (sizeof(wchar_t) == 4) | 
 | 13377 |             share_wstr = 1; | 
 | 13378 |     } | 
 | 13379 |  | 
 | 13380 |     /* Ensure we won't overflow the length. */ | 
 | 13381 |     if (length > (PY_SSIZE_T_MAX / char_size - 1)) { | 
 | 13382 |         PyErr_NoMemory(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13383 |         goto onError; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13384 |     } | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13385 |     data = PyObject_MALLOC((length + 1) * char_size); | 
 | 13386 |     if (data == NULL) { | 
 | 13387 |         PyErr_NoMemory(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13388 |         goto onError; | 
 | 13389 |     } | 
 | 13390 |  | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13391 |     _PyUnicode_DATA_ANY(self) = data; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13392 |     if (share_utf8) { | 
 | 13393 |         _PyUnicode_UTF8_LENGTH(self) = length; | 
 | 13394 |         _PyUnicode_UTF8(self) = data; | 
 | 13395 |     } | 
 | 13396 |     if (share_wstr) { | 
 | 13397 |         _PyUnicode_WSTR_LENGTH(self) = length; | 
 | 13398 |         _PyUnicode_WSTR(self) = (wchar_t *)data; | 
 | 13399 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13400 |  | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13401 |     Py_MEMCPY(data, PyUnicode_DATA(unicode), | 
 | 13402 |               PyUnicode_KIND_SIZE(kind, length + 1)); | 
 | 13403 |     Py_DECREF(unicode); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13404 |     assert(_PyUnicode_CheckConsistency(self, 1)); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13405 | #ifdef Py_DEBUG | 
 | 13406 |     _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); | 
 | 13407 | #endif | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13408 |     return (PyObject *)self; | 
 | 13409 |  | 
 | 13410 | onError: | 
 | 13411 |     Py_DECREF(unicode); | 
 | 13412 |     Py_DECREF(self); | 
 | 13413 |     return NULL; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13414 | } | 
 | 13415 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13416 | PyDoc_STRVAR(unicode_doc, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13417 |              "str(string[, encoding[, errors]]) -> str\n\ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13418 | \n\ | 
| Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13419 | Create a new string object from the given encoded string.\n\ | 
| Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13420 | encoding defaults to the current default string encoding.\n\ | 
 | 13421 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13422 |  | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13423 | static PyObject *unicode_iter(PyObject *seq); | 
 | 13424 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13425 | PyTypeObject PyUnicode_Type = { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13426 |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13427 |     "str",              /* tp_name */ | 
 | 13428 |     sizeof(PyUnicodeObject),        /* tp_size */ | 
 | 13429 |     0,                  /* tp_itemsize */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13430 |     /* Slots */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13431 |     (destructor)unicode_dealloc,    /* tp_dealloc */ | 
 | 13432 |     0,                  /* tp_print */ | 
 | 13433 |     0,                  /* tp_getattr */ | 
 | 13434 |     0,                  /* tp_setattr */ | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13435 |     0,                  /* tp_reserved */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13436 |     unicode_repr,           /* tp_repr */ | 
 | 13437 |     &unicode_as_number,         /* tp_as_number */ | 
 | 13438 |     &unicode_as_sequence,       /* tp_as_sequence */ | 
 | 13439 |     &unicode_as_mapping,        /* tp_as_mapping */ | 
 | 13440 |     (hashfunc) unicode_hash,        /* tp_hash*/ | 
 | 13441 |     0,                  /* tp_call*/ | 
 | 13442 |     (reprfunc) unicode_str,     /* tp_str */ | 
 | 13443 |     PyObject_GenericGetAttr,        /* tp_getattro */ | 
 | 13444 |     0,                  /* tp_setattro */ | 
 | 13445 |     0,                  /* tp_as_buffer */ | 
 | 13446 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13447 |     Py_TPFLAGS_UNICODE_SUBCLASS,    /* tp_flags */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13448 |     unicode_doc,            /* tp_doc */ | 
 | 13449 |     0,                  /* tp_traverse */ | 
 | 13450 |     0,                  /* tp_clear */ | 
 | 13451 |     PyUnicode_RichCompare,      /* tp_richcompare */ | 
 | 13452 |     0,                  /* tp_weaklistoffset */ | 
 | 13453 |     unicode_iter,           /* tp_iter */ | 
 | 13454 |     0,                  /* tp_iternext */ | 
 | 13455 |     unicode_methods,            /* tp_methods */ | 
 | 13456 |     0,                  /* tp_members */ | 
 | 13457 |     0,                  /* tp_getset */ | 
 | 13458 |     &PyBaseObject_Type,         /* tp_base */ | 
 | 13459 |     0,                  /* tp_dict */ | 
 | 13460 |     0,                  /* tp_descr_get */ | 
 | 13461 |     0,                  /* tp_descr_set */ | 
 | 13462 |     0,                  /* tp_dictoffset */ | 
 | 13463 |     0,                  /* tp_init */ | 
 | 13464 |     0,                  /* tp_alloc */ | 
 | 13465 |     unicode_new,            /* tp_new */ | 
 | 13466 |     PyObject_Del,           /* tp_free */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13467 | }; | 
 | 13468 |  | 
 | 13469 | /* Initialize the Unicode implementation */ | 
 | 13470 |  | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13471 | void _PyUnicode_Init(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13472 | { | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13473 |     int i; | 
 | 13474 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13475 |     /* XXX - move this array to unicodectype.c ? */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13476 |     Py_UCS2 linebreak[] = { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13477 |         0x000A, /* LINE FEED */ | 
 | 13478 |         0x000D, /* CARRIAGE RETURN */ | 
 | 13479 |         0x001C, /* FILE SEPARATOR */ | 
 | 13480 |         0x001D, /* GROUP SEPARATOR */ | 
 | 13481 |         0x001E, /* RECORD SEPARATOR */ | 
 | 13482 |         0x0085, /* NEXT LINE */ | 
 | 13483 |         0x2028, /* LINE SEPARATOR */ | 
 | 13484 |         0x2029, /* PARAGRAPH SEPARATOR */ | 
 | 13485 |     }; | 
 | 13486 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13487 |     /* Init the implementation */ | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13488 |     unicode_empty = PyUnicode_New(0, 0); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13489 |     assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13490 |     if (!unicode_empty) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13491 |         Py_FatalError("Can't create empty string"); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13492 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13493 |     for (i = 0; i < 256; i++) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13494 |         unicode_latin1[i] = NULL; | 
| Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13495 |     if (PyType_Ready(&PyUnicode_Type) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13496 |         Py_FatalError("Can't initialize 'unicode'"); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13497 |  | 
 | 13498 |     /* initialize the linebreak bloom filter */ | 
 | 13499 |     bloom_linebreak = make_bloom_mask( | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13500 |         PyUnicode_2BYTE_KIND, linebreak, | 
| Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13501 |         Py_ARRAY_LENGTH(linebreak)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13502 |  | 
 | 13503 |     PyType_Ready(&EncodingMapType); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13504 | } | 
 | 13505 |  | 
 | 13506 | /* Finalize the Unicode implementation */ | 
 | 13507 |  | 
| Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13508 | int | 
 | 13509 | PyUnicode_ClearFreeList(void) | 
 | 13510 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13511 |     return 0; | 
| Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13512 | } | 
 | 13513 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13514 | void | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13515 | _PyUnicode_Fini(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13516 | { | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13517 |     int i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13518 |  | 
| Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13519 |     Py_XDECREF(unicode_empty); | 
 | 13520 |     unicode_empty = NULL; | 
| Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13521 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13522 |     for (i = 0; i < 256; i++) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13523 |         if (unicode_latin1[i]) { | 
 | 13524 |             Py_DECREF(unicode_latin1[i]); | 
 | 13525 |             unicode_latin1[i] = NULL; | 
 | 13526 |         } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13527 |     } | 
| Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13528 |     (void)PyUnicode_ClearFreeList(); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13529 | } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13530 |  | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13531 | void | 
 | 13532 | PyUnicode_InternInPlace(PyObject **p) | 
 | 13533 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13534 |     register PyUnicodeObject *s = (PyUnicodeObject *)(*p); | 
 | 13535 |     PyObject *t; | 
| Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13536 | #ifdef Py_DEBUG | 
 | 13537 |     assert(s != NULL); | 
 | 13538 |     assert(_PyUnicode_CHECK(s)); | 
 | 13539 | #else | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13540 |     if (s == NULL || !PyUnicode_Check(s)) | 
| Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13541 |         return; | 
 | 13542 | #endif | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13543 |     /* If it's a subclass, we don't really know what putting | 
 | 13544 |        it in the interned dict might do. */ | 
 | 13545 |     if (!PyUnicode_CheckExact(s)) | 
 | 13546 |         return; | 
 | 13547 |     if (PyUnicode_CHECK_INTERNED(s)) | 
 | 13548 |         return; | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13549 |     if (_PyUnicode_READY_REPLACE(p)) { | 
| Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13550 |         assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13551 |         return; | 
 | 13552 |     } | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13553 |     s = (PyUnicodeObject *)(*p); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13554 |     if (interned == NULL) { | 
 | 13555 |         interned = PyDict_New(); | 
 | 13556 |         if (interned == NULL) { | 
 | 13557 |             PyErr_Clear(); /* Don't leave an exception */ | 
 | 13558 |             return; | 
 | 13559 |         } | 
 | 13560 |     } | 
 | 13561 |     /* It might be that the GetItem call fails even | 
 | 13562 |        though the key is present in the dictionary, | 
 | 13563 |        namely when this happens during a stack overflow. */ | 
 | 13564 |     Py_ALLOW_RECURSION | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13565 |         t = PyDict_GetItem(interned, (PyObject *)s); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13566 |     Py_END_ALLOW_RECURSION | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13567 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13568 |         if (t) { | 
 | 13569 |             Py_INCREF(t); | 
 | 13570 |             Py_DECREF(*p); | 
 | 13571 |             *p = t; | 
 | 13572 |             return; | 
 | 13573 |         } | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13574 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13575 |     PyThreadState_GET()->recursion_critical = 1; | 
 | 13576 |     if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { | 
 | 13577 |         PyErr_Clear(); | 
 | 13578 |         PyThreadState_GET()->recursion_critical = 0; | 
 | 13579 |         return; | 
 | 13580 |     } | 
 | 13581 |     PyThreadState_GET()->recursion_critical = 0; | 
 | 13582 |     /* The two references in interned are not counted by refcnt. | 
 | 13583 |        The deallocator will take care of this */ | 
 | 13584 |     Py_REFCNT(s) -= 2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13585 |     _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13586 | } | 
 | 13587 |  | 
 | 13588 | void | 
 | 13589 | PyUnicode_InternImmortal(PyObject **p) | 
 | 13590 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13591 |     PyUnicodeObject *u = (PyUnicodeObject *)*p; | 
 | 13592 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13593 |     PyUnicode_InternInPlace(p); | 
 | 13594 |     if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13595 |         _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13596 |         Py_INCREF(*p); | 
 | 13597 |     } | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13598 | } | 
 | 13599 |  | 
 | 13600 | PyObject * | 
 | 13601 | PyUnicode_InternFromString(const char *cp) | 
 | 13602 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13603 |     PyObject *s = PyUnicode_FromString(cp); | 
 | 13604 |     if (s == NULL) | 
 | 13605 |         return NULL; | 
 | 13606 |     PyUnicode_InternInPlace(&s); | 
 | 13607 |     return s; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13608 | } | 
 | 13609 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13610 | void | 
 | 13611 | _Py_ReleaseInternedUnicodeStrings(void) | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13612 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13613 |     PyObject *keys; | 
 | 13614 |     PyUnicodeObject *s; | 
 | 13615 |     Py_ssize_t i, n; | 
 | 13616 |     Py_ssize_t immortal_size = 0, mortal_size = 0; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13617 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13618 |     if (interned == NULL || !PyDict_Check(interned)) | 
 | 13619 |         return; | 
 | 13620 |     keys = PyDict_Keys(interned); | 
 | 13621 |     if (keys == NULL || !PyList_Check(keys)) { | 
 | 13622 |         PyErr_Clear(); | 
 | 13623 |         return; | 
 | 13624 |     } | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13625 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13626 |     /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak | 
 | 13627 |        detector, interned unicode strings are not forcibly deallocated; | 
 | 13628 |        rather, we give them their stolen references back, and then clear | 
 | 13629 |        and DECREF the interned dict. */ | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13630 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13631 |     n = PyList_GET_SIZE(keys); | 
 | 13632 |     fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13633 |             n); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13634 |     for (i = 0; i < n; i++) { | 
 | 13635 |         s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); | 
| Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13636 |         if (PyUnicode_READY(s) == -1) { | 
 | 13637 |             assert(0 && "could not ready string"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13638 |             fprintf(stderr, "could not ready string\n"); | 
| Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13639 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13640 |         switch (PyUnicode_CHECK_INTERNED(s)) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13641 |         case SSTATE_NOT_INTERNED: | 
 | 13642 |             /* XXX Shouldn't happen */ | 
 | 13643 |             break; | 
 | 13644 |         case SSTATE_INTERNED_IMMORTAL: | 
 | 13645 |             Py_REFCNT(s) += 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13646 |             immortal_size += PyUnicode_GET_LENGTH(s); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13647 |             break; | 
 | 13648 |         case SSTATE_INTERNED_MORTAL: | 
 | 13649 |             Py_REFCNT(s) += 2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13650 |             mortal_size += PyUnicode_GET_LENGTH(s); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13651 |             break; | 
 | 13652 |         default: | 
 | 13653 |             Py_FatalError("Inconsistent interned string state."); | 
 | 13654 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13655 |         _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13656 |     } | 
 | 13657 |     fprintf(stderr, "total size of all interned strings: " | 
 | 13658 |             "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " | 
 | 13659 |             "mortal/immortal\n", mortal_size, immortal_size); | 
 | 13660 |     Py_DECREF(keys); | 
 | 13661 |     PyDict_Clear(interned); | 
 | 13662 |     Py_DECREF(interned); | 
 | 13663 |     interned = NULL; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13664 | } | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13665 |  | 
 | 13666 |  | 
 | 13667 | /********************* Unicode Iterator **************************/ | 
 | 13668 |  | 
 | 13669 | typedef struct { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13670 |     PyObject_HEAD | 
 | 13671 |     Py_ssize_t it_index; | 
 | 13672 |     PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13673 | } unicodeiterobject; | 
 | 13674 |  | 
 | 13675 | static void | 
 | 13676 | unicodeiter_dealloc(unicodeiterobject *it) | 
 | 13677 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13678 |     _PyObject_GC_UNTRACK(it); | 
 | 13679 |     Py_XDECREF(it->it_seq); | 
 | 13680 |     PyObject_GC_Del(it); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13681 | } | 
 | 13682 |  | 
 | 13683 | static int | 
 | 13684 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) | 
 | 13685 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13686 |     Py_VISIT(it->it_seq); | 
 | 13687 |     return 0; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13688 | } | 
 | 13689 |  | 
 | 13690 | static PyObject * | 
 | 13691 | unicodeiter_next(unicodeiterobject *it) | 
 | 13692 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13693 |     PyUnicodeObject *seq; | 
 | 13694 |     PyObject *item; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13695 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13696 |     assert(it != NULL); | 
 | 13697 |     seq = it->it_seq; | 
 | 13698 |     if (seq == NULL) | 
 | 13699 |         return NULL; | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13700 |     assert(_PyUnicode_CHECK(seq)); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13701 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13702 |     if (it->it_index < PyUnicode_GET_LENGTH(seq)) { | 
 | 13703 |         int kind = PyUnicode_KIND(seq); | 
 | 13704 |         void *data = PyUnicode_DATA(seq); | 
 | 13705 |         Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); | 
 | 13706 |         item = PyUnicode_FromOrdinal(chr); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13707 |         if (item != NULL) | 
 | 13708 |             ++it->it_index; | 
 | 13709 |         return item; | 
 | 13710 |     } | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13711 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13712 |     Py_DECREF(seq); | 
 | 13713 |     it->it_seq = NULL; | 
 | 13714 |     return NULL; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13715 | } | 
 | 13716 |  | 
 | 13717 | static PyObject * | 
 | 13718 | unicodeiter_len(unicodeiterobject *it) | 
 | 13719 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13720 |     Py_ssize_t len = 0; | 
 | 13721 |     if (it->it_seq) | 
 | 13722 |         len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; | 
 | 13723 |     return PyLong_FromSsize_t(len); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13724 | } | 
 | 13725 |  | 
 | 13726 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); | 
 | 13727 |  | 
 | 13728 | static PyMethodDef unicodeiter_methods[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13729 |     {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13730 |      length_hint_doc}, | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13731 |     {NULL,      NULL}       /* sentinel */ | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13732 | }; | 
 | 13733 |  | 
 | 13734 | PyTypeObject PyUnicodeIter_Type = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13735 |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
 | 13736 |     "str_iterator",         /* tp_name */ | 
 | 13737 |     sizeof(unicodeiterobject),      /* tp_basicsize */ | 
 | 13738 |     0,                  /* tp_itemsize */ | 
 | 13739 |     /* methods */ | 
 | 13740 |     (destructor)unicodeiter_dealloc,    /* tp_dealloc */ | 
 | 13741 |     0,                  /* tp_print */ | 
 | 13742 |     0,                  /* tp_getattr */ | 
 | 13743 |     0,                  /* tp_setattr */ | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13744 |     0,                  /* tp_reserved */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13745 |     0,                  /* tp_repr */ | 
 | 13746 |     0,                  /* tp_as_number */ | 
 | 13747 |     0,                  /* tp_as_sequence */ | 
 | 13748 |     0,                  /* tp_as_mapping */ | 
 | 13749 |     0,                  /* tp_hash */ | 
 | 13750 |     0,                  /* tp_call */ | 
 | 13751 |     0,                  /* tp_str */ | 
 | 13752 |     PyObject_GenericGetAttr,        /* tp_getattro */ | 
 | 13753 |     0,                  /* tp_setattro */ | 
 | 13754 |     0,                  /* tp_as_buffer */ | 
 | 13755 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | 
 | 13756 |     0,                  /* tp_doc */ | 
 | 13757 |     (traverseproc)unicodeiter_traverse, /* tp_traverse */ | 
 | 13758 |     0,                  /* tp_clear */ | 
 | 13759 |     0,                  /* tp_richcompare */ | 
 | 13760 |     0,                  /* tp_weaklistoffset */ | 
 | 13761 |     PyObject_SelfIter,          /* tp_iter */ | 
 | 13762 |     (iternextfunc)unicodeiter_next,     /* tp_iternext */ | 
 | 13763 |     unicodeiter_methods,            /* tp_methods */ | 
 | 13764 |     0, | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13765 | }; | 
 | 13766 |  | 
 | 13767 | static PyObject * | 
 | 13768 | unicode_iter(PyObject *seq) | 
 | 13769 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13770 |     unicodeiterobject *it; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13771 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13772 |     if (!PyUnicode_Check(seq)) { | 
 | 13773 |         PyErr_BadInternalCall(); | 
 | 13774 |         return NULL; | 
 | 13775 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13776 |     if (PyUnicode_READY(seq) == -1) | 
 | 13777 |         return NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13778 |     it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); | 
 | 13779 |     if (it == NULL) | 
 | 13780 |         return NULL; | 
 | 13781 |     it->it_index = 0; | 
 | 13782 |     Py_INCREF(seq); | 
 | 13783 |     it->it_seq = (PyUnicodeObject *)seq; | 
 | 13784 |     _PyObject_GC_TRACK(it); | 
 | 13785 |     return (PyObject *)it; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13786 | } | 
 | 13787 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13788 | #define UNIOP(x) Py_UNICODE_##x | 
 | 13789 | #define UNIOP_t Py_UNICODE | 
 | 13790 | #include "uniops.h" | 
 | 13791 | #undef UNIOP | 
 | 13792 | #undef UNIOP_t | 
 | 13793 | #define UNIOP(x) Py_UCS4_##x | 
 | 13794 | #define UNIOP_t Py_UCS4 | 
 | 13795 | #include "uniops.h" | 
 | 13796 | #undef UNIOP | 
 | 13797 | #undef UNIOP_t | 
| Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13798 |  | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13799 | Py_UNICODE* | 
| Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13800 | PyUnicode_AsUnicodeCopy(PyObject *object) | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13801 | { | 
 | 13802 |     PyUnicodeObject *unicode = (PyUnicodeObject *)object; | 
 | 13803 |     Py_UNICODE *copy; | 
 | 13804 |     Py_ssize_t size; | 
 | 13805 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13806 |     if (!PyUnicode_Check(unicode)) { | 
 | 13807 |         PyErr_BadArgument(); | 
 | 13808 |         return NULL; | 
 | 13809 |     } | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13810 |     /* Ensure we won't overflow the size. */ | 
 | 13811 |     if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { | 
 | 13812 |         PyErr_NoMemory(); | 
 | 13813 |         return NULL; | 
 | 13814 |     } | 
 | 13815 |     size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ | 
 | 13816 |     size *= sizeof(Py_UNICODE); | 
 | 13817 |     copy = PyMem_Malloc(size); | 
 | 13818 |     if (copy == NULL) { | 
 | 13819 |         PyErr_NoMemory(); | 
 | 13820 |         return NULL; | 
 | 13821 |     } | 
 | 13822 |     memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); | 
 | 13823 |     return copy; | 
 | 13824 | } | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13825 |  | 
| Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13826 | /* A _string module, to export formatter_parser and formatter_field_name_split | 
 | 13827 |    to the string.Formatter class implemented in Python. */ | 
 | 13828 |  | 
 | 13829 | static PyMethodDef _string_methods[] = { | 
 | 13830 |     {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, | 
 | 13831 |      METH_O, PyDoc_STR("split the argument as a field name")}, | 
 | 13832 |     {"formatter_parser", (PyCFunction) formatter_parser, | 
 | 13833 |      METH_O, PyDoc_STR("parse the argument as a format string")}, | 
 | 13834 |     {NULL, NULL} | 
 | 13835 | }; | 
 | 13836 |  | 
 | 13837 | static struct PyModuleDef _string_module = { | 
 | 13838 |     PyModuleDef_HEAD_INIT, | 
 | 13839 |     "_string", | 
 | 13840 |     PyDoc_STR("string helper module"), | 
 | 13841 |     0, | 
 | 13842 |     _string_methods, | 
 | 13843 |     NULL, | 
 | 13844 |     NULL, | 
 | 13845 |     NULL, | 
 | 13846 |     NULL | 
 | 13847 | }; | 
 | 13848 |  | 
 | 13849 | PyMODINIT_FUNC | 
 | 13850 | PyInit__string(void) | 
 | 13851 | { | 
 | 13852 |     return PyModule_Create(&_string_module); | 
 | 13853 | } | 
 | 13854 |  | 
 | 13855 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13856 | #ifdef __cplusplus | 
 | 13857 | } | 
 | 13858 | #endif |