| 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" | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 44 | #include "bytes_methods.h" | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 |  | 
| Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 47 | #include <windows.h> | 
 | 48 | #endif | 
| Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 49 |  | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 50 | /* --- Globals ------------------------------------------------------------ | 
 | 51 |  | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 52 | NOTE: In the interpreter's initialization phase, some globals are currently | 
 | 53 |       initialized dynamically as needed. In the process Unicode objects may | 
 | 54 |       be created before the Unicode type is ready. | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 55 |  | 
 | 56 | */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 57 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 |  | 
 | 59 | #ifdef __cplusplus | 
 | 60 | extern "C" { | 
 | 61 | #endif | 
 | 62 |  | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 63 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ | 
 | 64 | #define MAX_UNICODE 0x10ffff | 
 | 65 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 66 | #ifdef Py_DEBUG | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 67 | #  define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 68 | #else | 
 | 69 | #  define _PyUnicode_CHECK(op) PyUnicode_Check(op) | 
 | 70 | #endif | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 71 |  | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 72 | #define _PyUnicode_UTF8(op)                             \ | 
 | 73 |     (((PyCompactUnicodeObject*)(op))->utf8) | 
 | 74 | #define PyUnicode_UTF8(op)                              \ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 75 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 76 |      assert(PyUnicode_IS_READY(op)),                    \ | 
 | 77 |      PyUnicode_IS_COMPACT_ASCII(op) ?                   \ | 
 | 78 |          ((char*)((PyASCIIObject*)(op) + 1)) :          \ | 
 | 79 |          _PyUnicode_UTF8(op)) | 
| Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 80 | #define _PyUnicode_UTF8_LENGTH(op)                      \ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 81 |     (((PyCompactUnicodeObject*)(op))->utf8_length) | 
 | 82 | #define PyUnicode_UTF8_LENGTH(op)                       \ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 83 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 84 |      assert(PyUnicode_IS_READY(op)),                    \ | 
 | 85 |      PyUnicode_IS_COMPACT_ASCII(op) ?                   \ | 
 | 86 |          ((PyASCIIObject*)(op))->length :               \ | 
 | 87 |          _PyUnicode_UTF8_LENGTH(op)) | 
| Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 88 | #define _PyUnicode_WSTR(op)                             \ | 
 | 89 |     (((PyASCIIObject*)(op))->wstr) | 
 | 90 | #define _PyUnicode_WSTR_LENGTH(op)                      \ | 
 | 91 |     (((PyCompactUnicodeObject*)(op))->wstr_length) | 
 | 92 | #define _PyUnicode_LENGTH(op)                           \ | 
 | 93 |     (((PyASCIIObject *)(op))->length) | 
 | 94 | #define _PyUnicode_STATE(op)                            \ | 
 | 95 |     (((PyASCIIObject *)(op))->state) | 
 | 96 | #define _PyUnicode_HASH(op)                             \ | 
 | 97 |     (((PyASCIIObject *)(op))->hash) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 98 | #define _PyUnicode_KIND(op)                             \ | 
 | 99 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 100 |      ((PyASCIIObject *)(op))->state.kind) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 101 | #define _PyUnicode_GET_LENGTH(op)                       \ | 
 | 102 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 103 |      ((PyASCIIObject *)(op))->length) | 
| Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 104 | #define _PyUnicode_DATA_ANY(op)                         \ | 
 | 105 |     (((PyUnicodeObject*)(op))->data.any) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 106 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 107 | #undef PyUnicode_READY | 
 | 108 | #define PyUnicode_READY(op)                             \ | 
 | 109 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
 | 110 |      (PyUnicode_IS_READY(op) ?                          \ | 
| Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 111 |       0 :                                               \ | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 112 |       _PyUnicode_Ready(op))) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 113 |  | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 114 | #define _PyUnicode_SHARE_UTF8(op)                       \ | 
 | 115 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
 | 116 |      assert(!PyUnicode_IS_COMPACT_ASCII(op)),           \ | 
 | 117 |      (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) | 
 | 118 | #define _PyUnicode_SHARE_WSTR(op)                       \ | 
 | 119 |     (assert(_PyUnicode_CHECK(op)),                      \ | 
 | 120 |      (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) | 
 | 121 |  | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 122 | /* true if the Unicode object has an allocated UTF-8 memory block | 
 | 123 |    (not shared with other data) */ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 124 | #define _PyUnicode_HAS_UTF8_MEMORY(op)                  \ | 
| Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 125 |     ((!PyUnicode_IS_COMPACT_ASCII(op)                   \ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 126 |       && _PyUnicode_UTF8(op)                            \ | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 127 |       && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) | 
 | 128 |  | 
| Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 129 | /* true if the Unicode object has an allocated wstr memory block | 
 | 130 |    (not shared with other data) */ | 
 | 131 | #define _PyUnicode_HAS_WSTR_MEMORY(op)                  \ | 
| Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 132 |     ((_PyUnicode_WSTR(op) &&                            \ | 
| Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 133 |       (!PyUnicode_IS_READY(op) ||                       \ | 
 | 134 |        _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) | 
 | 135 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 136 | /* Generic helper macro to convert characters of different types. | 
 | 137 |    from_type and to_type have to be valid type names, begin and end | 
 | 138 |    are pointers to the source characters which should be of type | 
 | 139 |    "from_type *".  to is a pointer of type "to_type *" and points to the | 
 | 140 |    buffer where the result characters are written to. */ | 
 | 141 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ | 
 | 142 |     do {                                                \ | 
| Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 143 |         to_type *_to = (to_type *) to;                  \ | 
 | 144 |         const from_type *_iter = (begin);               \ | 
 | 145 |         const from_type *_end = (end);                  \ | 
 | 146 |         Py_ssize_t n = (_end) - (_iter);                \ | 
 | 147 |         const from_type *_unrolled_end =                \ | 
| Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 148 |             _iter + _Py_SIZE_ROUND_DOWN(n, 4);          \ | 
| Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 149 |         while (_iter < (_unrolled_end)) {               \ | 
 | 150 |             _to[0] = (to_type) _iter[0];                \ | 
 | 151 |             _to[1] = (to_type) _iter[1];                \ | 
 | 152 |             _to[2] = (to_type) _iter[2];                \ | 
 | 153 |             _to[3] = (to_type) _iter[3];                \ | 
 | 154 |             _iter += 4; _to += 4;                       \ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 155 |         }                                               \ | 
| Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 156 |         while (_iter < (_end))                          \ | 
 | 157 |             *_to++ = (to_type) *_iter++;                \ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 158 |     } while (0) | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 159 |  | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 160 | /* This dictionary holds all interned unicode strings.  Note that references | 
 | 161 |    to strings in this dictionary are *not* counted in the string's ob_refcnt. | 
 | 162 |    When the interned string reaches a refcnt of 0 the string deallocation | 
 | 163 |    function will delete the reference from this dictionary. | 
 | 164 |  | 
 | 165 |    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] | 166 |    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] | 167 | */ | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 168 | static PyObject *interned = NULL; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 169 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 170 | /* The empty Unicode object is shared to improve performance. */ | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 171 | static PyObject *unicode_empty = NULL; | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 172 |  | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 173 | #define _Py_INCREF_UNICODE_EMPTY()                      \ | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 174 |     do {                                                \ | 
 | 175 |         if (unicode_empty != NULL)                      \ | 
 | 176 |             Py_INCREF(unicode_empty);                   \ | 
 | 177 |         else {                                          \ | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 178 |             unicode_empty = PyUnicode_New(0, 0);        \ | 
 | 179 |             if (unicode_empty != NULL) {                \ | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 180 |                 Py_INCREF(unicode_empty);               \ | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 181 |                 assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); \ | 
 | 182 |             }                                           \ | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 183 |         }                                               \ | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 184 |     } while (0) | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 185 |  | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 186 | #define _Py_RETURN_UNICODE_EMPTY()                      \ | 
 | 187 |     do {                                                \ | 
 | 188 |         _Py_INCREF_UNICODE_EMPTY();                     \ | 
 | 189 |         return unicode_empty;                           \ | 
 | 190 |     } while (0) | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 191 |  | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 192 | /* Forward declaration */ | 
 | 193 | Py_LOCAL_INLINE(int) | 
 | 194 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch); | 
 | 195 |  | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 196 | /* List of static strings. */ | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 197 | static _Py_Identifier *static_strings = NULL; | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 198 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 199 | /* Single character Unicode strings in the Latin-1 range are being | 
 | 200 |    shared as well. */ | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 201 | static PyObject *unicode_latin1[256] = {NULL}; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 202 |  | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 203 | /* Fast detection of the most frequent whitespace characters */ | 
 | 204 | const unsigned char _Py_ascii_whitespace[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 205 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 206 | /*     case 0x0009: * CHARACTER TABULATION */ | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 207 | /*     case 0x000A: * LINE FEED */ | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 208 | /*     case 0x000B: * LINE TABULATION */ | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 209 | /*     case 0x000C: * FORM FEED */ | 
 | 210 | /*     case 0x000D: * CARRIAGE RETURN */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 211 |     0, 1, 1, 1, 1, 1, 0, 0, | 
 | 212 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 213 | /*     case 0x001C: * FILE SEPARATOR */ | 
 | 214 | /*     case 0x001D: * GROUP SEPARATOR */ | 
 | 215 | /*     case 0x001E: * RECORD SEPARATOR */ | 
 | 216 | /*     case 0x001F: * UNIT SEPARATOR */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 217 |     0, 0, 0, 0, 1, 1, 1, 1, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 218 | /*     case 0x0020: * SPACE */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 219 |     1, 0, 0, 0, 0, 0, 0, 0, | 
 | 220 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 221 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 222 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 223 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 224 |     0, 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, | 
 | 228 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 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 | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 232 | }; | 
 | 233 |  | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 234 | /* forward */ | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 235 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); | 
| Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 236 | static PyObject* get_latin1_char(unsigned char ch); | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 237 | static int unicode_modifiable(PyObject *unicode); | 
 | 238 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 239 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 240 | static PyObject * | 
| Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 241 | _PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size); | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 242 | static PyObject * | 
 | 243 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); | 
 | 244 | static PyObject * | 
 | 245 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); | 
 | 246 |  | 
 | 247 | static PyObject * | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 248 | unicode_encode_call_errorhandler(const char *errors, | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 249 |        PyObject **errorHandler,const char *encoding, const char *reason, | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 250 |        PyObject *unicode, PyObject **exceptionObject, | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 251 |        Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); | 
 | 252 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 253 | static void | 
 | 254 | raise_encode_exception(PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 255 |                        const char *encoding, | 
| Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 256 |                        PyObject *unicode, | 
 | 257 |                        Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 258 |                        const char *reason); | 
| Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 259 |  | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 260 | /* Same for linebreaks */ | 
 | 261 | static unsigned char ascii_linebreak[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 262 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 263 | /*         0x000A, * LINE FEED */ | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 264 | /*         0x000B, * LINE TABULATION */ | 
 | 265 | /*         0x000C, * FORM FEED */ | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 266 | /*         0x000D, * CARRIAGE RETURN */ | 
| Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 267 |     0, 0, 1, 1, 1, 1, 0, 0, | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 268 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 269 | /*         0x001C, * FILE SEPARATOR */ | 
 | 270 | /*         0x001D, * GROUP SEPARATOR */ | 
 | 271 | /*         0x001E, * RECORD SEPARATOR */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 272 |     0, 0, 0, 0, 1, 1, 1, 0, | 
 | 273 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 274 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 275 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 276 |     0, 0, 0, 0, 0, 0, 0, 0, | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 277 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 278 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 279 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 280 |     0, 0, 0, 0, 0, 0, 0, 0, | 
 | 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 | 
| Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 286 | }; | 
 | 287 |  | 
| Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 288 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. | 
 | 289 |    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] | 290 | Py_UNICODE | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 291 | PyUnicode_GetMax(void) | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 292 | { | 
| Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 293 | #ifdef Py_UNICODE_WIDE | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 294 |     return 0x10FFFF; | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 295 | #else | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 296 |     /* This is actually an illegal character, so it should | 
 | 297 |        not be passed to unichr. */ | 
 | 298 |     return 0xFFFF; | 
| Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 299 | #endif | 
 | 300 | } | 
 | 301 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 302 | #ifdef Py_DEBUG | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 303 | int | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 304 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 305 | { | 
 | 306 |     PyASCIIObject *ascii; | 
 | 307 |     unsigned int kind; | 
 | 308 |  | 
 | 309 |     assert(PyUnicode_Check(op)); | 
 | 310 |  | 
 | 311 |     ascii = (PyASCIIObject *)op; | 
 | 312 |     kind = ascii->state.kind; | 
 | 313 |  | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 314 |     if (ascii->state.ascii == 1 && ascii->state.compact == 1) { | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 315 |         assert(kind == PyUnicode_1BYTE_KIND); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 316 |         assert(ascii->state.ready == 1); | 
 | 317 |     } | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 318 |     else { | 
| Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 319 |         PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; | 
| Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 320 |         void *data; | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 321 |  | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 322 |         if (ascii->state.compact == 1) { | 
 | 323 |             data = compact + 1; | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 324 |             assert(kind == PyUnicode_1BYTE_KIND | 
 | 325 |                    || kind == PyUnicode_2BYTE_KIND | 
 | 326 |                    || kind == PyUnicode_4BYTE_KIND); | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 327 |             assert(ascii->state.ascii == 0); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 328 |             assert(ascii->state.ready == 1); | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 329 |             assert (compact->utf8 != data); | 
| Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 330 |         } | 
 | 331 |         else { | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 332 |             PyUnicodeObject *unicode = (PyUnicodeObject *)op; | 
 | 333 |  | 
 | 334 |             data = unicode->data.any; | 
 | 335 |             if (kind == PyUnicode_WCHAR_KIND) { | 
| Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 336 |                 assert(ascii->length == 0); | 
 | 337 |                 assert(ascii->hash == -1); | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 338 |                 assert(ascii->state.compact == 0); | 
 | 339 |                 assert(ascii->state.ascii == 0); | 
 | 340 |                 assert(ascii->state.ready == 0); | 
| Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 341 |                 assert(ascii->state.interned == SSTATE_NOT_INTERNED); | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 342 |                 assert(ascii->wstr != NULL); | 
 | 343 |                 assert(data == NULL); | 
 | 344 |                 assert(compact->utf8 == NULL); | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 345 |             } | 
 | 346 |             else { | 
 | 347 |                 assert(kind == PyUnicode_1BYTE_KIND | 
 | 348 |                        || kind == PyUnicode_2BYTE_KIND | 
 | 349 |                        || kind == PyUnicode_4BYTE_KIND); | 
 | 350 |                 assert(ascii->state.compact == 0); | 
 | 351 |                 assert(ascii->state.ready == 1); | 
 | 352 |                 assert(data != NULL); | 
 | 353 |                 if (ascii->state.ascii) { | 
 | 354 |                     assert (compact->utf8 == data); | 
 | 355 |                     assert (compact->utf8_length == ascii->length); | 
 | 356 |                 } | 
 | 357 |                 else | 
 | 358 |                     assert (compact->utf8 != data); | 
 | 359 |             } | 
 | 360 |         } | 
 | 361 |         if (kind != PyUnicode_WCHAR_KIND) { | 
| Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 362 |             if ( | 
 | 363 | #if SIZEOF_WCHAR_T == 2 | 
 | 364 |                 kind == PyUnicode_2BYTE_KIND | 
 | 365 | #else | 
 | 366 |                 kind == PyUnicode_4BYTE_KIND | 
 | 367 | #endif | 
 | 368 |                ) | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 369 |             { | 
 | 370 |                 assert(ascii->wstr == data); | 
 | 371 |                 assert(compact->wstr_length == ascii->length); | 
 | 372 |             } else | 
 | 373 |                 assert(ascii->wstr != data); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 374 |         } | 
| Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 375 |  | 
 | 376 |         if (compact->utf8 == NULL) | 
 | 377 |             assert(compact->utf8_length == 0); | 
 | 378 |         if (ascii->wstr == NULL) | 
 | 379 |             assert(compact->wstr_length == 0); | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 380 |     } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 381 |     /* check that the best kind is used */ | 
 | 382 |     if (check_content && kind != PyUnicode_WCHAR_KIND) | 
 | 383 |     { | 
 | 384 |         Py_ssize_t i; | 
 | 385 |         Py_UCS4 maxchar = 0; | 
| Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 386 |         void *data; | 
 | 387 |         Py_UCS4 ch; | 
 | 388 |  | 
 | 389 |         data = PyUnicode_DATA(ascii); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 390 |         for (i=0; i < ascii->length; i++) | 
 | 391 |         { | 
| Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 392 |             ch = PyUnicode_READ(kind, data, i); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 393 |             if (ch > maxchar) | 
 | 394 |                 maxchar = ch; | 
 | 395 |         } | 
 | 396 |         if (kind == PyUnicode_1BYTE_KIND) { | 
| Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 397 |             if (ascii->state.ascii == 0) { | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 398 |                 assert(maxchar >= 128); | 
| Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 399 |                 assert(maxchar <= 255); | 
 | 400 |             } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 401 |             else | 
 | 402 |                 assert(maxchar < 128); | 
 | 403 |         } | 
| Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 404 |         else if (kind == PyUnicode_2BYTE_KIND) { | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 405 |             assert(maxchar >= 0x100); | 
| Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 406 |             assert(maxchar <= 0xFFFF); | 
 | 407 |         } | 
 | 408 |         else { | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 409 |             assert(maxchar >= 0x10000); | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 410 |             assert(maxchar <= MAX_UNICODE); | 
| Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 411 |         } | 
| Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 412 |         assert(PyUnicode_READ(kind, data, ascii->length) == 0); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 413 |     } | 
| Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 414 |     return 1; | 
 | 415 | } | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 416 | #endif | 
 | 417 |  | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 418 | static PyObject* | 
 | 419 | unicode_result_wchar(PyObject *unicode) | 
 | 420 | { | 
 | 421 | #ifndef Py_DEBUG | 
 | 422 |     Py_ssize_t len; | 
 | 423 |  | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 424 |     len = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 425 |     if (len == 0) { | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 426 |         Py_DECREF(unicode); | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 427 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 428 |     } | 
 | 429 |  | 
 | 430 |     if (len == 1) { | 
 | 431 |         wchar_t ch = _PyUnicode_WSTR(unicode)[0]; | 
| Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 432 |         if ((Py_UCS4)ch < 256) { | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 433 |             PyObject *latin1_char = get_latin1_char((unsigned char)ch); | 
 | 434 |             Py_DECREF(unicode); | 
 | 435 |             return latin1_char; | 
 | 436 |         } | 
 | 437 |     } | 
 | 438 |  | 
 | 439 |     if (_PyUnicode_Ready(unicode) < 0) { | 
| Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 440 |         Py_DECREF(unicode); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 441 |         return NULL; | 
 | 442 |     } | 
 | 443 | #else | 
| Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 444 |     assert(Py_REFCNT(unicode) == 1); | 
 | 445 |  | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 446 |     /* don't make the result ready in debug mode to ensure that the caller | 
 | 447 |        makes the string ready before using it */ | 
 | 448 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
 | 449 | #endif | 
 | 450 |     return unicode; | 
 | 451 | } | 
 | 452 |  | 
 | 453 | static PyObject* | 
 | 454 | unicode_result_ready(PyObject *unicode) | 
 | 455 | { | 
 | 456 |     Py_ssize_t length; | 
 | 457 |  | 
 | 458 |     length = PyUnicode_GET_LENGTH(unicode); | 
 | 459 |     if (length == 0) { | 
 | 460 |         if (unicode != unicode_empty) { | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 461 |             Py_DECREF(unicode); | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 462 |             _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 463 |         } | 
 | 464 |         return unicode_empty; | 
 | 465 |     } | 
 | 466 |  | 
 | 467 |     if (length == 1) { | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 468 |         void *data = PyUnicode_DATA(unicode); | 
 | 469 |         int kind = PyUnicode_KIND(unicode); | 
 | 470 |         Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 471 |         if (ch < 256) { | 
 | 472 |             PyObject *latin1_char = unicode_latin1[ch]; | 
 | 473 |             if (latin1_char != NULL) { | 
 | 474 |                 if (unicode != latin1_char) { | 
 | 475 |                     Py_INCREF(latin1_char); | 
 | 476 |                     Py_DECREF(unicode); | 
 | 477 |                 } | 
 | 478 |                 return latin1_char; | 
 | 479 |             } | 
 | 480 |             else { | 
 | 481 |                 assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
 | 482 |                 Py_INCREF(unicode); | 
 | 483 |                 unicode_latin1[ch] = unicode; | 
 | 484 |                 return unicode; | 
 | 485 |             } | 
 | 486 |         } | 
 | 487 |     } | 
 | 488 |  | 
 | 489 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
 | 490 |     return unicode; | 
 | 491 | } | 
 | 492 |  | 
 | 493 | static PyObject* | 
 | 494 | unicode_result(PyObject *unicode) | 
 | 495 | { | 
 | 496 |     assert(_PyUnicode_CHECK(unicode)); | 
 | 497 |     if (PyUnicode_IS_READY(unicode)) | 
 | 498 |         return unicode_result_ready(unicode); | 
 | 499 |     else | 
 | 500 |         return unicode_result_wchar(unicode); | 
 | 501 | } | 
 | 502 |  | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 503 | static PyObject* | 
 | 504 | unicode_result_unchanged(PyObject *unicode) | 
 | 505 | { | 
 | 506 |     if (PyUnicode_CheckExact(unicode)) { | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 507 |         if (PyUnicode_READY(unicode) == -1) | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 508 |             return NULL; | 
 | 509 |         Py_INCREF(unicode); | 
 | 510 |         return unicode; | 
 | 511 |     } | 
 | 512 |     else | 
 | 513 |         /* Subtype -- return genuine unicode string with the same value. */ | 
| Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 514 |         return _PyUnicode_Copy(unicode); | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 515 | } | 
 | 516 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 517 | #ifdef HAVE_MBCS | 
 | 518 | static OSVERSIONINFOEX winver; | 
 | 519 | #endif | 
 | 520 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 521 | /* --- Bloom Filters ----------------------------------------------------- */ | 
 | 522 |  | 
 | 523 | /* stuff to implement simple "bloom filters" for Unicode characters. | 
 | 524 |    to keep things simple, we use a single bitmask, using the least 5 | 
 | 525 |    bits from each unicode characters as the bit index. */ | 
 | 526 |  | 
 | 527 | /* the linebreak mask is set up by Unicode_Init below */ | 
 | 528 |  | 
| Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 529 | #if LONG_BIT >= 128 | 
 | 530 | #define BLOOM_WIDTH 128 | 
 | 531 | #elif LONG_BIT >= 64 | 
 | 532 | #define BLOOM_WIDTH 64 | 
 | 533 | #elif LONG_BIT >= 32 | 
 | 534 | #define BLOOM_WIDTH 32 | 
 | 535 | #else | 
 | 536 | #error "LONG_BIT is smaller than 32" | 
 | 537 | #endif | 
 | 538 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 539 | #define BLOOM_MASK unsigned long | 
 | 540 |  | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 541 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 542 |  | 
| Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 543 | #define BLOOM(mask, ch)     ((mask &  (1UL << ((ch) & (BLOOM_WIDTH - 1))))) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 544 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 545 | #define BLOOM_LINEBREAK(ch)                                             \ | 
 | 546 |     ((ch) < 128U ? ascii_linebreak[(ch)] :                              \ | 
 | 547 |      (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 548 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 549 | Py_LOCAL_INLINE(BLOOM_MASK) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 550 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 551 | { | 
| Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 552 | #define BLOOM_UPDATE(TYPE, MASK, PTR, LEN)             \ | 
 | 553 |     do {                                               \ | 
 | 554 |         TYPE *data = (TYPE *)PTR;                      \ | 
 | 555 |         TYPE *end = data + LEN;                        \ | 
 | 556 |         Py_UCS4 ch;                                    \ | 
 | 557 |         for (; data != end; data++) {                  \ | 
 | 558 |             ch = *data;                                \ | 
 | 559 |             MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \ | 
 | 560 |         }                                              \ | 
 | 561 |         break;                                         \ | 
 | 562 |     } while (0) | 
 | 563 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 564 |     /* calculate simple bloom-style bitmask for a given unicode string */ | 
 | 565 |  | 
| Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 566 |     BLOOM_MASK mask; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 567 |  | 
 | 568 |     mask = 0; | 
| Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 569 |     switch (kind) { | 
 | 570 |     case PyUnicode_1BYTE_KIND: | 
 | 571 |         BLOOM_UPDATE(Py_UCS1, mask, ptr, len); | 
 | 572 |         break; | 
 | 573 |     case PyUnicode_2BYTE_KIND: | 
 | 574 |         BLOOM_UPDATE(Py_UCS2, mask, ptr, len); | 
 | 575 |         break; | 
 | 576 |     case PyUnicode_4BYTE_KIND: | 
 | 577 |         BLOOM_UPDATE(Py_UCS4, mask, ptr, len); | 
 | 578 |         break; | 
 | 579 |     default: | 
 | 580 |         assert(0); | 
 | 581 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 582 |     return mask; | 
| Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 583 |  | 
 | 584 | #undef BLOOM_UPDATE | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 585 | } | 
 | 586 |  | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 587 | /* Compilation of templated routines */ | 
 | 588 |  | 
 | 589 | #include "stringlib/asciilib.h" | 
 | 590 | #include "stringlib/fastsearch.h" | 
 | 591 | #include "stringlib/partition.h" | 
 | 592 | #include "stringlib/split.h" | 
 | 593 | #include "stringlib/count.h" | 
 | 594 | #include "stringlib/find.h" | 
 | 595 | #include "stringlib/find_max_char.h" | 
 | 596 | #include "stringlib/localeutil.h" | 
 | 597 | #include "stringlib/undef.h" | 
 | 598 |  | 
 | 599 | #include "stringlib/ucs1lib.h" | 
 | 600 | #include "stringlib/fastsearch.h" | 
 | 601 | #include "stringlib/partition.h" | 
 | 602 | #include "stringlib/split.h" | 
 | 603 | #include "stringlib/count.h" | 
 | 604 | #include "stringlib/find.h" | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 605 | #include "stringlib/replace.h" | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 606 | #include "stringlib/find_max_char.h" | 
 | 607 | #include "stringlib/localeutil.h" | 
 | 608 | #include "stringlib/undef.h" | 
 | 609 |  | 
 | 610 | #include "stringlib/ucs2lib.h" | 
 | 611 | #include "stringlib/fastsearch.h" | 
 | 612 | #include "stringlib/partition.h" | 
 | 613 | #include "stringlib/split.h" | 
 | 614 | #include "stringlib/count.h" | 
 | 615 | #include "stringlib/find.h" | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 616 | #include "stringlib/replace.h" | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 617 | #include "stringlib/find_max_char.h" | 
 | 618 | #include "stringlib/localeutil.h" | 
 | 619 | #include "stringlib/undef.h" | 
 | 620 |  | 
 | 621 | #include "stringlib/ucs4lib.h" | 
 | 622 | #include "stringlib/fastsearch.h" | 
 | 623 | #include "stringlib/partition.h" | 
 | 624 | #include "stringlib/split.h" | 
 | 625 | #include "stringlib/count.h" | 
 | 626 | #include "stringlib/find.h" | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 627 | #include "stringlib/replace.h" | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 628 | #include "stringlib/find_max_char.h" | 
 | 629 | #include "stringlib/localeutil.h" | 
 | 630 | #include "stringlib/undef.h" | 
 | 631 |  | 
| Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 632 | #include "stringlib/unicodedefs.h" | 
 | 633 | #include "stringlib/fastsearch.h" | 
 | 634 | #include "stringlib/count.h" | 
 | 635 | #include "stringlib/find.h" | 
| Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 636 | #include "stringlib/undef.h" | 
| Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 637 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 638 | /* --- Unicode Object ----------------------------------------------------- */ | 
 | 639 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 640 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 641 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 642 |  | 
| Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 643 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, | 
 | 644 |                                      Py_ssize_t size, Py_UCS4 ch, | 
 | 645 |                                      int direction) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 646 | { | 
| Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 647 |     int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; | 
 | 648 |  | 
 | 649 |     switch (kind) { | 
 | 650 |     case PyUnicode_1BYTE_KIND: | 
 | 651 |         { | 
 | 652 |             Py_UCS1 ch1 = (Py_UCS1) ch; | 
 | 653 |             if (ch1 == ch) | 
 | 654 |                 return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); | 
 | 655 |             else | 
 | 656 |                 return -1; | 
 | 657 |         } | 
 | 658 |     case PyUnicode_2BYTE_KIND: | 
 | 659 |         { | 
 | 660 |             Py_UCS2 ch2 = (Py_UCS2) ch; | 
 | 661 |             if (ch2 == ch) | 
 | 662 |                 return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); | 
 | 663 |             else | 
 | 664 |                 return -1; | 
 | 665 |         } | 
 | 666 |     case PyUnicode_4BYTE_KIND: | 
 | 667 |         return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); | 
 | 668 |     default: | 
 | 669 |         assert(0); | 
 | 670 |         return -1; | 
| Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 671 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 672 | } | 
 | 673 |  | 
| Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 674 | #ifdef Py_DEBUG | 
 | 675 | /* Fill the data of an Unicode string with invalid characters to detect bugs | 
 | 676 |    earlier. | 
 | 677 |  | 
 | 678 |    _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for | 
 | 679 |    ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an | 
 | 680 |    invalid character in Unicode 6.0. */ | 
 | 681 | static void | 
 | 682 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) | 
 | 683 | { | 
 | 684 |     int kind = PyUnicode_KIND(unicode); | 
 | 685 |     Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); | 
 | 686 |     Py_ssize_t length = _PyUnicode_LENGTH(unicode); | 
 | 687 |     if (length <= old_length) | 
 | 688 |         return; | 
 | 689 |     memset(data + old_length * kind, 0xff, (length - old_length) * kind); | 
 | 690 | } | 
 | 691 | #endif | 
 | 692 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 693 | static PyObject* | 
 | 694 | resize_compact(PyObject *unicode, Py_ssize_t length) | 
 | 695 | { | 
 | 696 |     Py_ssize_t char_size; | 
 | 697 |     Py_ssize_t struct_size; | 
 | 698 |     Py_ssize_t new_size; | 
 | 699 |     int share_wstr; | 
| Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 700 |     PyObject *new_unicode; | 
| Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 701 | #ifdef Py_DEBUG | 
 | 702 |     Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); | 
 | 703 | #endif | 
 | 704 |  | 
| Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 705 |     assert(unicode_modifiable(unicode)); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 706 |     assert(PyUnicode_IS_READY(unicode)); | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 707 |     assert(PyUnicode_IS_COMPACT(unicode)); | 
 | 708 |  | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 709 |     char_size = PyUnicode_KIND(unicode); | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 710 |     if (PyUnicode_IS_ASCII(unicode)) | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 711 |         struct_size = sizeof(PyASCIIObject); | 
 | 712 |     else | 
 | 713 |         struct_size = sizeof(PyCompactUnicodeObject); | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 714 |     share_wstr = _PyUnicode_SHARE_WSTR(unicode); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 715 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 716 |     if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { | 
 | 717 |         PyErr_NoMemory(); | 
 | 718 |         return NULL; | 
 | 719 |     } | 
 | 720 |     new_size = (struct_size + (length + 1) * char_size); | 
 | 721 |  | 
| Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 722 |     _Py_DEC_REFTOTAL; | 
 | 723 |     _Py_ForgetReference(unicode); | 
 | 724 |  | 
 | 725 |     new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); | 
 | 726 |     if (new_unicode == NULL) { | 
| Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 727 |         _Py_NewReference(unicode); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 728 |         PyErr_NoMemory(); | 
 | 729 |         return NULL; | 
 | 730 |     } | 
| Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 731 |     unicode = new_unicode; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 732 |     _Py_NewReference(unicode); | 
| Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 733 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 734 |     _PyUnicode_LENGTH(unicode) = length; | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 735 |     if (share_wstr) { | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 |         _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 737 |         if (!PyUnicode_IS_ASCII(unicode)) | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 738 |             _PyUnicode_WSTR_LENGTH(unicode) = length; | 
 | 739 |     } | 
| Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 740 |     else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { | 
 | 741 |         PyObject_DEL(_PyUnicode_WSTR(unicode)); | 
 | 742 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 743 |     } | 
| Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 744 | #ifdef Py_DEBUG | 
 | 745 |     unicode_fill_invalid(unicode, old_length); | 
 | 746 | #endif | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 747 |     PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), | 
 | 748 |                     length, 0); | 
| Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 749 |     assert(_PyUnicode_CheckConsistency(unicode, 0)); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 750 |     return unicode; | 
 | 751 | } | 
 | 752 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 753 | static int | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 754 | resize_inplace(PyObject *unicode, Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 755 | { | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 756 |     wchar_t *wstr; | 
| Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 757 |     Py_ssize_t new_size; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 758 |     assert(!PyUnicode_IS_COMPACT(unicode)); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 759 |     assert(Py_REFCNT(unicode) == 1); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 760 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 761 |     if (PyUnicode_IS_READY(unicode)) { | 
 | 762 |         Py_ssize_t char_size; | 
| Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 763 |         int share_wstr, share_utf8; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 764 |         void *data; | 
| Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 765 | #ifdef Py_DEBUG | 
 | 766 |         Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); | 
 | 767 | #endif | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 768 |  | 
 | 769 |         data = _PyUnicode_DATA_ANY(unicode); | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 770 |         char_size = PyUnicode_KIND(unicode); | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 771 |         share_wstr = _PyUnicode_SHARE_WSTR(unicode); | 
 | 772 |         share_utf8 = _PyUnicode_SHARE_UTF8(unicode); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 773 |  | 
 | 774 |         if (length > (PY_SSIZE_T_MAX / char_size - 1)) { | 
 | 775 |             PyErr_NoMemory(); | 
 | 776 |             return -1; | 
 | 777 |         } | 
 | 778 |         new_size = (length + 1) * char_size; | 
 | 779 |  | 
| Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 780 |         if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) | 
 | 781 |         { | 
 | 782 |             PyObject_DEL(_PyUnicode_UTF8(unicode)); | 
 | 783 |             _PyUnicode_UTF8(unicode) = NULL; | 
 | 784 |             _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
 | 785 |         } | 
 | 786 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 787 |         data = (PyObject *)PyObject_REALLOC(data, new_size); | 
 | 788 |         if (data == NULL) { | 
 | 789 |             PyErr_NoMemory(); | 
 | 790 |             return -1; | 
 | 791 |         } | 
 | 792 |         _PyUnicode_DATA_ANY(unicode) = data; | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 793 |         if (share_wstr) { | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 794 |             _PyUnicode_WSTR(unicode) = data; | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 795 |             _PyUnicode_WSTR_LENGTH(unicode) = length; | 
 | 796 |         } | 
 | 797 |         if (share_utf8) { | 
| Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 798 |             _PyUnicode_UTF8(unicode) = data; | 
| Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 799 |             _PyUnicode_UTF8_LENGTH(unicode) = length; | 
 | 800 |         } | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 801 |         _PyUnicode_LENGTH(unicode) = length; | 
 | 802 |         PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); | 
| Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 803 | #ifdef Py_DEBUG | 
 | 804 |         unicode_fill_invalid(unicode, old_length); | 
 | 805 | #endif | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 806 |         if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 807 |             assert(_PyUnicode_CheckConsistency(unicode, 0)); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 808 |             return 0; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 809 |         } | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 810 |     } | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 811 |     assert(_PyUnicode_WSTR(unicode) != NULL); | 
 | 812 |  | 
 | 813 |     /* check for integer overflow */ | 
 | 814 |     if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { | 
 | 815 |         PyErr_NoMemory(); | 
 | 816 |         return -1; | 
 | 817 |     } | 
| Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 818 |     new_size = sizeof(wchar_t) * (length + 1); | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 819 |     wstr =  _PyUnicode_WSTR(unicode); | 
| Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 820 |     wstr = PyObject_REALLOC(wstr, new_size); | 
| Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 821 |     if (!wstr) { | 
 | 822 |         PyErr_NoMemory(); | 
 | 823 |         return -1; | 
 | 824 |     } | 
 | 825 |     _PyUnicode_WSTR(unicode) = wstr; | 
 | 826 |     _PyUnicode_WSTR(unicode)[length] = 0; | 
 | 827 |     _PyUnicode_WSTR_LENGTH(unicode) = length; | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 828 |     assert(_PyUnicode_CheckConsistency(unicode, 0)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 829 |     return 0; | 
 | 830 | } | 
 | 831 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 832 | static PyObject* | 
 | 833 | resize_copy(PyObject *unicode, Py_ssize_t length) | 
 | 834 | { | 
 | 835 |     Py_ssize_t copy_length; | 
| Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 836 |     if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 837 |         PyObject *copy; | 
| Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 838 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 839 |         if (PyUnicode_READY(unicode) == -1) | 
| Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 840 |             return NULL; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 841 |  | 
 | 842 |         copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); | 
 | 843 |         if (copy == NULL) | 
 | 844 |             return NULL; | 
 | 845 |  | 
 | 846 |         copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 847 |         _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 848 |         return copy; | 
| Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 849 |     } | 
 | 850 |     else { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 851 |         PyObject *w; | 
| Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 852 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 853 |         w = (PyObject*)_PyUnicode_New(length); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 854 |         if (w == NULL) | 
 | 855 |             return NULL; | 
 | 856 |         copy_length = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 857 |         copy_length = Py_MIN(copy_length, length); | 
| Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 858 |         Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), | 
 | 859 |                   copy_length * sizeof(wchar_t)); | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 860 |         return w; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 861 |     } | 
 | 862 | } | 
 | 863 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 864 | /* 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] | 865 |    Ux0000 terminated; some code (e.g. new_identifier) | 
 | 866 |    relies on that. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 867 |  | 
 | 868 |    XXX This allocator could further be enhanced by assuring that the | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 869 |    free list never reduces its size below 1. | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 870 |  | 
 | 871 | */ | 
 | 872 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 873 | static PyUnicodeObject * | 
 | 874 | _PyUnicode_New(Py_ssize_t length) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 875 | { | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 876 |     PyUnicodeObject *unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 877 |     size_t new_size; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 878 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 879 |     /* Optimization for empty strings */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 880 |     if (length == 0 && unicode_empty != NULL) { | 
 | 881 |         Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 882 |         return (PyUnicodeObject*)unicode_empty; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 883 |     } | 
 | 884 |  | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 885 |     /* Ensure we won't overflow the size. */ | 
 | 886 |     if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { | 
 | 887 |         return (PyUnicodeObject *)PyErr_NoMemory(); | 
 | 888 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 889 |     if (length < 0) { | 
 | 890 |         PyErr_SetString(PyExc_SystemError, | 
 | 891 |                         "Negative size passed to _PyUnicode_New"); | 
 | 892 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 893 |     } | 
 | 894 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 895 |     unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); | 
 | 896 |     if (unicode == NULL) | 
 | 897 |         return NULL; | 
 | 898 |     new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); | 
 | 899 |     _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); | 
 | 900 |     if (!_PyUnicode_WSTR(unicode)) { | 
| Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 901 |         Py_DECREF(unicode); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 902 |         PyErr_NoMemory(); | 
| Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 903 |         return NULL; | 
| Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 904 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 905 |  | 
| Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 906 |     /* Initialize the first element to guard against cases where | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 907 |      * the caller fails before initializing str -- unicode_resize() | 
 | 908 |      * reads str[0], and the Keep-Alive optimization can keep memory | 
 | 909 |      * allocated for str alive across a call to unicode_dealloc(unicode). | 
 | 910 |      * We don't want unicode_resize to read uninitialized memory in | 
 | 911 |      * that case. | 
 | 912 |      */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 913 |     _PyUnicode_WSTR(unicode)[0] = 0; | 
 | 914 |     _PyUnicode_WSTR(unicode)[length] = 0; | 
 | 915 |     _PyUnicode_WSTR_LENGTH(unicode) = length; | 
 | 916 |     _PyUnicode_HASH(unicode) = -1; | 
 | 917 |     _PyUnicode_STATE(unicode).interned = 0; | 
 | 918 |     _PyUnicode_STATE(unicode).kind = 0; | 
 | 919 |     _PyUnicode_STATE(unicode).compact = 0; | 
 | 920 |     _PyUnicode_STATE(unicode).ready = 0; | 
 | 921 |     _PyUnicode_STATE(unicode).ascii = 0; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 922 |     _PyUnicode_DATA_ANY(unicode) = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 923 |     _PyUnicode_LENGTH(unicode) = 0; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 924 |     _PyUnicode_UTF8(unicode) = NULL; | 
 | 925 |     _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 926 |     assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 927 |     return unicode; | 
 | 928 | } | 
 | 929 |  | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 930 | static const char* | 
 | 931 | unicode_kind_name(PyObject *unicode) | 
 | 932 | { | 
| Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 933 |     /* don't check consistency: unicode_kind_name() is called from | 
 | 934 |        _PyUnicode_Dump() */ | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 935 |     if (!PyUnicode_IS_COMPACT(unicode)) | 
 | 936 |     { | 
 | 937 |         if (!PyUnicode_IS_READY(unicode)) | 
 | 938 |             return "wstr"; | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 939 |         switch (PyUnicode_KIND(unicode)) | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 940 |         { | 
 | 941 |         case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 942 |             if (PyUnicode_IS_ASCII(unicode)) | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 943 |                 return "legacy ascii"; | 
 | 944 |             else | 
 | 945 |                 return "legacy latin1"; | 
 | 946 |         case PyUnicode_2BYTE_KIND: | 
 | 947 |             return "legacy UCS2"; | 
 | 948 |         case PyUnicode_4BYTE_KIND: | 
 | 949 |             return "legacy UCS4"; | 
 | 950 |         default: | 
 | 951 |             return "<legacy invalid kind>"; | 
 | 952 |         } | 
 | 953 |     } | 
 | 954 |     assert(PyUnicode_IS_READY(unicode)); | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 955 |     switch (PyUnicode_KIND(unicode)) { | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 956 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 957 |         if (PyUnicode_IS_ASCII(unicode)) | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 958 |             return "ascii"; | 
 | 959 |         else | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 960 |             return "latin1"; | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 961 |     case PyUnicode_2BYTE_KIND: | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 962 |         return "UCS2"; | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 963 |     case PyUnicode_4BYTE_KIND: | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 964 |         return "UCS4"; | 
| Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 965 |     default: | 
 | 966 |         return "<invalid compact kind>"; | 
 | 967 |     } | 
 | 968 | } | 
 | 969 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 970 | #ifdef Py_DEBUG | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 971 | /* Functions wrapping macros for use in debugger */ | 
 | 972 | char *_PyUnicode_utf8(void *unicode){ | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 973 |     return PyUnicode_UTF8(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | } | 
 | 975 |  | 
 | 976 | void *_PyUnicode_compact_data(void *unicode) { | 
 | 977 |     return _PyUnicode_COMPACT_DATA(unicode); | 
 | 978 | } | 
 | 979 | void *_PyUnicode_data(void *unicode){ | 
 | 980 |     printf("obj %p\n", unicode); | 
 | 981 |     printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); | 
 | 982 |     printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); | 
 | 983 |     printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); | 
 | 984 |     printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); | 
 | 985 |     printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); | 
 | 986 |     return PyUnicode_DATA(unicode); | 
 | 987 | } | 
| Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 988 |  | 
 | 989 | void | 
 | 990 | _PyUnicode_Dump(PyObject *op) | 
 | 991 | { | 
 | 992 |     PyASCIIObject *ascii = (PyASCIIObject *)op; | 
| Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 993 |     PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; | 
 | 994 |     PyUnicodeObject *unicode = (PyUnicodeObject *)op; | 
 | 995 |     void *data; | 
| Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 996 |  | 
| Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 997 |     if (ascii->state.compact) | 
| Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 998 |     { | 
 | 999 |         if (ascii->state.ascii) | 
 | 1000 |             data = (ascii + 1); | 
 | 1001 |         else | 
 | 1002 |             data = (compact + 1); | 
 | 1003 |     } | 
| Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1004 |     else | 
 | 1005 |         data = unicode->data.any; | 
| Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1006 |     printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); | 
 | 1007 |  | 
| Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1008 |     if (ascii->wstr == data) | 
 | 1009 |         printf("shared "); | 
 | 1010 |     printf("wstr=%p", ascii->wstr); | 
| Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1011 |  | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1012 |     if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { | 
| Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1013 |         printf(" (%zu), ", compact->wstr_length); | 
 | 1014 |         if (!ascii->state.compact && compact->utf8 == unicode->data.any) | 
 | 1015 |             printf("shared "); | 
 | 1016 |         printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); | 
| Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1017 |     } | 
| Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1018 |     printf(", data=%p\n", data); | 
| Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1019 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1020 | #endif | 
 | 1021 |  | 
 | 1022 | PyObject * | 
 | 1023 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) | 
 | 1024 | { | 
 | 1025 |     PyObject *obj; | 
 | 1026 |     PyCompactUnicodeObject *unicode; | 
 | 1027 |     void *data; | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1028 |     enum PyUnicode_Kind kind; | 
| Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1029 |     int is_sharing, is_ascii; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1030 |     Py_ssize_t char_size; | 
 | 1031 |     Py_ssize_t struct_size; | 
 | 1032 |  | 
 | 1033 |     /* Optimization for empty strings */ | 
 | 1034 |     if (size == 0 && unicode_empty != NULL) { | 
 | 1035 |         Py_INCREF(unicode_empty); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1036 |         return unicode_empty; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1037 |     } | 
 | 1038 |  | 
| Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1039 |     is_ascii = 0; | 
 | 1040 |     is_sharing = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1041 |     struct_size = sizeof(PyCompactUnicodeObject); | 
 | 1042 |     if (maxchar < 128) { | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1043 |         kind = PyUnicode_1BYTE_KIND; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 |         char_size = 1; | 
 | 1045 |         is_ascii = 1; | 
 | 1046 |         struct_size = sizeof(PyASCIIObject); | 
 | 1047 |     } | 
 | 1048 |     else if (maxchar < 256) { | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1049 |         kind = PyUnicode_1BYTE_KIND; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 |         char_size = 1; | 
 | 1051 |     } | 
 | 1052 |     else if (maxchar < 65536) { | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1053 |         kind = PyUnicode_2BYTE_KIND; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1054 |         char_size = 2; | 
 | 1055 |         if (sizeof(wchar_t) == 2) | 
 | 1056 |             is_sharing = 1; | 
 | 1057 |     } | 
 | 1058 |     else { | 
| Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1059 |         if (maxchar > MAX_UNICODE) { | 
 | 1060 |             PyErr_SetString(PyExc_SystemError, | 
 | 1061 |                             "invalid maximum character passed to PyUnicode_New"); | 
 | 1062 |             return NULL; | 
 | 1063 |         } | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1064 |         kind = PyUnicode_4BYTE_KIND; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1065 |         char_size = 4; | 
 | 1066 |         if (sizeof(wchar_t) == 4) | 
 | 1067 |             is_sharing = 1; | 
 | 1068 |     } | 
 | 1069 |  | 
 | 1070 |     /* Ensure we won't overflow the size. */ | 
 | 1071 |     if (size < 0) { | 
 | 1072 |         PyErr_SetString(PyExc_SystemError, | 
 | 1073 |                         "Negative size passed to PyUnicode_New"); | 
 | 1074 |         return NULL; | 
 | 1075 |     } | 
 | 1076 |     if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) | 
 | 1077 |         return PyErr_NoMemory(); | 
 | 1078 |  | 
 | 1079 |     /* Duplicated allocation code from _PyObject_New() instead of a call to | 
 | 1080 |      * PyObject_New() so we are able to allocate space for the object and | 
 | 1081 |      * it's data buffer. | 
 | 1082 |      */ | 
 | 1083 |     obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); | 
 | 1084 |     if (obj == NULL) | 
 | 1085 |         return PyErr_NoMemory(); | 
 | 1086 |     obj = PyObject_INIT(obj, &PyUnicode_Type); | 
 | 1087 |     if (obj == NULL) | 
 | 1088 |         return NULL; | 
 | 1089 |  | 
 | 1090 |     unicode = (PyCompactUnicodeObject *)obj; | 
 | 1091 |     if (is_ascii) | 
 | 1092 |         data = ((PyASCIIObject*)obj) + 1; | 
 | 1093 |     else | 
 | 1094 |         data = unicode + 1; | 
 | 1095 |     _PyUnicode_LENGTH(unicode) = size; | 
 | 1096 |     _PyUnicode_HASH(unicode) = -1; | 
 | 1097 |     _PyUnicode_STATE(unicode).interned = 0; | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1098 |     _PyUnicode_STATE(unicode).kind = kind; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1099 |     _PyUnicode_STATE(unicode).compact = 1; | 
 | 1100 |     _PyUnicode_STATE(unicode).ready = 1; | 
 | 1101 |     _PyUnicode_STATE(unicode).ascii = is_ascii; | 
 | 1102 |     if (is_ascii) { | 
 | 1103 |         ((char*)data)[size] = 0; | 
 | 1104 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 1105 |     } | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1106 |     else if (kind == PyUnicode_1BYTE_KIND) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1107 |         ((char*)data)[size] = 0; | 
 | 1108 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 1109 |         _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 |         unicode->utf8 = NULL; | 
| Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1111 |         unicode->utf8_length = 0; | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1112 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1113 |     else { | 
 | 1114 |         unicode->utf8 = NULL; | 
| Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1115 |         unicode->utf8_length = 0; | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1116 |         if (kind == PyUnicode_2BYTE_KIND) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1117 |             ((Py_UCS2*)data)[size] = 0; | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1118 |         else /* kind == PyUnicode_4BYTE_KIND */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1119 |             ((Py_UCS4*)data)[size] = 0; | 
 | 1120 |         if (is_sharing) { | 
 | 1121 |             _PyUnicode_WSTR_LENGTH(unicode) = size; | 
 | 1122 |             _PyUnicode_WSTR(unicode) = (wchar_t *)data; | 
 | 1123 |         } | 
 | 1124 |         else { | 
 | 1125 |             _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
 | 1126 |             _PyUnicode_WSTR(unicode) = NULL; | 
 | 1127 |         } | 
 | 1128 |     } | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1129 | #ifdef Py_DEBUG | 
| Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1130 |     unicode_fill_invalid((PyObject*)unicode, 0); | 
| Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1131 | #endif | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1132 |     assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1133 |     return obj; | 
 | 1134 | } | 
 | 1135 |  | 
 | 1136 | #if SIZEOF_WCHAR_T == 2 | 
 | 1137 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this | 
 | 1138 |    will decode surrogate pairs, the other conversions are implemented as macros | 
| Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1139 |    for efficiency. | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1140 |  | 
 | 1141 |    This function assumes that unicode can hold one more code point than wstr | 
 | 1142 |    characters for a terminating null character. */ | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1143 | static void | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1144 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1145 |                               PyObject *unicode) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | { | 
 | 1147 |     const wchar_t *iter; | 
 | 1148 |     Py_UCS4 *ucs4_out; | 
 | 1149 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1150 |     assert(unicode != NULL); | 
 | 1151 |     assert(_PyUnicode_CHECK(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1152 |     assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); | 
 | 1153 |     ucs4_out = PyUnicode_4BYTE_DATA(unicode); | 
 | 1154 |  | 
 | 1155 |     for (iter = begin; iter < end; ) { | 
 | 1156 |         assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + | 
 | 1157 |                            _PyUnicode_GET_LENGTH(unicode))); | 
| Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1158 |         if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) | 
 | 1159 |             && (iter+1) < end | 
 | 1160 |             && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1161 |         { | 
| Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1162 |             *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1163 |             iter += 2; | 
 | 1164 |         } | 
 | 1165 |         else { | 
 | 1166 |             *ucs4_out++ = *iter; | 
 | 1167 |             iter++; | 
 | 1168 |         } | 
 | 1169 |     } | 
 | 1170 |     assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + | 
 | 1171 |                         _PyUnicode_GET_LENGTH(unicode))); | 
 | 1172 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1173 | } | 
 | 1174 | #endif | 
 | 1175 |  | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1176 | static int | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1177 | unicode_check_modifiable(PyObject *unicode) | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1178 | { | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1179 |     if (!unicode_modifiable(unicode)) { | 
| Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1180 |         PyErr_SetString(PyExc_SystemError, | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1181 |                         "Cannot modify a string currently used"); | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1182 |         return -1; | 
 | 1183 |     } | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1184 |     return 0; | 
 | 1185 | } | 
 | 1186 |  | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1187 | static int | 
 | 1188 | _copy_characters(PyObject *to, Py_ssize_t to_start, | 
 | 1189 |                  PyObject *from, Py_ssize_t from_start, | 
 | 1190 |                  Py_ssize_t how_many, int check_maxchar) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1191 | { | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1192 |     unsigned int from_kind, to_kind; | 
 | 1193 |     void *from_data, *to_data; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1194 |  | 
| Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1195 |     assert(0 <= how_many); | 
 | 1196 |     assert(0 <= from_start); | 
 | 1197 |     assert(0 <= to_start); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1198 |     assert(PyUnicode_Check(from)); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1199 |     assert(PyUnicode_IS_READY(from)); | 
| Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1200 |     assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1201 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1202 |     assert(PyUnicode_Check(to)); | 
 | 1203 |     assert(PyUnicode_IS_READY(to)); | 
 | 1204 |     assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); | 
 | 1205 |  | 
| Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1206 |     if (how_many == 0) | 
 | 1207 |         return 0; | 
 | 1208 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1209 |     from_kind = PyUnicode_KIND(from); | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1210 |     from_data = PyUnicode_DATA(from); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1211 |     to_kind = PyUnicode_KIND(to); | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1212 |     to_data = PyUnicode_DATA(to); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1213 |  | 
| Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1214 | #ifdef Py_DEBUG | 
 | 1215 |     if (!check_maxchar | 
 | 1216 |         && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) | 
 | 1217 |     { | 
 | 1218 |         const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); | 
 | 1219 |         Py_UCS4 ch; | 
 | 1220 |         Py_ssize_t i; | 
 | 1221 |         for (i=0; i < how_many; i++) { | 
 | 1222 |             ch = PyUnicode_READ(from_kind, from_data, from_start + i); | 
 | 1223 |             assert(ch <= to_maxchar); | 
 | 1224 |         } | 
 | 1225 |     } | 
 | 1226 | #endif | 
 | 1227 |  | 
| Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1228 |     if (from_kind == to_kind) { | 
| Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1229 |         if (check_maxchar | 
 | 1230 |             && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) | 
 | 1231 |         { | 
| Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1232 |             /* Writing Latin-1 characters into an ASCII string requires to | 
 | 1233 |                check that all written characters are pure ASCII */ | 
| Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1234 |             Py_UCS4 max_char; | 
 | 1235 |             max_char = ucs1lib_find_max_char(from_data, | 
 | 1236 |                                              (Py_UCS1*)from_data + how_many); | 
 | 1237 |             if (max_char >= 128) | 
 | 1238 |                 return -1; | 
| Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1239 |         } | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1240 |         Py_MEMCPY((char*)to_data + to_kind * to_start, | 
 | 1241 |                   (char*)from_data + from_kind * from_start, | 
 | 1242 |                   to_kind * how_many); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1243 |     } | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1244 |     else if (from_kind == PyUnicode_1BYTE_KIND | 
 | 1245 |              && to_kind == PyUnicode_2BYTE_KIND) | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1246 |     { | 
 | 1247 |         _PyUnicode_CONVERT_BYTES( | 
 | 1248 |             Py_UCS1, Py_UCS2, | 
 | 1249 |             PyUnicode_1BYTE_DATA(from) + from_start, | 
 | 1250 |             PyUnicode_1BYTE_DATA(from) + from_start + how_many, | 
 | 1251 |             PyUnicode_2BYTE_DATA(to) + to_start | 
 | 1252 |             ); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1253 |     } | 
| Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1254 |     else if (from_kind == PyUnicode_1BYTE_KIND | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1255 |              && to_kind == PyUnicode_4BYTE_KIND) | 
 | 1256 |     { | 
 | 1257 |         _PyUnicode_CONVERT_BYTES( | 
 | 1258 |             Py_UCS1, Py_UCS4, | 
 | 1259 |             PyUnicode_1BYTE_DATA(from) + from_start, | 
 | 1260 |             PyUnicode_1BYTE_DATA(from) + from_start + how_many, | 
 | 1261 |             PyUnicode_4BYTE_DATA(to) + to_start | 
 | 1262 |             ); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1263 |     } | 
 | 1264 |     else if (from_kind == PyUnicode_2BYTE_KIND | 
 | 1265 |              && to_kind == PyUnicode_4BYTE_KIND) | 
 | 1266 |     { | 
 | 1267 |         _PyUnicode_CONVERT_BYTES( | 
 | 1268 |             Py_UCS2, Py_UCS4, | 
 | 1269 |             PyUnicode_2BYTE_DATA(from) + from_start, | 
 | 1270 |             PyUnicode_2BYTE_DATA(from) + from_start + how_many, | 
 | 1271 |             PyUnicode_4BYTE_DATA(to) + to_start | 
 | 1272 |             ); | 
| Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1273 |     } | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1274 |     else { | 
| Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1275 |         assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); | 
 | 1276 |  | 
| Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1277 |         if (!check_maxchar) { | 
 | 1278 |             if (from_kind == PyUnicode_2BYTE_KIND | 
 | 1279 |                 && to_kind == PyUnicode_1BYTE_KIND) | 
 | 1280 |             { | 
 | 1281 |                 _PyUnicode_CONVERT_BYTES( | 
 | 1282 |                     Py_UCS2, Py_UCS1, | 
 | 1283 |                     PyUnicode_2BYTE_DATA(from) + from_start, | 
 | 1284 |                     PyUnicode_2BYTE_DATA(from) + from_start + how_many, | 
 | 1285 |                     PyUnicode_1BYTE_DATA(to) + to_start | 
 | 1286 |                     ); | 
 | 1287 |             } | 
 | 1288 |             else if (from_kind == PyUnicode_4BYTE_KIND | 
 | 1289 |                      && to_kind == PyUnicode_1BYTE_KIND) | 
 | 1290 |             { | 
 | 1291 |                 _PyUnicode_CONVERT_BYTES( | 
 | 1292 |                     Py_UCS4, Py_UCS1, | 
 | 1293 |                     PyUnicode_4BYTE_DATA(from) + from_start, | 
 | 1294 |                     PyUnicode_4BYTE_DATA(from) + from_start + how_many, | 
 | 1295 |                     PyUnicode_1BYTE_DATA(to) + to_start | 
 | 1296 |                     ); | 
 | 1297 |             } | 
 | 1298 |             else if (from_kind == PyUnicode_4BYTE_KIND | 
 | 1299 |                      && to_kind == PyUnicode_2BYTE_KIND) | 
 | 1300 |             { | 
 | 1301 |                 _PyUnicode_CONVERT_BYTES( | 
 | 1302 |                     Py_UCS4, Py_UCS2, | 
 | 1303 |                     PyUnicode_4BYTE_DATA(from) + from_start, | 
 | 1304 |                     PyUnicode_4BYTE_DATA(from) + from_start + how_many, | 
 | 1305 |                     PyUnicode_2BYTE_DATA(to) + to_start | 
 | 1306 |                     ); | 
 | 1307 |             } | 
 | 1308 |             else { | 
 | 1309 |                 assert(0); | 
 | 1310 |                 return -1; | 
 | 1311 |             } | 
 | 1312 |         } | 
| Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1313 |         else { | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1314 |             const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1315 |             Py_UCS4 ch; | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1316 |             Py_ssize_t i; | 
 | 1317 |  | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1318 |             for (i=0; i < how_many; i++) { | 
 | 1319 |                 ch = PyUnicode_READ(from_kind, from_data, from_start + i); | 
| Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1320 |                 if (ch > to_maxchar) | 
 | 1321 |                     return -1; | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1322 |                 PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); | 
 | 1323 |             } | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1324 |         } | 
 | 1325 |     } | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1326 |     return 0; | 
 | 1327 | } | 
 | 1328 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1329 | void | 
 | 1330 | _PyUnicode_FastCopyCharacters( | 
 | 1331 |     PyObject *to, Py_ssize_t to_start, | 
 | 1332 |     PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1333 | { | 
 | 1334 |     (void)_copy_characters(to, to_start, from, from_start, how_many, 0); | 
 | 1335 | } | 
 | 1336 |  | 
 | 1337 | Py_ssize_t | 
 | 1338 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, | 
 | 1339 |                          PyObject *from, Py_ssize_t from_start, | 
 | 1340 |                          Py_ssize_t how_many) | 
 | 1341 | { | 
 | 1342 |     int err; | 
 | 1343 |  | 
 | 1344 |     if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { | 
 | 1345 |         PyErr_BadInternalCall(); | 
 | 1346 |         return -1; | 
 | 1347 |     } | 
 | 1348 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1349 |     if (PyUnicode_READY(from) == -1) | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1350 |         return -1; | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1351 |     if (PyUnicode_READY(to) == -1) | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1352 |         return -1; | 
 | 1353 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1354 |     if (from_start < 0) { | 
 | 1355 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 1356 |         return -1; | 
 | 1357 |     } | 
 | 1358 |     if (to_start < 0) { | 
 | 1359 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 1360 |         return -1; | 
 | 1361 |     } | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1362 |     how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); | 
 | 1363 |     if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { | 
 | 1364 |         PyErr_Format(PyExc_SystemError, | 
 | 1365 |                      "Cannot write %zi characters at %zi " | 
 | 1366 |                      "in a string of %zi characters", | 
 | 1367 |                      how_many, to_start, PyUnicode_GET_LENGTH(to)); | 
 | 1368 |         return -1; | 
 | 1369 |     } | 
 | 1370 |  | 
 | 1371 |     if (how_many == 0) | 
 | 1372 |         return 0; | 
 | 1373 |  | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1374 |     if (unicode_check_modifiable(to)) | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1375 |         return -1; | 
 | 1376 |  | 
 | 1377 |     err = _copy_characters(to, to_start, from, from_start, how_many, 1); | 
 | 1378 |     if (err) { | 
 | 1379 |         PyErr_Format(PyExc_SystemError, | 
 | 1380 |                      "Cannot copy %s characters " | 
 | 1381 |                      "into a string of %s characters", | 
 | 1382 |                      unicode_kind_name(from), | 
 | 1383 |                      unicode_kind_name(to)); | 
 | 1384 |         return -1; | 
 | 1385 |     } | 
| Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1386 |     return how_many; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1387 | } | 
 | 1388 |  | 
| Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1389 | /* Find the maximum code point and count the number of surrogate pairs so a | 
 | 1390 |    correct string length can be computed before converting a string to UCS4. | 
 | 1391 |    This function counts single surrogates as a character and not as a pair. | 
 | 1392 |  | 
 | 1393 |    Return 0 on success, or -1 on error. */ | 
 | 1394 | static int | 
 | 1395 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, | 
 | 1396 |                         Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1397 | { | 
 | 1398 |     const wchar_t *iter; | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1399 |     Py_UCS4 ch; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 |  | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1401 |     assert(num_surrogates != NULL && maxchar != NULL); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1402 |     *num_surrogates = 0; | 
 | 1403 |     *maxchar = 0; | 
 | 1404 |  | 
 | 1405 |     for (iter = begin; iter < end; ) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1406 | #if SIZEOF_WCHAR_T == 2 | 
| Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1407 |         if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) | 
 | 1408 |             && (iter+1) < end | 
 | 1409 |             && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) | 
 | 1410 |         { | 
 | 1411 |             ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); | 
 | 1412 |             ++(*num_surrogates); | 
 | 1413 |             iter += 2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1414 |         } | 
 | 1415 |         else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1416 | #endif | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1417 |         { | 
 | 1418 |             ch = *iter; | 
 | 1419 |             iter++; | 
 | 1420 |         } | 
 | 1421 |         if (ch > *maxchar) { | 
 | 1422 |             *maxchar = ch; | 
 | 1423 |             if (*maxchar > MAX_UNICODE) { | 
 | 1424 |                 PyErr_Format(PyExc_ValueError, | 
 | 1425 |                              "character U+%x is not in range [U+0000; U+10ffff]", | 
 | 1426 |                              ch); | 
 | 1427 |                 return -1; | 
 | 1428 |             } | 
 | 1429 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1430 |     } | 
 | 1431 |     return 0; | 
 | 1432 | } | 
 | 1433 |  | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1434 | int | 
 | 1435 | _PyUnicode_Ready(PyObject *unicode) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1436 | { | 
 | 1437 |     wchar_t *end; | 
 | 1438 |     Py_UCS4 maxchar = 0; | 
 | 1439 |     Py_ssize_t num_surrogates; | 
 | 1440 | #if SIZEOF_WCHAR_T == 2 | 
 | 1441 |     Py_ssize_t length_wo_surrogates; | 
 | 1442 | #endif | 
 | 1443 |  | 
| Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1444 |     /* _PyUnicode_Ready() is only intended for old-style API usage where | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1445 |        strings were created using _PyObject_New() and where no canonical | 
 | 1446 |        representation (the str field) has been set yet aka strings | 
 | 1447 |        which are not yet ready. */ | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1448 |     assert(_PyUnicode_CHECK(unicode)); | 
 | 1449 |     assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1450 |     assert(_PyUnicode_WSTR(unicode) != NULL); | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1451 |     assert(_PyUnicode_DATA_ANY(unicode) == NULL); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1452 |     assert(_PyUnicode_UTF8(unicode) == NULL); | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1453 |     /* Actually, it should neither be interned nor be anything else: */ | 
 | 1454 |     assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1455 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1456 |     end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); | 
| Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1457 |     if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1458 |                                 &maxchar, &num_surrogates) == -1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1459 |         return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1460 |  | 
 | 1461 |     if (maxchar < 256) { | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1462 |         _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); | 
 | 1463 |         if (!_PyUnicode_DATA_ANY(unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1464 |             PyErr_NoMemory(); | 
 | 1465 |             return -1; | 
 | 1466 |         } | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1467 |         _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 |                                 _PyUnicode_WSTR(unicode), end, | 
 | 1469 |                                 PyUnicode_1BYTE_DATA(unicode)); | 
 | 1470 |         PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; | 
 | 1471 |         _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 1472 |         _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; | 
 | 1473 |         if (maxchar < 128) { | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1474 |             _PyUnicode_STATE(unicode).ascii = 1; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1475 |             _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1476 |             _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1477 |         } | 
 | 1478 |         else { | 
| Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1479 |             _PyUnicode_STATE(unicode).ascii = 0; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1480 |             _PyUnicode_UTF8(unicode) = NULL; | 
 | 1481 |             _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1482 |         } | 
 | 1483 |         PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
 | 1484 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 1485 |         _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
 | 1486 |     } | 
 | 1487 |     /* In this case we might have to convert down from 4-byte native | 
 | 1488 |        wchar_t to 2-byte unicode. */ | 
 | 1489 |     else if (maxchar < 65536) { | 
 | 1490 |         assert(num_surrogates == 0 && | 
 | 1491 |                "FindMaxCharAndNumSurrogatePairs() messed up"); | 
 | 1492 |  | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1493 | #if SIZEOF_WCHAR_T == 2 | 
 | 1494 |         /* We can share representations and are done. */ | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1495 |         _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1496 |         PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; | 
 | 1497 |         _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 1498 |         _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1499 |         _PyUnicode_UTF8(unicode) = NULL; | 
 | 1500 |         _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1501 | #else | 
 | 1502 |         /* sizeof(wchar_t) == 4 */ | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1503 |         _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1504 |             2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1505 |         if (!_PyUnicode_DATA_ANY(unicode)) { | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1506 |             PyErr_NoMemory(); | 
 | 1507 |             return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1508 |         } | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1509 |         _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, | 
 | 1510 |                                 _PyUnicode_WSTR(unicode), end, | 
 | 1511 |                                 PyUnicode_2BYTE_DATA(unicode)); | 
 | 1512 |         PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; | 
 | 1513 |         _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
 | 1514 |         _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1515 |         _PyUnicode_UTF8(unicode) = NULL; | 
 | 1516 |         _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1517 |         PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
 | 1518 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 1519 |         _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
 | 1520 | #endif | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1521 |     } | 
 | 1522 |     /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ | 
 | 1523 |     else { | 
 | 1524 | #if SIZEOF_WCHAR_T == 2 | 
 | 1525 |         /* in case the native representation is 2-bytes, we need to allocate a | 
 | 1526 |            new normalized 4-byte version. */ | 
 | 1527 |         length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1528 |         _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); | 
 | 1529 |         if (!_PyUnicode_DATA_ANY(unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1530 |             PyErr_NoMemory(); | 
 | 1531 |             return -1; | 
 | 1532 |         } | 
 | 1533 |         _PyUnicode_LENGTH(unicode) = length_wo_surrogates; | 
 | 1534 |         _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1535 |         _PyUnicode_UTF8(unicode) = NULL; | 
 | 1536 |         _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1537 |         /* unicode_convert_wchar_to_ucs4() requires a ready string */ | 
 | 1538 |         _PyUnicode_STATE(unicode).ready = 1; | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1539 |         unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1540 |         PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
 | 1541 |         _PyUnicode_WSTR(unicode) = NULL; | 
 | 1542 |         _PyUnicode_WSTR_LENGTH(unicode) = 0; | 
 | 1543 | #else | 
 | 1544 |         assert(num_surrogates == 0); | 
 | 1545 |  | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1546 |         _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1547 |         _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1548 |         _PyUnicode_UTF8(unicode) = NULL; | 
 | 1549 |         _PyUnicode_UTF8_LENGTH(unicode) = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1550 |         _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; | 
 | 1551 | #endif | 
 | 1552 |         PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; | 
 | 1553 |     } | 
 | 1554 |     _PyUnicode_STATE(unicode).ready = 1; | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1555 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1556 |     return 0; | 
 | 1557 | } | 
 | 1558 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1559 | static void | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1560 | unicode_dealloc(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1561 | { | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1562 |     switch (PyUnicode_CHECK_INTERNED(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1563 |     case SSTATE_NOT_INTERNED: | 
 | 1564 |         break; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1565 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1566 |     case SSTATE_INTERNED_MORTAL: | 
 | 1567 |         /* revive dead object temporarily for DelItem */ | 
 | 1568 |         Py_REFCNT(unicode) = 3; | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1569 |         if (PyDict_DelItem(interned, unicode) != 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1570 |             Py_FatalError( | 
 | 1571 |                 "deletion of interned string failed"); | 
 | 1572 |         break; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1573 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1574 |     case SSTATE_INTERNED_IMMORTAL: | 
 | 1575 |         Py_FatalError("Immortal interned string died."); | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1576 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1577 |     default: | 
 | 1578 |         Py_FatalError("Inconsistent interned string state."); | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1579 |     } | 
 | 1580 |  | 
| Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1581 |     if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1582 |         PyObject_DEL(_PyUnicode_WSTR(unicode)); | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1583 |     if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1584 |         PyObject_DEL(_PyUnicode_UTF8(unicode)); | 
| Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1585 |     if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) | 
 | 1586 |         PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1587 |  | 
| Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1588 |     Py_TYPE(unicode)->tp_free(unicode); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1589 | } | 
 | 1590 |  | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1591 | #ifdef Py_DEBUG | 
 | 1592 | static int | 
 | 1593 | unicode_is_singleton(PyObject *unicode) | 
 | 1594 | { | 
 | 1595 |     PyASCIIObject *ascii = (PyASCIIObject *)unicode; | 
 | 1596 |     if (unicode == unicode_empty) | 
 | 1597 |         return 1; | 
 | 1598 |     if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) | 
 | 1599 |     { | 
 | 1600 |         Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); | 
 | 1601 |         if (ch < 256 && unicode_latin1[ch] == unicode) | 
 | 1602 |             return 1; | 
 | 1603 |     } | 
 | 1604 |     return 0; | 
 | 1605 | } | 
 | 1606 | #endif | 
 | 1607 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1608 | static int | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1609 | unicode_modifiable(PyObject *unicode) | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1610 | { | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1611 |     assert(_PyUnicode_CHECK(unicode)); | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1612 |     if (Py_REFCNT(unicode) != 1) | 
 | 1613 |         return 0; | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1614 |     if (_PyUnicode_HASH(unicode) != -1) | 
 | 1615 |         return 0; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1616 |     if (PyUnicode_CHECK_INTERNED(unicode)) | 
 | 1617 |         return 0; | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1618 |     if (!PyUnicode_CheckExact(unicode)) | 
 | 1619 |         return 0; | 
| Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1620 | #ifdef Py_DEBUG | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1621 |     /* singleton refcount is greater than 1 */ | 
 | 1622 |     assert(!unicode_is_singleton(unicode)); | 
| Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1623 | #endif | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1624 |     return 1; | 
 | 1625 | } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1626 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1627 | static int | 
 | 1628 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) | 
 | 1629 | { | 
 | 1630 |     PyObject *unicode; | 
 | 1631 |     Py_ssize_t old_length; | 
 | 1632 |  | 
 | 1633 |     assert(p_unicode != NULL); | 
 | 1634 |     unicode = *p_unicode; | 
 | 1635 |  | 
 | 1636 |     assert(unicode != NULL); | 
 | 1637 |     assert(PyUnicode_Check(unicode)); | 
 | 1638 |     assert(0 <= length); | 
 | 1639 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1640 |     if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1641 |         old_length = PyUnicode_WSTR_LENGTH(unicode); | 
 | 1642 |     else | 
 | 1643 |         old_length = PyUnicode_GET_LENGTH(unicode); | 
 | 1644 |     if (old_length == length) | 
 | 1645 |         return 0; | 
 | 1646 |  | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1647 |     if (length == 0) { | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1648 |         _Py_INCREF_UNICODE_EMPTY(); | 
 | 1649 |         if (!unicode_empty) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1650 |             return -1; | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1651 |         Py_DECREF(*p_unicode); | 
 | 1652 |         *p_unicode = unicode_empty; | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1653 |         return 0; | 
 | 1654 |     } | 
 | 1655 |  | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1656 |     if (!unicode_modifiable(unicode)) { | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1657 |         PyObject *copy = resize_copy(unicode, length); | 
 | 1658 |         if (copy == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1659 |             return -1; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1660 |         Py_DECREF(*p_unicode); | 
 | 1661 |         *p_unicode = copy; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1662 |         return 0; | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1663 |     } | 
 | 1664 |  | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1665 |     if (PyUnicode_IS_COMPACT(unicode)) { | 
| Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1666 |         PyObject *new_unicode = resize_compact(unicode, length); | 
 | 1667 |         if (new_unicode == NULL) | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1668 |             return -1; | 
| Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1669 |         *p_unicode = new_unicode; | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1670 |         return 0; | 
| Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1671 |     } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1672 |     return resize_inplace(unicode, length); | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1673 | } | 
 | 1674 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1675 | int | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1676 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) | 
| Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1677 | { | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1678 |     PyObject *unicode; | 
 | 1679 |     if (p_unicode == NULL) { | 
 | 1680 |         PyErr_BadInternalCall(); | 
 | 1681 |         return -1; | 
 | 1682 |     } | 
 | 1683 |     unicode = *p_unicode; | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1684 |     if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) | 
| Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1685 |     { | 
 | 1686 |         PyErr_BadInternalCall(); | 
 | 1687 |         return -1; | 
 | 1688 |     } | 
 | 1689 |     return unicode_resize(p_unicode, length); | 
| Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1690 | } | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1691 |  | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1692 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1693 |  | 
| Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1694 |    WARNING: The function doesn't copy the terminating null character and | 
 | 1695 |    doesn't check the maximum character (may write a latin1 character in an | 
 | 1696 |    ASCII string). */ | 
| Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1697 | static void | 
 | 1698 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, | 
 | 1699 |                    const char *str, Py_ssize_t len) | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1700 | { | 
 | 1701 |     enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); | 
 | 1702 |     void *data = PyUnicode_DATA(unicode); | 
| Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1703 |     const char *end = str + len; | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1704 |  | 
 | 1705 |     switch (kind) { | 
 | 1706 |     case PyUnicode_1BYTE_KIND: { | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1707 |         assert(index + len <= PyUnicode_GET_LENGTH(unicode)); | 
| Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1708 | #ifdef Py_DEBUG | 
 | 1709 |         if (PyUnicode_IS_ASCII(unicode)) { | 
 | 1710 |             Py_UCS4 maxchar = ucs1lib_find_max_char( | 
 | 1711 |                 (const Py_UCS1*)str, | 
 | 1712 |                 (const Py_UCS1*)str + len); | 
 | 1713 |             assert(maxchar < 128); | 
 | 1714 |         } | 
 | 1715 | #endif | 
| Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1716 |         memcpy((char *) data + index, str, len); | 
| Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1717 |         break; | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1718 |     } | 
 | 1719 |     case PyUnicode_2BYTE_KIND: { | 
 | 1720 |         Py_UCS2 *start = (Py_UCS2 *)data + index; | 
 | 1721 |         Py_UCS2 *ucs2 = start; | 
 | 1722 |         assert(index <= PyUnicode_GET_LENGTH(unicode)); | 
 | 1723 |  | 
| Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1724 |         for (; str < end; ++ucs2, ++str) | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1725 |             *ucs2 = (Py_UCS2)*str; | 
 | 1726 |  | 
 | 1727 |         assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); | 
| Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1728 |         break; | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1729 |     } | 
 | 1730 |     default: { | 
 | 1731 |         Py_UCS4 *start = (Py_UCS4 *)data + index; | 
 | 1732 |         Py_UCS4 *ucs4 = start; | 
 | 1733 |         assert(kind == PyUnicode_4BYTE_KIND); | 
 | 1734 |         assert(index <= PyUnicode_GET_LENGTH(unicode)); | 
 | 1735 |  | 
| Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1736 |         for (; str < end; ++ucs4, ++str) | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1737 |             *ucs4 = (Py_UCS4)*str; | 
 | 1738 |  | 
 | 1739 |         assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); | 
| Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1740 |     } | 
 | 1741 |     } | 
 | 1742 | } | 
 | 1743 |  | 
 | 1744 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1745 | static PyObject* | 
 | 1746 | get_latin1_char(unsigned char ch) | 
 | 1747 | { | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1748 |     PyObject *unicode = unicode_latin1[ch]; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1749 |     if (!unicode) { | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1750 |         unicode = PyUnicode_New(1, ch); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1751 |         if (!unicode) | 
 | 1752 |             return NULL; | 
 | 1753 |         PyUnicode_1BYTE_DATA(unicode)[0] = ch; | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1754 |         assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1755 |         unicode_latin1[ch] = unicode; | 
 | 1756 |     } | 
 | 1757 |     Py_INCREF(unicode); | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1758 |     return unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1759 | } | 
 | 1760 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1761 | PyObject * | 
 | 1762 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1763 | { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1764 |     PyObject *unicode; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1765 |     Py_UCS4 maxchar = 0; | 
 | 1766 |     Py_ssize_t num_surrogates; | 
 | 1767 |  | 
 | 1768 |     if (u == NULL) | 
 | 1769 |         return (PyObject*)_PyUnicode_New(size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1770 |  | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1771 |     /* If the Unicode data is known at construction time, we can apply | 
 | 1772 |        some optimizations which share commonly used objects. */ | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1773 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1774 |     /* Optimization for empty strings */ | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1775 |     if (size == 0) | 
 | 1776 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1777 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1778 |     /* Single character Unicode objects in the Latin-1 range are | 
 | 1779 |        shared when using this constructor */ | 
| Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1780 |     if (size == 1 && (Py_UCS4)*u < 256) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1781 |         return get_latin1_char((unsigned char)*u); | 
 | 1782 |  | 
 | 1783 |     /* If not empty and not single character, copy the Unicode data | 
 | 1784 |        into the new object */ | 
| Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1785 |     if (find_maxchar_surrogates(u, u + size, | 
 | 1786 |                                 &maxchar, &num_surrogates) == -1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1787 |         return NULL; | 
 | 1788 |  | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1789 |     unicode = PyUnicode_New(size - num_surrogates, maxchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1790 |     if (!unicode) | 
 | 1791 |         return NULL; | 
 | 1792 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1793 |     switch (PyUnicode_KIND(unicode)) { | 
 | 1794 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1795 |         _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1796 |                                 u, u + size, PyUnicode_1BYTE_DATA(unicode)); | 
 | 1797 |         break; | 
 | 1798 |     case PyUnicode_2BYTE_KIND: | 
 | 1799 | #if Py_UNICODE_SIZE == 2 | 
 | 1800 |         Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); | 
 | 1801 | #else | 
| Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1802 |         _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1803 |                                 u, u + size, PyUnicode_2BYTE_DATA(unicode)); | 
 | 1804 | #endif | 
 | 1805 |         break; | 
 | 1806 |     case PyUnicode_4BYTE_KIND: | 
 | 1807 | #if SIZEOF_WCHAR_T == 2 | 
 | 1808 |         /* This is the only case which has to process surrogates, thus | 
 | 1809 |            a simple copy loop is not enough and we need a function. */ | 
| Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1810 |         unicode_convert_wchar_to_ucs4(u, u + size, unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1811 | #else | 
 | 1812 |         assert(num_surrogates == 0); | 
 | 1813 |         Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); | 
 | 1814 | #endif | 
 | 1815 |         break; | 
 | 1816 |     default: | 
 | 1817 |         assert(0 && "Impossible state"); | 
 | 1818 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1819 |  | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1820 |     return unicode_result(unicode); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1821 | } | 
 | 1822 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1823 | PyObject * | 
 | 1824 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1825 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1826 |     if (size < 0) { | 
 | 1827 |         PyErr_SetString(PyExc_SystemError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1828 |                         "Negative size passed to PyUnicode_FromStringAndSize"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1829 |         return NULL; | 
 | 1830 |     } | 
| Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1831 |     if (u != NULL) | 
 | 1832 |         return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); | 
 | 1833 |     else | 
 | 1834 |         return (PyObject *)_PyUnicode_New(size); | 
| Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1835 | } | 
 | 1836 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1837 | PyObject * | 
 | 1838 | PyUnicode_FromString(const char *u) | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1839 | { | 
 | 1840 |     size_t size = strlen(u); | 
 | 1841 |     if (size > PY_SSIZE_T_MAX) { | 
 | 1842 |         PyErr_SetString(PyExc_OverflowError, "input too long"); | 
 | 1843 |         return NULL; | 
 | 1844 |     } | 
| Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1845 |     return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1846 | } | 
 | 1847 |  | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1848 | PyObject * | 
 | 1849 | _PyUnicode_FromId(_Py_Identifier *id) | 
 | 1850 | { | 
 | 1851 |     if (!id->object) { | 
| Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1852 |         id->object = PyUnicode_DecodeUTF8Stateful(id->string, | 
 | 1853 |                                                   strlen(id->string), | 
 | 1854 |                                                   NULL, NULL); | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1855 |         if (!id->object) | 
 | 1856 |             return NULL; | 
 | 1857 |         PyUnicode_InternInPlace(&id->object); | 
 | 1858 |         assert(!id->next); | 
 | 1859 |         id->next = static_strings; | 
 | 1860 |         static_strings = id; | 
 | 1861 |     } | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1862 |     return id->object; | 
 | 1863 | } | 
 | 1864 |  | 
 | 1865 | void | 
 | 1866 | _PyUnicode_ClearStaticStrings() | 
 | 1867 | { | 
| Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1868 |     _Py_Identifier *tmp, *s = static_strings; | 
 | 1869 |     while (s) { | 
 | 1870 |         Py_DECREF(s->object); | 
 | 1871 |         s->object = NULL; | 
 | 1872 |         tmp = s->next; | 
 | 1873 |         s->next = NULL; | 
 | 1874 |         s = tmp; | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1875 |     } | 
| Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1876 |     static_strings = NULL; | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1877 | } | 
 | 1878 |  | 
| Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1879 | /* Internal function, doesn't check maximum character */ | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1880 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1881 | PyObject* | 
 | 1882 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1883 | { | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1884 |     const unsigned char *s = (const unsigned char *)buffer; | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1885 |     PyObject *unicode; | 
| Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1886 |     if (size == 1) { | 
| Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1887 | #ifdef Py_DEBUG | 
| Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1888 |         assert((unsigned char)s[0] < 128); | 
| Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1889 | #endif | 
| Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1890 |         return get_latin1_char(s[0]); | 
| Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1891 |     } | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1892 |     unicode = PyUnicode_New(size, 127); | 
 | 1893 |     if (!unicode) | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1894 |         return NULL; | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1895 |     memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); | 
 | 1896 |     assert(_PyUnicode_CheckConsistency(unicode, 1)); | 
 | 1897 |     return unicode; | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1898 | } | 
 | 1899 |  | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1900 | static Py_UCS4 | 
 | 1901 | kind_maxchar_limit(unsigned int kind) | 
 | 1902 | { | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1903 |     switch (kind) { | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1904 |     case PyUnicode_1BYTE_KIND: | 
 | 1905 |         return 0x80; | 
 | 1906 |     case PyUnicode_2BYTE_KIND: | 
 | 1907 |         return 0x100; | 
 | 1908 |     case PyUnicode_4BYTE_KIND: | 
 | 1909 |         return 0x10000; | 
 | 1910 |     default: | 
 | 1911 |         assert(0 && "invalid kind"); | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1912 |         return MAX_UNICODE; | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1913 |     } | 
 | 1914 | } | 
 | 1915 |  | 
| Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1916 | Py_LOCAL_INLINE(Py_UCS4) | 
 | 1917 | align_maxchar(Py_UCS4 maxchar) | 
 | 1918 | { | 
 | 1919 |     if (maxchar <= 127) | 
 | 1920 |         return 127; | 
 | 1921 |     else if (maxchar <= 255) | 
 | 1922 |         return 255; | 
 | 1923 |     else if (maxchar <= 65535) | 
 | 1924 |         return 65535; | 
 | 1925 |     else | 
 | 1926 |         return MAX_UNICODE; | 
 | 1927 | } | 
 | 1928 |  | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1929 | static PyObject* | 
| Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1930 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1931 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1932 |     PyObject *res; | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1933 |     unsigned char max_char; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1934 |  | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1935 |     if (size == 0) | 
 | 1936 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1937 |     assert(size > 0); | 
| Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1938 |     if (size == 1) | 
 | 1939 |         return get_latin1_char(u[0]); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1940 |  | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1941 |     max_char = ucs1lib_find_max_char(u, u + size); | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1942 |     res = PyUnicode_New(size, max_char); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1943 |     if (!res) | 
 | 1944 |         return NULL; | 
 | 1945 |     memcpy(PyUnicode_1BYTE_DATA(res), u, size); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1946 |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1947 |     return res; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1948 | } | 
 | 1949 |  | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1950 | static PyObject* | 
 | 1951 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1952 | { | 
 | 1953 |     PyObject *res; | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1954 |     Py_UCS2 max_char; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1955 |  | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1956 |     if (size == 0) | 
 | 1957 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1958 |     assert(size > 0); | 
| Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1959 |     if (size == 1) { | 
 | 1960 |         Py_UCS4 ch = u[0]; | 
| Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1961 |         int kind; | 
 | 1962 |         void *data; | 
| Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1963 |         if (ch < 256) | 
 | 1964 |             return get_latin1_char((unsigned char)ch); | 
 | 1965 |  | 
 | 1966 |         res = PyUnicode_New(1, ch); | 
 | 1967 |         if (res == NULL) | 
 | 1968 |             return NULL; | 
| Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1969 |         kind = PyUnicode_KIND(res); | 
 | 1970 |         data = PyUnicode_DATA(res); | 
 | 1971 |         PyUnicode_WRITE(kind, data, 0, ch); | 
| Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1972 |         assert(_PyUnicode_CheckConsistency(res, 1)); | 
 | 1973 |         return res; | 
 | 1974 |     } | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1975 |  | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1976 |     max_char = ucs2lib_find_max_char(u, u + size); | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1977 |     res = PyUnicode_New(size, max_char); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1978 |     if (!res) | 
 | 1979 |         return NULL; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1980 |     if (max_char >= 256) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1981 |         memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1982 |     else { | 
 | 1983 |         _PyUnicode_CONVERT_BYTES( | 
 | 1984 |             Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); | 
 | 1985 |     } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1986 |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1987 |     return res; | 
 | 1988 | } | 
 | 1989 |  | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1990 | static PyObject* | 
 | 1991 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | { | 
 | 1993 |     PyObject *res; | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1994 |     Py_UCS4 max_char; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1995 |  | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1996 |     if (size == 0) | 
 | 1997 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1998 |     assert(size > 0); | 
| Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1999 |     if (size == 1) { | 
 | 2000 |         Py_UCS4 ch = u[0]; | 
| Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2001 |         int kind; | 
 | 2002 |         void *data; | 
| Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2003 |         if (ch < 256) | 
 | 2004 |             return get_latin1_char((unsigned char)ch); | 
 | 2005 |  | 
 | 2006 |         res = PyUnicode_New(1, ch); | 
 | 2007 |         if (res == NULL) | 
 | 2008 |             return NULL; | 
| Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2009 |         kind = PyUnicode_KIND(res); | 
 | 2010 |         data = PyUnicode_DATA(res); | 
 | 2011 |         PyUnicode_WRITE(kind, data, 0, ch); | 
| Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2012 |         assert(_PyUnicode_CheckConsistency(res, 1)); | 
 | 2013 |         return res; | 
 | 2014 |     } | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2015 |  | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2016 |     max_char = ucs4lib_find_max_char(u, u + size); | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2017 |     res = PyUnicode_New(size, max_char); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2018 |     if (!res) | 
 | 2019 |         return NULL; | 
| Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2020 |     if (max_char < 256) | 
 | 2021 |         _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, | 
 | 2022 |                                  PyUnicode_1BYTE_DATA(res)); | 
 | 2023 |     else if (max_char < 0x10000) | 
 | 2024 |         _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, | 
 | 2025 |                                  PyUnicode_2BYTE_DATA(res)); | 
 | 2026 |     else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2027 |         memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2028 |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2029 |     return res; | 
 | 2030 | } | 
 | 2031 |  | 
 | 2032 | PyObject* | 
 | 2033 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) | 
 | 2034 | { | 
| Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2035 |     if (size < 0) { | 
 | 2036 |         PyErr_SetString(PyExc_ValueError, "size must be positive"); | 
 | 2037 |         return NULL; | 
 | 2038 |     } | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2039 |     switch (kind) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2040 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2041 |         return _PyUnicode_FromUCS1(buffer, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2042 |     case PyUnicode_2BYTE_KIND: | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2043 |         return _PyUnicode_FromUCS2(buffer, size); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2044 |     case PyUnicode_4BYTE_KIND: | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2045 |         return _PyUnicode_FromUCS4(buffer, size); | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2046 |     default: | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2047 |         PyErr_SetString(PyExc_SystemError, "invalid kind"); | 
 | 2048 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2049 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2050 | } | 
 | 2051 |  | 
| Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2052 | Py_UCS4 | 
 | 2053 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) | 
 | 2054 | { | 
 | 2055 |     enum PyUnicode_Kind kind; | 
 | 2056 |     void *startptr, *endptr; | 
 | 2057 |  | 
 | 2058 |     assert(PyUnicode_IS_READY(unicode)); | 
 | 2059 |     assert(0 <= start); | 
 | 2060 |     assert(end <= PyUnicode_GET_LENGTH(unicode)); | 
 | 2061 |     assert(start <= end); | 
 | 2062 |  | 
 | 2063 |     if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) | 
 | 2064 |         return PyUnicode_MAX_CHAR_VALUE(unicode); | 
 | 2065 |  | 
 | 2066 |     if (start == end) | 
 | 2067 |         return 127; | 
 | 2068 |  | 
| Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2069 |     if (PyUnicode_IS_ASCII(unicode)) | 
 | 2070 |         return 127; | 
 | 2071 |  | 
| Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2072 |     kind = PyUnicode_KIND(unicode); | 
| Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2073 |     startptr = PyUnicode_DATA(unicode); | 
| Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2074 |     endptr = (char *)startptr + end * kind; | 
 | 2075 |     startptr = (char *)startptr + start * kind; | 
| Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2076 |     switch(kind) { | 
 | 2077 |     case PyUnicode_1BYTE_KIND: | 
 | 2078 |         return ucs1lib_find_max_char(startptr, endptr); | 
 | 2079 |     case PyUnicode_2BYTE_KIND: | 
 | 2080 |         return ucs2lib_find_max_char(startptr, endptr); | 
 | 2081 |     case PyUnicode_4BYTE_KIND: | 
 | 2082 |         return ucs4lib_find_max_char(startptr, endptr); | 
| Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2083 |     default: | 
| Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2084 |         assert(0); | 
 | 2085 |         return 0; | 
| Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2086 |     } | 
 | 2087 | } | 
 | 2088 |  | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2089 | /* Ensure that a string uses the most efficient storage, if it is not the | 
 | 2090 |    case: create a new string with of the right kind. Write NULL into *p_unicode | 
 | 2091 |    on error. */ | 
| Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2092 | static void | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2093 | unicode_adjust_maxchar(PyObject **p_unicode) | 
 | 2094 | { | 
 | 2095 |     PyObject *unicode, *copy; | 
 | 2096 |     Py_UCS4 max_char; | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2097 |     Py_ssize_t len; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2098 |     unsigned int kind; | 
 | 2099 |  | 
 | 2100 |     assert(p_unicode != NULL); | 
 | 2101 |     unicode = *p_unicode; | 
 | 2102 |     assert(PyUnicode_IS_READY(unicode)); | 
 | 2103 |     if (PyUnicode_IS_ASCII(unicode)) | 
 | 2104 |         return; | 
 | 2105 |  | 
 | 2106 |     len = PyUnicode_GET_LENGTH(unicode); | 
 | 2107 |     kind = PyUnicode_KIND(unicode); | 
 | 2108 |     if (kind == PyUnicode_1BYTE_KIND) { | 
 | 2109 |         const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2110 |         max_char = ucs1lib_find_max_char(u, u + len); | 
 | 2111 |         if (max_char >= 128) | 
 | 2112 |             return; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2113 |     } | 
 | 2114 |     else if (kind == PyUnicode_2BYTE_KIND) { | 
 | 2115 |         const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2116 |         max_char = ucs2lib_find_max_char(u, u + len); | 
 | 2117 |         if (max_char >= 256) | 
 | 2118 |             return; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2119 |     } | 
 | 2120 |     else { | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2121 |         const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2122 |         assert(kind == PyUnicode_4BYTE_KIND); | 
| Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2123 |         max_char = ucs4lib_find_max_char(u, u + len); | 
 | 2124 |         if (max_char >= 0x10000) | 
 | 2125 |             return; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2126 |     } | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2127 |     copy = PyUnicode_New(len, max_char); | 
| Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2128 |     if (copy != NULL) | 
 | 2129 |         _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2130 |     Py_DECREF(unicode); | 
 | 2131 |     *p_unicode = copy; | 
 | 2132 | } | 
 | 2133 |  | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2134 | PyObject* | 
| Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2135 | _PyUnicode_Copy(PyObject *unicode) | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2136 | { | 
| Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2137 |     Py_ssize_t length; | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2138 |     PyObject *copy; | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2139 |  | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2140 |     if (!PyUnicode_Check(unicode)) { | 
 | 2141 |         PyErr_BadInternalCall(); | 
 | 2142 |         return NULL; | 
 | 2143 |     } | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2144 |     if (PyUnicode_READY(unicode) == -1) | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2145 |         return NULL; | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2146 |  | 
| Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2147 |     length = PyUnicode_GET_LENGTH(unicode); | 
 | 2148 |     copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2149 |     if (!copy) | 
 | 2150 |         return NULL; | 
 | 2151 |     assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); | 
 | 2152 |  | 
| Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2153 |     Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), | 
 | 2154 |               length * PyUnicode_KIND(unicode)); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2155 |     assert(_PyUnicode_CheckConsistency(copy, 1)); | 
| Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2156 |     return copy; | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2157 | } | 
 | 2158 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2159 |  | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2160 | /* Widen Unicode objects to larger buffers. Don't write terminating null | 
 | 2161 |    character. Return NULL on error. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2162 |  | 
 | 2163 | void* | 
 | 2164 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) | 
 | 2165 | { | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2166 |     Py_ssize_t len; | 
 | 2167 |     void *result; | 
 | 2168 |     unsigned int skind; | 
 | 2169 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2170 |     if (PyUnicode_READY(s) == -1) | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2171 |         return NULL; | 
 | 2172 |  | 
 | 2173 |     len = PyUnicode_GET_LENGTH(s); | 
 | 2174 |     skind = PyUnicode_KIND(s); | 
 | 2175 |     if (skind >= kind) { | 
| Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2176 |         PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2177 |         return NULL; | 
 | 2178 |     } | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2179 |     switch (kind) { | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2180 |     case PyUnicode_2BYTE_KIND: | 
 | 2181 |         result = PyMem_Malloc(len * sizeof(Py_UCS2)); | 
 | 2182 |         if (!result) | 
 | 2183 |             return PyErr_NoMemory(); | 
 | 2184 |         assert(skind == PyUnicode_1BYTE_KIND); | 
 | 2185 |         _PyUnicode_CONVERT_BYTES( | 
 | 2186 |             Py_UCS1, Py_UCS2, | 
 | 2187 |             PyUnicode_1BYTE_DATA(s), | 
 | 2188 |             PyUnicode_1BYTE_DATA(s) + len, | 
 | 2189 |             result); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2190 |         return result; | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2191 |     case PyUnicode_4BYTE_KIND: | 
 | 2192 |         result = PyMem_Malloc(len * sizeof(Py_UCS4)); | 
 | 2193 |         if (!result) | 
 | 2194 |             return PyErr_NoMemory(); | 
 | 2195 |         if (skind == PyUnicode_2BYTE_KIND) { | 
 | 2196 |             _PyUnicode_CONVERT_BYTES( | 
 | 2197 |                 Py_UCS2, Py_UCS4, | 
 | 2198 |                 PyUnicode_2BYTE_DATA(s), | 
 | 2199 |                 PyUnicode_2BYTE_DATA(s) + len, | 
 | 2200 |                 result); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2201 |         } | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2202 |         else { | 
 | 2203 |             assert(skind == PyUnicode_1BYTE_KIND); | 
 | 2204 |             _PyUnicode_CONVERT_BYTES( | 
 | 2205 |                 Py_UCS1, Py_UCS4, | 
 | 2206 |                 PyUnicode_1BYTE_DATA(s), | 
 | 2207 |                 PyUnicode_1BYTE_DATA(s) + len, | 
 | 2208 |                 result); | 
 | 2209 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2210 |         return result; | 
| Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2211 |     default: | 
 | 2212 |         break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2213 |     } | 
| Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2214 |     PyErr_SetString(PyExc_SystemError, "invalid kind"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2215 |     return NULL; | 
 | 2216 | } | 
 | 2217 |  | 
 | 2218 | static Py_UCS4* | 
 | 2219 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, | 
 | 2220 |         int copy_null) | 
 | 2221 | { | 
 | 2222 |     int kind; | 
 | 2223 |     void *data; | 
 | 2224 |     Py_ssize_t len, targetlen; | 
 | 2225 |     if (PyUnicode_READY(string) == -1) | 
 | 2226 |         return NULL; | 
 | 2227 |     kind = PyUnicode_KIND(string); | 
 | 2228 |     data = PyUnicode_DATA(string); | 
 | 2229 |     len = PyUnicode_GET_LENGTH(string); | 
 | 2230 |     targetlen = len; | 
 | 2231 |     if (copy_null) | 
 | 2232 |         targetlen++; | 
 | 2233 |     if (!target) { | 
 | 2234 |         if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { | 
 | 2235 |             PyErr_NoMemory(); | 
 | 2236 |             return NULL; | 
 | 2237 |         } | 
 | 2238 |         target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); | 
 | 2239 |         if (!target) { | 
 | 2240 |             PyErr_NoMemory(); | 
 | 2241 |             return NULL; | 
 | 2242 |         } | 
 | 2243 |     } | 
 | 2244 |     else { | 
 | 2245 |         if (targetsize < targetlen) { | 
 | 2246 |             PyErr_Format(PyExc_SystemError, | 
 | 2247 |                          "string is longer than the buffer"); | 
 | 2248 |             if (copy_null && 0 < targetsize) | 
 | 2249 |                 target[0] = 0; | 
 | 2250 |             return NULL; | 
 | 2251 |         } | 
 | 2252 |     } | 
| Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2253 |     if (kind == PyUnicode_1BYTE_KIND) { | 
 | 2254 |         Py_UCS1 *start = (Py_UCS1 *) data; | 
 | 2255 |         _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, start, start + len, target); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2256 |     } | 
| Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2257 |     else if (kind == PyUnicode_2BYTE_KIND) { | 
 | 2258 |         Py_UCS2 *start = (Py_UCS2 *) data; | 
 | 2259 |         _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); | 
 | 2260 |     } | 
 | 2261 |     else { | 
 | 2262 |         assert(kind == PyUnicode_4BYTE_KIND); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2263 |         Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); | 
| Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2264 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2265 |     if (copy_null) | 
 | 2266 |         target[len] = 0; | 
 | 2267 |     return target; | 
 | 2268 | } | 
 | 2269 |  | 
 | 2270 | Py_UCS4* | 
 | 2271 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, | 
 | 2272 |                  int copy_null) | 
 | 2273 | { | 
| Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2274 |     if (target == NULL || targetsize < 0) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2275 |         PyErr_BadInternalCall(); | 
 | 2276 |         return NULL; | 
 | 2277 |     } | 
 | 2278 |     return as_ucs4(string, target, targetsize, copy_null); | 
 | 2279 | } | 
 | 2280 |  | 
 | 2281 | Py_UCS4* | 
 | 2282 | PyUnicode_AsUCS4Copy(PyObject *string) | 
 | 2283 | { | 
 | 2284 |     return as_ucs4(string, NULL, 0, 1); | 
 | 2285 | } | 
 | 2286 |  | 
 | 2287 | #ifdef HAVE_WCHAR_H | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2288 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2289 | PyObject * | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2290 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2292 |     if (w == NULL) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2293 |         if (size == 0) | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2294 |             _Py_RETURN_UNICODE_EMPTY(); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2295 |         PyErr_BadInternalCall(); | 
 | 2296 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 |     } | 
 | 2298 |  | 
| Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2299 |     if (size == -1) { | 
 | 2300 |         size = wcslen(w); | 
 | 2301 |     } | 
 | 2302 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2303 |     return PyUnicode_FromUnicode(w, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2304 | } | 
 | 2305 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2306 | #endif /* HAVE_WCHAR_H */ | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2307 |  | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2308 | static void | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2309 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2310 |         char c) | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2311 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2312 |     *fmt++ = '%'; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2313 |     if (longflag) | 
 | 2314 |         *fmt++ = 'l'; | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2315 |     else if (longlongflag) { | 
 | 2316 |         /* longlongflag should only ever be nonzero on machines with | 
 | 2317 |            HAVE_LONG_LONG defined */ | 
 | 2318 | #ifdef HAVE_LONG_LONG | 
 | 2319 |         char *f = PY_FORMAT_LONG_LONG; | 
 | 2320 |         while (*f) | 
 | 2321 |             *fmt++ = *f++; | 
 | 2322 | #else | 
 | 2323 |         /* we shouldn't ever get here */ | 
 | 2324 |         assert(0); | 
 | 2325 |         *fmt++ = 'l'; | 
 | 2326 | #endif | 
 | 2327 |     } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2328 |     else if (size_tflag) { | 
 | 2329 |         char *f = PY_FORMAT_SIZE_T; | 
 | 2330 |         while (*f) | 
 | 2331 |             *fmt++ = *f++; | 
 | 2332 |     } | 
 | 2333 |     *fmt++ = c; | 
 | 2334 |     *fmt = '\0'; | 
| Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2335 | } | 
 | 2336 |  | 
| Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2337 | /* maximum number of characters required for output of %lld or %p. | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2338 |    We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, | 
 | 2339 |    plus 1 for the sign.  53/22 is an upper bound for log10(256). */ | 
 | 2340 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2341 |  | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2342 | static int | 
 | 2343 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, | 
 | 2344 |                              Py_ssize_t width, Py_ssize_t precision) | 
 | 2345 | { | 
 | 2346 |     Py_ssize_t length, fill, arglen; | 
 | 2347 |     Py_UCS4 maxchar; | 
 | 2348 |  | 
 | 2349 |     if (PyUnicode_READY(str) == -1) | 
 | 2350 |         return -1; | 
 | 2351 |  | 
 | 2352 |     length = PyUnicode_GET_LENGTH(str); | 
 | 2353 |     if ((precision == -1 || precision >= length) | 
 | 2354 |         && width <= length) | 
 | 2355 |         return _PyUnicodeWriter_WriteStr(writer, str); | 
 | 2356 |  | 
 | 2357 |     if (precision != -1) | 
 | 2358 |         length = Py_MIN(precision, length); | 
 | 2359 |  | 
 | 2360 |     arglen = Py_MAX(length, width); | 
 | 2361 |     if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) | 
 | 2362 |         maxchar = _PyUnicode_FindMaxChar(str, 0, length); | 
 | 2363 |     else | 
 | 2364 |         maxchar = writer->maxchar; | 
 | 2365 |  | 
 | 2366 |     if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) | 
 | 2367 |         return -1; | 
 | 2368 |  | 
 | 2369 |     if (width > length) { | 
 | 2370 |         fill = width - length; | 
 | 2371 |         if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) | 
 | 2372 |             return -1; | 
 | 2373 |         writer->pos += fill; | 
 | 2374 |     } | 
 | 2375 |  | 
 | 2376 |     _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, | 
 | 2377 |                                   str, 0, length); | 
 | 2378 |     writer->pos += length; | 
 | 2379 |     return 0; | 
 | 2380 | } | 
 | 2381 |  | 
 | 2382 | static int | 
 | 2383 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, | 
 | 2384 |                               Py_ssize_t width, Py_ssize_t precision) | 
 | 2385 | { | 
 | 2386 |     /* UTF-8 */ | 
 | 2387 |     Py_ssize_t length; | 
 | 2388 |     PyObject *unicode; | 
 | 2389 |     int res; | 
 | 2390 |  | 
 | 2391 |     length = strlen(str); | 
 | 2392 |     if (precision != -1) | 
 | 2393 |         length = Py_MIN(length, precision); | 
 | 2394 |     unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); | 
 | 2395 |     if (unicode == NULL) | 
 | 2396 |         return -1; | 
 | 2397 |  | 
 | 2398 |     res = unicode_fromformat_write_str(writer, unicode, width, -1); | 
 | 2399 |     Py_DECREF(unicode); | 
 | 2400 |     return res; | 
 | 2401 | } | 
 | 2402 |  | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2403 | static const char* | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2404 | unicode_fromformat_arg(_PyUnicodeWriter *writer, | 
 | 2405 |                        const char *f, va_list *vargs) | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2406 | { | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2407 |     const char *p; | 
 | 2408 |     Py_ssize_t len; | 
 | 2409 |     int zeropad; | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2410 |     Py_ssize_t width; | 
 | 2411 |     Py_ssize_t precision; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2412 |     int longflag; | 
 | 2413 |     int longlongflag; | 
 | 2414 |     int size_tflag; | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2415 |     Py_ssize_t fill; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2416 |  | 
 | 2417 |     p = f; | 
 | 2418 |     f++; | 
| Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2419 |     zeropad = 0; | 
 | 2420 |     if (*f == '0') { | 
 | 2421 |         zeropad = 1; | 
 | 2422 |         f++; | 
 | 2423 |     } | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2424 |  | 
 | 2425 |     /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2426 |     width = -1; | 
 | 2427 |     if (Py_ISDIGIT((unsigned)*f)) { | 
 | 2428 |         width = *f - '0'; | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2429 |         f++; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2430 |         while (Py_ISDIGIT((unsigned)*f)) { | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2431 |             if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { | 
| Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2432 |                 PyErr_SetString(PyExc_ValueError, | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2433 |                                 "width too big"); | 
| Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2434 |                 return NULL; | 
 | 2435 |             } | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2436 |             width = (width * 10) + (*f - '0'); | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2437 |             f++; | 
 | 2438 |         } | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2439 |     } | 
 | 2440 |     precision = -1; | 
 | 2441 |     if (*f == '.') { | 
 | 2442 |         f++; | 
 | 2443 |         if (Py_ISDIGIT((unsigned)*f)) { | 
 | 2444 |             precision = (*f - '0'); | 
 | 2445 |             f++; | 
 | 2446 |             while (Py_ISDIGIT((unsigned)*f)) { | 
 | 2447 |                 if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { | 
 | 2448 |                     PyErr_SetString(PyExc_ValueError, | 
 | 2449 |                                     "precision too big"); | 
 | 2450 |                     return NULL; | 
 | 2451 |                 } | 
 | 2452 |                 precision = (precision * 10) + (*f - '0'); | 
 | 2453 |                 f++; | 
 | 2454 |             } | 
 | 2455 |         } | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2456 |         if (*f == '%') { | 
 | 2457 |             /* "%.3%s" => f points to "3" */ | 
 | 2458 |             f--; | 
 | 2459 |         } | 
 | 2460 |     } | 
 | 2461 |     if (*f == '\0') { | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2462 |         /* bogus format "%.123" => go backward, f points to "3" */ | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2463 |         f--; | 
 | 2464 |     } | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2465 |  | 
 | 2466 |     /* Handle %ld, %lu, %lld and %llu. */ | 
 | 2467 |     longflag = 0; | 
 | 2468 |     longlongflag = 0; | 
| Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2469 |     size_tflag = 0; | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2470 |     if (*f == 'l') { | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2471 |         if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2472 |             longflag = 1; | 
 | 2473 |             ++f; | 
 | 2474 |         } | 
 | 2475 | #ifdef HAVE_LONG_LONG | 
 | 2476 |         else if (f[1] == 'l' && | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2477 |                  (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2478 |             longlongflag = 1; | 
 | 2479 |             f += 2; | 
 | 2480 |         } | 
 | 2481 | #endif | 
 | 2482 |     } | 
 | 2483 |     /* handle the size_t flag. */ | 
| Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2484 |     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] | 2485 |         size_tflag = 1; | 
 | 2486 |         ++f; | 
 | 2487 |     } | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2488 |  | 
 | 2489 |     if (f[1] == '\0') | 
 | 2490 |         writer->overallocate = 0; | 
 | 2491 |  | 
 | 2492 |     switch (*f) { | 
 | 2493 |     case 'c': | 
 | 2494 |     { | 
 | 2495 |         int ordinal = va_arg(*vargs, int); | 
| Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2496 |         if (ordinal < 0 || ordinal > MAX_UNICODE) { | 
| Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2497 |             PyErr_SetString(PyExc_OverflowError, | 
| Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2498 |                             "character argument not in range(0x110000)"); | 
 | 2499 |             return NULL; | 
 | 2500 |         } | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2501 |         if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2502 |             return NULL; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2503 |         break; | 
 | 2504 |     } | 
 | 2505 |  | 
 | 2506 |     case 'i': | 
 | 2507 |     case 'd': | 
 | 2508 |     case 'u': | 
 | 2509 |     case 'x': | 
 | 2510 |     { | 
 | 2511 |         /* used by sprintf */ | 
 | 2512 |         char fmt[10]; /* should be enough for "%0lld\0" */ | 
| Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2513 |         char buffer[MAX_LONG_LONG_CHARS]; | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2514 |         Py_ssize_t arglen; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2515 |  | 
 | 2516 |         if (*f == 'u') { | 
 | 2517 |             makefmt(fmt, longflag, longlongflag, size_tflag, *f); | 
 | 2518 |  | 
 | 2519 |             if (longflag) | 
 | 2520 |                 len = sprintf(buffer, fmt, | 
 | 2521 |                         va_arg(*vargs, unsigned long)); | 
 | 2522 | #ifdef HAVE_LONG_LONG | 
 | 2523 |             else if (longlongflag) | 
 | 2524 |                 len = sprintf(buffer, fmt, | 
 | 2525 |                         va_arg(*vargs, unsigned PY_LONG_LONG)); | 
 | 2526 | #endif | 
 | 2527 |             else if (size_tflag) | 
 | 2528 |                 len = sprintf(buffer, fmt, | 
 | 2529 |                         va_arg(*vargs, size_t)); | 
 | 2530 |             else | 
 | 2531 |                 len = sprintf(buffer, fmt, | 
 | 2532 |                         va_arg(*vargs, unsigned int)); | 
 | 2533 |         } | 
 | 2534 |         else if (*f == 'x') { | 
 | 2535 |             makefmt(fmt, 0, 0, 0, 'x'); | 
 | 2536 |             len = sprintf(buffer, fmt, va_arg(*vargs, int)); | 
 | 2537 |         } | 
 | 2538 |         else { | 
 | 2539 |             makefmt(fmt, longflag, longlongflag, size_tflag, *f); | 
 | 2540 |  | 
 | 2541 |             if (longflag) | 
 | 2542 |                 len = sprintf(buffer, fmt, | 
 | 2543 |                         va_arg(*vargs, long)); | 
 | 2544 | #ifdef HAVE_LONG_LONG | 
 | 2545 |             else if (longlongflag) | 
 | 2546 |                 len = sprintf(buffer, fmt, | 
 | 2547 |                         va_arg(*vargs, PY_LONG_LONG)); | 
 | 2548 | #endif | 
 | 2549 |             else if (size_tflag) | 
 | 2550 |                 len = sprintf(buffer, fmt, | 
 | 2551 |                         va_arg(*vargs, Py_ssize_t)); | 
 | 2552 |             else | 
 | 2553 |                 len = sprintf(buffer, fmt, | 
 | 2554 |                         va_arg(*vargs, int)); | 
 | 2555 |         } | 
 | 2556 |         assert(len >= 0); | 
 | 2557 |  | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2558 |         if (precision < len) | 
 | 2559 |             precision = len; | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2560 |  | 
 | 2561 |         arglen = Py_MAX(precision, width); | 
 | 2562 |         assert(ucs1lib_find_max_char((Py_UCS1*)buffer, (Py_UCS1*)buffer + len) <= 127); | 
 | 2563 |         if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) | 
 | 2564 |             return NULL; | 
 | 2565 |  | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2566 |         if (width > precision) { | 
 | 2567 |             Py_UCS4 fillchar; | 
 | 2568 |             fill = width - precision; | 
 | 2569 |             fillchar = zeropad?'0':' '; | 
| Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2570 |             if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) | 
 | 2571 |                 return NULL; | 
 | 2572 |             writer->pos += fill; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2573 |         } | 
| Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2574 |         if (precision > len) { | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2575 |             fill = precision - len; | 
| Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2576 |             if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) | 
 | 2577 |                 return NULL; | 
 | 2578 |             writer->pos += fill; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2579 |         } | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2580 |  | 
 | 2581 |         unicode_write_cstr(writer->buffer, writer->pos, buffer, len); | 
 | 2582 |         writer->pos += len; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2583 |         break; | 
 | 2584 |     } | 
 | 2585 |  | 
 | 2586 |     case 'p': | 
 | 2587 |     { | 
 | 2588 |         char number[MAX_LONG_LONG_CHARS]; | 
 | 2589 |  | 
 | 2590 |         len = sprintf(number, "%p", va_arg(*vargs, void*)); | 
 | 2591 |         assert(len >= 0); | 
 | 2592 |  | 
 | 2593 |         /* %p is ill-defined:  ensure leading 0x. */ | 
 | 2594 |         if (number[1] == 'X') | 
 | 2595 |             number[1] = 'x'; | 
 | 2596 |         else if (number[1] != 'x') { | 
 | 2597 |             memmove(number + 2, number, | 
 | 2598 |                     strlen(number) + 1); | 
 | 2599 |             number[0] = '0'; | 
 | 2600 |             number[1] = 'x'; | 
 | 2601 |             len += 2; | 
 | 2602 |         } | 
 | 2603 |  | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2604 |         assert(ucs1lib_find_max_char((Py_UCS1*)number, (Py_UCS1*)number + len) <= 127); | 
 | 2605 |         if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2606 |             return NULL; | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2607 |         unicode_write_cstr(writer->buffer, writer->pos, number, len); | 
 | 2608 |         writer->pos += len; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2609 |         break; | 
 | 2610 |     } | 
 | 2611 |  | 
 | 2612 |     case 's': | 
 | 2613 |     { | 
 | 2614 |         /* UTF-8 */ | 
 | 2615 |         const char *s = va_arg(*vargs, const char*); | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2616 |         if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2617 |             return NULL; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2618 |         break; | 
 | 2619 |     } | 
 | 2620 |  | 
 | 2621 |     case 'U': | 
 | 2622 |     { | 
 | 2623 |         PyObject *obj = va_arg(*vargs, PyObject *); | 
 | 2624 |         assert(obj && _PyUnicode_CHECK(obj)); | 
 | 2625 |  | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2626 |         if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2627 |             return NULL; | 
 | 2628 |         break; | 
 | 2629 |     } | 
 | 2630 |  | 
 | 2631 |     case 'V': | 
 | 2632 |     { | 
 | 2633 |         PyObject *obj = va_arg(*vargs, PyObject *); | 
 | 2634 |         const char *str = va_arg(*vargs, const char *); | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2635 |         if (obj) { | 
 | 2636 |             assert(_PyUnicode_CHECK(obj)); | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2637 |             if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2638 |                 return NULL; | 
 | 2639 |         } | 
 | 2640 |         else { | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2641 |             assert(str != NULL); | 
 | 2642 |             if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2643 |                 return NULL; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2644 |         } | 
 | 2645 |         break; | 
 | 2646 |     } | 
 | 2647 |  | 
 | 2648 |     case 'S': | 
 | 2649 |     { | 
 | 2650 |         PyObject *obj = va_arg(*vargs, PyObject *); | 
 | 2651 |         PyObject *str; | 
 | 2652 |         assert(obj); | 
 | 2653 |         str = PyObject_Str(obj); | 
 | 2654 |         if (!str) | 
 | 2655 |             return NULL; | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2656 |         if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2657 |             Py_DECREF(str); | 
 | 2658 |             return NULL; | 
 | 2659 |         } | 
 | 2660 |         Py_DECREF(str); | 
 | 2661 |         break; | 
 | 2662 |     } | 
 | 2663 |  | 
 | 2664 |     case 'R': | 
 | 2665 |     { | 
 | 2666 |         PyObject *obj = va_arg(*vargs, PyObject *); | 
 | 2667 |         PyObject *repr; | 
 | 2668 |         assert(obj); | 
 | 2669 |         repr = PyObject_Repr(obj); | 
 | 2670 |         if (!repr) | 
 | 2671 |             return NULL; | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2672 |         if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2673 |             Py_DECREF(repr); | 
 | 2674 |             return NULL; | 
 | 2675 |         } | 
 | 2676 |         Py_DECREF(repr); | 
 | 2677 |         break; | 
 | 2678 |     } | 
 | 2679 |  | 
 | 2680 |     case 'A': | 
 | 2681 |     { | 
 | 2682 |         PyObject *obj = va_arg(*vargs, PyObject *); | 
 | 2683 |         PyObject *ascii; | 
 | 2684 |         assert(obj); | 
 | 2685 |         ascii = PyObject_ASCII(obj); | 
 | 2686 |         if (!ascii) | 
 | 2687 |             return NULL; | 
| Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2688 |         if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2689 |             Py_DECREF(ascii); | 
 | 2690 |             return NULL; | 
 | 2691 |         } | 
 | 2692 |         Py_DECREF(ascii); | 
 | 2693 |         break; | 
 | 2694 |     } | 
 | 2695 |  | 
 | 2696 |     case '%': | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2697 |         if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2698 |             return NULL; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2699 |         break; | 
 | 2700 |  | 
 | 2701 |     default: | 
 | 2702 |         /* if we stumble upon an unknown formatting code, copy the rest | 
 | 2703 |            of the format string to the output string. (we cannot just | 
 | 2704 |            skip the code, since there's no way to know what's in the | 
 | 2705 |            argument list) */ | 
 | 2706 |         len = strlen(p); | 
 | 2707 |         if (_PyUnicodeWriter_WriteCstr(writer, p, len) == -1) | 
 | 2708 |             return NULL; | 
 | 2709 |         f = p+len; | 
 | 2710 |         return f; | 
 | 2711 |     } | 
 | 2712 |  | 
 | 2713 |     f++; | 
| Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2714 |     return f; | 
 | 2715 | } | 
 | 2716 |  | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2717 | PyObject * | 
 | 2718 | PyUnicode_FromFormatV(const char *format, va_list vargs) | 
 | 2719 | { | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2720 |     va_list vargs2; | 
 | 2721 |     const char *f; | 
 | 2722 |     _PyUnicodeWriter writer; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2723 |  | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2724 |     _PyUnicodeWriter_Init(&writer); | 
 | 2725 |     writer.min_length = strlen(format) + 100; | 
 | 2726 |     writer.overallocate = 1; | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2727 |  | 
 | 2728 |     /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). | 
 | 2729 |        Copy it to be able to pass a reference to a subfunction. */ | 
 | 2730 |     Py_VA_COPY(vargs2, vargs); | 
 | 2731 |  | 
 | 2732 |     for (f = format; *f; ) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2733 |         if (*f == '%') { | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2734 |             f = unicode_fromformat_arg(&writer, f, &vargs2); | 
 | 2735 |             if (f == NULL) | 
 | 2736 |                 goto fail; | 
| Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2737 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2738 |         else { | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2739 |             const char *p; | 
 | 2740 |             Py_ssize_t len; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2741 |  | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2742 |             p = f; | 
 | 2743 |             do | 
 | 2744 |             { | 
 | 2745 |                 if ((unsigned char)*p > 127) { | 
 | 2746 |                     PyErr_Format(PyExc_ValueError, | 
 | 2747 |                         "PyUnicode_FromFormatV() expects an ASCII-encoded format " | 
 | 2748 |                         "string, got a non-ASCII byte: 0x%02x", | 
 | 2749 |                         (unsigned char)*p); | 
 | 2750 |                     return NULL; | 
 | 2751 |                 } | 
 | 2752 |                 p++; | 
 | 2753 |             } | 
 | 2754 |             while (*p != '\0' && *p != '%'); | 
 | 2755 |             len = p - f; | 
 | 2756 |  | 
 | 2757 |             if (*p == '\0') | 
 | 2758 |                 writer.overallocate = 0; | 
 | 2759 |             if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) | 
 | 2760 |                 goto fail; | 
 | 2761 |             unicode_write_cstr(writer.buffer, writer.pos, f, len); | 
 | 2762 |             writer.pos += len; | 
 | 2763 |  | 
 | 2764 |             f = p; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2765 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2766 |     } | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2767 |     return _PyUnicodeWriter_Finish(&writer); | 
 | 2768 |  | 
 | 2769 |   fail: | 
 | 2770 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2771 |     return NULL; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2772 | } | 
 | 2773 |  | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2774 | PyObject * | 
 | 2775 | PyUnicode_FromFormat(const char *format, ...) | 
 | 2776 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2777 |     PyObject* ret; | 
 | 2778 |     va_list vargs; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2779 |  | 
 | 2780 | #ifdef HAVE_STDARG_PROTOTYPES | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2781 |     va_start(vargs, format); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2782 | #else | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2783 |     va_start(vargs); | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2784 | #endif | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2785 |     ret = PyUnicode_FromFormatV(format, vargs); | 
 | 2786 |     va_end(vargs); | 
 | 2787 |     return ret; | 
| Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2788 | } | 
 | 2789 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2790 | #ifdef HAVE_WCHAR_H | 
 | 2791 |  | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2792 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): | 
 | 2793 |    convert a Unicode object to a wide character string. | 
 | 2794 |  | 
| Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2795 |    - 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] | 2796 |      character) required to convert the unicode object. Ignore size argument. | 
 | 2797 |  | 
| Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2798 |    - Otherwise: return the number of wide characters (excluding the null | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2799 |      character) written into w. Write at most size wide characters (including | 
| Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2800 |      the null character). */ | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2801 | static Py_ssize_t | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2802 | unicode_aswidechar(PyObject *unicode, | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2803 |                    wchar_t *w, | 
 | 2804 |                    Py_ssize_t size) | 
 | 2805 | { | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2806 |     Py_ssize_t res; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2807 |     const wchar_t *wstr; | 
 | 2808 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2809 |     wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2810 |     if (wstr == NULL) | 
 | 2811 |         return -1; | 
 | 2812 |  | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2813 |     if (w != NULL) { | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2814 |         if (size > res) | 
 | 2815 |             size = res + 1; | 
 | 2816 |         else | 
 | 2817 |             res = size; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2818 |         Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2819 |         return res; | 
 | 2820 |     } | 
 | 2821 |     else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2822 |         return res + 1; | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2823 | } | 
 | 2824 |  | 
 | 2825 | Py_ssize_t | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2826 | PyUnicode_AsWideChar(PyObject *unicode, | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2827 |                      wchar_t *w, | 
 | 2828 |                      Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2829 | { | 
 | 2830 |     if (unicode == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2831 |         PyErr_BadInternalCall(); | 
 | 2832 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2833 |     } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2834 |     return unicode_aswidechar(unicode, w, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2835 | } | 
 | 2836 |  | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2837 | wchar_t* | 
| Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2838 | PyUnicode_AsWideCharString(PyObject *unicode, | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2839 |                            Py_ssize_t *size) | 
 | 2840 | { | 
 | 2841 |     wchar_t* buffer; | 
 | 2842 |     Py_ssize_t buflen; | 
 | 2843 |  | 
 | 2844 |     if (unicode == NULL) { | 
 | 2845 |         PyErr_BadInternalCall(); | 
 | 2846 |         return NULL; | 
 | 2847 |     } | 
 | 2848 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2849 |     buflen = unicode_aswidechar(unicode, NULL, 0); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2850 |     if (buflen == -1) | 
 | 2851 |         return NULL; | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2852 |     if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2853 |         PyErr_NoMemory(); | 
 | 2854 |         return NULL; | 
 | 2855 |     } | 
 | 2856 |  | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2857 |     buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); | 
 | 2858 |     if (buffer == NULL) { | 
 | 2859 |         PyErr_NoMemory(); | 
 | 2860 |         return NULL; | 
 | 2861 |     } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2862 |     buflen = unicode_aswidechar(unicode, buffer, buflen); | 
| Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2863 |     if (buflen == -1) { | 
 | 2864 |         PyMem_FREE(buffer); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2865 |         return NULL; | 
| Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2866 |     } | 
| Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2867 |     if (size != NULL) | 
 | 2868 |         *size = buflen; | 
| Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2869 |     return buffer; | 
 | 2870 | } | 
 | 2871 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2872 | #endif /* HAVE_WCHAR_H */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2873 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2874 | PyObject * | 
 | 2875 | PyUnicode_FromOrdinal(int ordinal) | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2876 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2877 |     PyObject *v; | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2878 |     void *data; | 
 | 2879 |     int kind; | 
 | 2880 |  | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2881 |     if (ordinal < 0 || ordinal > MAX_UNICODE) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2882 |         PyErr_SetString(PyExc_ValueError, | 
 | 2883 |                         "chr() arg not in range(0x110000)"); | 
 | 2884 |         return NULL; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2885 |     } | 
| Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2886 |  | 
| Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2887 |     if ((Py_UCS4)ordinal < 256) | 
 | 2888 |         return get_latin1_char((unsigned char)ordinal); | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2889 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2890 |     v = PyUnicode_New(1, ordinal); | 
 | 2891 |     if (v == NULL) | 
 | 2892 |         return NULL; | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2893 |     kind = PyUnicode_KIND(v); | 
 | 2894 |     data = PyUnicode_DATA(v); | 
 | 2895 |     PyUnicode_WRITE(kind, data, 0, ordinal); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2896 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2897 |     return v; | 
| Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2898 | } | 
 | 2899 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2900 | PyObject * | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2901 | PyUnicode_FromObject(PyObject *obj) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2902 | { | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2903 |     /* XXX Perhaps we should make this API an alias of | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2904 |        PyObject_Str() instead ?! */ | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2905 |     if (PyUnicode_CheckExact(obj)) { | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2906 |         if (PyUnicode_READY(obj) == -1) | 
| Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2907 |             return NULL; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2908 |         Py_INCREF(obj); | 
 | 2909 |         return obj; | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2910 |     } | 
 | 2911 |     if (PyUnicode_Check(obj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2912 |         /* For a Unicode subtype that's not a Unicode object, | 
 | 2913 |            return a true Unicode object with the same data. */ | 
| Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2914 |         return _PyUnicode_Copy(obj); | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2915 |     } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2916 |     PyErr_Format(PyExc_TypeError, | 
 | 2917 |                  "Can't convert '%.100s' object to str implicitly", | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2918 |                  Py_TYPE(obj)->tp_name); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2919 |     return NULL; | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2920 | } | 
 | 2921 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2922 | PyObject * | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2923 | PyUnicode_FromEncodedObject(PyObject *obj, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2924 |                             const char *encoding, | 
 | 2925 |                             const char *errors) | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2926 | { | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2927 |     Py_buffer buffer; | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2928 |     PyObject *v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2929 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2930 |     if (obj == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2931 |         PyErr_BadInternalCall(); | 
 | 2932 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2933 |     } | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2934 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2935 |     /* Decoding bytes objects is the most common case and should be fast */ | 
 | 2936 |     if (PyBytes_Check(obj)) { | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2937 |         if (PyBytes_GET_SIZE(obj) == 0) | 
 | 2938 |             _Py_RETURN_UNICODE_EMPTY(); | 
 | 2939 |         v = PyUnicode_Decode( | 
 | 2940 |                 PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), | 
 | 2941 |                 encoding, errors); | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2942 |         return v; | 
 | 2943 |     } | 
 | 2944 |  | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2945 |     if (PyUnicode_Check(obj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2946 |         PyErr_SetString(PyExc_TypeError, | 
 | 2947 |                         "decoding str is not supported"); | 
 | 2948 |         return NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2949 |     } | 
| Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2950 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2951 |     /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ | 
 | 2952 |     if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { | 
 | 2953 |         PyErr_Format(PyExc_TypeError, | 
 | 2954 |                      "coercing to str: need bytes, bytearray " | 
 | 2955 |                      "or buffer-like object, %.80s found", | 
 | 2956 |                      Py_TYPE(obj)->tp_name); | 
 | 2957 |         return NULL; | 
| Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2958 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2959 |  | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2960 |     if (buffer.len == 0) { | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2961 |         PyBuffer_Release(&buffer); | 
 | 2962 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2963 |     } | 
| Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2964 |  | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2965 |     v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); | 
| Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2966 |     PyBuffer_Release(&buffer); | 
| Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2967 |     return v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2968 | } | 
 | 2969 |  | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2970 | /* Convert encoding to lower case and replace '_' with '-' in order to | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2971 |    catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), | 
 | 2972 |    1 on success. */ | 
| Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2973 | int | 
 | 2974 | _Py_normalize_encoding(const char *encoding, | 
 | 2975 |                        char *lower, | 
 | 2976 |                        size_t lower_len) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2977 | { | 
| Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2978 |     const char *e; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2979 |     char *l; | 
 | 2980 |     char *l_end; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2981 |  | 
| Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2982 |     if (encoding == NULL) { | 
 | 2983 |         strcpy(lower, "utf-8"); | 
 | 2984 |         return 1; | 
 | 2985 |     } | 
| Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2986 |     e = encoding; | 
 | 2987 |     l = lower; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2988 |     l_end = &lower[lower_len - 1]; | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2989 |     while (*e) { | 
 | 2990 |         if (l == l_end) | 
 | 2991 |             return 0; | 
| David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2992 |         if (Py_ISUPPER(*e)) { | 
 | 2993 |             *l++ = Py_TOLOWER(*e++); | 
| Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2994 |         } | 
 | 2995 |         else if (*e == '_') { | 
 | 2996 |             *l++ = '-'; | 
 | 2997 |             e++; | 
 | 2998 |         } | 
 | 2999 |         else { | 
 | 3000 |             *l++ = *e++; | 
 | 3001 |         } | 
 | 3002 |     } | 
 | 3003 |     *l = '\0'; | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3004 |     return 1; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3005 | } | 
 | 3006 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3007 | PyObject * | 
 | 3008 | PyUnicode_Decode(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3009 |                  Py_ssize_t size, | 
 | 3010 |                  const char *encoding, | 
 | 3011 |                  const char *errors) | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3012 | { | 
 | 3013 |     PyObject *buffer = NULL, *unicode; | 
 | 3014 |     Py_buffer info; | 
 | 3015 |     char lower[11];  /* Enough for any encoding shortcut */ | 
 | 3016 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3017 |     /* Shortcuts for common default encodings */ | 
| Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3018 |     if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3019 |         if ((strcmp(lower, "utf-8") == 0) || | 
 | 3020 |             (strcmp(lower, "utf8") == 0)) | 
| Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3021 |             return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3022 |         else if ((strcmp(lower, "latin-1") == 0) || | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3023 |                  (strcmp(lower, "latin1") == 0) || | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3024 |                  (strcmp(lower, "iso-8859-1") == 0)) | 
 | 3025 |             return PyUnicode_DecodeLatin1(s, size, errors); | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3026 | #ifdef HAVE_MBCS | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3027 |         else if (strcmp(lower, "mbcs") == 0) | 
 | 3028 |             return PyUnicode_DecodeMBCS(s, size, errors); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3029 | #endif | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3030 |         else if (strcmp(lower, "ascii") == 0) | 
 | 3031 |             return PyUnicode_DecodeASCII(s, size, errors); | 
 | 3032 |         else if (strcmp(lower, "utf-16") == 0) | 
 | 3033 |             return PyUnicode_DecodeUTF16(s, size, errors, 0); | 
 | 3034 |         else if (strcmp(lower, "utf-32") == 0) | 
 | 3035 |             return PyUnicode_DecodeUTF32(s, size, errors, 0); | 
 | 3036 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 |  | 
 | 3038 |     /* Decode via the codec registry */ | 
| Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3039 |     buffer = NULL; | 
| Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3040 |     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] | 3041 |         goto onError; | 
| Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3042 |     buffer = PyMemoryView_FromBuffer(&info); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3043 |     if (buffer == NULL) | 
 | 3044 |         goto onError; | 
 | 3045 |     unicode = PyCodec_Decode(buffer, encoding, errors); | 
 | 3046 |     if (unicode == NULL) | 
 | 3047 |         goto onError; | 
 | 3048 |     if (!PyUnicode_Check(unicode)) { | 
 | 3049 |         PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3050 |                      "decoder did not return a str object (type=%.400s)", | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3051 |                      Py_TYPE(unicode)->tp_name); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3052 |         Py_DECREF(unicode); | 
 | 3053 |         goto onError; | 
 | 3054 |     } | 
 | 3055 |     Py_DECREF(buffer); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3056 |     return unicode_result(unicode); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3057 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3058 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3059 |     Py_XDECREF(buffer); | 
 | 3060 |     return NULL; | 
 | 3061 | } | 
 | 3062 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3063 | PyObject * | 
 | 3064 | PyUnicode_AsDecodedObject(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3065 |                           const char *encoding, | 
 | 3066 |                           const char *errors) | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3067 | { | 
 | 3068 |     PyObject *v; | 
 | 3069 |  | 
 | 3070 |     if (!PyUnicode_Check(unicode)) { | 
 | 3071 |         PyErr_BadArgument(); | 
 | 3072 |         goto onError; | 
 | 3073 |     } | 
 | 3074 |  | 
 | 3075 |     if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3076 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3077 |  | 
 | 3078 |     /* Decode via the codec registry */ | 
 | 3079 |     v = PyCodec_Decode(unicode, encoding, errors); | 
 | 3080 |     if (v == NULL) | 
 | 3081 |         goto onError; | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3082 |     return unicode_result(v); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3083 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3084 |   onError: | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3085 |     return NULL; | 
 | 3086 | } | 
 | 3087 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3088 | PyObject * | 
 | 3089 | PyUnicode_AsDecodedUnicode(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3090 |                            const char *encoding, | 
 | 3091 |                            const char *errors) | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3092 | { | 
 | 3093 |     PyObject *v; | 
 | 3094 |  | 
 | 3095 |     if (!PyUnicode_Check(unicode)) { | 
 | 3096 |         PyErr_BadArgument(); | 
 | 3097 |         goto onError; | 
 | 3098 |     } | 
 | 3099 |  | 
 | 3100 |     if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3101 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3102 |  | 
 | 3103 |     /* Decode via the codec registry */ | 
 | 3104 |     v = PyCodec_Decode(unicode, encoding, errors); | 
 | 3105 |     if (v == NULL) | 
 | 3106 |         goto onError; | 
 | 3107 |     if (!PyUnicode_Check(v)) { | 
 | 3108 |         PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3109 |                      "decoder did not return a str object (type=%.400s)", | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3110 |                      Py_TYPE(v)->tp_name); | 
 | 3111 |         Py_DECREF(v); | 
 | 3112 |         goto onError; | 
 | 3113 |     } | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3114 |     return unicode_result(v); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3115 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3116 |   onError: | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3117 |     return NULL; | 
 | 3118 | } | 
 | 3119 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3120 | PyObject * | 
 | 3121 | PyUnicode_Encode(const Py_UNICODE *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3122 |                  Py_ssize_t size, | 
 | 3123 |                  const char *encoding, | 
 | 3124 |                  const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3125 | { | 
 | 3126 |     PyObject *v, *unicode; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3127 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3128 |     unicode = PyUnicode_FromUnicode(s, size); | 
 | 3129 |     if (unicode == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3130 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3131 |     v = PyUnicode_AsEncodedString(unicode, encoding, errors); | 
 | 3132 |     Py_DECREF(unicode); | 
 | 3133 |     return v; | 
 | 3134 | } | 
 | 3135 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3136 | PyObject * | 
 | 3137 | PyUnicode_AsEncodedObject(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3138 |                           const char *encoding, | 
 | 3139 |                           const char *errors) | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3140 | { | 
 | 3141 |     PyObject *v; | 
 | 3142 |  | 
 | 3143 |     if (!PyUnicode_Check(unicode)) { | 
 | 3144 |         PyErr_BadArgument(); | 
 | 3145 |         goto onError; | 
 | 3146 |     } | 
 | 3147 |  | 
 | 3148 |     if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3149 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3150 |  | 
 | 3151 |     /* Encode via the codec registry */ | 
 | 3152 |     v = PyCodec_Encode(unicode, encoding, errors); | 
 | 3153 |     if (v == NULL) | 
 | 3154 |         goto onError; | 
 | 3155 |     return v; | 
 | 3156 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3157 |   onError: | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3158 |     return NULL; | 
 | 3159 | } | 
 | 3160 |  | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3161 | static size_t | 
 | 3162 | wcstombs_errorpos(const wchar_t *wstr) | 
 | 3163 | { | 
 | 3164 |     size_t len; | 
 | 3165 | #if SIZEOF_WCHAR_T == 2 | 
 | 3166 |     wchar_t buf[3]; | 
 | 3167 | #else | 
 | 3168 |     wchar_t buf[2]; | 
 | 3169 | #endif | 
 | 3170 |     char outbuf[MB_LEN_MAX]; | 
 | 3171 |     const wchar_t *start, *previous; | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3172 |  | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3173 | #if SIZEOF_WCHAR_T == 2 | 
 | 3174 |     buf[2] = 0; | 
 | 3175 | #else | 
 | 3176 |     buf[1] = 0; | 
 | 3177 | #endif | 
 | 3178 |     start = wstr; | 
 | 3179 |     while (*wstr != L'\0') | 
 | 3180 |     { | 
 | 3181 |         previous = wstr; | 
 | 3182 | #if SIZEOF_WCHAR_T == 2 | 
 | 3183 |         if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) | 
 | 3184 |             && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) | 
 | 3185 |         { | 
 | 3186 |             buf[0] = wstr[0]; | 
 | 3187 |             buf[1] = wstr[1]; | 
 | 3188 |             wstr += 2; | 
 | 3189 |         } | 
 | 3190 |         else { | 
 | 3191 |             buf[0] = *wstr; | 
 | 3192 |             buf[1] = 0; | 
 | 3193 |             wstr++; | 
 | 3194 |         } | 
 | 3195 | #else | 
 | 3196 |         buf[0] = *wstr; | 
 | 3197 |         wstr++; | 
 | 3198 | #endif | 
 | 3199 |         len = wcstombs(outbuf, buf, sizeof(outbuf)); | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3200 |         if (len == (size_t)-1) | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3201 |             return previous - start; | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3202 |     } | 
 | 3203 |  | 
 | 3204 |     /* failed to find the unencodable character */ | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3205 |     return 0; | 
 | 3206 | } | 
 | 3207 |  | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3208 | static int | 
 | 3209 | locale_error_handler(const char *errors, int *surrogateescape) | 
 | 3210 | { | 
 | 3211 |     if (errors == NULL) { | 
 | 3212 |         *surrogateescape = 0; | 
 | 3213 |         return 0; | 
 | 3214 |     } | 
 | 3215 |  | 
 | 3216 |     if (strcmp(errors, "strict") == 0) { | 
 | 3217 |         *surrogateescape = 0; | 
 | 3218 |         return 0; | 
 | 3219 |     } | 
| Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3220 |     if (strcmp(errors, "surrogateescape") == 0) { | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3221 |         *surrogateescape = 1; | 
 | 3222 |         return 0; | 
 | 3223 |     } | 
 | 3224 |     PyErr_Format(PyExc_ValueError, | 
 | 3225 |                  "only 'strict' and 'surrogateescape' error handlers " | 
 | 3226 |                  "are supported, not '%s'", | 
 | 3227 |                  errors); | 
 | 3228 |     return -1; | 
 | 3229 | } | 
 | 3230 |  | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3231 | PyObject * | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3232 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3233 | { | 
 | 3234 |     Py_ssize_t wlen, wlen2; | 
 | 3235 |     wchar_t *wstr; | 
 | 3236 |     PyObject *bytes = NULL; | 
 | 3237 |     char *errmsg; | 
| Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3238 |     PyObject *reason = NULL; | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3239 |     PyObject *exc; | 
 | 3240 |     size_t error_pos; | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3241 |     int surrogateescape; | 
 | 3242 |  | 
 | 3243 |     if (locale_error_handler(errors, &surrogateescape) < 0) | 
 | 3244 |         return NULL; | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3245 |  | 
 | 3246 |     wstr = PyUnicode_AsWideCharString(unicode, &wlen); | 
 | 3247 |     if (wstr == NULL) | 
 | 3248 |         return NULL; | 
 | 3249 |  | 
 | 3250 |     wlen2 = wcslen(wstr); | 
 | 3251 |     if (wlen2 != wlen) { | 
 | 3252 |         PyMem_Free(wstr); | 
 | 3253 |         PyErr_SetString(PyExc_TypeError, "embedded null character"); | 
 | 3254 |         return NULL; | 
 | 3255 |     } | 
 | 3256 |  | 
 | 3257 |     if (surrogateescape) { | 
| Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3258 |         /* "surrogateescape" error handler */ | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3259 |         char *str; | 
 | 3260 |  | 
 | 3261 |         str = _Py_wchar2char(wstr, &error_pos); | 
 | 3262 |         if (str == NULL) { | 
 | 3263 |             if (error_pos == (size_t)-1) { | 
 | 3264 |                 PyErr_NoMemory(); | 
 | 3265 |                 PyMem_Free(wstr); | 
 | 3266 |                 return NULL; | 
 | 3267 |             } | 
 | 3268 |             else { | 
 | 3269 |                 goto encode_error; | 
 | 3270 |             } | 
 | 3271 |         } | 
 | 3272 |         PyMem_Free(wstr); | 
 | 3273 |  | 
 | 3274 |         bytes = PyBytes_FromString(str); | 
 | 3275 |         PyMem_Free(str); | 
 | 3276 |     } | 
 | 3277 |     else { | 
| Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3278 |         /* strict mode */ | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3279 |         size_t len, len2; | 
 | 3280 |  | 
 | 3281 |         len = wcstombs(NULL, wstr, 0); | 
 | 3282 |         if (len == (size_t)-1) { | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3283 |             error_pos = (size_t)-1; | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3284 |             goto encode_error; | 
 | 3285 |         } | 
 | 3286 |  | 
 | 3287 |         bytes = PyBytes_FromStringAndSize(NULL, len); | 
 | 3288 |         if (bytes == NULL) { | 
 | 3289 |             PyMem_Free(wstr); | 
 | 3290 |             return NULL; | 
 | 3291 |         } | 
 | 3292 |  | 
 | 3293 |         len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); | 
 | 3294 |         if (len2 == (size_t)-1 || len2 > len) { | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3295 |             error_pos = (size_t)-1; | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3296 |             goto encode_error; | 
 | 3297 |         } | 
 | 3298 |         PyMem_Free(wstr); | 
 | 3299 |     } | 
 | 3300 |     return bytes; | 
 | 3301 |  | 
 | 3302 | encode_error: | 
 | 3303 |     errmsg = strerror(errno); | 
 | 3304 |     assert(errmsg != NULL); | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3305 |  | 
 | 3306 |     if (error_pos == (size_t)-1) | 
 | 3307 |         error_pos = wcstombs_errorpos(wstr); | 
 | 3308 |  | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3309 |     PyMem_Free(wstr); | 
 | 3310 |     Py_XDECREF(bytes); | 
 | 3311 |  | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3312 |     if (errmsg != NULL) { | 
 | 3313 |         size_t errlen; | 
 | 3314 |         wstr = _Py_char2wchar(errmsg, &errlen); | 
 | 3315 |         if (wstr != NULL) { | 
 | 3316 |             reason = PyUnicode_FromWideChar(wstr, errlen); | 
| Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3317 |             PyMem_RawFree(wstr); | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3318 |         } else | 
 | 3319 |             errmsg = NULL; | 
 | 3320 |     } | 
 | 3321 |     if (errmsg == NULL) | 
| Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3322 |         reason = PyUnicode_FromString( | 
 | 3323 |             "wcstombs() encountered an unencodable " | 
 | 3324 |             "wide character"); | 
 | 3325 |     if (reason == NULL) | 
 | 3326 |         return NULL; | 
 | 3327 |  | 
 | 3328 |     exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", | 
 | 3329 |                                 "locale", unicode, | 
 | 3330 |                                 (Py_ssize_t)error_pos, | 
 | 3331 |                                 (Py_ssize_t)(error_pos+1), | 
 | 3332 |                                 reason); | 
 | 3333 |     Py_DECREF(reason); | 
 | 3334 |     if (exc != NULL) { | 
 | 3335 |         PyCodec_StrictErrors(exc); | 
 | 3336 |         Py_XDECREF(exc); | 
 | 3337 |     } | 
| Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3338 |     return NULL; | 
 | 3339 | } | 
 | 3340 |  | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3341 | PyObject * | 
 | 3342 | PyUnicode_EncodeFSDefault(PyObject *unicode) | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3343 | { | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3344 | #ifdef HAVE_MBCS | 
| Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3345 |     return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3346 | #elif defined(__APPLE__) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3347 |     return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3348 | #else | 
| Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3349 |     PyInterpreterState *interp = PyThreadState_GET()->interp; | 
 | 3350 |     /* Bootstrap check: if the filesystem codec is implemented in Python, we | 
 | 3351 |        cannot use it to encode and decode filenames before it is loaded. Load | 
 | 3352 |        the Python codec requires to encode at least its own filename. Use the C | 
 | 3353 |        version of the locale codec until the codec registry is initialized and | 
 | 3354 |        the Python codec is loaded. | 
 | 3355 |  | 
 | 3356 |        Py_FileSystemDefaultEncoding is shared between all interpreters, we | 
 | 3357 |        cannot only rely on it: check also interp->fscodec_initialized for | 
 | 3358 |        subinterpreters. */ | 
 | 3359 |     if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3360 |         return PyUnicode_AsEncodedString(unicode, | 
 | 3361 |                                          Py_FileSystemDefaultEncoding, | 
 | 3362 |                                          "surrogateescape"); | 
| Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3363 |     } | 
 | 3364 |     else { | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3365 |         return PyUnicode_EncodeLocale(unicode, "surrogateescape"); | 
| Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3366 |     } | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3367 | #endif | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3368 | } | 
 | 3369 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3370 | PyObject * | 
 | 3371 | PyUnicode_AsEncodedString(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3372 |                           const char *encoding, | 
 | 3373 |                           const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3374 | { | 
 | 3375 |     PyObject *v; | 
| Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3376 |     char lower[11];  /* Enough for any encoding shortcut */ | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3377 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 |     if (!PyUnicode_Check(unicode)) { | 
 | 3379 |         PyErr_BadArgument(); | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3380 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3381 |     } | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3382 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3383 |     /* Shortcuts for common default encodings */ | 
| Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3384 |     if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3385 |         if ((strcmp(lower, "utf-8") == 0) || | 
 | 3386 |             (strcmp(lower, "utf8") == 0)) | 
| Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3387 |         { | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3388 |             if (errors == NULL || strcmp(errors, "strict") == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3389 |                 return _PyUnicode_AsUTF8String(unicode, NULL); | 
| Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3390 |             else | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3391 |                 return _PyUnicode_AsUTF8String(unicode, errors); | 
| Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3392 |         } | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3393 |         else if ((strcmp(lower, "latin-1") == 0) || | 
| Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3394 |                  (strcmp(lower, "latin1") == 0) || | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3395 |                  (strcmp(lower, "iso-8859-1") == 0)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3396 |             return _PyUnicode_AsLatin1String(unicode, errors); | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3397 | #ifdef HAVE_MBCS | 
| Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3398 |         else if (strcmp(lower, "mbcs") == 0) | 
 | 3399 |             return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3400 | #endif | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3401 |         else if (strcmp(lower, "ascii") == 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3402 |             return _PyUnicode_AsASCIIString(unicode, errors); | 
| Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3403 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3404 |  | 
 | 3405 |     /* Encode via the codec registry */ | 
 | 3406 |     v = PyCodec_Encode(unicode, encoding, errors); | 
 | 3407 |     if (v == NULL) | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3408 |         return NULL; | 
 | 3409 |  | 
 | 3410 |     /* The normal path */ | 
 | 3411 |     if (PyBytes_Check(v)) | 
 | 3412 |         return v; | 
 | 3413 |  | 
 | 3414 |     /* 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] | 3415 |     if (PyByteArray_Check(v)) { | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3416 |         int error; | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3417 |         PyObject *b; | 
| Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3418 |  | 
 | 3419 |         error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, | 
 | 3420 |             "encoder %s returned bytearray instead of bytes", | 
 | 3421 |             encoding); | 
 | 3422 |         if (error) { | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3423 |             Py_DECREF(v); | 
 | 3424 |             return NULL; | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3425 |         } | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3426 |  | 
| Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3427 |         b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); | 
 | 3428 |         Py_DECREF(v); | 
 | 3429 |         return b; | 
 | 3430 |     } | 
 | 3431 |  | 
 | 3432 |     PyErr_Format(PyExc_TypeError, | 
 | 3433 |                  "encoder did not return a bytes object (type=%.400s)", | 
 | 3434 |                  Py_TYPE(v)->tp_name); | 
 | 3435 |     Py_DECREF(v); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3436 |     return NULL; | 
 | 3437 | } | 
 | 3438 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3439 | PyObject * | 
 | 3440 | PyUnicode_AsEncodedUnicode(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3441 |                            const char *encoding, | 
 | 3442 |                            const char *errors) | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3443 | { | 
 | 3444 |     PyObject *v; | 
 | 3445 |  | 
 | 3446 |     if (!PyUnicode_Check(unicode)) { | 
 | 3447 |         PyErr_BadArgument(); | 
 | 3448 |         goto onError; | 
 | 3449 |     } | 
 | 3450 |  | 
 | 3451 |     if (encoding == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3452 |         encoding = PyUnicode_GetDefaultEncoding(); | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3453 |  | 
 | 3454 |     /* Encode via the codec registry */ | 
 | 3455 |     v = PyCodec_Encode(unicode, encoding, errors); | 
 | 3456 |     if (v == NULL) | 
 | 3457 |         goto onError; | 
 | 3458 |     if (!PyUnicode_Check(v)) { | 
 | 3459 |         PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3460 |                      "encoder did not return an str object (type=%.400s)", | 
| Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3461 |                      Py_TYPE(v)->tp_name); | 
 | 3462 |         Py_DECREF(v); | 
 | 3463 |         goto onError; | 
 | 3464 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3465 |     return v; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3466 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3467 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3468 |     return NULL; | 
 | 3469 | } | 
 | 3470 |  | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3471 | static size_t | 
 | 3472 | mbstowcs_errorpos(const char *str, size_t len) | 
 | 3473 | { | 
 | 3474 | #ifdef HAVE_MBRTOWC | 
 | 3475 |     const char *start = str; | 
 | 3476 |     mbstate_t mbs; | 
 | 3477 |     size_t converted; | 
 | 3478 |     wchar_t ch; | 
 | 3479 |  | 
 | 3480 |     memset(&mbs, 0, sizeof mbs); | 
 | 3481 |     while (len) | 
 | 3482 |     { | 
 | 3483 |         converted = mbrtowc(&ch, (char*)str, len, &mbs); | 
 | 3484 |         if (converted == 0) | 
 | 3485 |             /* Reached end of string */ | 
 | 3486 |             break; | 
 | 3487 |         if (converted == (size_t)-1 || converted == (size_t)-2) { | 
 | 3488 |             /* Conversion error or incomplete character */ | 
 | 3489 |             return str - start; | 
 | 3490 |         } | 
 | 3491 |         else { | 
 | 3492 |             str += converted; | 
 | 3493 |             len -= converted; | 
 | 3494 |         } | 
 | 3495 |     } | 
 | 3496 |     /* failed to find the undecodable byte sequence */ | 
 | 3497 |     return 0; | 
 | 3498 | #endif | 
 | 3499 |     return 0; | 
 | 3500 | } | 
 | 3501 |  | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3502 | PyObject* | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3503 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3504 |                               const char *errors) | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3505 | { | 
 | 3506 |     wchar_t smallbuf[256]; | 
 | 3507 |     size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); | 
 | 3508 |     wchar_t *wstr; | 
 | 3509 |     size_t wlen, wlen2; | 
 | 3510 |     PyObject *unicode; | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3511 |     int surrogateescape; | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3512 |     size_t error_pos; | 
 | 3513 |     char *errmsg; | 
 | 3514 |     PyObject *reason, *exc; | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3515 |  | 
 | 3516 |     if (locale_error_handler(errors, &surrogateescape) < 0) | 
 | 3517 |         return NULL; | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3518 |  | 
 | 3519 |     if (str[len] != '\0' || len != strlen(str)) { | 
 | 3520 |         PyErr_SetString(PyExc_TypeError, "embedded null character"); | 
 | 3521 |         return NULL; | 
 | 3522 |     } | 
 | 3523 |  | 
| Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3524 |     if (surrogateescape) { | 
 | 3525 |         /* "surrogateescape" error handler */ | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3526 |         wstr = _Py_char2wchar(str, &wlen); | 
 | 3527 |         if (wstr == NULL) { | 
 | 3528 |             if (wlen == (size_t)-1) | 
 | 3529 |                 PyErr_NoMemory(); | 
 | 3530 |             else | 
 | 3531 |                 PyErr_SetFromErrno(PyExc_OSError); | 
 | 3532 |             return NULL; | 
 | 3533 |         } | 
 | 3534 |  | 
 | 3535 |         unicode = PyUnicode_FromWideChar(wstr, wlen); | 
| Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3536 |         PyMem_RawFree(wstr); | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3537 |     } | 
 | 3538 |     else { | 
| Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3539 |         /* strict mode */ | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3540 | #ifndef HAVE_BROKEN_MBSTOWCS | 
 | 3541 |         wlen = mbstowcs(NULL, str, 0); | 
 | 3542 | #else | 
 | 3543 |         wlen = len; | 
 | 3544 | #endif | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3545 |         if (wlen == (size_t)-1) | 
 | 3546 |             goto decode_error; | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3547 |         if (wlen+1 <= smallbuf_len) { | 
 | 3548 |             wstr = smallbuf; | 
 | 3549 |         } | 
 | 3550 |         else { | 
 | 3551 |             if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) | 
 | 3552 |                 return PyErr_NoMemory(); | 
 | 3553 |  | 
 | 3554 |             wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); | 
 | 3555 |             if (!wstr) | 
 | 3556 |                 return PyErr_NoMemory(); | 
 | 3557 |         } | 
 | 3558 |  | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3559 |         wlen2 = mbstowcs(wstr, str, wlen+1); | 
 | 3560 |         if (wlen2 == (size_t)-1) { | 
 | 3561 |             if (wstr != smallbuf) | 
 | 3562 |                 PyMem_Free(wstr); | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3563 |             goto decode_error; | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3564 |         } | 
 | 3565 | #ifdef HAVE_BROKEN_MBSTOWCS | 
 | 3566 |         assert(wlen2 == wlen); | 
 | 3567 | #endif | 
 | 3568 |         unicode = PyUnicode_FromWideChar(wstr, wlen2); | 
 | 3569 |         if (wstr != smallbuf) | 
 | 3570 |             PyMem_Free(wstr); | 
 | 3571 |     } | 
 | 3572 |     return unicode; | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3573 |  | 
 | 3574 | decode_error: | 
 | 3575 |     errmsg = strerror(errno); | 
 | 3576 |     assert(errmsg != NULL); | 
 | 3577 |  | 
 | 3578 |     error_pos = mbstowcs_errorpos(str, len); | 
 | 3579 |     if (errmsg != NULL) { | 
 | 3580 |         size_t errlen; | 
 | 3581 |         wstr = _Py_char2wchar(errmsg, &errlen); | 
 | 3582 |         if (wstr != NULL) { | 
 | 3583 |             reason = PyUnicode_FromWideChar(wstr, errlen); | 
| Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3584 |             PyMem_RawFree(wstr); | 
| Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3585 |         } else | 
 | 3586 |             errmsg = NULL; | 
 | 3587 |     } | 
 | 3588 |     if (errmsg == NULL) | 
 | 3589 |         reason = PyUnicode_FromString( | 
 | 3590 |             "mbstowcs() encountered an invalid multibyte sequence"); | 
 | 3591 |     if (reason == NULL) | 
 | 3592 |         return NULL; | 
 | 3593 |  | 
 | 3594 |     exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", | 
 | 3595 |                                 "locale", str, len, | 
 | 3596 |                                 (Py_ssize_t)error_pos, | 
 | 3597 |                                 (Py_ssize_t)(error_pos+1), | 
 | 3598 |                                 reason); | 
 | 3599 |     Py_DECREF(reason); | 
 | 3600 |     if (exc != NULL) { | 
 | 3601 |         PyCodec_StrictErrors(exc); | 
 | 3602 |         Py_XDECREF(exc); | 
 | 3603 |     } | 
 | 3604 |     return NULL; | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3605 | } | 
 | 3606 |  | 
 | 3607 | PyObject* | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3608 | PyUnicode_DecodeLocale(const char *str, const char *errors) | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3609 | { | 
 | 3610 |     Py_ssize_t size = (Py_ssize_t)strlen(str); | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3611 |     return PyUnicode_DecodeLocaleAndSize(str, size, errors); | 
| Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3612 | } | 
 | 3613 |  | 
 | 3614 |  | 
 | 3615 | PyObject* | 
| Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3616 | PyUnicode_DecodeFSDefault(const char *s) { | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3617 |     Py_ssize_t size = (Py_ssize_t)strlen(s); | 
| Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3618 |     return PyUnicode_DecodeFSDefaultAndSize(s, size); | 
 | 3619 | } | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3620 |  | 
| Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3621 | PyObject* | 
 | 3622 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) | 
 | 3623 | { | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3624 | #ifdef HAVE_MBCS | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3625 |     return PyUnicode_DecodeMBCS(s, size, NULL); | 
 | 3626 | #elif defined(__APPLE__) | 
| Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3627 |     return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3628 | #else | 
| Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3629 |     PyInterpreterState *interp = PyThreadState_GET()->interp; | 
 | 3630 |     /* Bootstrap check: if the filesystem codec is implemented in Python, we | 
 | 3631 |        cannot use it to encode and decode filenames before it is loaded. Load | 
 | 3632 |        the Python codec requires to encode at least its own filename. Use the C | 
 | 3633 |        version of the locale codec until the codec registry is initialized and | 
 | 3634 |        the Python codec is loaded. | 
 | 3635 |  | 
 | 3636 |        Py_FileSystemDefaultEncoding is shared between all interpreters, we | 
 | 3637 |        cannot only rely on it: check also interp->fscodec_initialized for | 
 | 3638 |        subinterpreters. */ | 
 | 3639 |     if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3640 |         return PyUnicode_Decode(s, size, | 
 | 3641 |                                 Py_FileSystemDefaultEncoding, | 
| Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3642 |                                 "surrogateescape"); | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3643 |     } | 
 | 3644 |     else { | 
| Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3645 |         return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3646 |     } | 
| Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3647 | #endif | 
| Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3648 | } | 
 | 3649 |  | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3650 |  | 
 | 3651 | int | 
| Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3652 | _PyUnicode_HasNULChars(PyObject* str) | 
| Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3653 | { | 
| Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3654 |     Py_ssize_t pos; | 
| Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3655 |  | 
| Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3656 |     if (PyUnicode_READY(str) == -1) | 
| Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3657 |         return -1; | 
| Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3658 |     pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), | 
 | 3659 |                    PyUnicode_GET_LENGTH(str), '\0', 1); | 
 | 3660 |     if (pos == -1) | 
 | 3661 |         return 0; | 
 | 3662 |     else | 
 | 3663 |         return 1; | 
| Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3664 | } | 
 | 3665 |  | 
| Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3666 | int | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3667 | PyUnicode_FSConverter(PyObject* arg, void* addr) | 
 | 3668 | { | 
 | 3669 |     PyObject *output = NULL; | 
 | 3670 |     Py_ssize_t size; | 
 | 3671 |     void *data; | 
| Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3672 |     if (arg == NULL) { | 
 | 3673 |         Py_DECREF(*(PyObject**)addr); | 
 | 3674 |         return 1; | 
 | 3675 |     } | 
| Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3676 |     if (PyBytes_Check(arg)) { | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3677 |         output = arg; | 
 | 3678 |         Py_INCREF(output); | 
 | 3679 |     } | 
 | 3680 |     else { | 
 | 3681 |         arg = PyUnicode_FromObject(arg); | 
 | 3682 |         if (!arg) | 
 | 3683 |             return 0; | 
| Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3684 |         output = PyUnicode_EncodeFSDefault(arg); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3685 |         Py_DECREF(arg); | 
 | 3686 |         if (!output) | 
 | 3687 |             return 0; | 
 | 3688 |         if (!PyBytes_Check(output)) { | 
 | 3689 |             Py_DECREF(output); | 
 | 3690 |             PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); | 
 | 3691 |             return 0; | 
 | 3692 |         } | 
 | 3693 |     } | 
| Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3694 |     size = PyBytes_GET_SIZE(output); | 
 | 3695 |     data = PyBytes_AS_STRING(output); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3696 |     if (size != strlen(data)) { | 
| Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3697 |         PyErr_SetString(PyExc_TypeError, "embedded NUL character"); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3698 |         Py_DECREF(output); | 
 | 3699 |         return 0; | 
 | 3700 |     } | 
 | 3701 |     *(PyObject**)addr = output; | 
| Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3702 |     return Py_CLEANUP_SUPPORTED; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3703 | } | 
 | 3704 |  | 
 | 3705 |  | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3706 | int | 
 | 3707 | PyUnicode_FSDecoder(PyObject* arg, void* addr) | 
 | 3708 | { | 
 | 3709 |     PyObject *output = NULL; | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3710 |     if (arg == NULL) { | 
 | 3711 |         Py_DECREF(*(PyObject**)addr); | 
 | 3712 |         return 1; | 
 | 3713 |     } | 
 | 3714 |     if (PyUnicode_Check(arg)) { | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3715 |         if (PyUnicode_READY(arg) == -1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3716 |             return 0; | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3717 |         output = arg; | 
 | 3718 |         Py_INCREF(output); | 
 | 3719 |     } | 
 | 3720 |     else { | 
 | 3721 |         arg = PyBytes_FromObject(arg); | 
 | 3722 |         if (!arg) | 
 | 3723 |             return 0; | 
 | 3724 |         output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), | 
 | 3725 |                                                   PyBytes_GET_SIZE(arg)); | 
 | 3726 |         Py_DECREF(arg); | 
 | 3727 |         if (!output) | 
 | 3728 |             return 0; | 
 | 3729 |         if (!PyUnicode_Check(output)) { | 
 | 3730 |             Py_DECREF(output); | 
 | 3731 |             PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); | 
 | 3732 |             return 0; | 
 | 3733 |         } | 
 | 3734 |     } | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3735 |     if (PyUnicode_READY(output) == -1) { | 
| Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3736 |         Py_DECREF(output); | 
 | 3737 |         return 0; | 
 | 3738 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3739 |     if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), | 
| Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3740 |                  PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { | 
| Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3741 |         PyErr_SetString(PyExc_TypeError, "embedded NUL character"); | 
 | 3742 |         Py_DECREF(output); | 
 | 3743 |         return 0; | 
 | 3744 |     } | 
 | 3745 |     *(PyObject**)addr = output; | 
 | 3746 |     return Py_CLEANUP_SUPPORTED; | 
 | 3747 | } | 
 | 3748 |  | 
 | 3749 |  | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3750 | char* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3751 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3752 | { | 
| Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3753 |     PyObject *bytes; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3754 |  | 
| Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3755 |     if (!PyUnicode_Check(unicode)) { | 
 | 3756 |         PyErr_BadArgument(); | 
 | 3757 |         return NULL; | 
 | 3758 |     } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3759 |     if (PyUnicode_READY(unicode) == -1) | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3760 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3761 |  | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3762 |     if (PyUnicode_UTF8(unicode) == NULL) { | 
 | 3763 |         assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3764 |         bytes = _PyUnicode_AsUTF8String(unicode, "strict"); | 
 | 3765 |         if (bytes == NULL) | 
 | 3766 |             return NULL; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3767 |         _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); | 
 | 3768 |         if (_PyUnicode_UTF8(unicode) == NULL) { | 
| Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame^] | 3769 |             PyErr_NoMemory(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3770 |             Py_DECREF(bytes); | 
 | 3771 |             return NULL; | 
 | 3772 |         } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3773 |         _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); | 
 | 3774 |         Py_MEMCPY(_PyUnicode_UTF8(unicode), | 
 | 3775 |                   PyBytes_AS_STRING(bytes), | 
 | 3776 |                   _PyUnicode_UTF8_LENGTH(unicode) + 1); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3777 |         Py_DECREF(bytes); | 
 | 3778 |     } | 
 | 3779 |  | 
 | 3780 |     if (psize) | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3781 |         *psize = PyUnicode_UTF8_LENGTH(unicode); | 
 | 3782 |     return PyUnicode_UTF8(unicode); | 
| Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3783 | } | 
 | 3784 |  | 
 | 3785 | char* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3786 | PyUnicode_AsUTF8(PyObject *unicode) | 
| Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3787 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3788 |     return PyUnicode_AsUTF8AndSize(unicode, NULL); | 
 | 3789 | } | 
 | 3790 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3791 | Py_UNICODE * | 
 | 3792 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) | 
 | 3793 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3794 |     const unsigned char *one_byte; | 
 | 3795 | #if SIZEOF_WCHAR_T == 4 | 
 | 3796 |     const Py_UCS2 *two_bytes; | 
 | 3797 | #else | 
 | 3798 |     const Py_UCS4 *four_bytes; | 
 | 3799 |     const Py_UCS4 *ucs4_end; | 
 | 3800 |     Py_ssize_t num_surrogates; | 
 | 3801 | #endif | 
 | 3802 |     wchar_t *w; | 
 | 3803 |     wchar_t *wchar_end; | 
 | 3804 |  | 
 | 3805 |     if (!PyUnicode_Check(unicode)) { | 
 | 3806 |         PyErr_BadArgument(); | 
 | 3807 |         return NULL; | 
 | 3808 |     } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3809 |     if (_PyUnicode_WSTR(unicode) == NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3810 |         /* Non-ASCII compact unicode object */ | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3811 |         assert(_PyUnicode_KIND(unicode) != 0); | 
 | 3812 |         assert(PyUnicode_IS_READY(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3813 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3814 |         if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3815 | #if SIZEOF_WCHAR_T == 2 | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3816 |             four_bytes = PyUnicode_4BYTE_DATA(unicode); | 
 | 3817 |             ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3818 |             num_surrogates = 0; | 
 | 3819 |  | 
 | 3820 |             for (; four_bytes < ucs4_end; ++four_bytes) { | 
 | 3821 |                 if (*four_bytes > 0xFFFF) | 
 | 3822 |                     ++num_surrogates; | 
 | 3823 |             } | 
 | 3824 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3825 |             _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( | 
 | 3826 |                     sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); | 
 | 3827 |             if (!_PyUnicode_WSTR(unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3828 |                 PyErr_NoMemory(); | 
 | 3829 |                 return NULL; | 
 | 3830 |             } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3831 |             _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3832 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3833 |             w = _PyUnicode_WSTR(unicode); | 
 | 3834 |             wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); | 
 | 3835 |             four_bytes = PyUnicode_4BYTE_DATA(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3836 |             for (; four_bytes < ucs4_end; ++four_bytes, ++w) { | 
 | 3837 |                 if (*four_bytes > 0xFFFF) { | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3838 |                     assert(*four_bytes <= MAX_UNICODE); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3839 |                     /* encode surrogate pair in this case */ | 
| Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3840 |                     *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); | 
 | 3841 |                     *w   = Py_UNICODE_LOW_SURROGATE(*four_bytes); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3842 |                 } | 
 | 3843 |                 else | 
 | 3844 |                     *w = *four_bytes; | 
 | 3845 |  | 
 | 3846 |                 if (w > wchar_end) { | 
 | 3847 |                     assert(0 && "Miscalculated string end"); | 
 | 3848 |                 } | 
 | 3849 |             } | 
 | 3850 |             *w = 0; | 
 | 3851 | #else | 
 | 3852 |             /* sizeof(wchar_t) == 4 */ | 
 | 3853 |             Py_FatalError("Impossible unicode object state, wstr and str " | 
 | 3854 |                           "should share memory already."); | 
 | 3855 |             return NULL; | 
 | 3856 | #endif | 
 | 3857 |         } | 
 | 3858 |         else { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3859 |             _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * | 
 | 3860 |                                                   (_PyUnicode_LENGTH(unicode) + 1)); | 
 | 3861 |             if (!_PyUnicode_WSTR(unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3862 |                 PyErr_NoMemory(); | 
 | 3863 |                 return NULL; | 
 | 3864 |             } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3865 |             if (!PyUnicode_IS_COMPACT_ASCII(unicode)) | 
 | 3866 |                 _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); | 
 | 3867 |             w = _PyUnicode_WSTR(unicode); | 
 | 3868 |             wchar_end = w + _PyUnicode_LENGTH(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3869 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3870 |             if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { | 
 | 3871 |                 one_byte = PyUnicode_1BYTE_DATA(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3872 |                 for (; w < wchar_end; ++one_byte, ++w) | 
 | 3873 |                     *w = *one_byte; | 
 | 3874 |                 /* null-terminate the wstr */ | 
 | 3875 |                 *w = 0; | 
 | 3876 |             } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3877 |             else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3878 | #if SIZEOF_WCHAR_T == 4 | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3879 |                 two_bytes = PyUnicode_2BYTE_DATA(unicode); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3880 |                 for (; w < wchar_end; ++two_bytes, ++w) | 
 | 3881 |                     *w = *two_bytes; | 
 | 3882 |                 /* null-terminate the wstr */ | 
 | 3883 |                 *w = 0; | 
 | 3884 | #else | 
 | 3885 |                 /* sizeof(wchar_t) == 2 */ | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3886 |                 PyObject_FREE(_PyUnicode_WSTR(unicode)); | 
 | 3887 |                 _PyUnicode_WSTR(unicode) = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3888 |                 Py_FatalError("Impossible unicode object state, wstr " | 
 | 3889 |                               "and str should share memory already."); | 
 | 3890 |                 return NULL; | 
 | 3891 | #endif | 
 | 3892 |             } | 
 | 3893 |             else { | 
 | 3894 |                 assert(0 && "This should never happen."); | 
 | 3895 |             } | 
 | 3896 |         } | 
 | 3897 |     } | 
 | 3898 |     if (size != NULL) | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3899 |         *size = PyUnicode_WSTR_LENGTH(unicode); | 
 | 3900 |     return _PyUnicode_WSTR(unicode); | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3901 | } | 
 | 3902 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3903 | Py_UNICODE * | 
 | 3904 | PyUnicode_AsUnicode(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3905 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3906 |     return PyUnicode_AsUnicodeAndSize(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3907 | } | 
 | 3908 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3909 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3910 | Py_ssize_t | 
 | 3911 | PyUnicode_GetSize(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3912 | { | 
 | 3913 |     if (!PyUnicode_Check(unicode)) { | 
 | 3914 |         PyErr_BadArgument(); | 
 | 3915 |         goto onError; | 
 | 3916 |     } | 
 | 3917 |     return PyUnicode_GET_SIZE(unicode); | 
 | 3918 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3919 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3920 |     return -1; | 
 | 3921 | } | 
 | 3922 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3923 | Py_ssize_t | 
 | 3924 | PyUnicode_GetLength(PyObject *unicode) | 
 | 3925 | { | 
| Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3926 |     if (!PyUnicode_Check(unicode)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3927 |         PyErr_BadArgument(); | 
 | 3928 |         return -1; | 
 | 3929 |     } | 
| Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3930 |     if (PyUnicode_READY(unicode) == -1) | 
 | 3931 |         return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3932 |     return PyUnicode_GET_LENGTH(unicode); | 
 | 3933 | } | 
 | 3934 |  | 
 | 3935 | Py_UCS4 | 
 | 3936 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) | 
 | 3937 | { | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3938 |     void *data; | 
 | 3939 |     int kind; | 
 | 3940 |  | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3941 |     if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { | 
 | 3942 |         PyErr_BadArgument(); | 
 | 3943 |         return (Py_UCS4)-1; | 
 | 3944 |     } | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3945 |     if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3946 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3947 |         return (Py_UCS4)-1; | 
 | 3948 |     } | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3949 |     data = PyUnicode_DATA(unicode); | 
 | 3950 |     kind = PyUnicode_KIND(unicode); | 
 | 3951 |     return PyUnicode_READ(kind, data, index); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3952 | } | 
 | 3953 |  | 
 | 3954 | int | 
 | 3955 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) | 
 | 3956 | { | 
 | 3957 |     if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3958 |         PyErr_BadArgument(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3959 |         return -1; | 
 | 3960 |     } | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3961 |     assert(PyUnicode_IS_READY(unicode)); | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3962 |     if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3963 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 3964 |         return -1; | 
 | 3965 |     } | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3966 |     if (unicode_check_modifiable(unicode)) | 
| Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3967 |         return -1; | 
| Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3968 |     if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { | 
 | 3969 |         PyErr_SetString(PyExc_ValueError, "character out of range"); | 
 | 3970 |         return -1; | 
 | 3971 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3972 |     PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), | 
 | 3973 |                     index, ch); | 
 | 3974 |     return 0; | 
 | 3975 | } | 
 | 3976 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3977 | const char * | 
 | 3978 | PyUnicode_GetDefaultEncoding(void) | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3979 | { | 
| Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3980 |     return "utf-8"; | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3981 | } | 
 | 3982 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3983 | /* create or adjust a UnicodeDecodeError */ | 
 | 3984 | static void | 
 | 3985 | make_decode_exception(PyObject **exceptionObject, | 
 | 3986 |                       const char *encoding, | 
 | 3987 |                       const char *input, Py_ssize_t length, | 
 | 3988 |                       Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 3989 |                       const char *reason) | 
 | 3990 | { | 
 | 3991 |     if (*exceptionObject == NULL) { | 
 | 3992 |         *exceptionObject = PyUnicodeDecodeError_Create( | 
 | 3993 |             encoding, input, length, startpos, endpos, reason); | 
 | 3994 |     } | 
 | 3995 |     else { | 
 | 3996 |         if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) | 
 | 3997 |             goto onError; | 
 | 3998 |         if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) | 
 | 3999 |             goto onError; | 
 | 4000 |         if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) | 
 | 4001 |             goto onError; | 
 | 4002 |     } | 
 | 4003 |     return; | 
 | 4004 |  | 
 | 4005 | onError: | 
 | 4006 |     Py_DECREF(*exceptionObject); | 
 | 4007 |     *exceptionObject = NULL; | 
 | 4008 | } | 
 | 4009 |  | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4010 | #ifdef HAVE_MBCS | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4011 | /* error handling callback helper: | 
 | 4012 |    build arguments, call the callback and check the arguments, | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4013 |    if no exception occurred, copy the replacement to the output | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4014 |    and adjust various state variables. | 
 | 4015 |    return 0 on success, -1 on error | 
 | 4016 | */ | 
 | 4017 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4018 | static int | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4019 | unicode_decode_call_errorhandler_wchar( | 
 | 4020 |     const char *errors, PyObject **errorHandler, | 
 | 4021 |     const char *encoding, const char *reason, | 
 | 4022 |     const char **input, const char **inend, Py_ssize_t *startinpos, | 
 | 4023 |     Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | 
 | 4024 |     PyObject **output, Py_ssize_t *outpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4025 | { | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4026 |     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] | 4027 |  | 
 | 4028 |     PyObject *restuple = NULL; | 
 | 4029 |     PyObject *repunicode = NULL; | 
| Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4030 |     Py_ssize_t outsize; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4031 |     Py_ssize_t insize; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4032 |     Py_ssize_t requiredsize; | 
 | 4033 |     Py_ssize_t newpos; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4034 |     PyObject *inputobj = NULL; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4035 |     wchar_t *repwstr; | 
 | 4036 |     Py_ssize_t repwlen; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4037 |  | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4038 |     assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); | 
 | 4039 |     outsize = _PyUnicode_WSTR_LENGTH(*output); | 
| Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4040 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4041 |     if (*errorHandler == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4042 |         *errorHandler = PyCodec_LookupError(errors); | 
 | 4043 |         if (*errorHandler == NULL) | 
 | 4044 |             goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4045 |     } | 
 | 4046 |  | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4047 |     make_decode_exception(exceptionObject, | 
 | 4048 |         encoding, | 
 | 4049 |         *input, *inend - *input, | 
 | 4050 |         *startinpos, *endinpos, | 
 | 4051 |         reason); | 
 | 4052 |     if (*exceptionObject == NULL) | 
 | 4053 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4054 |  | 
 | 4055 |     restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); | 
 | 4056 |     if (restuple == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4057 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4058 |     if (!PyTuple_Check(restuple)) { | 
| Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4059 |         PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4060 |         goto onError; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4061 |     } | 
 | 4062 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4063 |         goto onError; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4064 |  | 
 | 4065 |     /* Copy back the bytes variables, which might have been modified by the | 
 | 4066 |        callback */ | 
 | 4067 |     inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); | 
 | 4068 |     if (!inputobj) | 
 | 4069 |         goto onError; | 
 | 4070 |     if (!PyBytes_Check(inputobj)) { | 
 | 4071 |         PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); | 
 | 4072 |     } | 
 | 4073 |     *input = PyBytes_AS_STRING(inputobj); | 
 | 4074 |     insize = PyBytes_GET_SIZE(inputobj); | 
 | 4075 |     *inend = *input + insize; | 
 | 4076 |     /* we can DECREF safely, as the exception has another reference, | 
 | 4077 |        so the object won't go away. */ | 
 | 4078 |     Py_DECREF(inputobj); | 
 | 4079 |  | 
 | 4080 |     if (newpos<0) | 
 | 4081 |         newpos = insize+newpos; | 
 | 4082 |     if (newpos<0 || newpos>insize) { | 
 | 4083 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); | 
 | 4084 |         goto onError; | 
 | 4085 |     } | 
 | 4086 |  | 
 | 4087 |     repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); | 
 | 4088 |     if (repwstr == NULL) | 
 | 4089 |         goto onError; | 
 | 4090 |     /* need more space? (at least enough for what we | 
 | 4091 |        have+the replacement+the rest of the string (starting | 
 | 4092 |        at the new input position), so we won't have to check space | 
 | 4093 |        when there are no errors in the rest of the string) */ | 
 | 4094 |     requiredsize = *outpos + repwlen + insize-newpos; | 
 | 4095 |     if (requiredsize > outsize) { | 
 | 4096 |         if (requiredsize < 2*outsize) | 
 | 4097 |             requiredsize = 2*outsize; | 
 | 4098 |         if (unicode_resize(output, requiredsize) < 0) | 
 | 4099 |             goto onError; | 
 | 4100 |     } | 
 | 4101 |     wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); | 
 | 4102 |     *outpos += repwlen; | 
 | 4103 |  | 
 | 4104 |     *endinpos = newpos; | 
 | 4105 |     *inptr = *input + newpos; | 
 | 4106 |  | 
 | 4107 |     /* we made it! */ | 
 | 4108 |     Py_XDECREF(restuple); | 
 | 4109 |     return 0; | 
 | 4110 |  | 
 | 4111 |   onError: | 
 | 4112 |     Py_XDECREF(restuple); | 
 | 4113 |     return -1; | 
 | 4114 | } | 
 | 4115 | #endif   /* HAVE_MBCS */ | 
 | 4116 |  | 
 | 4117 | static int | 
 | 4118 | unicode_decode_call_errorhandler_writer( | 
 | 4119 |     const char *errors, PyObject **errorHandler, | 
 | 4120 |     const char *encoding, const char *reason, | 
 | 4121 |     const char **input, const char **inend, Py_ssize_t *startinpos, | 
 | 4122 |     Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | 
 | 4123 |     _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) | 
 | 4124 | { | 
 | 4125 |     static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; | 
 | 4126 |  | 
 | 4127 |     PyObject *restuple = NULL; | 
 | 4128 |     PyObject *repunicode = NULL; | 
 | 4129 |     Py_ssize_t insize; | 
 | 4130 |     Py_ssize_t newpos; | 
| Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4131 |     Py_ssize_t replen; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4132 |     PyObject *inputobj = NULL; | 
 | 4133 |  | 
 | 4134 |     if (*errorHandler == NULL) { | 
 | 4135 |         *errorHandler = PyCodec_LookupError(errors); | 
 | 4136 |         if (*errorHandler == NULL) | 
 | 4137 |             goto onError; | 
 | 4138 |     } | 
 | 4139 |  | 
 | 4140 |     make_decode_exception(exceptionObject, | 
 | 4141 |         encoding, | 
 | 4142 |         *input, *inend - *input, | 
 | 4143 |         *startinpos, *endinpos, | 
 | 4144 |         reason); | 
 | 4145 |     if (*exceptionObject == NULL) | 
 | 4146 |         goto onError; | 
 | 4147 |  | 
 | 4148 |     restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); | 
 | 4149 |     if (restuple == NULL) | 
 | 4150 |         goto onError; | 
 | 4151 |     if (!PyTuple_Check(restuple)) { | 
 | 4152 |         PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
 | 4153 |         goto onError; | 
 | 4154 |     } | 
 | 4155 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4156 |         goto onError; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4157 |  | 
 | 4158 |     /* Copy back the bytes variables, which might have been modified by the | 
 | 4159 |        callback */ | 
 | 4160 |     inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); | 
 | 4161 |     if (!inputobj) | 
 | 4162 |         goto onError; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4163 |     if (!PyBytes_Check(inputobj)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4164 |         PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4165 |     } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4166 |     *input = PyBytes_AS_STRING(inputobj); | 
 | 4167 |     insize = PyBytes_GET_SIZE(inputobj); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4168 |     *inend = *input + insize; | 
| Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4169 |     /* we can DECREF safely, as the exception has another reference, | 
 | 4170 |        so the object won't go away. */ | 
 | 4171 |     Py_DECREF(inputobj); | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4172 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4173 |     if (newpos<0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4174 |         newpos = insize+newpos; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4175 |     if (newpos<0 || newpos>insize) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4176 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); | 
 | 4177 |         goto onError; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4178 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4179 |  | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4180 |     if (PyUnicode_READY(repunicode) < 0) | 
 | 4181 |         goto onError; | 
| Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4182 |     replen = PyUnicode_GET_LENGTH(repunicode); | 
 | 4183 |     writer->min_length += replen; | 
 | 4184 |     if (replen > 1) | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4185 |         writer->overallocate = 1; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4186 |     if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) | 
| Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4187 |         goto onError; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4188 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4189 |     *endinpos = newpos; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4190 |     *inptr = *input + newpos; | 
| Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4191 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4192 |     /* we made it! */ | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4193 |     Py_XDECREF(restuple); | 
 | 4194 |     return 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4195 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4196 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4197 |     Py_XDECREF(restuple); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4198 |     return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4199 | } | 
 | 4200 |  | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4201 | /* --- UTF-7 Codec -------------------------------------------------------- */ | 
 | 4202 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4203 | /* See RFC2152 for details.  We encode conservatively and decode liberally. */ | 
 | 4204 |  | 
 | 4205 | /* Three simple macros defining base-64. */ | 
 | 4206 |  | 
 | 4207 | /* Is c a base-64 character? */ | 
 | 4208 |  | 
 | 4209 | #define IS_BASE64(c) \ | 
 | 4210 |     (((c) >= 'A' && (c) <= 'Z') ||     \ | 
 | 4211 |      ((c) >= 'a' && (c) <= 'z') ||     \ | 
 | 4212 |      ((c) >= '0' && (c) <= '9') ||     \ | 
 | 4213 |      (c) == '+' || (c) == '/') | 
 | 4214 |  | 
 | 4215 | /* given that c is a base-64 character, what is its base-64 value? */ | 
 | 4216 |  | 
 | 4217 | #define FROM_BASE64(c)                                                  \ | 
 | 4218 |     (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' :                           \ | 
 | 4219 |      ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 :                      \ | 
 | 4220 |      ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 :                      \ | 
 | 4221 |      (c) == '+' ? 62 : 63) | 
 | 4222 |  | 
 | 4223 | /* What is the base-64 character of the bottom 6 bits of n? */ | 
 | 4224 |  | 
 | 4225 | #define TO_BASE64(n)  \ | 
 | 4226 |     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) | 
 | 4227 |  | 
 | 4228 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be | 
 | 4229 |  * decoded as itself.  We are permissive on decoding; the only ASCII | 
 | 4230 |  * byte not decoding to itself is the + which begins a base64 | 
 | 4231 |  * string. */ | 
 | 4232 |  | 
 | 4233 | #define DECODE_DIRECT(c)                                \ | 
 | 4234 |     ((c) <= 127 && (c) != '+') | 
 | 4235 |  | 
 | 4236 | /* The UTF-7 encoder treats ASCII characters differently according to | 
 | 4237 |  * whether they are Set D, Set O, Whitespace, or special (i.e. none of | 
 | 4238 |  * the above).  See RFC2152.  This array identifies these different | 
 | 4239 |  * sets: | 
 | 4240 |  * 0 : "Set D" | 
 | 4241 |  *     alphanumeric and '(),-./:? | 
 | 4242 |  * 1 : "Set O" | 
 | 4243 |  *     !"#$%&*;<=>@[]^_`{|} | 
 | 4244 |  * 2 : "whitespace" | 
 | 4245 |  *     ht nl cr sp | 
 | 4246 |  * 3 : special (must be base64 encoded) | 
 | 4247 |  *     everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) | 
 | 4248 |  */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4249 |  | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4250 | static | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4251 | char utf7_category[128] = { | 
 | 4252 | /* nul soh stx etx eot enq ack bel bs  ht  nl  vt  np  cr  so  si  */ | 
 | 4253 |     3,  3,  3,  3,  3,  3,  3,  3,  3,  2,  2,  3,  3,  2,  3,  3, | 
 | 4254 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em  sub esc fs  gs  rs  us  */ | 
 | 4255 |     3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3, | 
 | 4256 | /* sp   !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /  */ | 
 | 4257 |     2,  1,  1,  1,  1,  1,  1,  0,  0,  0,  1,  3,  0,  0,  0,  0, | 
 | 4258 | /*  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?  */ | 
 | 4259 |     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  0, | 
 | 4260 | /*  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O  */ | 
 | 4261 |     1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, | 
 | 4262 | /*  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _  */ | 
 | 4263 |     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  3,  1,  1,  1, | 
 | 4264 | /*  `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o  */ | 
 | 4265 |     1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, | 
 | 4266 | /*  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~  del */ | 
 | 4267 |     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] | 4268 | }; | 
 | 4269 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4270 | /* ENCODE_DIRECT: this character should be encoded as itself.  The | 
 | 4271 |  * answer depends on whether we are encoding set O as itself, and also | 
 | 4272 |  * on whether we are encoding whitespace as itself.  RFC2152 makes it | 
 | 4273 |  * clear that the answers to these questions vary between | 
 | 4274 |  * applications, so this code needs to be flexible.  */ | 
| Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4275 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4276 | #define ENCODE_DIRECT(c, directO, directWS)             \ | 
 | 4277 |     ((c) < 128 && (c) > 0 &&                            \ | 
 | 4278 |      ((utf7_category[(c)] == 0) ||                      \ | 
 | 4279 |       (directWS && (utf7_category[(c)] == 2)) ||        \ | 
 | 4280 |       (directO && (utf7_category[(c)] == 1)))) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4281 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4282 | PyObject * | 
 | 4283 | PyUnicode_DecodeUTF7(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4284 |                      Py_ssize_t size, | 
 | 4285 |                      const char *errors) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4286 | { | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4287 |     return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); | 
 | 4288 | } | 
 | 4289 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4290 | /* The decoder.  The only state we preserve is our read position, | 
 | 4291 |  * i.e. how many characters we have consumed.  So if we end in the | 
 | 4292 |  * middle of a shift sequence we have to back off the read position | 
 | 4293 |  * and the output to the beginning of the sequence, otherwise we lose | 
 | 4294 |  * all the shift state (seen bits, number of bits seen, high | 
 | 4295 |  * surrogate). */ | 
 | 4296 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4297 | PyObject * | 
 | 4298 | PyUnicode_DecodeUTF7Stateful(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4299 |                              Py_ssize_t size, | 
 | 4300 |                              const char *errors, | 
 | 4301 |                              Py_ssize_t *consumed) | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4302 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4303 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4304 |     Py_ssize_t startinpos; | 
 | 4305 |     Py_ssize_t endinpos; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4306 |     const char *e; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4307 |     _PyUnicodeWriter writer; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4308 |     const char *errmsg = ""; | 
 | 4309 |     int inShift = 0; | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4310 |     Py_ssize_t shiftOutStart; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4311 |     unsigned int base64bits = 0; | 
 | 4312 |     unsigned long base64buffer = 0; | 
| Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4313 |     Py_UCS4 surrogate = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4314 |     PyObject *errorHandler = NULL; | 
 | 4315 |     PyObject *exc = NULL; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4316 |  | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4317 |     if (size == 0) { | 
 | 4318 |         if (consumed) | 
 | 4319 |             *consumed = 0; | 
| Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4320 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4321 |     } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4322 |  | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4323 |     /* Start off assuming it's all ASCII. Widen later as necessary. */ | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4324 |     _PyUnicodeWriter_Init(&writer); | 
 | 4325 |     writer.min_length = size; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4326 |  | 
 | 4327 |     shiftOutStart = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4328 |     e = s + size; | 
 | 4329 |  | 
 | 4330 |     while (s < e) { | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4331 |         Py_UCS4 ch; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4332 |       restart: | 
| Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4333 |         ch = (unsigned char) *s; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4334 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4335 |         if (inShift) { /* in a base-64 section */ | 
 | 4336 |             if (IS_BASE64(ch)) { /* consume a base-64 character */ | 
 | 4337 |                 base64buffer = (base64buffer << 6) | FROM_BASE64(ch); | 
 | 4338 |                 base64bits += 6; | 
 | 4339 |                 s++; | 
 | 4340 |                 if (base64bits >= 16) { | 
 | 4341 |                     /* we have enough bits for a UTF-16 value */ | 
| Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4342 |                     Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4343 |                     base64bits -= 16; | 
 | 4344 |                     base64buffer &= (1 << base64bits) - 1; /* clear high bits */ | 
| Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4345 |                     assert(outCh <= 0xffff); | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4346 |                     if (surrogate) { | 
 | 4347 |                         /* expecting a second surrogate */ | 
| Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4348 |                         if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { | 
 | 4349 |                             Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4350 |                             if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4351 |                                 goto onError; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4352 |                             surrogate = 0; | 
| Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4353 |                             continue; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4354 |                         } | 
 | 4355 |                         else { | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4356 |                             if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) | 
| Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4357 |                                 goto onError; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4358 |                             surrogate = 0; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4359 |                         } | 
 | 4360 |                     } | 
| Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4361 |                     if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4362 |                         /* first surrogate */ | 
 | 4363 |                         surrogate = outCh; | 
 | 4364 |                     } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4365 |                     else { | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4366 |                         if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4367 |                             goto onError; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4368 |                     } | 
 | 4369 |                 } | 
 | 4370 |             } | 
 | 4371 |             else { /* now leaving a base-64 section */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4372 |                 inShift = 0; | 
 | 4373 |                 s++; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4374 |                 if (surrogate) { | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4375 |                     if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) | 
| Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4376 |                         goto onError; | 
| Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4377 |                     surrogate = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4378 |                 } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4379 |                 if (base64bits > 0) { /* left-over bits */ | 
 | 4380 |                     if (base64bits >= 6) { | 
 | 4381 |                         /* We've seen at least one base-64 character */ | 
 | 4382 |                         errmsg = "partial character in shift sequence"; | 
 | 4383 |                         goto utf7Error; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4384 |                     } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4385 |                     else { | 
 | 4386 |                         /* Some bits remain; they should be zero */ | 
 | 4387 |                         if (base64buffer != 0) { | 
 | 4388 |                             errmsg = "non-zero padding bits in shift sequence"; | 
 | 4389 |                             goto utf7Error; | 
 | 4390 |                         } | 
 | 4391 |                     } | 
 | 4392 |                 } | 
 | 4393 |                 if (ch != '-') { | 
 | 4394 |                     /* '-' is absorbed; other terminating | 
 | 4395 |                        characters are preserved */ | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4396 |                     if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4397 |                         goto onError; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4398 |                 } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4399 |             } | 
 | 4400 |         } | 
 | 4401 |         else if ( ch == '+' ) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4402 |             startinpos = s-starts; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4403 |             s++; /* consume '+' */ | 
 | 4404 |             if (s < e && *s == '-') { /* '+-' encodes '+' */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4405 |                 s++; | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4406 |                 if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4407 |                     goto onError; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4408 |             } | 
 | 4409 |             else { /* begin base64-encoded section */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4410 |                 inShift = 1; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4411 |                 shiftOutStart = writer.pos; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4412 |                 base64bits = 0; | 
| Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4413 |                 base64buffer = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4414 |             } | 
 | 4415 |         } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4416 |         else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4417 |             s++; | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4418 |             if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4419 |                 goto onError; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4420 |         } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4421 |         else { | 
 | 4422 |             startinpos = s-starts; | 
 | 4423 |             s++; | 
 | 4424 |             errmsg = "unexpected special character"; | 
 | 4425 |             goto utf7Error; | 
 | 4426 |         } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4427 |         continue; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4428 | utf7Error: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4429 |         endinpos = s-starts; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4430 |         if (unicode_decode_call_errorhandler_writer( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4431 |                 errors, &errorHandler, | 
 | 4432 |                 "utf7", errmsg, | 
 | 4433 |                 &starts, &e, &startinpos, &endinpos, &exc, &s, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4434 |                 &writer)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4435 |             goto onError; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4436 |     } | 
 | 4437 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4438 |     /* end of string */ | 
 | 4439 |  | 
 | 4440 |     if (inShift && !consumed) { /* in shift sequence, no more to follow */ | 
 | 4441 |         /* if we're in an inconsistent state, that's an error */ | 
 | 4442 |         if (surrogate || | 
 | 4443 |                 (base64bits >= 6) || | 
 | 4444 |                 (base64bits > 0 && base64buffer != 0)) { | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4445 |             endinpos = size; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4446 |             if (unicode_decode_call_errorhandler_writer( | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4447 |                     errors, &errorHandler, | 
 | 4448 |                     "utf7", "unterminated shift sequence", | 
 | 4449 |                     &starts, &e, &startinpos, &endinpos, &exc, &s, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4450 |                     &writer)) | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4451 |                 goto onError; | 
 | 4452 |             if (s < e) | 
 | 4453 |                 goto restart; | 
 | 4454 |         } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4455 |     } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4456 |  | 
 | 4457 |     /* return state */ | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4458 |     if (consumed) { | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4459 |         if (inShift) { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4460 |             writer.pos = shiftOutStart; /* back off output */ | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4461 |             *consumed = startinpos; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4462 |         } | 
 | 4463 |         else { | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4464 |             *consumed = s-starts; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4465 |         } | 
| Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4466 |     } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4467 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4468 |     Py_XDECREF(errorHandler); | 
 | 4469 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4470 |     return _PyUnicodeWriter_Finish(&writer); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4471 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4472 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4473 |     Py_XDECREF(errorHandler); | 
 | 4474 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4475 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4476 |     return NULL; | 
 | 4477 | } | 
 | 4478 |  | 
 | 4479 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4480 | PyObject * | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4481 | _PyUnicode_EncodeUTF7(PyObject *str, | 
 | 4482 |                       int base64SetO, | 
 | 4483 |                       int base64WhiteSpace, | 
 | 4484 |                       const char *errors) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4485 | { | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4486 |     int kind; | 
 | 4487 |     void *data; | 
 | 4488 |     Py_ssize_t len; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4489 |     PyObject *v; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4490 |     int inShift = 0; | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4491 |     Py_ssize_t i; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4492 |     unsigned int base64bits = 0; | 
 | 4493 |     unsigned long base64buffer = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4494 |     char * out; | 
 | 4495 |     char * start; | 
 | 4496 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4497 |     if (PyUnicode_READY(str) == -1) | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4498 |         return NULL; | 
 | 4499 |     kind = PyUnicode_KIND(str); | 
 | 4500 |     data = PyUnicode_DATA(str); | 
 | 4501 |     len = PyUnicode_GET_LENGTH(str); | 
 | 4502 |  | 
 | 4503 |     if (len == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4504 |         return PyBytes_FromStringAndSize(NULL, 0); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4505 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4506 |     /* It might be possible to tighten this worst case */ | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4507 |     if (len > PY_SSIZE_T_MAX / 8) | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4508 |         return PyErr_NoMemory(); | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4509 |     v = PyBytes_FromStringAndSize(NULL, len * 8); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4510 |     if (v == NULL) | 
 | 4511 |         return NULL; | 
 | 4512 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4513 |     start = out = PyBytes_AS_STRING(v); | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4514 |     for (i = 0; i < len; ++i) { | 
| Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4515 |         Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4516 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4517 |         if (inShift) { | 
 | 4518 |             if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { | 
 | 4519 |                 /* shifting out */ | 
 | 4520 |                 if (base64bits) { /* output remaining bits */ | 
 | 4521 |                     *out++ = TO_BASE64(base64buffer << (6-base64bits)); | 
 | 4522 |                     base64buffer = 0; | 
 | 4523 |                     base64bits = 0; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4524 |                 } | 
 | 4525 |                 inShift = 0; | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4526 |                 /* Characters not in the BASE64 set implicitly unshift the sequence | 
 | 4527 |                    so no '-' is required, except if the character is itself a '-' */ | 
 | 4528 |                 if (IS_BASE64(ch) || ch == '-') { | 
 | 4529 |                     *out++ = '-'; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4530 |                 } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4531 |                 *out++ = (char) ch; | 
 | 4532 |             } | 
 | 4533 |             else { | 
 | 4534 |                 goto encode_char; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4535 |             } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4536 |         } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4537 |         else { /* not in a shift sequence */ | 
 | 4538 |             if (ch == '+') { | 
 | 4539 |                 *out++ = '+'; | 
 | 4540 |                         *out++ = '-'; | 
 | 4541 |             } | 
 | 4542 |             else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { | 
 | 4543 |                 *out++ = (char) ch; | 
 | 4544 |             } | 
 | 4545 |             else { | 
 | 4546 |                 *out++ = '+'; | 
 | 4547 |                 inShift = 1; | 
 | 4548 |                 goto encode_char; | 
 | 4549 |             } | 
 | 4550 |         } | 
 | 4551 |         continue; | 
 | 4552 | encode_char: | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4553 |         if (ch >= 0x10000) { | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4554 |             assert(ch <= MAX_UNICODE); | 
| Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4555 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4556 |             /* code first surrogate */ | 
 | 4557 |             base64bits += 16; | 
| Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4558 |             base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4559 |             while (base64bits >= 6) { | 
 | 4560 |                 *out++ = TO_BASE64(base64buffer >> (base64bits-6)); | 
 | 4561 |                 base64bits -= 6; | 
 | 4562 |             } | 
 | 4563 |             /* prepare second surrogate */ | 
| Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4564 |             ch = Py_UNICODE_LOW_SURROGATE(ch); | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4565 |         } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4566 |         base64bits += 16; | 
 | 4567 |         base64buffer = (base64buffer << 16) | ch; | 
 | 4568 |         while (base64bits >= 6) { | 
 | 4569 |             *out++ = TO_BASE64(base64buffer >> (base64bits-6)); | 
 | 4570 |             base64bits -= 6; | 
 | 4571 |         } | 
| Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4572 |     } | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4573 |     if (base64bits) | 
 | 4574 |         *out++= TO_BASE64(base64buffer << (6-base64bits) ); | 
 | 4575 |     if (inShift) | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4576 |         *out++ = '-'; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4577 |     if (_PyBytes_Resize(&v, out - start) < 0) | 
 | 4578 |         return NULL; | 
 | 4579 |     return v; | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4580 | } | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4581 | PyObject * | 
 | 4582 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, | 
 | 4583 |                      Py_ssize_t size, | 
 | 4584 |                      int base64SetO, | 
 | 4585 |                      int base64WhiteSpace, | 
 | 4586 |                      const char *errors) | 
 | 4587 | { | 
 | 4588 |     PyObject *result; | 
 | 4589 |     PyObject *tmp = PyUnicode_FromUnicode(s, size); | 
 | 4590 |     if (tmp == NULL) | 
 | 4591 |         return NULL; | 
| Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4592 |     result = _PyUnicode_EncodeUTF7(tmp, base64SetO, | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4593 |                                    base64WhiteSpace, errors); | 
 | 4594 |     Py_DECREF(tmp); | 
 | 4595 |     return result; | 
 | 4596 | } | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4597 |  | 
| Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4598 | #undef IS_BASE64 | 
 | 4599 | #undef FROM_BASE64 | 
 | 4600 | #undef TO_BASE64 | 
 | 4601 | #undef DECODE_DIRECT | 
 | 4602 | #undef ENCODE_DIRECT | 
| Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4603 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4604 | /* --- UTF-8 Codec -------------------------------------------------------- */ | 
 | 4605 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4606 | PyObject * | 
 | 4607 | PyUnicode_DecodeUTF8(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4608 |                      Py_ssize_t size, | 
 | 4609 |                      const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4610 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4611 |     return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); | 
 | 4612 | } | 
 | 4613 |  | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4614 | #include "stringlib/asciilib.h" | 
 | 4615 | #include "stringlib/codecs.h" | 
 | 4616 | #include "stringlib/undef.h" | 
 | 4617 |  | 
| Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4618 | #include "stringlib/ucs1lib.h" | 
 | 4619 | #include "stringlib/codecs.h" | 
 | 4620 | #include "stringlib/undef.h" | 
 | 4621 |  | 
 | 4622 | #include "stringlib/ucs2lib.h" | 
 | 4623 | #include "stringlib/codecs.h" | 
 | 4624 | #include "stringlib/undef.h" | 
 | 4625 |  | 
 | 4626 | #include "stringlib/ucs4lib.h" | 
 | 4627 | #include "stringlib/codecs.h" | 
 | 4628 | #include "stringlib/undef.h" | 
 | 4629 |  | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4630 | /* Mask to quickly check whether a C 'long' contains a | 
 | 4631 |    non-ASCII, UTF8-encoded char. */ | 
 | 4632 | #if (SIZEOF_LONG == 8) | 
| Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4633 | # define ASCII_CHAR_MASK 0x8080808080808080UL | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4634 | #elif (SIZEOF_LONG == 4) | 
| Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4635 | # define ASCII_CHAR_MASK 0x80808080UL | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4636 | #else | 
 | 4637 | # error C 'long' size should be either 4 or 8! | 
 | 4638 | #endif | 
 | 4639 |  | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4640 | static Py_ssize_t | 
 | 4641 | ascii_decode(const char *start, const char *end, Py_UCS1 *dest) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4642 | { | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4643 |     const char *p = start; | 
| Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4644 |     const char *aligned_end = (const char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4645 |  | 
| Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4646 |     /* | 
 | 4647 |      * Issue #17237: m68k is a bit different from most architectures in | 
 | 4648 |      * that objects do not use "natural alignment" - for example, int and | 
 | 4649 |      * long are only aligned at 2-byte boundaries.  Therefore the assert() | 
 | 4650 |      * won't work; also, tests have shown that skipping the "optimised | 
 | 4651 |      * version" will even speed up m68k. | 
 | 4652 |      */ | 
 | 4653 | #if !defined(__m68k__) | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4654 | #if SIZEOF_LONG <= SIZEOF_VOID_P | 
| Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4655 |     assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); | 
 | 4656 |     if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4657 |         /* Fast path, see in STRINGLIB(utf8_decode) for | 
 | 4658 |            an explanation. */ | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4659 |         /* Help allocation */ | 
 | 4660 |         const char *_p = p; | 
 | 4661 |         Py_UCS1 * q = dest; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4662 |         while (_p < aligned_end) { | 
 | 4663 |             unsigned long value = *(const unsigned long *) _p; | 
 | 4664 |             if (value & ASCII_CHAR_MASK) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4665 |                 break; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4666 |             *((unsigned long *)q) = value; | 
 | 4667 |             _p += SIZEOF_LONG; | 
 | 4668 |             q += SIZEOF_LONG; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4669 |         } | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4670 |         p = _p; | 
 | 4671 |         while (p < end) { | 
 | 4672 |             if ((unsigned char)*p & 0x80) | 
 | 4673 |                 break; | 
 | 4674 |             *q++ = *p++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4675 |         } | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4676 |         return p - start; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4677 |     } | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4678 | #endif | 
| Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4679 | #endif | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4680 |     while (p < end) { | 
 | 4681 |         /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h | 
 | 4682 |            for an explanation. */ | 
| Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4683 |         if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4684 |             /* Help allocation */ | 
 | 4685 |             const char *_p = p; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4686 |             while (_p < aligned_end) { | 
 | 4687 |                 unsigned long value = *(unsigned long *) _p; | 
 | 4688 |                 if (value & ASCII_CHAR_MASK) | 
 | 4689 |                     break; | 
 | 4690 |                 _p += SIZEOF_LONG; | 
 | 4691 |             } | 
 | 4692 |             p = _p; | 
 | 4693 |             if (_p == end) | 
 | 4694 |                 break; | 
 | 4695 |         } | 
 | 4696 |         if ((unsigned char)*p & 0x80) | 
 | 4697 |             break; | 
 | 4698 |         ++p; | 
 | 4699 |     } | 
 | 4700 |     memcpy(dest, start, p - start); | 
 | 4701 |     return p - start; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4702 | } | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4703 |  | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4704 | PyObject * | 
 | 4705 | PyUnicode_DecodeUTF8Stateful(const char *s, | 
 | 4706 |                              Py_ssize_t size, | 
 | 4707 |                              const char *errors, | 
 | 4708 |                              Py_ssize_t *consumed) | 
 | 4709 | { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4710 |     _PyUnicodeWriter writer; | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4711 |     const char *starts = s; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4712 |     const char *end = s + size; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4713 |  | 
 | 4714 |     Py_ssize_t startinpos; | 
 | 4715 |     Py_ssize_t endinpos; | 
 | 4716 |     const char *errmsg = ""; | 
 | 4717 |     PyObject *errorHandler = NULL; | 
 | 4718 |     PyObject *exc = NULL; | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4719 |  | 
 | 4720 |     if (size == 0) { | 
 | 4721 |         if (consumed) | 
 | 4722 |             *consumed = 0; | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4723 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4724 |     } | 
 | 4725 |  | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4726 |     /* ASCII is equivalent to the first 128 ordinals in Unicode. */ | 
 | 4727 |     if (size == 1 && (unsigned char)s[0] < 128) { | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4728 |         if (consumed) | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4729 |             *consumed = 1; | 
 | 4730 |         return get_latin1_char((unsigned char)s[0]); | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4731 |     } | 
 | 4732 |  | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4733 |     _PyUnicodeWriter_Init(&writer); | 
| Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4734 |     writer.min_length = size; | 
 | 4735 |     if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4736 |         goto onError; | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4737 |  | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4738 |     writer.pos = ascii_decode(s, end, writer.data); | 
 | 4739 |     s += writer.pos; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4740 |     while (s < end) { | 
 | 4741 |         Py_UCS4 ch; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4742 |         int kind = writer.kind; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4743 |         if (kind == PyUnicode_1BYTE_KIND) { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4744 |             if (PyUnicode_IS_ASCII(writer.buffer)) | 
 | 4745 |                 ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4746 |             else | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4747 |                 ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4748 |         } else if (kind == PyUnicode_2BYTE_KIND) { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4749 |             ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4750 |         } else { | 
 | 4751 |             assert(kind == PyUnicode_4BYTE_KIND); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4752 |             ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4753 |         } | 
 | 4754 |  | 
 | 4755 |         switch (ch) { | 
 | 4756 |         case 0: | 
 | 4757 |             if (s == end || consumed) | 
 | 4758 |                 goto End; | 
 | 4759 |             errmsg = "unexpected end of data"; | 
 | 4760 |             startinpos = s - starts; | 
| Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4761 |             endinpos = end - starts; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4762 |             break; | 
 | 4763 |         case 1: | 
 | 4764 |             errmsg = "invalid start byte"; | 
 | 4765 |             startinpos = s - starts; | 
 | 4766 |             endinpos = startinpos + 1; | 
 | 4767 |             break; | 
 | 4768 |         case 2: | 
| Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4769 |         case 3: | 
 | 4770 |         case 4: | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4771 |             errmsg = "invalid continuation byte"; | 
 | 4772 |             startinpos = s - starts; | 
| Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4773 |             endinpos = startinpos + ch - 1; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4774 |             break; | 
 | 4775 |         default: | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4776 |             if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4777 |                 goto onError; | 
 | 4778 |             continue; | 
 | 4779 |         } | 
 | 4780 |  | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4781 |         if (unicode_decode_call_errorhandler_writer( | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4782 |                 errors, &errorHandler, | 
 | 4783 |                 "utf-8", errmsg, | 
 | 4784 |                 &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4785 |                 &writer)) | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4786 |             goto onError; | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4787 |     } | 
 | 4788 |  | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4789 | End: | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4790 |     if (consumed) | 
 | 4791 |         *consumed = s - starts; | 
 | 4792 |  | 
 | 4793 |     Py_XDECREF(errorHandler); | 
 | 4794 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4795 |     return _PyUnicodeWriter_Finish(&writer); | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4796 |  | 
 | 4797 | onError: | 
 | 4798 |     Py_XDECREF(errorHandler); | 
 | 4799 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4800 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4801 |     return NULL; | 
| Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4802 | } | 
 | 4803 |  | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4804 | #ifdef __APPLE__ | 
 | 4805 |  | 
 | 4806 | /* Simplified UTF-8 decoder using surrogateescape error handler, | 
| Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4807 |    used to decode the command line arguments on Mac OS X. | 
 | 4808 |  | 
 | 4809 |    Return a pointer to a newly allocated wide character string (use | 
| Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4810 |    PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4811 |  | 
 | 4812 | wchar_t* | 
 | 4813 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) | 
 | 4814 | { | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4815 |     const char *e; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4816 |     wchar_t *unicode; | 
 | 4817 |     Py_ssize_t outpos; | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4818 |  | 
 | 4819 |     /* Note: size will always be longer than the resulting Unicode | 
 | 4820 |        character count */ | 
| Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4821 |     if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4822 |         return NULL; | 
| Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4823 |     unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4824 |     if (!unicode) | 
 | 4825 |         return NULL; | 
 | 4826 |  | 
 | 4827 |     /* Unpack UTF-8 encoded data */ | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4828 |     e = s + size; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4829 |     outpos = 0; | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4830 |     while (s < e) { | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4831 |         Py_UCS4 ch; | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4832 | #if SIZEOF_WCHAR_T == 4 | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4833 |         ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4834 | #else | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4835 |         ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4836 | #endif | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4837 |         if (ch > 0xFF) { | 
 | 4838 | #if SIZEOF_WCHAR_T == 4 | 
 | 4839 |             assert(0); | 
 | 4840 | #else | 
 | 4841 |             assert(Py_UNICODE_IS_SURROGATE(ch)); | 
 | 4842 |             /*  compute and append the two surrogates: */ | 
 | 4843 |             unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); | 
 | 4844 |             unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); | 
 | 4845 | #endif | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4846 |         } | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4847 |         else { | 
 | 4848 |             if (!ch && s == e) | 
 | 4849 |                 break; | 
 | 4850 |             /* surrogateescape */ | 
 | 4851 |             unicode[outpos++] = 0xDC00 + (unsigned char)*s++; | 
 | 4852 |         } | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4853 |     } | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4854 |     unicode[outpos] = L'\0'; | 
| Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4855 |     return unicode; | 
 | 4856 | } | 
 | 4857 |  | 
 | 4858 | #endif /* __APPLE__ */ | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4859 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4860 | /* Primary internal function which creates utf8 encoded bytes objects. | 
 | 4861 |  | 
 | 4862 |    Allocation strategy:  if the string is short, convert into a stack buffer | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4863 |    and allocate exactly as much space needed at the end.  Else allocate the | 
 | 4864 |    maximum possible needed (4 result bytes per Unicode character), and return | 
 | 4865 |    the excess memory at the end. | 
| Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4866 | */ | 
| Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4867 | PyObject * | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4868 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4869 | { | 
| Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4870 |     enum PyUnicode_Kind kind; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4871 |     void *data; | 
 | 4872 |     Py_ssize_t size; | 
| Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4873 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4874 |     if (!PyUnicode_Check(unicode)) { | 
 | 4875 |         PyErr_BadArgument(); | 
 | 4876 |         return NULL; | 
 | 4877 |     } | 
 | 4878 |  | 
 | 4879 |     if (PyUnicode_READY(unicode) == -1) | 
 | 4880 |         return NULL; | 
 | 4881 |  | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4882 |     if (PyUnicode_UTF8(unicode)) | 
 | 4883 |         return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), | 
 | 4884 |                                          PyUnicode_UTF8_LENGTH(unicode)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4885 |  | 
 | 4886 |     kind = PyUnicode_KIND(unicode); | 
 | 4887 |     data = PyUnicode_DATA(unicode); | 
 | 4888 |     size = PyUnicode_GET_LENGTH(unicode); | 
 | 4889 |  | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4890 |     switch (kind) { | 
| Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4891 |     default: | 
 | 4892 |         assert(0); | 
 | 4893 |     case PyUnicode_1BYTE_KIND: | 
 | 4894 |         /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ | 
 | 4895 |         assert(!PyUnicode_IS_ASCII(unicode)); | 
 | 4896 |         return ucs1lib_utf8_encoder(unicode, data, size, errors); | 
 | 4897 |     case PyUnicode_2BYTE_KIND: | 
 | 4898 |         return ucs2lib_utf8_encoder(unicode, data, size, errors); | 
 | 4899 |     case PyUnicode_4BYTE_KIND: | 
 | 4900 |         return ucs4lib_utf8_encoder(unicode, data, size, errors); | 
| Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4901 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4902 | } | 
 | 4903 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4904 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4905 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, | 
 | 4906 |                      Py_ssize_t size, | 
 | 4907 |                      const char *errors) | 
 | 4908 | { | 
 | 4909 |     PyObject *v, *unicode; | 
 | 4910 |  | 
 | 4911 |     unicode = PyUnicode_FromUnicode(s, size); | 
 | 4912 |     if (unicode == NULL) | 
 | 4913 |         return NULL; | 
 | 4914 |     v = _PyUnicode_AsUTF8String(unicode, errors); | 
 | 4915 |     Py_DECREF(unicode); | 
 | 4916 |     return v; | 
 | 4917 | } | 
 | 4918 |  | 
 | 4919 | PyObject * | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4920 | PyUnicode_AsUTF8String(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4921 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4922 |     return _PyUnicode_AsUTF8String(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4923 | } | 
 | 4924 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4925 | /* --- UTF-32 Codec ------------------------------------------------------- */ | 
 | 4926 |  | 
 | 4927 | PyObject * | 
 | 4928 | PyUnicode_DecodeUTF32(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4929 |                       Py_ssize_t size, | 
 | 4930 |                       const char *errors, | 
 | 4931 |                       int *byteorder) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4932 | { | 
 | 4933 |     return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); | 
 | 4934 | } | 
 | 4935 |  | 
 | 4936 | PyObject * | 
 | 4937 | PyUnicode_DecodeUTF32Stateful(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4938 |                               Py_ssize_t size, | 
 | 4939 |                               const char *errors, | 
 | 4940 |                               int *byteorder, | 
 | 4941 |                               Py_ssize_t *consumed) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4942 | { | 
 | 4943 |     const char *starts = s; | 
 | 4944 |     Py_ssize_t startinpos; | 
 | 4945 |     Py_ssize_t endinpos; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4946 |     _PyUnicodeWriter writer; | 
| Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4947 |     const unsigned char *q, *e; | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4948 |     int le, bo = 0;       /* assume native ordering by default */ | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4949 |     const char *errmsg = ""; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4950 |     PyObject *errorHandler = NULL; | 
 | 4951 |     PyObject *exc = NULL; | 
| Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4952 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4953 |     q = (unsigned char *)s; | 
 | 4954 |     e = q + size; | 
 | 4955 |  | 
 | 4956 |     if (byteorder) | 
 | 4957 |         bo = *byteorder; | 
 | 4958 |  | 
 | 4959 |     /* Check for BOM marks (U+FEFF) in the input and adjust current | 
 | 4960 |        byte order setting accordingly. In native mode, the leading BOM | 
 | 4961 |        mark is skipped, in all other modes, it is copied to the output | 
 | 4962 |        stream as-is (giving a ZWNBSP character). */ | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4963 |     if (bo == 0 && size >= 4) { | 
 | 4964 |         Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; | 
 | 4965 |         if (bom == 0x0000FEFF) { | 
 | 4966 |             bo = -1; | 
 | 4967 |             q += 4; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4968 |         } | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4969 |         else if (bom == 0xFFFE0000) { | 
 | 4970 |             bo = 1; | 
 | 4971 |             q += 4; | 
 | 4972 |         } | 
 | 4973 |         if (byteorder) | 
 | 4974 |             *byteorder = bo; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4975 |     } | 
 | 4976 |  | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4977 |     if (q == e) { | 
 | 4978 |         if (consumed) | 
 | 4979 |             *consumed = size; | 
| Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4980 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4981 |     } | 
 | 4982 |  | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4983 | #ifdef WORDS_BIGENDIAN | 
 | 4984 |     le = bo < 0; | 
 | 4985 | #else | 
 | 4986 |     le = bo <= 0; | 
 | 4987 | #endif | 
 | 4988 |  | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4989 |     _PyUnicodeWriter_Init(&writer); | 
| Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4990 |     writer.min_length = (e - q + 3) / 4; | 
 | 4991 |     if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4992 |         goto onError; | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4993 |  | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4994 |     while (1) { | 
 | 4995 |         Py_UCS4 ch = 0; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4996 |         Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); | 
| Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4997 |  | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4998 |         if (e - q >= 4) { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4999 |             enum PyUnicode_Kind kind = writer.kind; | 
 | 5000 |             void *data = writer.data; | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5001 |             const unsigned char *last = e - 4; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5002 |             Py_ssize_t pos = writer.pos; | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5003 |             if (le) { | 
 | 5004 |                 do { | 
 | 5005 |                     ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; | 
 | 5006 |                     if (ch > maxch) | 
 | 5007 |                         break; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5008 |                     PyUnicode_WRITE(kind, data, pos++, ch); | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5009 |                     q += 4; | 
 | 5010 |                 } while (q <= last); | 
 | 5011 |             } | 
 | 5012 |             else { | 
 | 5013 |                 do { | 
 | 5014 |                     ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; | 
 | 5015 |                     if (ch > maxch) | 
 | 5016 |                         break; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5017 |                     PyUnicode_WRITE(kind, data, pos++, ch); | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5018 |                     q += 4; | 
 | 5019 |                 } while (q <= last); | 
 | 5020 |             } | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5021 |             writer.pos = pos; | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5022 |         } | 
 | 5023 |  | 
 | 5024 |         if (ch <= maxch) { | 
 | 5025 |             if (q == e || consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5026 |                 break; | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5027 |             /* remaining bytes at the end? (size should be divisible by 4) */ | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5028 |             errmsg = "truncated data"; | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5029 |             startinpos = ((const char *)q) - starts; | 
 | 5030 |             endinpos = ((const char *)e) - starts; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5031 |         } | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5032 |         else { | 
 | 5033 |             if (ch < 0x110000) { | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5034 |                 if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5035 |                     goto onError; | 
 | 5036 |                 q += 4; | 
 | 5037 |                 continue; | 
 | 5038 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5039 |             errmsg = "codepoint not in range(0x110000)"; | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5040 |             startinpos = ((const char *)q) - starts; | 
 | 5041 |             endinpos = startinpos + 4; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5042 |         } | 
| Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5043 |  | 
 | 5044 |         /* The remaining input chars are ignored if the callback | 
 | 5045 |            chooses to skip the input */ | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5046 |         if (unicode_decode_call_errorhandler_writer( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5047 |                 errors, &errorHandler, | 
 | 5048 |                 "utf32", errmsg, | 
 | 5049 |                 &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5050 |                 &writer)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5051 |             goto onError; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5052 |     } | 
 | 5053 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5054 |     if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5055 |         *consumed = (const char *)q-starts; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5056 |  | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5057 |     Py_XDECREF(errorHandler); | 
 | 5058 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5059 |     return _PyUnicodeWriter_Finish(&writer); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5060 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5061 |   onError: | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5062 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5063 |     Py_XDECREF(errorHandler); | 
 | 5064 |     Py_XDECREF(exc); | 
 | 5065 |     return NULL; | 
 | 5066 | } | 
 | 5067 |  | 
 | 5068 | PyObject * | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5069 | _PyUnicode_EncodeUTF32(PyObject *str, | 
 | 5070 |                        const char *errors, | 
 | 5071 |                        int byteorder) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5072 | { | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5073 |     int kind; | 
 | 5074 |     void *data; | 
 | 5075 |     Py_ssize_t len; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5076 |     PyObject *v; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5077 |     unsigned char *p; | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5078 |     Py_ssize_t nsize, i; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5079 |     /* Offsets from p for storing byte pairs in the right order. */ | 
| Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5080 | #if PY_LITTLE_ENDIAN | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5081 |     int iorder[] = {0, 1, 2, 3}; | 
 | 5082 | #else | 
 | 5083 |     int iorder[] = {3, 2, 1, 0}; | 
 | 5084 | #endif | 
 | 5085 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5086 | #define STORECHAR(CH)                           \ | 
 | 5087 |     do {                                        \ | 
 | 5088 |         p[iorder[3]] = ((CH) >> 24) & 0xff;     \ | 
 | 5089 |         p[iorder[2]] = ((CH) >> 16) & 0xff;     \ | 
 | 5090 |         p[iorder[1]] = ((CH) >> 8) & 0xff;      \ | 
 | 5091 |         p[iorder[0]] = (CH) & 0xff;             \ | 
 | 5092 |         p += 4;                                 \ | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5093 |     } while(0) | 
 | 5094 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5095 |     if (!PyUnicode_Check(str)) { | 
 | 5096 |         PyErr_BadArgument(); | 
 | 5097 |         return NULL; | 
 | 5098 |     } | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5099 |     if (PyUnicode_READY(str) == -1) | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5100 |         return NULL; | 
 | 5101 |     kind = PyUnicode_KIND(str); | 
 | 5102 |     data = PyUnicode_DATA(str); | 
 | 5103 |     len = PyUnicode_GET_LENGTH(str); | 
 | 5104 |  | 
 | 5105 |     nsize = len + (byteorder == 0); | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5106 |     if (nsize > PY_SSIZE_T_MAX / 4) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5107 |         return PyErr_NoMemory(); | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5108 |     v = PyBytes_FromStringAndSize(NULL, nsize * 4); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5109 |     if (v == NULL) | 
 | 5110 |         return NULL; | 
 | 5111 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5112 |     p = (unsigned char *)PyBytes_AS_STRING(v); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5113 |     if (byteorder == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5114 |         STORECHAR(0xFEFF); | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5115 |     if (len == 0) | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5116 |         goto done; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5117 |  | 
 | 5118 |     if (byteorder == -1) { | 
 | 5119 |         /* force LE */ | 
 | 5120 |         iorder[0] = 0; | 
 | 5121 |         iorder[1] = 1; | 
 | 5122 |         iorder[2] = 2; | 
 | 5123 |         iorder[3] = 3; | 
 | 5124 |     } | 
 | 5125 |     else if (byteorder == 1) { | 
 | 5126 |         /* force BE */ | 
 | 5127 |         iorder[0] = 3; | 
 | 5128 |         iorder[1] = 2; | 
 | 5129 |         iorder[2] = 1; | 
 | 5130 |         iorder[3] = 0; | 
 | 5131 |     } | 
 | 5132 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5133 |     for (i = 0; i < len; i++) | 
 | 5134 |         STORECHAR(PyUnicode_READ(kind, data, i)); | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5135 |  | 
 | 5136 |   done: | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5137 |     return v; | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5138 | #undef STORECHAR | 
 | 5139 | } | 
 | 5140 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5141 | PyObject * | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5142 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, | 
 | 5143 |                       Py_ssize_t size, | 
 | 5144 |                       const char *errors, | 
 | 5145 |                       int byteorder) | 
 | 5146 | { | 
 | 5147 |     PyObject *result; | 
 | 5148 |     PyObject *tmp = PyUnicode_FromUnicode(s, size); | 
 | 5149 |     if (tmp == NULL) | 
 | 5150 |         return NULL; | 
 | 5151 |     result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); | 
 | 5152 |     Py_DECREF(tmp); | 
 | 5153 |     return result; | 
 | 5154 | } | 
 | 5155 |  | 
 | 5156 | PyObject * | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5157 | PyUnicode_AsUTF32String(PyObject *unicode) | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5158 | { | 
| Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5159 |     return _PyUnicode_EncodeUTF32(unicode, NULL, 0); | 
| Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5160 | } | 
 | 5161 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5162 | /* --- UTF-16 Codec ------------------------------------------------------- */ | 
 | 5163 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5164 | PyObject * | 
 | 5165 | PyUnicode_DecodeUTF16(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5166 |                       Py_ssize_t size, | 
 | 5167 |                       const char *errors, | 
 | 5168 |                       int *byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5169 | { | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5170 |     return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); | 
 | 5171 | } | 
 | 5172 |  | 
 | 5173 | PyObject * | 
 | 5174 | PyUnicode_DecodeUTF16Stateful(const char *s, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5175 |                               Py_ssize_t size, | 
 | 5176 |                               const char *errors, | 
 | 5177 |                               int *byteorder, | 
 | 5178 |                               Py_ssize_t *consumed) | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5179 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5180 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5181 |     Py_ssize_t startinpos; | 
 | 5182 |     Py_ssize_t endinpos; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5183 |     _PyUnicodeWriter writer; | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5184 |     const unsigned char *q, *e; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5185 |     int bo = 0;       /* assume native ordering by default */ | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5186 |     int native_ordering; | 
| Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5187 |     const char *errmsg = ""; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5188 |     PyObject *errorHandler = NULL; | 
 | 5189 |     PyObject *exc = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5190 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5191 |     q = (unsigned char *)s; | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5192 |     e = q + size; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5193 |  | 
 | 5194 |     if (byteorder) | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5195 |         bo = *byteorder; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5196 |  | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5197 |     /* Check for BOM marks (U+FEFF) in the input and adjust current | 
 | 5198 |        byte order setting accordingly. In native mode, the leading BOM | 
 | 5199 |        mark is skipped, in all other modes, it is copied to the output | 
 | 5200 |        stream as-is (giving a ZWNBSP character). */ | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5201 |     if (bo == 0 && size >= 2) { | 
 | 5202 |         const Py_UCS4 bom = (q[1] << 8) | q[0]; | 
 | 5203 |         if (bom == 0xFEFF) { | 
 | 5204 |             q += 2; | 
 | 5205 |             bo = -1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5206 |         } | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5207 |         else if (bom == 0xFFFE) { | 
 | 5208 |             q += 2; | 
 | 5209 |             bo = 1; | 
 | 5210 |         } | 
 | 5211 |         if (byteorder) | 
 | 5212 |             *byteorder = bo; | 
| Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5213 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5214 |  | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5215 |     if (q == e) { | 
 | 5216 |         if (consumed) | 
 | 5217 |             *consumed = size; | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5218 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5219 |     } | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5220 |  | 
| Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5221 | #if PY_LITTLE_ENDIAN | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5222 |     native_ordering = bo <= 0; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5223 | #else | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5224 |     native_ordering = bo >= 0; | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5225 | #endif | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5226 |  | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5227 |     /* Note: size will always be longer than the resulting Unicode | 
 | 5228 |        character count */ | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5229 |     _PyUnicodeWriter_Init(&writer); | 
| Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5230 |     writer.min_length = (e - q + 1) / 2; | 
 | 5231 |     if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5232 |         goto onError; | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5233 |  | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5234 |     while (1) { | 
 | 5235 |         Py_UCS4 ch = 0; | 
 | 5236 |         if (e - q >= 2) { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5237 |             int kind = writer.kind; | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5238 |             if (kind == PyUnicode_1BYTE_KIND) { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5239 |                 if (PyUnicode_IS_ASCII(writer.buffer)) | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5240 |                     ch = asciilib_utf16_decode(&q, e, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5241 |                             (Py_UCS1*)writer.data, &writer.pos, | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5242 |                             native_ordering); | 
 | 5243 |                 else | 
 | 5244 |                     ch = ucs1lib_utf16_decode(&q, e, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5245 |                             (Py_UCS1*)writer.data, &writer.pos, | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5246 |                             native_ordering); | 
 | 5247 |             } else if (kind == PyUnicode_2BYTE_KIND) { | 
 | 5248 |                 ch = ucs2lib_utf16_decode(&q, e, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5249 |                         (Py_UCS2*)writer.data, &writer.pos, | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5250 |                         native_ordering); | 
 | 5251 |             } else { | 
 | 5252 |                 assert(kind == PyUnicode_4BYTE_KIND); | 
 | 5253 |                 ch = ucs4lib_utf16_decode(&q, e, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5254 |                         (Py_UCS4*)writer.data, &writer.pos, | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5255 |                         native_ordering); | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5256 |             } | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5257 |         } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5258 |  | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5259 |         switch (ch) | 
 | 5260 |         { | 
 | 5261 |         case 0: | 
 | 5262 |             /* remaining byte at the end? (size should be even) */ | 
 | 5263 |             if (q == e || consumed) | 
 | 5264 |                 goto End; | 
 | 5265 |             errmsg = "truncated data"; | 
 | 5266 |             startinpos = ((const char *)q) - starts; | 
 | 5267 |             endinpos = ((const char *)e) - starts; | 
 | 5268 |             break; | 
 | 5269 |             /* The remaining input chars are ignored if the callback | 
 | 5270 |                chooses to skip the input */ | 
 | 5271 |         case 1: | 
| Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5272 |             q -= 2; | 
 | 5273 |             if (consumed) | 
| Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5274 |                 goto End; | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5275 |             errmsg = "unexpected end of data"; | 
| Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5276 |             startinpos = ((const char *)q) - starts; | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5277 |             endinpos = ((const char *)e) - starts; | 
 | 5278 |             break; | 
 | 5279 |         case 2: | 
 | 5280 |             errmsg = "illegal encoding"; | 
 | 5281 |             startinpos = ((const char *)q) - 2 - starts; | 
 | 5282 |             endinpos = startinpos + 2; | 
 | 5283 |             break; | 
 | 5284 |         case 3: | 
 | 5285 |             errmsg = "illegal UTF-16 surrogate"; | 
 | 5286 |             startinpos = ((const char *)q) - 4 - starts; | 
 | 5287 |             endinpos = startinpos + 2; | 
 | 5288 |             break; | 
 | 5289 |         default: | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5290 |             if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5291 |                 goto onError; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5292 |             continue; | 
 | 5293 |         } | 
 | 5294 |  | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5295 |         if (unicode_decode_call_errorhandler_writer( | 
| Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5296 |                 errors, | 
 | 5297 |                 &errorHandler, | 
 | 5298 |                 "utf16", errmsg, | 
 | 5299 |                 &starts, | 
 | 5300 |                 (const char **)&e, | 
 | 5301 |                 &startinpos, | 
 | 5302 |                 &endinpos, | 
 | 5303 |                 &exc, | 
 | 5304 |                 (const char **)&q, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5305 |                 &writer)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5306 |             goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 |     } | 
 | 5308 |  | 
| Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5309 | End: | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5310 |     if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5311 |         *consumed = (const char *)q-starts; | 
| Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5312 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5313 |     Py_XDECREF(errorHandler); | 
 | 5314 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5315 |     return _PyUnicodeWriter_Finish(&writer); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5316 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5317 |   onError: | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5318 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5319 |     Py_XDECREF(errorHandler); | 
 | 5320 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 |     return NULL; | 
 | 5322 | } | 
 | 5323 |  | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5324 | PyObject * | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5325 | _PyUnicode_EncodeUTF16(PyObject *str, | 
 | 5326 |                        const char *errors, | 
 | 5327 |                        int byteorder) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5328 | { | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5329 |     enum PyUnicode_Kind kind; | 
 | 5330 |     const void *data; | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5331 |     Py_ssize_t len; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5332 |     PyObject *v; | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5333 |     unsigned short *out; | 
 | 5334 |     Py_ssize_t bytesize; | 
 | 5335 |     Py_ssize_t pairs; | 
| Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5336 | #if PY_BIG_ENDIAN | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5337 |     int native_ordering = byteorder >= 0; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5338 | #else | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5339 |     int native_ordering = byteorder <= 0; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5340 | #endif | 
 | 5341 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5342 |     if (!PyUnicode_Check(str)) { | 
 | 5343 |         PyErr_BadArgument(); | 
 | 5344 |         return NULL; | 
 | 5345 |     } | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5346 |     if (PyUnicode_READY(str) == -1) | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5347 |         return NULL; | 
 | 5348 |     kind = PyUnicode_KIND(str); | 
 | 5349 |     data = PyUnicode_DATA(str); | 
 | 5350 |     len = PyUnicode_GET_LENGTH(str); | 
| Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5351 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5352 |     pairs = 0; | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5353 |     if (kind == PyUnicode_4BYTE_KIND) { | 
 | 5354 |         const Py_UCS4 *in = (const Py_UCS4 *)data; | 
 | 5355 |         const Py_UCS4 *end = in + len; | 
 | 5356 |         while (in < end) | 
 | 5357 |             if (*in++ >= 0x10000) | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5358 |                 pairs++; | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5359 |     } | 
 | 5360 |     if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5361 |         return PyErr_NoMemory(); | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5362 |     bytesize = (len + pairs + (byteorder == 0)) * 2; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5363 |     v = PyBytes_FromStringAndSize(NULL, bytesize); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5364 |     if (v == NULL) | 
 | 5365 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5366 |  | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5367 |     /* output buffer is 2-bytes aligned */ | 
| Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5368 |     assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5369 |     out = (unsigned short *)PyBytes_AS_STRING(v); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 |     if (byteorder == 0) | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5371 |         *out++ = 0xFEFF; | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5372 |     if (len == 0) | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5373 |         goto done; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5374 |  | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5375 |     switch (kind) { | 
 | 5376 |     case PyUnicode_1BYTE_KIND: { | 
 | 5377 |         ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); | 
 | 5378 |         break; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5379 |     } | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5380 |     case PyUnicode_2BYTE_KIND: { | 
 | 5381 |         ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); | 
 | 5382 |         break; | 
| Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5383 |     } | 
| Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5384 |     case PyUnicode_4BYTE_KIND: { | 
 | 5385 |         ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); | 
 | 5386 |         break; | 
 | 5387 |     } | 
 | 5388 |     default: | 
 | 5389 |         assert(0); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5390 |     } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5391 |  | 
 | 5392 |   done: | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5393 |     return v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5394 | } | 
 | 5395 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5396 | PyObject * | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5397 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, | 
 | 5398 |                       Py_ssize_t size, | 
 | 5399 |                       const char *errors, | 
 | 5400 |                       int byteorder) | 
 | 5401 | { | 
 | 5402 |     PyObject *result; | 
 | 5403 |     PyObject *tmp = PyUnicode_FromUnicode(s, size); | 
 | 5404 |     if (tmp == NULL) | 
 | 5405 |         return NULL; | 
 | 5406 |     result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); | 
 | 5407 |     Py_DECREF(tmp); | 
 | 5408 |     return result; | 
 | 5409 | } | 
 | 5410 |  | 
 | 5411 | PyObject * | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5412 | PyUnicode_AsUTF16String(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5413 | { | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5414 |     return _PyUnicode_EncodeUTF16(unicode, NULL, 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | } | 
 | 5416 |  | 
 | 5417 | /* --- Unicode Escape Codec ----------------------------------------------- */ | 
 | 5418 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5419 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines | 
 | 5420 |    if all the escapes in the string make it still a valid ASCII string. | 
 | 5421 |    Returns -1 if any escapes were found which cause the string to | 
 | 5422 |    pop out of ASCII range.  Otherwise returns the length of the | 
 | 5423 |    required buffer to hold the string. | 
 | 5424 |    */ | 
| Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5425 | static Py_ssize_t | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5426 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) | 
 | 5427 | { | 
 | 5428 |     const unsigned char *p = (const unsigned char *)s; | 
 | 5429 |     const unsigned char *end = p + size; | 
 | 5430 |     Py_ssize_t length = 0; | 
 | 5431 |  | 
 | 5432 |     if (size < 0) | 
 | 5433 |         return -1; | 
 | 5434 |  | 
 | 5435 |     for (; p < end; ++p) { | 
 | 5436 |         if (*p > 127) { | 
 | 5437 |             /* Non-ASCII */ | 
 | 5438 |             return -1; | 
 | 5439 |         } | 
 | 5440 |         else if (*p != '\\') { | 
 | 5441 |             /* Normal character */ | 
 | 5442 |             ++length; | 
 | 5443 |         } | 
 | 5444 |         else { | 
 | 5445 |             /* Backslash-escape, check next char */ | 
 | 5446 |             ++p; | 
 | 5447 |             /* Escape sequence reaches till end of string or | 
 | 5448 |                non-ASCII follow-up. */ | 
 | 5449 |             if (p >= end || *p > 127) | 
 | 5450 |                 return -1; | 
 | 5451 |             switch (*p) { | 
 | 5452 |             case '\n': | 
 | 5453 |                 /* backslash + \n result in zero characters */ | 
 | 5454 |                 break; | 
 | 5455 |             case '\\': case '\'': case '\"': | 
 | 5456 |             case 'b': case 'f': case 't': | 
 | 5457 |             case 'n': case 'r': case 'v': case 'a': | 
 | 5458 |                 ++length; | 
 | 5459 |                 break; | 
 | 5460 |             case '0': case '1': case '2': case '3': | 
 | 5461 |             case '4': case '5': case '6': case '7': | 
 | 5462 |             case 'x': case 'u': case 'U': case 'N': | 
 | 5463 |                 /* these do not guarantee ASCII characters */ | 
 | 5464 |                 return -1; | 
 | 5465 |             default: | 
 | 5466 |                 /* count the backslash + the other character */ | 
 | 5467 |                 length += 2; | 
 | 5468 |             } | 
 | 5469 |         } | 
 | 5470 |     } | 
 | 5471 |     return length; | 
 | 5472 | } | 
 | 5473 |  | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5474 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; | 
| Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5475 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5476 | PyObject * | 
 | 5477 | PyUnicode_DecodeUnicodeEscape(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5478 |                               Py_ssize_t size, | 
| Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5479 |                               const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5481 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5482 |     Py_ssize_t startinpos; | 
 | 5483 |     Py_ssize_t endinpos; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5484 |     _PyUnicodeWriter writer; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5485 |     const char *end; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5486 |     char* message; | 
 | 5487 |     Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5488 |     PyObject *errorHandler = NULL; | 
 | 5489 |     PyObject *exc = NULL; | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5490 |     Py_ssize_t len; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5491 |  | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5492 |     len = length_of_escaped_ascii_string(s, size); | 
| Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5493 |     if (len == 0) | 
 | 5494 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5495 |  | 
 | 5496 |     /* After length_of_escaped_ascii_string() there are two alternatives, | 
 | 5497 |        either the string is pure ASCII with named escapes like \n, etc. | 
 | 5498 |        and we determined it's exact size (common case) | 
 | 5499 |        or it contains \x, \u, ... escape sequences.  then we create a | 
 | 5500 |        legacy wchar string and resize it at the end of this function. */ | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5501 |     _PyUnicodeWriter_Init(&writer); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5502 |     if (len > 0) { | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5503 |         writer.min_length = len; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5504 |     } | 
 | 5505 |     else { | 
 | 5506 |         /* Escaped strings will always be longer than the resulting | 
 | 5507 |            Unicode string, so we start with size here and then reduce the | 
 | 5508 |            length after conversion to the true value. | 
 | 5509 |            (but if the error callback returns a long replacement string | 
 | 5510 |            we'll have to allocate more space) */ | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5511 |         writer.min_length = size; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5512 |     } | 
 | 5513 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5514 |     if (size == 0) | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5515 |         return _PyUnicodeWriter_Finish(&writer); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5516 |     end = s + size; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5517 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5518 |     while (s < end) { | 
 | 5519 |         unsigned char c; | 
| Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5520 |         Py_UCS4 x; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5521 |         int digits; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5522 |  | 
 | 5523 |         /* Non-escape characters are interpreted as Unicode ordinals */ | 
 | 5524 |         if (*s != '\\') { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5525 |             x = (unsigned char)*s; | 
 | 5526 |             s++; | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5527 |             if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5528 |                 goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5529 |             continue; | 
 | 5530 |         } | 
 | 5531 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5532 |         startinpos = s-starts; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5533 |         /* \ - Escapes */ | 
 | 5534 |         s++; | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5535 |         c = *s++; | 
 | 5536 |         if (s > end) | 
 | 5537 |             c = '\0'; /* Invalid after \ */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5538 |  | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5539 |         switch (c) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5541 |             /* \x escapes */ | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5542 | #define WRITECHAR(ch)                                                      \ | 
 | 5543 |             do {                                                           \ | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5544 |                 if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0)    \ | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5545 |                     goto onError;                                          \ | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5546 |             } while(0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5547 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5548 |         case '\n': break; | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5549 |         case '\\': WRITECHAR('\\'); break; | 
 | 5550 |         case '\'': WRITECHAR('\''); break; | 
 | 5551 |         case '\"': WRITECHAR('\"'); break; | 
 | 5552 |         case 'b': WRITECHAR('\b'); break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5553 |         /* FF */ | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5554 |         case 'f': WRITECHAR('\014'); break; | 
 | 5555 |         case 't': WRITECHAR('\t'); break; | 
 | 5556 |         case 'n': WRITECHAR('\n'); break; | 
 | 5557 |         case 'r': WRITECHAR('\r'); break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5558 |         /* VT */ | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5559 |         case 'v': WRITECHAR('\013'); break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5560 |         /* BEL, not classic C */ | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5561 |         case 'a': WRITECHAR('\007'); break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5563 |             /* \OOO (octal) escapes */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5564 |         case '0': case '1': case '2': case '3': | 
 | 5565 |         case '4': case '5': case '6': case '7': | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5566 |             x = s[-1] - '0'; | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5567 |             if (s < end && '0' <= *s && *s <= '7') { | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5568 |                 x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5569 |                 if (s < end && '0' <= *s && *s <= '7') | 
| Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5570 |                     x = (x<<3) + *s++ - '0'; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5571 |             } | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5572 |             WRITECHAR(x); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5573 |             break; | 
 | 5574 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5575 |             /* hex escapes */ | 
 | 5576 |             /* \xXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5577 |         case 'x': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5578 |             digits = 2; | 
 | 5579 |             message = "truncated \\xXX escape"; | 
 | 5580 |             goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5581 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5582 |             /* \uXXXX */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5583 |         case 'u': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5584 |             digits = 4; | 
 | 5585 |             message = "truncated \\uXXXX escape"; | 
 | 5586 |             goto hexescape; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5587 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5588 |             /* \UXXXXXXXX */ | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5589 |         case 'U': | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5590 |             digits = 8; | 
 | 5591 |             message = "truncated \\UXXXXXXXX escape"; | 
 | 5592 |         hexescape: | 
 | 5593 |             chr = 0; | 
| Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5594 |             if (end - s < digits) { | 
 | 5595 |                 /* count only hex digits */ | 
 | 5596 |                 for (; s < end; ++s) { | 
 | 5597 |                     c = (unsigned char)*s; | 
 | 5598 |                     if (!Py_ISXDIGIT(c)) | 
 | 5599 |                         goto error; | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5600 |                 } | 
| Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5601 |                 goto error; | 
 | 5602 |             } | 
 | 5603 |             for (; digits--; ++s) { | 
 | 5604 |                 c = (unsigned char)*s; | 
 | 5605 |                 if (!Py_ISXDIGIT(c)) | 
 | 5606 |                     goto error; | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5607 |                 chr = (chr<<4) & ~0xF; | 
 | 5608 |                 if (c >= '0' && c <= '9') | 
 | 5609 |                     chr += c - '0'; | 
 | 5610 |                 else if (c >= 'a' && c <= 'f') | 
 | 5611 |                     chr += 10 + c - 'a'; | 
 | 5612 |                 else | 
 | 5613 |                     chr += 10 + c - 'A'; | 
 | 5614 |             } | 
| Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5615 |             if (chr == 0xffffffff && PyErr_Occurred()) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5616 |                 /* _decoding_error will have already written into the | 
 | 5617 |                    target buffer. */ | 
 | 5618 |                 break; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5619 |         store: | 
| Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5620 |             /* when we get here, chr is a 32-bit unicode character */ | 
| Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5621 |             message = "illegal Unicode character"; | 
 | 5622 |             if (chr > MAX_UNICODE) | 
| Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5623 |                 goto error; | 
| Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5624 |             WRITECHAR(chr); | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5625 |             break; | 
 | 5626 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5627 |             /* \N{name} */ | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5628 |         case 'N': | 
 | 5629 |             message = "malformed \\N character escape"; | 
 | 5630 |             if (ucnhash_CAPI == NULL) { | 
 | 5631 |                 /* load the unicode data module */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5632 |                 ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( | 
 | 5633 |                                                 PyUnicodeData_CAPSULE_NAME, 1); | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5634 |                 if (ucnhash_CAPI == NULL) | 
 | 5635 |                     goto ucnhashError; | 
 | 5636 |             } | 
 | 5637 |             if (*s == '{') { | 
 | 5638 |                 const char *start = s+1; | 
 | 5639 |                 /* look for the closing brace */ | 
 | 5640 |                 while (*s != '}' && s < end) | 
 | 5641 |                     s++; | 
 | 5642 |                 if (s > start && s < end && *s == '}') { | 
 | 5643 |                     /* found a name.  look it up in the unicode database */ | 
 | 5644 |                     message = "unknown Unicode character name"; | 
 | 5645 |                     s++; | 
| Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5646 |                     if (s - start - 1 <= INT_MAX && | 
| Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5647 |                         ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), | 
| Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5648 |                                               &chr, 0)) | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5649 |                         goto store; | 
 | 5650 |                 } | 
 | 5651 |             } | 
| Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5652 |             goto error; | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5653 |  | 
 | 5654 |         default: | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5655 |             if (s > end) { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5656 |                 message = "\\ at end of string"; | 
 | 5657 |                 s--; | 
| Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5658 |                 goto error; | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5659 |             } | 
 | 5660 |             else { | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5661 |                 WRITECHAR('\\'); | 
| Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5662 |                 WRITECHAR((unsigned char)s[-1]); | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5663 |             } | 
| Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5664 |             break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 |         } | 
| Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5666 |         continue; | 
 | 5667 |  | 
 | 5668 |       error: | 
 | 5669 |         endinpos = s-starts; | 
| Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5670 |         if (unicode_decode_call_errorhandler_writer( | 
| Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5671 |                 errors, &errorHandler, | 
 | 5672 |                 "unicodeescape", message, | 
 | 5673 |                 &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5674 |                 &writer)) | 
| Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5675 |             goto onError; | 
 | 5676 |         continue; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 |     } | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5678 | #undef WRITECHAR | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5679 |  | 
| Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5680 |     Py_XDECREF(errorHandler); | 
 | 5681 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5682 |     return _PyUnicodeWriter_Finish(&writer); | 
| Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5683 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5684 |   ucnhashError: | 
| Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5685 |     PyErr_SetString( | 
 | 5686 |         PyExc_UnicodeError, | 
 | 5687 |         "\\N escapes not supported (can't load unicodedata module)" | 
 | 5688 |         ); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5689 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5690 |     Py_XDECREF(errorHandler); | 
 | 5691 |     Py_XDECREF(exc); | 
| Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5692 |     return NULL; | 
 | 5693 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5694 |   onError: | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5695 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5696 |     Py_XDECREF(errorHandler); | 
 | 5697 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5698 |     return NULL; | 
 | 5699 | } | 
 | 5700 |  | 
 | 5701 | /* Return a Unicode-Escape string version of the Unicode object. | 
 | 5702 |  | 
 | 5703 |    If quotes is true, the string is enclosed in u"" or u'' quotes as | 
 | 5704 |    appropriate. | 
 | 5705 |  | 
 | 5706 | */ | 
 | 5707 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5708 | PyObject * | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5709 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5710 | { | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5711 |     Py_ssize_t i, len; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5712 |     PyObject *repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 |     char *p; | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5714 |     int kind; | 
 | 5715 |     void *data; | 
 | 5716 |     Py_ssize_t expandsize = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 |  | 
| Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5718 |     /* Initial allocation is based on the longest-possible character | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5719 |        escape. | 
 | 5720 |  | 
| Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5721 |        For UCS1 strings it's '\xxx', 4 bytes per source character. | 
 | 5722 |        For UCS2 strings it's '\uxxxx', 6 bytes per source character. | 
 | 5723 |        For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5724 |     */ | 
 | 5725 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5726 |     if (!PyUnicode_Check(unicode)) { | 
 | 5727 |         PyErr_BadArgument(); | 
 | 5728 |         return NULL; | 
 | 5729 |     } | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5730 |     if (PyUnicode_READY(unicode) == -1) | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5731 |         return NULL; | 
 | 5732 |     len = PyUnicode_GET_LENGTH(unicode); | 
 | 5733 |     kind = PyUnicode_KIND(unicode); | 
 | 5734 |     data = PyUnicode_DATA(unicode); | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5735 |     switch (kind) { | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5736 |     case PyUnicode_1BYTE_KIND: expandsize = 4; break; | 
 | 5737 |     case PyUnicode_2BYTE_KIND: expandsize = 6; break; | 
 | 5738 |     case PyUnicode_4BYTE_KIND: expandsize = 10; break; | 
 | 5739 |     } | 
 | 5740 |  | 
 | 5741 |     if (len == 0) | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5742 |         return PyBytes_FromStringAndSize(NULL, 0); | 
 | 5743 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5744 |     if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5745 |         return PyErr_NoMemory(); | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5746 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5747 |     repr = PyBytes_FromStringAndSize(NULL, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5748 |                                      2 | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5749 |                                      + expandsize*len | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5750 |                                      + 1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 |     if (repr == NULL) | 
 | 5752 |         return NULL; | 
 | 5753 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5754 |     p = PyBytes_AS_STRING(repr); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5755 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5756 |     for (i = 0; i < len; i++) { | 
| Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5757 |         Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5758 |  | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5759 |         /* Escape backslashes */ | 
 | 5760 |         if (ch == '\\') { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 |             *p++ = '\\'; | 
 | 5762 |             *p++ = (char) ch; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5763 |             continue; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5764 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5765 |  | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5766 |         /* Map 21-bit characters to '\U00xxxxxx' */ | 
 | 5767 |         else if (ch >= 0x10000) { | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5768 |             assert(ch <= MAX_UNICODE); | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5769 |             *p++ = '\\'; | 
 | 5770 |             *p++ = 'U'; | 
| Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5771 |             *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; | 
 | 5772 |             *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; | 
 | 5773 |             *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; | 
 | 5774 |             *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; | 
 | 5775 |             *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; | 
 | 5776 |             *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; | 
 | 5777 |             *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; | 
 | 5778 |             *p++ = Py_hexdigits[ch & 0x0000000F]; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5779 |             continue; | 
| Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5780 |         } | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5781 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 |         /* Map 16-bit characters to '\uxxxx' */ | 
| Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5783 |         if (ch >= 256) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5784 |             *p++ = '\\'; | 
 | 5785 |             *p++ = 'u'; | 
| Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5786 |             *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; | 
 | 5787 |             *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; | 
 | 5788 |             *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; | 
 | 5789 |             *p++ = Py_hexdigits[ch & 0x000F]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5791 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5792 |         /* Map special whitespace to '\t', \n', '\r' */ | 
 | 5793 |         else if (ch == '\t') { | 
 | 5794 |             *p++ = '\\'; | 
 | 5795 |             *p++ = 't'; | 
 | 5796 |         } | 
 | 5797 |         else if (ch == '\n') { | 
 | 5798 |             *p++ = '\\'; | 
 | 5799 |             *p++ = 'n'; | 
 | 5800 |         } | 
 | 5801 |         else if (ch == '\r') { | 
 | 5802 |             *p++ = '\\'; | 
 | 5803 |             *p++ = 'r'; | 
 | 5804 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5805 |  | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5806 |         /* Map non-printable US ASCII to '\xhh' */ | 
| Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5807 |         else if (ch < ' ' || ch >= 0x7F) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5808 |             *p++ = '\\'; | 
| Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5809 |             *p++ = 'x'; | 
| Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5810 |             *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; | 
 | 5811 |             *p++ = Py_hexdigits[ch & 0x000F]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5812 |         } | 
| Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5813 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5814 |         /* Copy everything else as-is */ | 
 | 5815 |         else | 
 | 5816 |             *p++ = (char) ch; | 
 | 5817 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5818 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5819 |     assert(p - PyBytes_AS_STRING(repr) > 0); | 
 | 5820 |     if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) | 
 | 5821 |         return NULL; | 
 | 5822 |     return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5823 | } | 
 | 5824 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5825 | PyObject * | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5826 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, | 
 | 5827 |                               Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5828 | { | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5829 |     PyObject *result; | 
 | 5830 |     PyObject *tmp = PyUnicode_FromUnicode(s, size); | 
 | 5831 |     if (tmp == NULL) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5832 |         return NULL; | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5833 |     result = PyUnicode_AsUnicodeEscapeString(tmp); | 
 | 5834 |     Py_DECREF(tmp); | 
 | 5835 |     return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | } | 
 | 5837 |  | 
 | 5838 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ | 
 | 5839 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5840 | PyObject * | 
 | 5841 | PyUnicode_DecodeRawUnicodeEscape(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5842 |                                  Py_ssize_t size, | 
 | 5843 |                                  const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5845 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5846 |     Py_ssize_t startinpos; | 
 | 5847 |     Py_ssize_t endinpos; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5848 |     _PyUnicodeWriter writer; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5849 |     const char *end; | 
 | 5850 |     const char *bs; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5851 |     PyObject *errorHandler = NULL; | 
 | 5852 |     PyObject *exc = NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5853 |  | 
| Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5854 |     if (size == 0) | 
 | 5855 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5856 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5857 |     /* Escaped strings will always be longer than the resulting | 
 | 5858 |        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] | 5859 |        length after conversion to the true value. (But decoding error | 
 | 5860 |        handler might have to resize the string) */ | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5861 |     _PyUnicodeWriter_Init(&writer); | 
 | 5862 |     writer.min_length = size; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5863 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5864 |     end = s + size; | 
 | 5865 |     while (s < end) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5866 |         unsigned char c; | 
 | 5867 |         Py_UCS4 x; | 
 | 5868 |         int i; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5869 |         int count; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5870 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5871 |         /* Non-escape characters are interpreted as Unicode ordinals */ | 
 | 5872 |         if (*s != '\\') { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5873 |             x = (unsigned char)*s++; | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5874 |             if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5875 |                 goto onError; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5876 |             continue; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5877 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5878 |         startinpos = s-starts; | 
 | 5879 |  | 
 | 5880 |         /* \u-escapes are only interpreted iff the number of leading | 
 | 5881 |            backslashes if odd */ | 
 | 5882 |         bs = s; | 
 | 5883 |         for (;s < end;) { | 
 | 5884 |             if (*s != '\\') | 
 | 5885 |                 break; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5886 |             x = (unsigned char)*s++; | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5887 |             if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5888 |                 goto onError; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5889 |         } | 
 | 5890 |         if (((s - bs) & 1) == 0 || | 
 | 5891 |             s >= end || | 
 | 5892 |             (*s != 'u' && *s != 'U')) { | 
 | 5893 |             continue; | 
 | 5894 |         } | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5895 |         writer.pos--; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5896 |         count = *s=='u' ? 4 : 8; | 
 | 5897 |         s++; | 
 | 5898 |  | 
 | 5899 |         /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5900 |         for (x = 0, i = 0; i < count; ++i, ++s) { | 
 | 5901 |             c = (unsigned char)*s; | 
| David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5902 |             if (!Py_ISXDIGIT(c)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5903 |                 endinpos = s-starts; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5904 |                 if (unicode_decode_call_errorhandler_writer( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5905 |                         errors, &errorHandler, | 
 | 5906 |                         "rawunicodeescape", "truncated \\uXXXX", | 
 | 5907 |                         &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5908 |                         &writer)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5909 |                     goto onError; | 
 | 5910 |                 goto nextByte; | 
 | 5911 |             } | 
 | 5912 |             x = (x<<4) & ~0xF; | 
 | 5913 |             if (c >= '0' && c <= '9') | 
 | 5914 |                 x += c - '0'; | 
 | 5915 |             else if (c >= 'a' && c <= 'f') | 
 | 5916 |                 x += 10 + c - 'a'; | 
 | 5917 |             else | 
 | 5918 |                 x += 10 + c - 'A'; | 
 | 5919 |         } | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5920 |         if (x <= MAX_UNICODE) { | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5921 |             if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5922 |                 goto onError; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5923 |         } | 
 | 5924 |         else { | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5925 |             endinpos = s-starts; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5926 |             if (unicode_decode_call_errorhandler_writer( | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5927 |                     errors, &errorHandler, | 
 | 5928 |                     "rawunicodeescape", "\\Uxxxxxxxx out of range", | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5929 |                     &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5930 |                     &writer)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5931 |                 goto onError; | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5932 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5933 |       nextByte: | 
 | 5934 |         ; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5936 |     Py_XDECREF(errorHandler); | 
 | 5937 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5938 |     return _PyUnicodeWriter_Finish(&writer); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5939 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5940 |   onError: | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5941 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5942 |     Py_XDECREF(errorHandler); | 
 | 5943 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 |     return NULL; | 
 | 5945 | } | 
 | 5946 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5947 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5948 | PyObject * | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5949 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | { | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5951 |     PyObject *repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5952 |     char *p; | 
 | 5953 |     char *q; | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5954 |     Py_ssize_t expandsize, pos; | 
 | 5955 |     int kind; | 
 | 5956 |     void *data; | 
 | 5957 |     Py_ssize_t len; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5958 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5959 |     if (!PyUnicode_Check(unicode)) { | 
 | 5960 |         PyErr_BadArgument(); | 
 | 5961 |         return NULL; | 
 | 5962 |     } | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5963 |     if (PyUnicode_READY(unicode) == -1) | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5964 |         return NULL; | 
 | 5965 |     kind = PyUnicode_KIND(unicode); | 
 | 5966 |     data = PyUnicode_DATA(unicode); | 
 | 5967 |     len = PyUnicode_GET_LENGTH(unicode); | 
| Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 5968 |     /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 | 
 | 5969 |        bytes, and 1 byte characters 4. */ | 
 | 5970 |     expandsize = kind * 2 + 2; | 
| Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5971 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5972 |     if (len > PY_SSIZE_T_MAX / expandsize) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5973 |         return PyErr_NoMemory(); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5974 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5975 |     repr = PyBytes_FromStringAndSize(NULL, expandsize * len); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 |     if (repr == NULL) | 
 | 5977 |         return NULL; | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5978 |     if (len == 0) | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5979 |         return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 |  | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5981 |     p = q = PyBytes_AS_STRING(repr); | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5982 |     for (pos = 0; pos < len; pos++) { | 
 | 5983 |         Py_UCS4 ch = PyUnicode_READ(kind, data, pos); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5984 |         /* Map 32-bit characters to '\Uxxxxxxxx' */ | 
 | 5985 |         if (ch >= 0x10000) { | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5986 |             assert(ch <= MAX_UNICODE); | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5987 |             *p++ = '\\'; | 
 | 5988 |             *p++ = 'U'; | 
| Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5989 |             *p++ = Py_hexdigits[(ch >> 28) & 0xf]; | 
 | 5990 |             *p++ = Py_hexdigits[(ch >> 24) & 0xf]; | 
 | 5991 |             *p++ = Py_hexdigits[(ch >> 20) & 0xf]; | 
 | 5992 |             *p++ = Py_hexdigits[(ch >> 16) & 0xf]; | 
 | 5993 |             *p++ = Py_hexdigits[(ch >> 12) & 0xf]; | 
 | 5994 |             *p++ = Py_hexdigits[(ch >> 8) & 0xf]; | 
 | 5995 |             *p++ = Py_hexdigits[(ch >> 4) & 0xf]; | 
 | 5996 |             *p++ = Py_hexdigits[ch & 15]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5997 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5998 |         /* Map 16-bit characters to '\uxxxx' */ | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5999 |         else if (ch >= 256) { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 |             *p++ = '\\'; | 
 | 6001 |             *p++ = 'u'; | 
| Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6002 |             *p++ = Py_hexdigits[(ch >> 12) & 0xf]; | 
 | 6003 |             *p++ = Py_hexdigits[(ch >> 8) & 0xf]; | 
 | 6004 |             *p++ = Py_hexdigits[(ch >> 4) & 0xf]; | 
 | 6005 |             *p++ = Py_hexdigits[ch & 15]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6006 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6007 |         /* Copy everything else as-is */ | 
 | 6008 |         else | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 |             *p++ = (char) ch; | 
 | 6010 |     } | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6011 |  | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6012 |     assert(p > q); | 
 | 6013 |     if (_PyBytes_Resize(&repr, p - q) < 0) | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6014 |         return NULL; | 
 | 6015 |     return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | } | 
 | 6017 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6018 | PyObject * | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6019 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, | 
 | 6020 |                                  Py_ssize_t size) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | { | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6022 |     PyObject *result; | 
 | 6023 |     PyObject *tmp = PyUnicode_FromUnicode(s, size); | 
 | 6024 |     if (tmp == NULL) | 
| Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6025 |         return NULL; | 
| Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6026 |     result = PyUnicode_AsRawUnicodeEscapeString(tmp); | 
 | 6027 |     Py_DECREF(tmp); | 
 | 6028 |     return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | } | 
 | 6030 |  | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6031 | /* --- Unicode Internal Codec ------------------------------------------- */ | 
 | 6032 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6033 | PyObject * | 
 | 6034 | _PyUnicode_DecodeUnicodeInternal(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6035 |                                  Py_ssize_t size, | 
 | 6036 |                                  const char *errors) | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6037 | { | 
 | 6038 |     const char *starts = s; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6039 |     Py_ssize_t startinpos; | 
 | 6040 |     Py_ssize_t endinpos; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6041 |     _PyUnicodeWriter writer; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6042 |     const char *end; | 
 | 6043 |     const char *reason; | 
 | 6044 |     PyObject *errorHandler = NULL; | 
 | 6045 |     PyObject *exc = NULL; | 
 | 6046 |  | 
| Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6047 |     if (PyErr_WarnEx(PyExc_DeprecationWarning, | 
| Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6048 |                      "unicode_internal codec has been deprecated", | 
| Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6049 |                      1)) | 
 | 6050 |         return NULL; | 
 | 6051 |  | 
| Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6052 |     if (size == 0) | 
 | 6053 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6054 |  | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6055 |     _PyUnicodeWriter_Init(&writer); | 
 | 6056 |     if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { | 
 | 6057 |         PyErr_NoMemory(); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6058 |         goto onError; | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6059 |     } | 
 | 6060 |     writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6061 |  | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6062 |     end = s + size; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6063 |     while (s < end) { | 
| Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6064 |         Py_UNICODE uch; | 
| Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6065 |         Py_UCS4 ch; | 
| Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6066 |         if (end - s < Py_UNICODE_SIZE) { | 
| Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6067 |             endinpos = end-starts; | 
 | 6068 |             reason = "truncated input"; | 
 | 6069 |             goto error; | 
 | 6070 |         } | 
| Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6071 |         /* We copy the raw representation one byte at a time because the | 
 | 6072 |            pointer may be unaligned (see test_codeccallbacks). */ | 
| Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6073 |         ((char *) &uch)[0] = s[0]; | 
 | 6074 |         ((char *) &uch)[1] = s[1]; | 
| Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6075 | #ifdef Py_UNICODE_WIDE | 
| Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6076 |         ((char *) &uch)[2] = s[2]; | 
 | 6077 |         ((char *) &uch)[3] = s[3]; | 
| Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6078 | #endif | 
| Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6079 |         ch = uch; | 
| Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6080 | #ifdef Py_UNICODE_WIDE | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6081 |         /* We have to sanity check the raw data, otherwise doom looms for | 
 | 6082 |            some malformed UCS-4 data. */ | 
| Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6083 |         if (ch > 0x10ffff) { | 
| Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6084 |             endinpos = s - starts + Py_UNICODE_SIZE; | 
 | 6085 |             reason = "illegal code point (> 0x10FFFF)"; | 
 | 6086 |             goto error; | 
| Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6087 |         } | 
| Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6088 | #endif | 
| Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6089 |         s += Py_UNICODE_SIZE; | 
 | 6090 | #ifndef Py_UNICODE_WIDE | 
| Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6091 |         if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) | 
| Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6092 |         { | 
| Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6093 |             Py_UNICODE uch2; | 
 | 6094 |             ((char *) &uch2)[0] = s[0]; | 
 | 6095 |             ((char *) &uch2)[1] = s[1]; | 
| Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6096 |             if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) | 
| Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6097 |             { | 
| Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6098 |                 ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); | 
| Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6099 |                 s += Py_UNICODE_SIZE; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6100 |             } | 
 | 6101 |         } | 
| Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6102 | #endif | 
 | 6103 |  | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6104 |         if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) | 
| Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6105 |             goto onError; | 
| Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6106 |         continue; | 
 | 6107 |  | 
 | 6108 |   error: | 
 | 6109 |         startinpos = s - starts; | 
| Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6110 |         if (unicode_decode_call_errorhandler_writer( | 
| Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6111 |                 errors, &errorHandler, | 
 | 6112 |                 "unicode_internal", reason, | 
 | 6113 |                 &starts, &end, &startinpos, &endinpos, &exc, &s, | 
| Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6114 |                 &writer)) | 
| Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6115 |             goto onError; | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6116 |     } | 
 | 6117 |  | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6118 |     Py_XDECREF(errorHandler); | 
 | 6119 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6120 |     return _PyUnicodeWriter_Finish(&writer); | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6121 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6122 |   onError: | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6123 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6124 |     Py_XDECREF(errorHandler); | 
 | 6125 |     Py_XDECREF(exc); | 
 | 6126 |     return NULL; | 
 | 6127 | } | 
 | 6128 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | /* --- Latin-1 Codec ------------------------------------------------------ */ | 
 | 6130 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6131 | PyObject * | 
 | 6132 | PyUnicode_DecodeLatin1(const char *s, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6133 |                        Py_ssize_t size, | 
 | 6134 |                        const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6135 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 |     /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ | 
| Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6137 |     return _PyUnicode_FromUCS1((unsigned char*)s, size); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | } | 
 | 6139 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6140 | /* create or adjust a UnicodeEncodeError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6141 | static void | 
 | 6142 | make_encode_exception(PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6143 |                       const char *encoding, | 
| Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6144 |                       PyObject *unicode, | 
 | 6145 |                       Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 6146 |                       const char *reason) | 
 | 6147 | { | 
 | 6148 |     if (*exceptionObject == NULL) { | 
 | 6149 |         *exceptionObject = PyObject_CallFunction( | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6150 |             PyExc_UnicodeEncodeError, "sOnns", | 
| Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6151 |             encoding, unicode, startpos, endpos, reason); | 
 | 6152 |     } | 
 | 6153 |     else { | 
 | 6154 |         if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) | 
 | 6155 |             goto onError; | 
 | 6156 |         if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) | 
 | 6157 |             goto onError; | 
 | 6158 |         if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) | 
 | 6159 |             goto onError; | 
 | 6160 |         return; | 
 | 6161 |       onError: | 
 | 6162 |         Py_DECREF(*exceptionObject); | 
 | 6163 |         *exceptionObject = NULL; | 
 | 6164 |     } | 
 | 6165 | } | 
 | 6166 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6167 | /* raises a UnicodeEncodeError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6168 | static void | 
 | 6169 | raise_encode_exception(PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6170 |                        const char *encoding, | 
| Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6171 |                        PyObject *unicode, | 
 | 6172 |                        Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 6173 |                        const char *reason) | 
 | 6174 | { | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6175 |     make_encode_exception(exceptionObject, | 
| Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6176 |                           encoding, unicode, startpos, endpos, reason); | 
 | 6177 |     if (*exceptionObject != NULL) | 
 | 6178 |         PyCodec_StrictErrors(*exceptionObject); | 
 | 6179 | } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6180 |  | 
 | 6181 | /* error handling callback helper: | 
 | 6182 |    build arguments, call the callback and check the arguments, | 
 | 6183 |    put the result into newpos and return the replacement string, which | 
 | 6184 |    has to be freed by the caller */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6185 | static PyObject * | 
 | 6186 | unicode_encode_call_errorhandler(const char *errors, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6187 |                                  PyObject **errorHandler, | 
 | 6188 |                                  const char *encoding, const char *reason, | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6189 |                                  PyObject *unicode, PyObject **exceptionObject, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6190 |                                  Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 6191 |                                  Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6192 | { | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6193 |     static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6194 |     Py_ssize_t len; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6195 |     PyObject *restuple; | 
 | 6196 |     PyObject *resunicode; | 
 | 6197 |  | 
 | 6198 |     if (*errorHandler == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6199 |         *errorHandler = PyCodec_LookupError(errors); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6200 |         if (*errorHandler == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6201 |             return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6202 |     } | 
 | 6203 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6204 |     if (PyUnicode_READY(unicode) == -1) | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6205 |         return NULL; | 
 | 6206 |     len = PyUnicode_GET_LENGTH(unicode); | 
 | 6207 |  | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6208 |     make_encode_exception(exceptionObject, | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6209 |                           encoding, unicode, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6210 |     if (*exceptionObject == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6211 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6212 |  | 
 | 6213 |     restuple = PyObject_CallFunctionObjArgs( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 |         *errorHandler, *exceptionObject, NULL); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6215 |     if (restuple == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6216 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6217 |     if (!PyTuple_Check(restuple)) { | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6218 |         PyErr_SetString(PyExc_TypeError, &argparse[3]); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6219 |         Py_DECREF(restuple); | 
 | 6220 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6221 |     } | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6222 |     if (!PyArg_ParseTuple(restuple, argparse, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6223 |                           &resunicode, newpos)) { | 
 | 6224 |         Py_DECREF(restuple); | 
 | 6225 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6226 |     } | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6227 |     if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { | 
 | 6228 |         PyErr_SetString(PyExc_TypeError, &argparse[3]); | 
 | 6229 |         Py_DECREF(restuple); | 
 | 6230 |         return NULL; | 
 | 6231 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6232 |     if (*newpos<0) | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6233 |         *newpos = len + *newpos; | 
 | 6234 |     if (*newpos<0 || *newpos>len) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6235 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); | 
 | 6236 |         Py_DECREF(restuple); | 
 | 6237 |         return NULL; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6238 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6239 |     Py_INCREF(resunicode); | 
 | 6240 |     Py_DECREF(restuple); | 
 | 6241 |     return resunicode; | 
 | 6242 | } | 
 | 6243 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6244 | static PyObject * | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6245 | unicode_encode_ucs1(PyObject *unicode, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6246 |                     const char *errors, | 
| Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6247 |                     unsigned int limit) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6248 | { | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6249 |     /* input state */ | 
 | 6250 |     Py_ssize_t pos=0, size; | 
 | 6251 |     int kind; | 
 | 6252 |     void *data; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6253 |     /* output object */ | 
 | 6254 |     PyObject *res; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6255 |     /* pointer into the output */ | 
 | 6256 |     char *str; | 
 | 6257 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6258 |     Py_ssize_t ressize; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6259 |     const char *encoding = (limit == 256) ? "latin-1" : "ascii"; | 
 | 6260 |     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] | 6261 |     PyObject *errorHandler = NULL; | 
 | 6262 |     PyObject *exc = NULL; | 
 | 6263 |     /* the following variable is used for caching string comparisons | 
 | 6264 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ | 
 | 6265 |     int known_errorHandler = -1; | 
 | 6266 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6267 |     if (PyUnicode_READY(unicode) == -1) | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6268 |         return NULL; | 
 | 6269 |     size = PyUnicode_GET_LENGTH(unicode); | 
 | 6270 |     kind = PyUnicode_KIND(unicode); | 
 | 6271 |     data = PyUnicode_DATA(unicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6272 |     /* allocate enough for a simple encoding without | 
 | 6273 |        replacements, if we need more, we'll resize */ | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6274 |     if (size == 0) | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6275 |         return PyBytes_FromStringAndSize(NULL, 0); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6276 |     res = PyBytes_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6277 |     if (res == NULL) | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6278 |         return NULL; | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6279 |     str = PyBytes_AS_STRING(res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6280 |     ressize = size; | 
 | 6281 |  | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6282 |     while (pos < size) { | 
 | 6283 |         Py_UCS4 c = PyUnicode_READ(kind, data, pos); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6284 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 |         /* can we encode this? */ | 
 | 6286 |         if (c<limit) { | 
 | 6287 |             /* no overflow check, because we know that the space is enough */ | 
 | 6288 |             *str++ = (char)c; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6289 |             ++pos; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6290 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6291 |         else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6292 |             Py_ssize_t requiredsize; | 
 | 6293 |             PyObject *repunicode; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6294 |             Py_ssize_t repsize, newpos, respos, i; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6295 |             /* startpos for collecting unencodable chars */ | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6296 |             Py_ssize_t collstart = pos; | 
 | 6297 |             Py_ssize_t collend = pos; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6298 |             /* find all unecodable characters */ | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6299 |             while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 |                 ++collend; | 
 | 6301 |             /* cache callback name lookup (if not done yet, i.e. it's the first error) */ | 
 | 6302 |             if (known_errorHandler==-1) { | 
 | 6303 |                 if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 6304 |                     known_errorHandler = 1; | 
 | 6305 |                 else if (!strcmp(errors, "replace")) | 
 | 6306 |                     known_errorHandler = 2; | 
 | 6307 |                 else if (!strcmp(errors, "ignore")) | 
 | 6308 |                     known_errorHandler = 3; | 
 | 6309 |                 else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 6310 |                     known_errorHandler = 4; | 
 | 6311 |                 else | 
 | 6312 |                     known_errorHandler = 0; | 
 | 6313 |             } | 
 | 6314 |             switch (known_errorHandler) { | 
 | 6315 |             case 1: /* strict */ | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6316 |                 raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6317 |                 goto onError; | 
 | 6318 |             case 2: /* replace */ | 
 | 6319 |                 while (collstart++<collend) | 
 | 6320 |                     *str++ = '?'; /* fall through */ | 
 | 6321 |             case 3: /* ignore */ | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6322 |                 pos = collend; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6323 |                 break; | 
 | 6324 |             case 4: /* xmlcharrefreplace */ | 
 | 6325 |                 respos = str - PyBytes_AS_STRING(res); | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6326 |                 /* determine replacement size */ | 
 | 6327 |                 for (i = collstart, repsize = 0; i < collend; ++i) { | 
 | 6328 |                     Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 6329 |                     if (ch < 10) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6330 |                         repsize += 2+1+1; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6331 |                     else if (ch < 100) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6332 |                         repsize += 2+2+1; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6333 |                     else if (ch < 1000) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6334 |                         repsize += 2+3+1; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6335 |                     else if (ch < 10000) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6336 |                         repsize += 2+4+1; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6337 |                     else if (ch < 100000) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6338 |                         repsize += 2+5+1; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6339 |                     else if (ch < 1000000) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6340 |                         repsize += 2+6+1; | 
| Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6341 |                     else { | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6342 |                         assert(ch <= MAX_UNICODE); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6343 |                         repsize += 2+7+1; | 
| Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6344 |                     } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6345 |                 } | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6346 |                 requiredsize = respos+repsize+(size-collend); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6347 |                 if (requiredsize > ressize) { | 
 | 6348 |                     if (requiredsize<2*ressize) | 
 | 6349 |                         requiredsize = 2*ressize; | 
 | 6350 |                     if (_PyBytes_Resize(&res, requiredsize)) | 
 | 6351 |                         goto onError; | 
 | 6352 |                     str = PyBytes_AS_STRING(res) + respos; | 
 | 6353 |                     ressize = requiredsize; | 
 | 6354 |                 } | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6355 |                 /* generate replacement */ | 
 | 6356 |                 for (i = collstart; i < collend; ++i) { | 
 | 6357 |                     str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6358 |                 } | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6359 |                 pos = collend; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6360 |                 break; | 
 | 6361 |             default: | 
 | 6362 |                 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6363 |                                                               encoding, reason, unicode, &exc, | 
 | 6364 |                                                               collstart, collend, &newpos); | 
 | 6365 |                 if (repunicode == NULL || (PyUnicode_Check(repunicode) && | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6366 |                                            PyUnicode_READY(repunicode) == -1)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6367 |                     goto onError; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6368 |                 if (PyBytes_Check(repunicode)) { | 
 | 6369 |                     /* Directly copy bytes result to output. */ | 
 | 6370 |                     repsize = PyBytes_Size(repunicode); | 
 | 6371 |                     if (repsize > 1) { | 
 | 6372 |                         /* Make room for all additional bytes. */ | 
| Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6373 |                         respos = str - PyBytes_AS_STRING(res); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6374 |                         if (_PyBytes_Resize(&res, ressize+repsize-1)) { | 
 | 6375 |                             Py_DECREF(repunicode); | 
 | 6376 |                             goto onError; | 
 | 6377 |                         } | 
| Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6378 |                         str = PyBytes_AS_STRING(res) + respos; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6379 |                         ressize += repsize-1; | 
 | 6380 |                     } | 
 | 6381 |                     memcpy(str, PyBytes_AsString(repunicode), repsize); | 
 | 6382 |                     str += repsize; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6383 |                     pos = newpos; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6384 |                     Py_DECREF(repunicode); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6385 |                     break; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6386 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6387 |                 /* need more space? (at least enough for what we | 
 | 6388 |                    have+the replacement+the rest of the string, so | 
 | 6389 |                    we won't have to check space for encodable characters) */ | 
 | 6390 |                 respos = str - PyBytes_AS_STRING(res); | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6391 |                 repsize = PyUnicode_GET_LENGTH(repunicode); | 
 | 6392 |                 requiredsize = respos+repsize+(size-collend); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6393 |                 if (requiredsize > ressize) { | 
 | 6394 |                     if (requiredsize<2*ressize) | 
 | 6395 |                         requiredsize = 2*ressize; | 
 | 6396 |                     if (_PyBytes_Resize(&res, requiredsize)) { | 
 | 6397 |                         Py_DECREF(repunicode); | 
 | 6398 |                         goto onError; | 
 | 6399 |                     } | 
 | 6400 |                     str = PyBytes_AS_STRING(res) + respos; | 
 | 6401 |                     ressize = requiredsize; | 
 | 6402 |                 } | 
 | 6403 |                 /* check if there is anything unencodable in the replacement | 
 | 6404 |                    and copy it to the output */ | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6405 |                 for (i = 0; repsize-->0; ++i, ++str) { | 
 | 6406 |                     c = PyUnicode_READ_CHAR(repunicode, i); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6407 |                     if (c >= limit) { | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6408 |                         raise_encode_exception(&exc, encoding, unicode, | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6409 |                                                pos, pos+1, reason); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6410 |                         Py_DECREF(repunicode); | 
 | 6411 |                         goto onError; | 
 | 6412 |                     } | 
 | 6413 |                     *str = (char)c; | 
 | 6414 |                 } | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6415 |                 pos = newpos; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6416 |                 Py_DECREF(repunicode); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6417 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6418 |         } | 
 | 6419 |     } | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6420 |     /* Resize if we allocated to much */ | 
 | 6421 |     size = str - PyBytes_AS_STRING(res); | 
 | 6422 |     if (size < ressize) { /* If this falls res will be NULL */ | 
| Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6423 |         assert(size >= 0); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6424 |         if (_PyBytes_Resize(&res, size) < 0) | 
 | 6425 |             goto onError; | 
 | 6426 |     } | 
 | 6427 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6428 |     Py_XDECREF(errorHandler); | 
 | 6429 |     Py_XDECREF(exc); | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6430 |     return res; | 
 | 6431 |  | 
 | 6432 |   onError: | 
 | 6433 |     Py_XDECREF(res); | 
 | 6434 |     Py_XDECREF(errorHandler); | 
 | 6435 |     Py_XDECREF(exc); | 
 | 6436 |     return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6437 | } | 
 | 6438 |  | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6439 | /* Deprecated */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6440 | PyObject * | 
 | 6441 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, | 
| Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6442 |                        Py_ssize_t size, | 
 | 6443 |                        const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | { | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6445 |     PyObject *result; | 
 | 6446 |     PyObject *unicode = PyUnicode_FromUnicode(p, size); | 
 | 6447 |     if (unicode == NULL) | 
 | 6448 |         return NULL; | 
 | 6449 |     result = unicode_encode_ucs1(unicode, errors, 256); | 
 | 6450 |     Py_DECREF(unicode); | 
 | 6451 |     return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | } | 
 | 6453 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6454 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6455 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6456 | { | 
 | 6457 |     if (!PyUnicode_Check(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6458 |         PyErr_BadArgument(); | 
 | 6459 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6461 |     if (PyUnicode_READY(unicode) == -1) | 
 | 6462 |         return NULL; | 
 | 6463 |     /* Fast path: if it is a one-byte string, construct | 
 | 6464 |        bytes object directly. */ | 
 | 6465 |     if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) | 
 | 6466 |         return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), | 
 | 6467 |                                          PyUnicode_GET_LENGTH(unicode)); | 
 | 6468 |     /* Non-Latin-1 characters present. Defer to above function to | 
 | 6469 |        raise the exception. */ | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6470 |     return unicode_encode_ucs1(unicode, errors, 256); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6471 | } | 
 | 6472 |  | 
 | 6473 | PyObject* | 
 | 6474 | PyUnicode_AsLatin1String(PyObject *unicode) | 
 | 6475 | { | 
 | 6476 |     return _PyUnicode_AsLatin1String(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6477 | } | 
 | 6478 |  | 
 | 6479 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ | 
 | 6480 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6481 | PyObject * | 
 | 6482 | PyUnicode_DecodeASCII(const char *s, | 
 | 6483 |                       Py_ssize_t size, | 
 | 6484 |                       const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6485 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6486 |     const char *starts = s; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6487 |     _PyUnicodeWriter writer; | 
| Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6488 |     int kind; | 
 | 6489 |     void *data; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6490 |     Py_ssize_t startinpos; | 
 | 6491 |     Py_ssize_t endinpos; | 
 | 6492 |     Py_ssize_t outpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6493 |     const char *e; | 
 | 6494 |     PyObject *errorHandler = NULL; | 
 | 6495 |     PyObject *exc = NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6496 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6497 |     if (size == 0) | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6498 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6499 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6500 |     /* ASCII is equivalent to the first 128 ordinals in Unicode. */ | 
| Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6501 |     if (size == 1 && (unsigned char)s[0] < 128) | 
 | 6502 |         return get_latin1_char((unsigned char)s[0]); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6503 |  | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6504 |     _PyUnicodeWriter_Init(&writer); | 
| Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6505 |     writer.min_length = size; | 
 | 6506 |     if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6507 |         return NULL; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6508 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6509 |     e = s + size; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6510 |     data = writer.data; | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6511 |     outpos = ascii_decode(s, e, (Py_UCS1 *)data); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6512 |     writer.pos = outpos; | 
 | 6513 |     if (writer.pos == size) | 
 | 6514 |         return _PyUnicodeWriter_Finish(&writer); | 
| Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6515 |  | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6516 |     s += writer.pos; | 
 | 6517 |     kind = writer.kind; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6518 |     while (s < e) { | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6519 |         unsigned char c = (unsigned char)*s; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6520 |         if (c < 128) { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6521 |             PyUnicode_WRITE(kind, data, writer.pos, c); | 
 | 6522 |             writer.pos++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6523 |             ++s; | 
 | 6524 |         } | 
 | 6525 |         else { | 
 | 6526 |             startinpos = s-starts; | 
 | 6527 |             endinpos = startinpos + 1; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6528 |             if (unicode_decode_call_errorhandler_writer( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 |                     errors, &errorHandler, | 
 | 6530 |                     "ascii", "ordinal not in range(128)", | 
 | 6531 |                     &starts, &e, &startinpos, &endinpos, &exc, &s, | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6532 |                     &writer)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6533 |                 goto onError; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6534 |             kind = writer.kind; | 
 | 6535 |             data = writer.data; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6536 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6537 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6538 |     Py_XDECREF(errorHandler); | 
 | 6539 |     Py_XDECREF(exc); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6540 |     return _PyUnicodeWriter_Finish(&writer); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6541 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6542 |   onError: | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6543 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6544 |     Py_XDECREF(errorHandler); | 
 | 6545 |     Py_XDECREF(exc); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6546 |     return NULL; | 
 | 6547 | } | 
 | 6548 |  | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6549 | /* Deprecated */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6550 | PyObject * | 
 | 6551 | PyUnicode_EncodeASCII(const Py_UNICODE *p, | 
 | 6552 |                       Py_ssize_t size, | 
 | 6553 |                       const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6554 | { | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6555 |     PyObject *result; | 
 | 6556 |     PyObject *unicode = PyUnicode_FromUnicode(p, size); | 
 | 6557 |     if (unicode == NULL) | 
 | 6558 |         return NULL; | 
 | 6559 |     result = unicode_encode_ucs1(unicode, errors, 128); | 
 | 6560 |     Py_DECREF(unicode); | 
 | 6561 |     return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6562 | } | 
 | 6563 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6564 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6565 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6566 | { | 
 | 6567 |     if (!PyUnicode_Check(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6568 |         PyErr_BadArgument(); | 
 | 6569 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6570 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6571 |     if (PyUnicode_READY(unicode) == -1) | 
 | 6572 |         return NULL; | 
 | 6573 |     /* Fast path: if it is an ASCII-only string, construct bytes object | 
 | 6574 |        directly. Else defer to above function to raise the exception. */ | 
| Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6575 |     if (PyUnicode_IS_ASCII(unicode)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6576 |         return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), | 
 | 6577 |                                          PyUnicode_GET_LENGTH(unicode)); | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6578 |     return unicode_encode_ucs1(unicode, errors, 128); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6579 | } | 
 | 6580 |  | 
 | 6581 | PyObject * | 
 | 6582 | PyUnicode_AsASCIIString(PyObject *unicode) | 
 | 6583 | { | 
 | 6584 |     return _PyUnicode_AsASCIIString(unicode, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6585 | } | 
 | 6586 |  | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6587 | #ifdef HAVE_MBCS | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6588 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6589 | /* --- MBCS codecs for Windows -------------------------------------------- */ | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6590 |  | 
| Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6591 | #if SIZEOF_INT < SIZEOF_SIZE_T | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6592 | #define NEED_RETRY | 
 | 6593 | #endif | 
 | 6594 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6595 | #ifndef WC_ERR_INVALID_CHARS | 
 | 6596 | #  define WC_ERR_INVALID_CHARS 0x0080 | 
 | 6597 | #endif | 
 | 6598 |  | 
 | 6599 | static char* | 
 | 6600 | code_page_name(UINT code_page, PyObject **obj) | 
 | 6601 | { | 
 | 6602 |     *obj = NULL; | 
 | 6603 |     if (code_page == CP_ACP) | 
 | 6604 |         return "mbcs"; | 
 | 6605 |     if (code_page == CP_UTF7) | 
 | 6606 |         return "CP_UTF7"; | 
 | 6607 |     if (code_page == CP_UTF8) | 
 | 6608 |         return "CP_UTF8"; | 
 | 6609 |  | 
 | 6610 |     *obj = PyBytes_FromFormat("cp%u", code_page); | 
 | 6611 |     if (*obj == NULL) | 
 | 6612 |         return NULL; | 
 | 6613 |     return PyBytes_AS_STRING(*obj); | 
 | 6614 | } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6615 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6616 | static int | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6617 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6618 | { | 
 | 6619 |     const char *curr = s + offset; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6620 |     const char *prev; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6621 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6622 |     if (!IsDBCSLeadByteEx(code_page, *curr)) | 
 | 6623 |         return 0; | 
 | 6624 |  | 
 | 6625 |     prev = CharPrevExA(code_page, s, curr, 0); | 
 | 6626 |     if (prev == curr) | 
 | 6627 |         return 1; | 
 | 6628 |     /* FIXME: This code is limited to "true" double-byte encodings, | 
 | 6629 |        as it assumes an incomplete character consists of a single | 
 | 6630 |        byte. */ | 
 | 6631 |     if (curr - prev == 2) | 
 | 6632 |         return 1; | 
 | 6633 |     if (!IsDBCSLeadByteEx(code_page, *prev)) | 
 | 6634 |         return 1; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6635 |     return 0; | 
 | 6636 | } | 
 | 6637 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6638 | static DWORD | 
 | 6639 | decode_code_page_flags(UINT code_page) | 
 | 6640 | { | 
 | 6641 |     if (code_page == CP_UTF7) { | 
 | 6642 |         /* The CP_UTF7 decoder only supports flags=0 */ | 
 | 6643 |         return 0; | 
 | 6644 |     } | 
 | 6645 |     else | 
 | 6646 |         return MB_ERR_INVALID_CHARS; | 
 | 6647 | } | 
 | 6648 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6649 | /* | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6650 |  * Decode a byte string from a Windows code page into unicode object in strict | 
 | 6651 |  * mode. | 
 | 6652 |  * | 
| Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6653 |  * Returns consumed size if succeed, returns -2 on decode error, or raise an | 
 | 6654 |  * OSError and returns -1 on other error. | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6655 |  */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6656 | static int | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6657 | decode_code_page_strict(UINT code_page, | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6658 |                         PyObject **v, | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6659 |                         const char *in, | 
 | 6660 |                         int insize) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6661 | { | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6662 |     const DWORD flags = decode_code_page_flags(code_page); | 
| Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6663 |     wchar_t *out; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6664 |     DWORD outsize; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6665 |  | 
 | 6666 |     /* First get the size of the result */ | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6667 |     assert(insize > 0); | 
 | 6668 |     outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); | 
 | 6669 |     if (outsize <= 0) | 
 | 6670 |         goto error; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6671 |  | 
 | 6672 |     if (*v == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6673 |         /* Create unicode object */ | 
| Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6674 |         /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6675 |         *v = (PyObject*)_PyUnicode_New(outsize); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6676 |         if (*v == NULL) | 
 | 6677 |             return -1; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6678 |         out = PyUnicode_AS_UNICODE(*v); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6679 |     } | 
 | 6680 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6681 |         /* Extend unicode object */ | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6682 |         Py_ssize_t n = PyUnicode_GET_SIZE(*v); | 
| Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6683 |         if (unicode_resize(v, n + outsize) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6684 |             return -1; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6685 |         out = PyUnicode_AS_UNICODE(*v) + n; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6686 |     } | 
 | 6687 |  | 
 | 6688 |     /* Do the conversion */ | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6689 |     outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); | 
 | 6690 |     if (outsize <= 0) | 
 | 6691 |         goto error; | 
 | 6692 |     return insize; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6693 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6694 | error: | 
 | 6695 |     if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) | 
 | 6696 |         return -2; | 
 | 6697 |     PyErr_SetFromWindowsErr(0); | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6698 |     return -1; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6699 | } | 
 | 6700 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6701 | /* | 
 | 6702 |  * Decode a byte string from a code page into unicode object with an error | 
 | 6703 |  * handler. | 
 | 6704 |  * | 
| Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6705 |  * Returns consumed size if succeed, or raise an OSError or | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6706 |  * UnicodeDecodeError exception and returns -1 on error. | 
 | 6707 |  */ | 
 | 6708 | static int | 
 | 6709 | decode_code_page_errors(UINT code_page, | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6710 |                         PyObject **v, | 
 | 6711 |                         const char *in, const int size, | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6712 |                         const char *errors) | 
 | 6713 | { | 
 | 6714 |     const char *startin = in; | 
 | 6715 |     const char *endin = in + size; | 
 | 6716 |     const DWORD flags = decode_code_page_flags(code_page); | 
 | 6717 |     /* Ideally, we should get reason from FormatMessage. This is the Windows | 
 | 6718 |        2000 English version of the message. */ | 
 | 6719 |     const char *reason = "No mapping for the Unicode character exists " | 
 | 6720 |                          "in the target code page."; | 
 | 6721 |     /* each step cannot decode more than 1 character, but a character can be | 
 | 6722 |        represented as a surrogate pair */ | 
 | 6723 |     wchar_t buffer[2], *startout, *out; | 
| Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6724 |     int insize; | 
 | 6725 |     Py_ssize_t outsize; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6726 |     PyObject *errorHandler = NULL; | 
 | 6727 |     PyObject *exc = NULL; | 
 | 6728 |     PyObject *encoding_obj = NULL; | 
 | 6729 |     char *encoding; | 
 | 6730 |     DWORD err; | 
 | 6731 |     int ret = -1; | 
 | 6732 |  | 
 | 6733 |     assert(size > 0); | 
 | 6734 |  | 
 | 6735 |     encoding = code_page_name(code_page, &encoding_obj); | 
 | 6736 |     if (encoding == NULL) | 
 | 6737 |         return -1; | 
 | 6738 |  | 
 | 6739 |     if (errors == NULL || strcmp(errors, "strict") == 0) { | 
 | 6740 |         /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a | 
 | 6741 |            UnicodeDecodeError. */ | 
 | 6742 |         make_decode_exception(&exc, encoding, in, size, 0, 0, reason); | 
 | 6743 |         if (exc != NULL) { | 
 | 6744 |             PyCodec_StrictErrors(exc); | 
 | 6745 |             Py_CLEAR(exc); | 
 | 6746 |         } | 
 | 6747 |         goto error; | 
 | 6748 |     } | 
 | 6749 |  | 
 | 6750 |     if (*v == NULL) { | 
 | 6751 |         /* Create unicode object */ | 
 | 6752 |         if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { | 
 | 6753 |             PyErr_NoMemory(); | 
 | 6754 |             goto error; | 
 | 6755 |         } | 
| Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6756 |         /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6757 |         *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6758 |         if (*v == NULL) | 
 | 6759 |             goto error; | 
 | 6760 |         startout = PyUnicode_AS_UNICODE(*v); | 
 | 6761 |     } | 
 | 6762 |     else { | 
 | 6763 |         /* Extend unicode object */ | 
 | 6764 |         Py_ssize_t n = PyUnicode_GET_SIZE(*v); | 
 | 6765 |         if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { | 
 | 6766 |             PyErr_NoMemory(); | 
 | 6767 |             goto error; | 
 | 6768 |         } | 
| Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6769 |         if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6770 |             goto error; | 
 | 6771 |         startout = PyUnicode_AS_UNICODE(*v) + n; | 
 | 6772 |     } | 
 | 6773 |  | 
 | 6774 |     /* Decode the byte string character per character */ | 
 | 6775 |     out = startout; | 
 | 6776 |     while (in < endin) | 
 | 6777 |     { | 
 | 6778 |         /* Decode a character */ | 
 | 6779 |         insize = 1; | 
 | 6780 |         do | 
 | 6781 |         { | 
 | 6782 |             outsize = MultiByteToWideChar(code_page, flags, | 
 | 6783 |                                           in, insize, | 
 | 6784 |                                           buffer, Py_ARRAY_LENGTH(buffer)); | 
 | 6785 |             if (outsize > 0) | 
 | 6786 |                 break; | 
 | 6787 |             err = GetLastError(); | 
 | 6788 |             if (err != ERROR_NO_UNICODE_TRANSLATION | 
 | 6789 |                 && err != ERROR_INSUFFICIENT_BUFFER) | 
 | 6790 |             { | 
 | 6791 |                 PyErr_SetFromWindowsErr(0); | 
 | 6792 |                 goto error; | 
 | 6793 |             } | 
 | 6794 |             insize++; | 
 | 6795 |         } | 
 | 6796 |         /* 4=maximum length of a UTF-8 sequence */ | 
 | 6797 |         while (insize <= 4 && (in + insize) <= endin); | 
 | 6798 |  | 
 | 6799 |         if (outsize <= 0) { | 
 | 6800 |             Py_ssize_t startinpos, endinpos, outpos; | 
 | 6801 |  | 
 | 6802 |             startinpos = in - startin; | 
 | 6803 |             endinpos = startinpos + 1; | 
 | 6804 |             outpos = out - PyUnicode_AS_UNICODE(*v); | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6805 |             if (unicode_decode_call_errorhandler_wchar( | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6806 |                     errors, &errorHandler, | 
 | 6807 |                     encoding, reason, | 
 | 6808 |                     &startin, &endin, &startinpos, &endinpos, &exc, &in, | 
| Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6809 |                     v, &outpos)) | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6810 |             { | 
 | 6811 |                 goto error; | 
 | 6812 |             } | 
| Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6813 |             out = PyUnicode_AS_UNICODE(*v) + outpos; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6814 |         } | 
 | 6815 |         else { | 
 | 6816 |             in += insize; | 
 | 6817 |             memcpy(out, buffer, outsize * sizeof(wchar_t)); | 
 | 6818 |             out += outsize; | 
 | 6819 |         } | 
 | 6820 |     } | 
 | 6821 |  | 
 | 6822 |     /* write a NUL character at the end */ | 
 | 6823 |     *out = 0; | 
 | 6824 |  | 
 | 6825 |     /* Extend unicode object */ | 
 | 6826 |     outsize = out - startout; | 
 | 6827 |     assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); | 
| Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6828 |     if (unicode_resize(v, outsize) < 0) | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6829 |         goto error; | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6830 |     ret = size; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6831 |  | 
 | 6832 | error: | 
 | 6833 |     Py_XDECREF(encoding_obj); | 
 | 6834 |     Py_XDECREF(errorHandler); | 
 | 6835 |     Py_XDECREF(exc); | 
 | 6836 |     return ret; | 
 | 6837 | } | 
 | 6838 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6839 | static PyObject * | 
 | 6840 | decode_code_page_stateful(int code_page, | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6841 |                           const char *s, Py_ssize_t size, | 
 | 6842 |                           const char *errors, Py_ssize_t *consumed) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6843 | { | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6844 |     PyObject *v = NULL; | 
 | 6845 |     int chunk_size, final, converted, done; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6846 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6847 |     if (code_page < 0) { | 
 | 6848 |         PyErr_SetString(PyExc_ValueError, "invalid code page number"); | 
 | 6849 |         return NULL; | 
 | 6850 |     } | 
 | 6851 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6852 |     if (consumed) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6853 |         *consumed = 0; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6854 |  | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6855 |     do | 
 | 6856 |     { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6857 | #ifdef NEED_RETRY | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6858 |         if (size > INT_MAX) { | 
 | 6859 |             chunk_size = INT_MAX; | 
 | 6860 |             final = 0; | 
 | 6861 |             done = 0; | 
 | 6862 |         } | 
 | 6863 |         else | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6864 | #endif | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6865 |         { | 
 | 6866 |             chunk_size = (int)size; | 
 | 6867 |             final = (consumed == NULL); | 
 | 6868 |             done = 1; | 
 | 6869 |         } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6870 |  | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6871 |         /* Skip trailing lead-byte unless 'final' is set */ | 
 | 6872 |         if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) | 
 | 6873 |             --chunk_size; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6874 |  | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6875 |         if (chunk_size == 0 && done) { | 
 | 6876 |             if (v != NULL) | 
 | 6877 |                 break; | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6878 |             _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6879 |         } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6880 |  | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6881 |  | 
 | 6882 |         converted = decode_code_page_strict(code_page, &v, | 
 | 6883 |                                             s, chunk_size); | 
 | 6884 |         if (converted == -2) | 
 | 6885 |             converted = decode_code_page_errors(code_page, &v, | 
 | 6886 |                                                 s, chunk_size, | 
 | 6887 |                                                 errors); | 
 | 6888 |         assert(converted != 0); | 
 | 6889 |  | 
 | 6890 |         if (converted < 0) { | 
 | 6891 |             Py_XDECREF(v); | 
 | 6892 |             return NULL; | 
 | 6893 |         } | 
 | 6894 |  | 
 | 6895 |         if (consumed) | 
 | 6896 |             *consumed += converted; | 
 | 6897 |  | 
 | 6898 |         s += converted; | 
 | 6899 |         size -= converted; | 
 | 6900 |     } while (!done); | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6901 |  | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6902 |     return unicode_result(v); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6903 | } | 
 | 6904 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6905 | PyObject * | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6906 | PyUnicode_DecodeCodePageStateful(int code_page, | 
 | 6907 |                                  const char *s, | 
 | 6908 |                                  Py_ssize_t size, | 
 | 6909 |                                  const char *errors, | 
 | 6910 |                                  Py_ssize_t *consumed) | 
 | 6911 | { | 
 | 6912 |     return decode_code_page_stateful(code_page, s, size, errors, consumed); | 
 | 6913 | } | 
 | 6914 |  | 
 | 6915 | PyObject * | 
 | 6916 | PyUnicode_DecodeMBCSStateful(const char *s, | 
 | 6917 |                              Py_ssize_t size, | 
 | 6918 |                              const char *errors, | 
 | 6919 |                              Py_ssize_t *consumed) | 
 | 6920 | { | 
 | 6921 |     return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); | 
 | 6922 | } | 
 | 6923 |  | 
 | 6924 | PyObject * | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6925 | PyUnicode_DecodeMBCS(const char *s, | 
 | 6926 |                      Py_ssize_t size, | 
 | 6927 |                      const char *errors) | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6928 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6929 |     return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); | 
 | 6930 | } | 
 | 6931 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6932 | static DWORD | 
 | 6933 | encode_code_page_flags(UINT code_page, const char *errors) | 
 | 6934 | { | 
 | 6935 |     if (code_page == CP_UTF8) { | 
 | 6936 |         if (winver.dwMajorVersion >= 6) | 
 | 6937 |             /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista | 
 | 6938 |                and later */ | 
 | 6939 |             return WC_ERR_INVALID_CHARS; | 
 | 6940 |         else | 
 | 6941 |             /* CP_UTF8 only supports flags=0 on Windows older than Vista */ | 
 | 6942 |             return 0; | 
 | 6943 |     } | 
 | 6944 |     else if (code_page == CP_UTF7) { | 
 | 6945 |         /* CP_UTF7 only supports flags=0 */ | 
 | 6946 |         return 0; | 
 | 6947 |     } | 
 | 6948 |     else { | 
 | 6949 |         if (errors != NULL && strcmp(errors, "replace") == 0) | 
 | 6950 |             return 0; | 
 | 6951 |         else | 
 | 6952 |             return WC_NO_BEST_FIT_CHARS; | 
 | 6953 |     } | 
 | 6954 | } | 
 | 6955 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6956 | /* | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6957 |  * Encode a Unicode string to a Windows code page into a byte string in strict | 
 | 6958 |  * mode. | 
 | 6959 |  * | 
 | 6960 |  * Returns consumed characters if succeed, returns -2 on encode error, or raise | 
| Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6961 |  * an OSError and returns -1 on other error. | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6962 |  */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6963 | static int | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6964 | encode_code_page_strict(UINT code_page, PyObject **outbytes, | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6965 |                         PyObject *unicode, Py_ssize_t offset, int len, | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6966 |                         const char* errors) | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6967 | { | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6968 |     BOOL usedDefaultChar = FALSE; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6969 |     BOOL *pusedDefaultChar = &usedDefaultChar; | 
 | 6970 |     int outsize; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6971 |     PyObject *exc = NULL; | 
| Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6972 |     wchar_t *p; | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6973 |     Py_ssize_t size; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6974 |     const DWORD flags = encode_code_page_flags(code_page, NULL); | 
 | 6975 |     char *out; | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6976 |     /* Create a substring so that we can get the UTF-16 representation | 
 | 6977 |        of just the slice under consideration. */ | 
 | 6978 |     PyObject *substring; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6979 |  | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6980 |     assert(len > 0); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6981 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6982 |     if (code_page != CP_UTF8 && code_page != CP_UTF7) | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6983 |         pusedDefaultChar = &usedDefaultChar; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6984 |     else | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6985 |         pusedDefaultChar = NULL; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6986 |  | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6987 |     substring = PyUnicode_Substring(unicode, offset, offset+len); | 
 | 6988 |     if (substring == NULL) | 
 | 6989 |         return -1; | 
 | 6990 |     p = PyUnicode_AsUnicodeAndSize(substring, &size); | 
 | 6991 |     if (p == NULL) { | 
 | 6992 |         Py_DECREF(substring); | 
 | 6993 |         return -1; | 
 | 6994 |     } | 
| Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6995 |     assert(size <= INT_MAX); | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6996 |  | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6997 |     /* First get the size of the result */ | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6998 |     outsize = WideCharToMultiByte(code_page, flags, | 
| Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6999 |                                   p, (int)size, | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7000 |                                   NULL, 0, | 
 | 7001 |                                   NULL, pusedDefaultChar); | 
 | 7002 |     if (outsize <= 0) | 
 | 7003 |         goto error; | 
 | 7004 |     /* If we used a default char, then we failed! */ | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7005 |     if (pusedDefaultChar && *pusedDefaultChar) { | 
 | 7006 |         Py_DECREF(substring); | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7007 |         return -2; | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7008 |     } | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7009 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7010 |     if (*outbytes == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7011 |         /* Create string object */ | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7012 |         *outbytes = PyBytes_FromStringAndSize(NULL, outsize); | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7013 |         if (*outbytes == NULL) { | 
 | 7014 |             Py_DECREF(substring); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7015 |             return -1; | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7016 |         } | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7017 |         out = PyBytes_AS_STRING(*outbytes); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7018 |     } | 
 | 7019 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7020 |         /* Extend string object */ | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7021 |         const Py_ssize_t n = PyBytes_Size(*outbytes); | 
 | 7022 |         if (outsize > PY_SSIZE_T_MAX - n) { | 
 | 7023 |             PyErr_NoMemory(); | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7024 |             Py_DECREF(substring); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7025 |             return -1; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7026 |         } | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7027 |         if (_PyBytes_Resize(outbytes, n + outsize) < 0) { | 
 | 7028 |             Py_DECREF(substring); | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7029 |             return -1; | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7030 |         } | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7031 |         out = PyBytes_AS_STRING(*outbytes) + n; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7032 |     } | 
 | 7033 |  | 
 | 7034 |     /* Do the conversion */ | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7035 |     outsize = WideCharToMultiByte(code_page, flags, | 
| Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7036 |                                   p, (int)size, | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7037 |                                   out, outsize, | 
 | 7038 |                                   NULL, pusedDefaultChar); | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7039 |     Py_CLEAR(substring); | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7040 |     if (outsize <= 0) | 
 | 7041 |         goto error; | 
 | 7042 |     if (pusedDefaultChar && *pusedDefaultChar) | 
 | 7043 |         return -2; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7044 |     return 0; | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7045 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7046 | error: | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7047 |     Py_XDECREF(substring); | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7048 |     if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) | 
 | 7049 |         return -2; | 
 | 7050 |     PyErr_SetFromWindowsErr(0); | 
| Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7051 |     return -1; | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7052 | } | 
 | 7053 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7054 | /* | 
 | 7055 |  * Encode a Unicode string to a Windows code page into a byte string using a | 
 | 7056 |  * error handler. | 
 | 7057 |  * | 
| Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7058 |  * Returns consumed characters if succeed, or raise an OSError and returns | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7059 |  * -1 on other error. | 
 | 7060 |  */ | 
 | 7061 | static int | 
 | 7062 | encode_code_page_errors(UINT code_page, PyObject **outbytes, | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7063 |                         PyObject *unicode, Py_ssize_t unicode_offset, | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7064 |                         Py_ssize_t insize, const char* errors) | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7065 | { | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7066 |     const DWORD flags = encode_code_page_flags(code_page, errors); | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7067 |     Py_ssize_t pos = unicode_offset; | 
 | 7068 |     Py_ssize_t endin = unicode_offset + insize; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7069 |     /* Ideally, we should get reason from FormatMessage. This is the Windows | 
 | 7070 |        2000 English version of the message. */ | 
 | 7071 |     const char *reason = "invalid character"; | 
 | 7072 |     /* 4=maximum length of a UTF-8 sequence */ | 
 | 7073 |     char buffer[4]; | 
 | 7074 |     BOOL usedDefaultChar = FALSE, *pusedDefaultChar; | 
 | 7075 |     Py_ssize_t outsize; | 
 | 7076 |     char *out; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7077 |     PyObject *errorHandler = NULL; | 
 | 7078 |     PyObject *exc = NULL; | 
 | 7079 |     PyObject *encoding_obj = NULL; | 
 | 7080 |     char *encoding; | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7081 |     Py_ssize_t newpos, newoutsize; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7082 |     PyObject *rep; | 
 | 7083 |     int ret = -1; | 
 | 7084 |  | 
 | 7085 |     assert(insize > 0); | 
 | 7086 |  | 
 | 7087 |     encoding = code_page_name(code_page, &encoding_obj); | 
 | 7088 |     if (encoding == NULL) | 
 | 7089 |         return -1; | 
 | 7090 |  | 
 | 7091 |     if (errors == NULL || strcmp(errors, "strict") == 0) { | 
 | 7092 |         /* The last error was ERROR_NO_UNICODE_TRANSLATION, | 
 | 7093 |            then we raise a UnicodeEncodeError. */ | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7094 |         make_encode_exception(&exc, encoding, unicode, 0, 0, reason); | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7095 |         if (exc != NULL) { | 
 | 7096 |             PyCodec_StrictErrors(exc); | 
 | 7097 |             Py_DECREF(exc); | 
 | 7098 |         } | 
 | 7099 |         Py_XDECREF(encoding_obj); | 
 | 7100 |         return -1; | 
 | 7101 |     } | 
 | 7102 |  | 
 | 7103 |     if (code_page != CP_UTF8 && code_page != CP_UTF7) | 
 | 7104 |         pusedDefaultChar = &usedDefaultChar; | 
 | 7105 |     else | 
 | 7106 |         pusedDefaultChar = NULL; | 
 | 7107 |  | 
 | 7108 |     if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { | 
 | 7109 |         PyErr_NoMemory(); | 
 | 7110 |         goto error; | 
 | 7111 |     } | 
 | 7112 |     outsize = insize * Py_ARRAY_LENGTH(buffer); | 
 | 7113 |  | 
 | 7114 |     if (*outbytes == NULL) { | 
 | 7115 |         /* Create string object */ | 
 | 7116 |         *outbytes = PyBytes_FromStringAndSize(NULL, outsize); | 
 | 7117 |         if (*outbytes == NULL) | 
 | 7118 |             goto error; | 
 | 7119 |         out = PyBytes_AS_STRING(*outbytes); | 
 | 7120 |     } | 
 | 7121 |     else { | 
 | 7122 |         /* Extend string object */ | 
 | 7123 |         Py_ssize_t n = PyBytes_Size(*outbytes); | 
 | 7124 |         if (n > PY_SSIZE_T_MAX - outsize) { | 
 | 7125 |             PyErr_NoMemory(); | 
 | 7126 |             goto error; | 
 | 7127 |         } | 
 | 7128 |         if (_PyBytes_Resize(outbytes, n + outsize) < 0) | 
 | 7129 |             goto error; | 
 | 7130 |         out = PyBytes_AS_STRING(*outbytes) + n; | 
 | 7131 |     } | 
 | 7132 |  | 
 | 7133 |     /* Encode the string character per character */ | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7134 |     while (pos < endin) | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7135 |     { | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7136 |         Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); | 
 | 7137 |         wchar_t chars[2]; | 
 | 7138 |         int charsize; | 
 | 7139 |         if (ch < 0x10000) { | 
 | 7140 |             chars[0] = (wchar_t)ch; | 
 | 7141 |             charsize = 1; | 
 | 7142 |         } | 
 | 7143 |         else { | 
| Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7144 |             chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); | 
 | 7145 |             chars[1] = Py_UNICODE_LOW_SURROGATE(ch); | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7146 |             charsize = 2; | 
 | 7147 |         } | 
 | 7148 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7149 |         outsize = WideCharToMultiByte(code_page, flags, | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7150 |                                       chars, charsize, | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7151 |                                       buffer, Py_ARRAY_LENGTH(buffer), | 
 | 7152 |                                       NULL, pusedDefaultChar); | 
 | 7153 |         if (outsize > 0) { | 
 | 7154 |             if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) | 
 | 7155 |             { | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7156 |                 pos++; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7157 |                 memcpy(out, buffer, outsize); | 
 | 7158 |                 out += outsize; | 
 | 7159 |                 continue; | 
 | 7160 |             } | 
 | 7161 |         } | 
 | 7162 |         else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { | 
 | 7163 |             PyErr_SetFromWindowsErr(0); | 
 | 7164 |             goto error; | 
 | 7165 |         } | 
 | 7166 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7167 |         rep = unicode_encode_call_errorhandler( | 
 | 7168 |                   errors, &errorHandler, encoding, reason, | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7169 |                   unicode, &exc, | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7170 |                   pos, pos + 1, &newpos); | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7171 |         if (rep == NULL) | 
 | 7172 |             goto error; | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7173 |         pos = newpos; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7174 |  | 
 | 7175 |         if (PyBytes_Check(rep)) { | 
 | 7176 |             outsize = PyBytes_GET_SIZE(rep); | 
 | 7177 |             if (outsize != 1) { | 
 | 7178 |                 Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); | 
 | 7179 |                 newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); | 
 | 7180 |                 if (_PyBytes_Resize(outbytes, newoutsize) < 0) { | 
 | 7181 |                     Py_DECREF(rep); | 
 | 7182 |                     goto error; | 
 | 7183 |                 } | 
 | 7184 |                 out = PyBytes_AS_STRING(*outbytes) + offset; | 
 | 7185 |             } | 
 | 7186 |             memcpy(out, PyBytes_AS_STRING(rep), outsize); | 
 | 7187 |             out += outsize; | 
 | 7188 |         } | 
 | 7189 |         else { | 
 | 7190 |             Py_ssize_t i; | 
 | 7191 |             enum PyUnicode_Kind kind; | 
 | 7192 |             void *data; | 
 | 7193 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7194 |             if (PyUnicode_READY(rep) == -1) { | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7195 |                 Py_DECREF(rep); | 
 | 7196 |                 goto error; | 
 | 7197 |             } | 
 | 7198 |  | 
 | 7199 |             outsize = PyUnicode_GET_LENGTH(rep); | 
 | 7200 |             if (outsize != 1) { | 
 | 7201 |                 Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); | 
 | 7202 |                 newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); | 
 | 7203 |                 if (_PyBytes_Resize(outbytes, newoutsize) < 0) { | 
 | 7204 |                     Py_DECREF(rep); | 
 | 7205 |                     goto error; | 
 | 7206 |                 } | 
 | 7207 |                 out = PyBytes_AS_STRING(*outbytes) + offset; | 
 | 7208 |             } | 
 | 7209 |             kind = PyUnicode_KIND(rep); | 
 | 7210 |             data = PyUnicode_DATA(rep); | 
 | 7211 |             for (i=0; i < outsize; i++) { | 
 | 7212 |                 Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 7213 |                 if (ch > 127) { | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7214 |                     raise_encode_exception(&exc, | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7215 |                         encoding, unicode, | 
 | 7216 |                         pos, pos + 1, | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7217 |                         "unable to encode error handler result to ASCII"); | 
 | 7218 |                     Py_DECREF(rep); | 
 | 7219 |                     goto error; | 
 | 7220 |                 } | 
 | 7221 |                 *out = (unsigned char)ch; | 
 | 7222 |                 out++; | 
 | 7223 |             } | 
 | 7224 |         } | 
 | 7225 |         Py_DECREF(rep); | 
 | 7226 |     } | 
 | 7227 |     /* write a NUL byte */ | 
 | 7228 |     *out = 0; | 
 | 7229 |     outsize = out - PyBytes_AS_STRING(*outbytes); | 
 | 7230 |     assert(outsize <= PyBytes_GET_SIZE(*outbytes)); | 
 | 7231 |     if (_PyBytes_Resize(outbytes, outsize) < 0) | 
 | 7232 |         goto error; | 
 | 7233 |     ret = 0; | 
 | 7234 |  | 
 | 7235 | error: | 
 | 7236 |     Py_XDECREF(encoding_obj); | 
 | 7237 |     Py_XDECREF(errorHandler); | 
 | 7238 |     Py_XDECREF(exc); | 
 | 7239 |     return ret; | 
 | 7240 | } | 
 | 7241 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7242 | static PyObject * | 
 | 7243 | encode_code_page(int code_page, | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7244 |                  PyObject *unicode, | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7245 |                  const char *errors) | 
 | 7246 | { | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7247 |     Py_ssize_t len; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7248 |     PyObject *outbytes = NULL; | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7249 |     Py_ssize_t offset; | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7250 |     int chunk_len, ret, done; | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7251 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7252 |     if (PyUnicode_READY(unicode) == -1) | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7253 |         return NULL; | 
 | 7254 |     len = PyUnicode_GET_LENGTH(unicode); | 
| Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7255 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7256 |     if (code_page < 0) { | 
 | 7257 |         PyErr_SetString(PyExc_ValueError, "invalid code page number"); | 
 | 7258 |         return NULL; | 
 | 7259 |     } | 
 | 7260 |  | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7261 |     if (len == 0) | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7262 |         return PyBytes_FromStringAndSize(NULL, 0); | 
 | 7263 |  | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7264 |     offset = 0; | 
 | 7265 |     do | 
 | 7266 |     { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7267 | #ifdef NEED_RETRY | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7268 |         /* UTF-16 encoding may double the size, so use only INT_MAX/2 | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7269 |            chunks. */ | 
 | 7270 |         if (len > INT_MAX/2) { | 
 | 7271 |             chunk_len = INT_MAX/2; | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7272 |             done = 0; | 
 | 7273 |         } | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7274 |         else | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7275 | #endif | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7276 |         { | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7277 |             chunk_len = (int)len; | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7278 |             done = 1; | 
 | 7279 |         } | 
| Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7280 |  | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7281 |         ret = encode_code_page_strict(code_page, &outbytes, | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7282 |                                       unicode, offset, chunk_len, | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7283 |                                       errors); | 
 | 7284 |         if (ret == -2) | 
 | 7285 |             ret = encode_code_page_errors(code_page, &outbytes, | 
 | 7286 |                                           unicode, offset, | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7287 |                                           chunk_len, errors); | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7288 |         if (ret < 0) { | 
 | 7289 |             Py_XDECREF(outbytes); | 
 | 7290 |             return NULL; | 
 | 7291 |         } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7292 |  | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7293 |         offset += chunk_len; | 
| Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7294 |         len -= chunk_len; | 
| Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7295 |     } while (!done); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7296 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7297 |     return outbytes; | 
 | 7298 | } | 
 | 7299 |  | 
 | 7300 | PyObject * | 
 | 7301 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, | 
 | 7302 |                      Py_ssize_t size, | 
 | 7303 |                      const char *errors) | 
 | 7304 | { | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7305 |     PyObject *unicode, *res; | 
 | 7306 |     unicode = PyUnicode_FromUnicode(p, size); | 
 | 7307 |     if (unicode == NULL) | 
 | 7308 |         return NULL; | 
 | 7309 |     res = encode_code_page(CP_ACP, unicode, errors); | 
 | 7310 |     Py_DECREF(unicode); | 
 | 7311 |     return res; | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7312 | } | 
 | 7313 |  | 
 | 7314 | PyObject * | 
 | 7315 | PyUnicode_EncodeCodePage(int code_page, | 
 | 7316 |                          PyObject *unicode, | 
 | 7317 |                          const char *errors) | 
 | 7318 | { | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7319 |     return encode_code_page(code_page, unicode, errors); | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7320 | } | 
| Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7321 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7322 | PyObject * | 
 | 7323 | PyUnicode_AsMBCSString(PyObject *unicode) | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7324 | { | 
 | 7325 |     if (!PyUnicode_Check(unicode)) { | 
 | 7326 |         PyErr_BadArgument(); | 
 | 7327 |         return NULL; | 
 | 7328 |     } | 
| Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7329 |     return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); | 
| Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7330 | } | 
 | 7331 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7332 | #undef NEED_RETRY | 
 | 7333 |  | 
| Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7334 | #endif /* HAVE_MBCS */ | 
| Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7335 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7336 | /* --- Character Mapping Codec -------------------------------------------- */ | 
 | 7337 |  | 
| Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7338 | static int | 
 | 7339 | charmap_decode_string(const char *s, | 
 | 7340 |                       Py_ssize_t size, | 
 | 7341 |                       PyObject *mapping, | 
 | 7342 |                       const char *errors, | 
 | 7343 |                       _PyUnicodeWriter *writer) | 
 | 7344 | { | 
 | 7345 |     const char *starts = s; | 
 | 7346 |     const char *e; | 
 | 7347 |     Py_ssize_t startinpos, endinpos; | 
 | 7348 |     PyObject *errorHandler = NULL, *exc = NULL; | 
 | 7349 |     Py_ssize_t maplen; | 
 | 7350 |     enum PyUnicode_Kind mapkind; | 
 | 7351 |     void *mapdata; | 
 | 7352 |     Py_UCS4 x; | 
 | 7353 |     unsigned char ch; | 
 | 7354 |  | 
 | 7355 |     if (PyUnicode_READY(mapping) == -1) | 
 | 7356 |         return -1; | 
 | 7357 |  | 
 | 7358 |     maplen = PyUnicode_GET_LENGTH(mapping); | 
 | 7359 |     mapdata = PyUnicode_DATA(mapping); | 
 | 7360 |     mapkind = PyUnicode_KIND(mapping); | 
 | 7361 |  | 
 | 7362 |     e = s + size; | 
 | 7363 |  | 
 | 7364 |     if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { | 
 | 7365 |         /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 | 
 | 7366 |          * is disabled in encoding aliases, latin1 is preferred because | 
 | 7367 |          * its implementation is faster. */ | 
 | 7368 |         Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; | 
 | 7369 |         Py_UCS1 *outdata = (Py_UCS1 *)writer->data; | 
 | 7370 |         Py_UCS4 maxchar = writer->maxchar; | 
 | 7371 |  | 
 | 7372 |         assert (writer->kind == PyUnicode_1BYTE_KIND); | 
 | 7373 |         while (s < e) { | 
 | 7374 |             ch = *s; | 
 | 7375 |             x = mapdata_ucs1[ch]; | 
 | 7376 |             if (x > maxchar) { | 
 | 7377 |                 if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) | 
 | 7378 |                     goto onError; | 
 | 7379 |                 maxchar = writer->maxchar; | 
 | 7380 |                 outdata = (Py_UCS1 *)writer->data; | 
 | 7381 |             } | 
 | 7382 |             outdata[writer->pos] = x; | 
 | 7383 |             writer->pos++; | 
 | 7384 |             ++s; | 
 | 7385 |         } | 
 | 7386 |         return 0; | 
 | 7387 |     } | 
 | 7388 |  | 
 | 7389 |     while (s < e) { | 
 | 7390 |         if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { | 
 | 7391 |             enum PyUnicode_Kind outkind = writer->kind; | 
 | 7392 |             Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; | 
 | 7393 |             if (outkind == PyUnicode_1BYTE_KIND) { | 
 | 7394 |                 Py_UCS1 *outdata = (Py_UCS1 *)writer->data; | 
 | 7395 |                 Py_UCS4 maxchar = writer->maxchar; | 
 | 7396 |                 while (s < e) { | 
 | 7397 |                     ch = *s; | 
 | 7398 |                     x = mapdata_ucs2[ch]; | 
 | 7399 |                     if (x > maxchar) | 
 | 7400 |                         goto Error; | 
 | 7401 |                     outdata[writer->pos] = x; | 
 | 7402 |                     writer->pos++; | 
 | 7403 |                     ++s; | 
 | 7404 |                 } | 
 | 7405 |                 break; | 
 | 7406 |             } | 
 | 7407 |             else if (outkind == PyUnicode_2BYTE_KIND) { | 
 | 7408 |                 Py_UCS2 *outdata = (Py_UCS2 *)writer->data; | 
 | 7409 |                 while (s < e) { | 
 | 7410 |                     ch = *s; | 
 | 7411 |                     x = mapdata_ucs2[ch]; | 
 | 7412 |                     if (x == 0xFFFE) | 
 | 7413 |                         goto Error; | 
 | 7414 |                     outdata[writer->pos] = x; | 
 | 7415 |                     writer->pos++; | 
 | 7416 |                     ++s; | 
 | 7417 |                 } | 
 | 7418 |                 break; | 
 | 7419 |             } | 
 | 7420 |         } | 
 | 7421 |         ch = *s; | 
 | 7422 |  | 
 | 7423 |         if (ch < maplen) | 
 | 7424 |             x = PyUnicode_READ(mapkind, mapdata, ch); | 
 | 7425 |         else | 
 | 7426 |             x = 0xfffe; /* invalid value */ | 
 | 7427 | Error: | 
 | 7428 |         if (x == 0xfffe) | 
 | 7429 |         { | 
 | 7430 |             /* undefined mapping */ | 
 | 7431 |             startinpos = s-starts; | 
 | 7432 |             endinpos = startinpos+1; | 
 | 7433 |             if (unicode_decode_call_errorhandler_writer( | 
 | 7434 |                     errors, &errorHandler, | 
 | 7435 |                     "charmap", "character maps to <undefined>", | 
 | 7436 |                     &starts, &e, &startinpos, &endinpos, &exc, &s, | 
 | 7437 |                     writer)) { | 
 | 7438 |                 goto onError; | 
 | 7439 |             } | 
 | 7440 |             continue; | 
 | 7441 |         } | 
 | 7442 |  | 
 | 7443 |         if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) | 
 | 7444 |             goto onError; | 
 | 7445 |         ++s; | 
 | 7446 |     } | 
 | 7447 |     Py_XDECREF(errorHandler); | 
 | 7448 |     Py_XDECREF(exc); | 
 | 7449 |     return 0; | 
 | 7450 |  | 
 | 7451 | onError: | 
 | 7452 |     Py_XDECREF(errorHandler); | 
 | 7453 |     Py_XDECREF(exc); | 
 | 7454 |     return -1; | 
 | 7455 | } | 
 | 7456 |  | 
 | 7457 | static int | 
 | 7458 | charmap_decode_mapping(const char *s, | 
 | 7459 |                        Py_ssize_t size, | 
 | 7460 |                        PyObject *mapping, | 
 | 7461 |                        const char *errors, | 
 | 7462 |                        _PyUnicodeWriter *writer) | 
 | 7463 | { | 
 | 7464 |     const char *starts = s; | 
 | 7465 |     const char *e; | 
 | 7466 |     Py_ssize_t startinpos, endinpos; | 
 | 7467 |     PyObject *errorHandler = NULL, *exc = NULL; | 
 | 7468 |     unsigned char ch; | 
| Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7469 |     PyObject *key, *item = NULL; | 
| Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7470 |  | 
 | 7471 |     e = s + size; | 
 | 7472 |  | 
 | 7473 |     while (s < e) { | 
 | 7474 |         ch = *s; | 
 | 7475 |  | 
 | 7476 |         /* Get mapping (char ordinal -> integer, Unicode char or None) */ | 
 | 7477 |         key = PyLong_FromLong((long)ch); | 
 | 7478 |         if (key == NULL) | 
 | 7479 |             goto onError; | 
 | 7480 |  | 
 | 7481 |         item = PyObject_GetItem(mapping, key); | 
 | 7482 |         Py_DECREF(key); | 
 | 7483 |         if (item == NULL) { | 
 | 7484 |             if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 7485 |                 /* No mapping found means: mapping is undefined. */ | 
 | 7486 |                 PyErr_Clear(); | 
 | 7487 |                 goto Undefined; | 
 | 7488 |             } else | 
 | 7489 |                 goto onError; | 
 | 7490 |         } | 
 | 7491 |  | 
 | 7492 |         /* Apply mapping */ | 
 | 7493 |         if (item == Py_None) | 
 | 7494 |             goto Undefined; | 
 | 7495 |         if (PyLong_Check(item)) { | 
 | 7496 |             long value = PyLong_AS_LONG(item); | 
 | 7497 |             if (value == 0xFFFE) | 
 | 7498 |                 goto Undefined; | 
 | 7499 |             if (value < 0 || value > MAX_UNICODE) { | 
 | 7500 |                 PyErr_Format(PyExc_TypeError, | 
 | 7501 |                              "character mapping must be in range(0x%lx)", | 
 | 7502 |                              (unsigned long)MAX_UNICODE + 1); | 
 | 7503 |                 goto onError; | 
 | 7504 |             } | 
 | 7505 |  | 
 | 7506 |             if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) | 
 | 7507 |                 goto onError; | 
 | 7508 |         } | 
 | 7509 |         else if (PyUnicode_Check(item)) { | 
 | 7510 |             if (PyUnicode_READY(item) == -1) | 
 | 7511 |                 goto onError; | 
 | 7512 |             if (PyUnicode_GET_LENGTH(item) == 1) { | 
 | 7513 |                 Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); | 
 | 7514 |                 if (value == 0xFFFE) | 
 | 7515 |                     goto Undefined; | 
 | 7516 |                 if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) | 
 | 7517 |                     goto onError; | 
 | 7518 |             } | 
 | 7519 |             else { | 
 | 7520 |                 writer->overallocate = 1; | 
 | 7521 |                 if (_PyUnicodeWriter_WriteStr(writer, item) == -1) | 
 | 7522 |                     goto onError; | 
 | 7523 |             } | 
 | 7524 |         } | 
 | 7525 |         else { | 
 | 7526 |             /* wrong return value */ | 
 | 7527 |             PyErr_SetString(PyExc_TypeError, | 
 | 7528 |                             "character mapping must return integer, None or str"); | 
 | 7529 |             goto onError; | 
 | 7530 |         } | 
 | 7531 |         Py_CLEAR(item); | 
 | 7532 |         ++s; | 
 | 7533 |         continue; | 
 | 7534 |  | 
 | 7535 | Undefined: | 
 | 7536 |         /* undefined mapping */ | 
 | 7537 |         Py_CLEAR(item); | 
 | 7538 |         startinpos = s-starts; | 
 | 7539 |         endinpos = startinpos+1; | 
 | 7540 |         if (unicode_decode_call_errorhandler_writer( | 
 | 7541 |                 errors, &errorHandler, | 
 | 7542 |                 "charmap", "character maps to <undefined>", | 
 | 7543 |                 &starts, &e, &startinpos, &endinpos, &exc, &s, | 
 | 7544 |                 writer)) { | 
 | 7545 |             goto onError; | 
 | 7546 |         } | 
 | 7547 |     } | 
 | 7548 |     Py_XDECREF(errorHandler); | 
 | 7549 |     Py_XDECREF(exc); | 
 | 7550 |     return 0; | 
 | 7551 |  | 
 | 7552 | onError: | 
 | 7553 |     Py_XDECREF(item); | 
 | 7554 |     Py_XDECREF(errorHandler); | 
 | 7555 |     Py_XDECREF(exc); | 
 | 7556 |     return -1; | 
 | 7557 | } | 
 | 7558 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7559 | PyObject * | 
 | 7560 | PyUnicode_DecodeCharmap(const char *s, | 
 | 7561 |                         Py_ssize_t size, | 
 | 7562 |                         PyObject *mapping, | 
 | 7563 |                         const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7564 | { | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7565 |     _PyUnicodeWriter writer; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7566 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7567 |     /* Default to Latin-1 */ | 
 | 7568 |     if (mapping == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7569 |         return PyUnicode_DecodeLatin1(s, size, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7570 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7571 |     if (size == 0) | 
| Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7572 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7573 |     _PyUnicodeWriter_Init(&writer); | 
| Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7574 |     writer.min_length = size; | 
 | 7575 |     if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7576 |         goto onError; | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7577 |  | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7578 |     if (PyUnicode_CheckExact(mapping)) { | 
| Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7579 |         if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) | 
 | 7580 |             goto onError; | 
| Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7581 |     } | 
 | 7582 |     else { | 
| Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7583 |         if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) | 
 | 7584 |             goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7585 |     } | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7586 |     return _PyUnicodeWriter_Finish(&writer); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7587 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7588 |   onError: | 
| Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7589 |     _PyUnicodeWriter_Dealloc(&writer); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7590 |     return NULL; | 
 | 7591 | } | 
 | 7592 |  | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7593 | /* Charmap encoding: the lookup table */ | 
 | 7594 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7595 | struct encoding_map { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 |     PyObject_HEAD | 
 | 7597 |     unsigned char level1[32]; | 
 | 7598 |     int count2, count3; | 
 | 7599 |     unsigned char level23[1]; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7600 | }; | 
 | 7601 |  | 
 | 7602 | static PyObject* | 
 | 7603 | encoding_map_size(PyObject *obj, PyObject* args) | 
 | 7604 | { | 
 | 7605 |     struct encoding_map *map = (struct encoding_map*)obj; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7606 |     return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7607 |                            128*map->count3); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7608 | } | 
 | 7609 |  | 
 | 7610 | static PyMethodDef encoding_map_methods[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7611 |     {"size", encoding_map_size, METH_NOARGS, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7612 |      PyDoc_STR("Return the size (in bytes) of this object") }, | 
 | 7613 |     { 0 } | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7614 | }; | 
 | 7615 |  | 
 | 7616 | static void | 
 | 7617 | encoding_map_dealloc(PyObject* o) | 
 | 7618 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7619 |     PyObject_FREE(o); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7620 | } | 
 | 7621 |  | 
 | 7622 | static PyTypeObject EncodingMapType = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7623 |     PyVarObject_HEAD_INIT(NULL, 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7624 |     "EncodingMap",          /*tp_name*/ | 
 | 7625 |     sizeof(struct encoding_map),   /*tp_basicsize*/ | 
 | 7626 |     0,                      /*tp_itemsize*/ | 
 | 7627 |     /* methods */ | 
 | 7628 |     encoding_map_dealloc,   /*tp_dealloc*/ | 
 | 7629 |     0,                      /*tp_print*/ | 
 | 7630 |     0,                      /*tp_getattr*/ | 
 | 7631 |     0,                      /*tp_setattr*/ | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7632 |     0,                      /*tp_reserved*/ | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7633 |     0,                      /*tp_repr*/ | 
 | 7634 |     0,                      /*tp_as_number*/ | 
 | 7635 |     0,                      /*tp_as_sequence*/ | 
 | 7636 |     0,                      /*tp_as_mapping*/ | 
 | 7637 |     0,                      /*tp_hash*/ | 
 | 7638 |     0,                      /*tp_call*/ | 
 | 7639 |     0,                      /*tp_str*/ | 
 | 7640 |     0,                      /*tp_getattro*/ | 
 | 7641 |     0,                      /*tp_setattro*/ | 
 | 7642 |     0,                      /*tp_as_buffer*/ | 
 | 7643 |     Py_TPFLAGS_DEFAULT,     /*tp_flags*/ | 
 | 7644 |     0,                      /*tp_doc*/ | 
 | 7645 |     0,                      /*tp_traverse*/ | 
 | 7646 |     0,                      /*tp_clear*/ | 
 | 7647 |     0,                      /*tp_richcompare*/ | 
 | 7648 |     0,                      /*tp_weaklistoffset*/ | 
 | 7649 |     0,                      /*tp_iter*/ | 
 | 7650 |     0,                      /*tp_iternext*/ | 
 | 7651 |     encoding_map_methods,   /*tp_methods*/ | 
 | 7652 |     0,                      /*tp_members*/ | 
 | 7653 |     0,                      /*tp_getset*/ | 
 | 7654 |     0,                      /*tp_base*/ | 
 | 7655 |     0,                      /*tp_dict*/ | 
 | 7656 |     0,                      /*tp_descr_get*/ | 
 | 7657 |     0,                      /*tp_descr_set*/ | 
 | 7658 |     0,                      /*tp_dictoffset*/ | 
 | 7659 |     0,                      /*tp_init*/ | 
 | 7660 |     0,                      /*tp_alloc*/ | 
 | 7661 |     0,                      /*tp_new*/ | 
 | 7662 |     0,                      /*tp_free*/ | 
 | 7663 |     0,                      /*tp_is_gc*/ | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7664 | }; | 
 | 7665 |  | 
 | 7666 | PyObject* | 
 | 7667 | PyUnicode_BuildEncodingMap(PyObject* string) | 
 | 7668 | { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7669 |     PyObject *result; | 
 | 7670 |     struct encoding_map *mresult; | 
 | 7671 |     int i; | 
 | 7672 |     int need_dict = 0; | 
 | 7673 |     unsigned char level1[32]; | 
 | 7674 |     unsigned char level2[512]; | 
 | 7675 |     unsigned char *mlevel1, *mlevel2, *mlevel3; | 
 | 7676 |     int count2 = 0, count3 = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7677 |     int kind; | 
 | 7678 |     void *data; | 
| Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7679 |     Py_ssize_t length; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7680 |     Py_UCS4 ch; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7681 |  | 
| Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7682 |     if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7683 |         PyErr_BadArgument(); | 
 | 7684 |         return NULL; | 
 | 7685 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7686 |     kind = PyUnicode_KIND(string); | 
 | 7687 |     data = PyUnicode_DATA(string); | 
| Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7688 |     length = PyUnicode_GET_LENGTH(string); | 
 | 7689 |     length = Py_MIN(length, 256); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7690 |     memset(level1, 0xFF, sizeof level1); | 
 | 7691 |     memset(level2, 0xFF, sizeof level2); | 
 | 7692 |  | 
 | 7693 |     /* If there isn't a one-to-one mapping of NULL to \0, | 
 | 7694 |        or if there are non-BMP characters, we need to use | 
 | 7695 |        a mapping dictionary. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7696 |     if (PyUnicode_READ(kind, data, 0) != 0) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7697 |         need_dict = 1; | 
| Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7698 |     for (i = 1; i < length; i++) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7699 |         int l1, l2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7700 |         ch = PyUnicode_READ(kind, data, i); | 
 | 7701 |         if (ch == 0 || ch > 0xFFFF) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7702 |             need_dict = 1; | 
 | 7703 |             break; | 
 | 7704 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7705 |         if (ch == 0xFFFE) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7706 |             /* unmapped character */ | 
 | 7707 |             continue; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7708 |         l1 = ch >> 11; | 
 | 7709 |         l2 = ch >> 7; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7710 |         if (level1[l1] == 0xFF) | 
 | 7711 |             level1[l1] = count2++; | 
 | 7712 |         if (level2[l2] == 0xFF) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7713 |             level2[l2] = count3++; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7714 |     } | 
 | 7715 |  | 
 | 7716 |     if (count2 >= 0xFF || count3 >= 0xFF) | 
 | 7717 |         need_dict = 1; | 
 | 7718 |  | 
 | 7719 |     if (need_dict) { | 
 | 7720 |         PyObject *result = PyDict_New(); | 
 | 7721 |         PyObject *key, *value; | 
 | 7722 |         if (!result) | 
 | 7723 |             return NULL; | 
| Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7724 |         for (i = 0; i < length; i++) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7725 |             key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7726 |             value = PyLong_FromLong(i); | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7727 |             if (!key || !value) | 
 | 7728 |                 goto failed1; | 
 | 7729 |             if (PyDict_SetItem(result, key, value) == -1) | 
 | 7730 |                 goto failed1; | 
 | 7731 |             Py_DECREF(key); | 
 | 7732 |             Py_DECREF(value); | 
 | 7733 |         } | 
 | 7734 |         return result; | 
 | 7735 |       failed1: | 
 | 7736 |         Py_XDECREF(key); | 
 | 7737 |         Py_XDECREF(value); | 
 | 7738 |         Py_DECREF(result); | 
 | 7739 |         return NULL; | 
 | 7740 |     } | 
 | 7741 |  | 
 | 7742 |     /* Create a three-level trie */ | 
 | 7743 |     result = PyObject_MALLOC(sizeof(struct encoding_map) + | 
 | 7744 |                              16*count2 + 128*count3 - 1); | 
 | 7745 |     if (!result) | 
 | 7746 |         return PyErr_NoMemory(); | 
 | 7747 |     PyObject_Init(result, &EncodingMapType); | 
 | 7748 |     mresult = (struct encoding_map*)result; | 
 | 7749 |     mresult->count2 = count2; | 
 | 7750 |     mresult->count3 = count3; | 
 | 7751 |     mlevel1 = mresult->level1; | 
 | 7752 |     mlevel2 = mresult->level23; | 
 | 7753 |     mlevel3 = mresult->level23 + 16*count2; | 
 | 7754 |     memcpy(mlevel1, level1, 32); | 
 | 7755 |     memset(mlevel2, 0xFF, 16*count2); | 
 | 7756 |     memset(mlevel3, 0, 128*count3); | 
 | 7757 |     count3 = 0; | 
| Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7758 |     for (i = 1; i < length; i++) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7759 |         int o1, o2, o3, i2, i3; | 
| Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7760 |         Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 7761 |         if (ch == 0xFFFE) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7762 |             /* unmapped character */ | 
 | 7763 |             continue; | 
| Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7764 |         o1 = ch>>11; | 
 | 7765 |         o2 = (ch>>7) & 0xF; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7766 |         i2 = 16*mlevel1[o1] + o2; | 
 | 7767 |         if (mlevel2[i2] == 0xFF) | 
 | 7768 |             mlevel2[i2] = count3++; | 
| Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7769 |         o3 = ch & 0x7F; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7770 |         i3 = 128*mlevel2[i2] + o3; | 
 | 7771 |         mlevel3[i3] = i; | 
 | 7772 |     } | 
 | 7773 |     return result; | 
 | 7774 | } | 
 | 7775 |  | 
 | 7776 | static int | 
| Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7777 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7778 | { | 
 | 7779 |     struct encoding_map *map = (struct encoding_map*)mapping; | 
 | 7780 |     int l1 = c>>11; | 
 | 7781 |     int l2 = (c>>7) & 0xF; | 
 | 7782 |     int l3 = c & 0x7F; | 
 | 7783 |     int i; | 
 | 7784 |  | 
| Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7785 |     if (c > 0xFFFF) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7786 |         return -1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7787 |     if (c == 0) | 
 | 7788 |         return 0; | 
 | 7789 |     /* level 1*/ | 
 | 7790 |     i = map->level1[l1]; | 
 | 7791 |     if (i == 0xFF) { | 
 | 7792 |         return -1; | 
 | 7793 |     } | 
 | 7794 |     /* level 2*/ | 
 | 7795 |     i = map->level23[16*i+l2]; | 
 | 7796 |     if (i == 0xFF) { | 
 | 7797 |         return -1; | 
 | 7798 |     } | 
 | 7799 |     /* level 3 */ | 
 | 7800 |     i = map->level23[16*map->count2 + 128*i + l3]; | 
 | 7801 |     if (i == 0) { | 
 | 7802 |         return -1; | 
 | 7803 |     } | 
 | 7804 |     return i; | 
 | 7805 | } | 
 | 7806 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7807 | /* Lookup the character ch in the mapping. If the character | 
 | 7808 |    can't be found, Py_None is returned (or NULL, if another | 
| Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7809 |    error occurred). */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7810 | static PyObject * | 
| Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7811 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7812 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7813 |     PyObject *w = PyLong_FromLong((long)c); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7814 |     PyObject *x; | 
 | 7815 |  | 
 | 7816 |     if (w == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7817 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7818 |     x = PyObject_GetItem(mapping, w); | 
 | 7819 |     Py_DECREF(w); | 
 | 7820 |     if (x == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7821 |         if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 7822 |             /* No mapping found means: mapping is undefined. */ | 
 | 7823 |             PyErr_Clear(); | 
 | 7824 |             x = Py_None; | 
 | 7825 |             Py_INCREF(x); | 
 | 7826 |             return x; | 
 | 7827 |         } else | 
 | 7828 |             return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7829 |     } | 
| Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7830 |     else if (x == Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7831 |         return x; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7832 |     else if (PyLong_Check(x)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7833 |         long value = PyLong_AS_LONG(x); | 
 | 7834 |         if (value < 0 || value > 255) { | 
 | 7835 |             PyErr_SetString(PyExc_TypeError, | 
 | 7836 |                             "character mapping must be in range(256)"); | 
 | 7837 |             Py_DECREF(x); | 
 | 7838 |             return NULL; | 
 | 7839 |         } | 
 | 7840 |         return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7841 |     } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7842 |     else if (PyBytes_Check(x)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7843 |         return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7845 |         /* wrong return value */ | 
 | 7846 |         PyErr_Format(PyExc_TypeError, | 
 | 7847 |                      "character mapping must return integer, bytes or None, not %.400s", | 
 | 7848 |                      x->ob_type->tp_name); | 
 | 7849 |         Py_DECREF(x); | 
 | 7850 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7851 |     } | 
 | 7852 | } | 
 | 7853 |  | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7854 | static int | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7855 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7856 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7857 |     Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); | 
 | 7858 |     /* exponentially overallocate to minimize reallocations */ | 
 | 7859 |     if (requiredsize < 2*outsize) | 
 | 7860 |         requiredsize = 2*outsize; | 
 | 7861 |     if (_PyBytes_Resize(outobj, requiredsize)) | 
 | 7862 |         return -1; | 
 | 7863 |     return 0; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7864 | } | 
 | 7865 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7866 | typedef enum charmapencode_result { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7867 |     enc_SUCCESS, enc_FAILED, enc_EXCEPTION | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7868 | } charmapencode_result; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7869 | /* 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] | 7870 |    various state variables. Resize the output bytes object if not enough | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7871 |    space is available. Return a new reference to the object that | 
 | 7872 |    was put in the output buffer, or Py_None, if the mapping was undefined | 
 | 7873 |    (in which case no character was written) or NULL, if a | 
| Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7874 |    reallocation error occurred. The caller must decref the result */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7875 | static charmapencode_result | 
| Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7876 | charmapencode_output(Py_UCS4 c, PyObject *mapping, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7877 |                      PyObject **outobj, Py_ssize_t *outpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7878 | { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7879 |     PyObject *rep; | 
 | 7880 |     char *outstart; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7881 |     Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7882 |  | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7883 |     if (Py_TYPE(mapping) == &EncodingMapType) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7884 |         int res = encoding_map_lookup(c, mapping); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7885 |         Py_ssize_t requiredsize = *outpos+1; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7886 |         if (res == -1) | 
 | 7887 |             return enc_FAILED; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7888 |         if (outsize<requiredsize) | 
 | 7889 |             if (charmapencode_resize(outobj, outpos, requiredsize)) | 
 | 7890 |                 return enc_EXCEPTION; | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7891 |         outstart = PyBytes_AS_STRING(*outobj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 |         outstart[(*outpos)++] = (char)res; | 
 | 7893 |         return enc_SUCCESS; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7894 |     } | 
 | 7895 |  | 
 | 7896 |     rep = charmapencode_lookup(c, mapping); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7897 |     if (rep==NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7898 |         return enc_EXCEPTION; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7899 |     else if (rep==Py_None) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7900 |         Py_DECREF(rep); | 
 | 7901 |         return enc_FAILED; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7902 |     } else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 |         if (PyLong_Check(rep)) { | 
 | 7904 |             Py_ssize_t requiredsize = *outpos+1; | 
 | 7905 |             if (outsize<requiredsize) | 
 | 7906 |                 if (charmapencode_resize(outobj, outpos, requiredsize)) { | 
 | 7907 |                     Py_DECREF(rep); | 
 | 7908 |                     return enc_EXCEPTION; | 
 | 7909 |                 } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7910 |             outstart = PyBytes_AS_STRING(*outobj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7911 |             outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7912 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7913 |         else { | 
 | 7914 |             const char *repchars = PyBytes_AS_STRING(rep); | 
 | 7915 |             Py_ssize_t repsize = PyBytes_GET_SIZE(rep); | 
 | 7916 |             Py_ssize_t requiredsize = *outpos+repsize; | 
 | 7917 |             if (outsize<requiredsize) | 
 | 7918 |                 if (charmapencode_resize(outobj, outpos, requiredsize)) { | 
 | 7919 |                     Py_DECREF(rep); | 
 | 7920 |                     return enc_EXCEPTION; | 
 | 7921 |                 } | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7922 |             outstart = PyBytes_AS_STRING(*outobj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7923 |             memcpy(outstart + *outpos, repchars, repsize); | 
 | 7924 |             *outpos += repsize; | 
 | 7925 |         } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7926 |     } | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7927 |     Py_DECREF(rep); | 
 | 7928 |     return enc_SUCCESS; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7929 | } | 
 | 7930 |  | 
 | 7931 | /* handle an error in PyUnicode_EncodeCharmap | 
 | 7932 |    Return 0 on success, -1 on error */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7933 | static int | 
 | 7934 | charmap_encoding_error( | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7935 |     PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7936 |     PyObject **exceptionObject, | 
| Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7937 |     int *known_errorHandler, PyObject **errorHandler, const char *errors, | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7938 |     PyObject **res, Py_ssize_t *respos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7939 | { | 
 | 7940 |     PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7941 |     Py_ssize_t size, repsize; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7942 |     Py_ssize_t newpos; | 
| Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7943 |     enum PyUnicode_Kind kind; | 
 | 7944 |     void *data; | 
 | 7945 |     Py_ssize_t index; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7946 |     /* startpos for collecting unencodable chars */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7947 |     Py_ssize_t collstartpos = *inpos; | 
 | 7948 |     Py_ssize_t collendpos = *inpos+1; | 
 | 7949 |     Py_ssize_t collpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7950 |     char *encoding = "charmap"; | 
 | 7951 |     char *reason = "character maps to <undefined>"; | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7952 |     charmapencode_result x; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7953 |     Py_UCS4 ch; | 
| Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7954 |     int val; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7955 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7956 |     if (PyUnicode_READY(unicode) == -1) | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7957 |         return -1; | 
 | 7958 |     size = PyUnicode_GET_LENGTH(unicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7959 |     /* find all unencodable characters */ | 
 | 7960 |     while (collendpos < size) { | 
| Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7961 |         PyObject *rep; | 
| Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7962 |         if (Py_TYPE(mapping) == &EncodingMapType) { | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7963 |             ch = PyUnicode_READ_CHAR(unicode, collendpos); | 
| Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7964 |             val = encoding_map_lookup(ch, mapping); | 
 | 7965 |             if (val != -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7966 |                 break; | 
 | 7967 |             ++collendpos; | 
 | 7968 |             continue; | 
 | 7969 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7970 |  | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7971 |         ch = PyUnicode_READ_CHAR(unicode, collendpos); | 
 | 7972 |         rep = charmapencode_lookup(ch, mapping); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7973 |         if (rep==NULL) | 
 | 7974 |             return -1; | 
 | 7975 |         else if (rep!=Py_None) { | 
 | 7976 |             Py_DECREF(rep); | 
 | 7977 |             break; | 
 | 7978 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7979 |         Py_DECREF(rep); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7980 |         ++collendpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7981 |     } | 
 | 7982 |     /* cache callback name lookup | 
 | 7983 |      * (if not done yet, i.e. it's the first error) */ | 
 | 7984 |     if (*known_errorHandler==-1) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 |         if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 7986 |             *known_errorHandler = 1; | 
 | 7987 |         else if (!strcmp(errors, "replace")) | 
 | 7988 |             *known_errorHandler = 2; | 
 | 7989 |         else if (!strcmp(errors, "ignore")) | 
 | 7990 |             *known_errorHandler = 3; | 
 | 7991 |         else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 7992 |             *known_errorHandler = 4; | 
 | 7993 |         else | 
 | 7994 |             *known_errorHandler = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7995 |     } | 
 | 7996 |     switch (*known_errorHandler) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7997 |     case 1: /* strict */ | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7998 |         raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7999 |         return -1; | 
 | 8000 |     case 2: /* replace */ | 
 | 8001 |         for (collpos = collstartpos; collpos<collendpos; ++collpos) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8002 |             x = charmapencode_output('?', mapping, res, respos); | 
 | 8003 |             if (x==enc_EXCEPTION) { | 
 | 8004 |                 return -1; | 
 | 8005 |             } | 
 | 8006 |             else if (x==enc_FAILED) { | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8007 |                 raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8008 |                 return -1; | 
 | 8009 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8010 |         } | 
 | 8011 |         /* fall through */ | 
 | 8012 |     case 3: /* ignore */ | 
 | 8013 |         *inpos = collendpos; | 
 | 8014 |         break; | 
 | 8015 |     case 4: /* xmlcharrefreplace */ | 
 | 8016 |         /* generate replacement (temporarily (mis)uses p) */ | 
 | 8017 |         for (collpos = collstartpos; collpos < collendpos; ++collpos) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8018 |             char buffer[2+29+1+1]; | 
 | 8019 |             char *cp; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8020 |             sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8021 |             for (cp = buffer; *cp; ++cp) { | 
 | 8022 |                 x = charmapencode_output(*cp, mapping, res, respos); | 
 | 8023 |                 if (x==enc_EXCEPTION) | 
 | 8024 |                     return -1; | 
 | 8025 |                 else if (x==enc_FAILED) { | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8026 |                     raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8027 |                     return -1; | 
 | 8028 |                 } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8029 |             } | 
 | 8030 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8031 |         *inpos = collendpos; | 
 | 8032 |         break; | 
 | 8033 |     default: | 
 | 8034 |         repunicode = unicode_encode_call_errorhandler(errors, errorHandler, | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8035 |                                                       encoding, reason, unicode, exceptionObject, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8036 |                                                       collstartpos, collendpos, &newpos); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8037 |         if (repunicode == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8038 |             return -1; | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8039 |         if (PyBytes_Check(repunicode)) { | 
 | 8040 |             /* Directly copy bytes result to output. */ | 
 | 8041 |             Py_ssize_t outsize = PyBytes_Size(*res); | 
 | 8042 |             Py_ssize_t requiredsize; | 
 | 8043 |             repsize = PyBytes_Size(repunicode); | 
 | 8044 |             requiredsize = *respos + repsize; | 
 | 8045 |             if (requiredsize > outsize) | 
 | 8046 |                 /* Make room for all additional bytes. */ | 
 | 8047 |                 if (charmapencode_resize(res, respos, requiredsize)) { | 
 | 8048 |                     Py_DECREF(repunicode); | 
 | 8049 |                     return -1; | 
 | 8050 |                 } | 
 | 8051 |             memcpy(PyBytes_AsString(*res) + *respos, | 
 | 8052 |                    PyBytes_AsString(repunicode),  repsize); | 
 | 8053 |             *respos += repsize; | 
 | 8054 |             *inpos = newpos; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8055 |             Py_DECREF(repunicode); | 
| Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8056 |             break; | 
| Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8057 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8058 |         /* generate replacement  */ | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8059 |         if (PyUnicode_READY(repunicode) == -1) { | 
| Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8060 |             Py_DECREF(repunicode); | 
 | 8061 |             return -1; | 
 | 8062 |         } | 
| Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8063 |         repsize = PyUnicode_GET_LENGTH(repunicode); | 
| Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8064 |         data = PyUnicode_DATA(repunicode); | 
 | 8065 |         kind = PyUnicode_KIND(repunicode); | 
 | 8066 |         for (index = 0; index < repsize; index++) { | 
 | 8067 |             Py_UCS4 repch = PyUnicode_READ(kind, data, index); | 
 | 8068 |             x = charmapencode_output(repch, mapping, res, respos); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8069 |             if (x==enc_EXCEPTION) { | 
| Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8070 |                 Py_DECREF(repunicode); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 |                 return -1; | 
 | 8072 |             } | 
 | 8073 |             else if (x==enc_FAILED) { | 
 | 8074 |                 Py_DECREF(repunicode); | 
| Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8075 |                 raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8076 |                 return -1; | 
 | 8077 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8078 |         } | 
 | 8079 |         *inpos = newpos; | 
 | 8080 |         Py_DECREF(repunicode); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8081 |     } | 
 | 8082 |     return 0; | 
 | 8083 | } | 
 | 8084 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8085 | PyObject * | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8086 | _PyUnicode_EncodeCharmap(PyObject *unicode, | 
 | 8087 |                          PyObject *mapping, | 
 | 8088 |                          const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8090 |     /* output object */ | 
 | 8091 |     PyObject *res = NULL; | 
 | 8092 |     /* current input position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8093 |     Py_ssize_t inpos = 0; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8094 |     Py_ssize_t size; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8095 |     /* current output position */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8096 |     Py_ssize_t respos = 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8097 |     PyObject *errorHandler = NULL; | 
 | 8098 |     PyObject *exc = NULL; | 
 | 8099 |     /* the following variable is used for caching string comparisons | 
 | 8100 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
 | 8101 |      * 3=ignore, 4=xmlcharrefreplace */ | 
 | 8102 |     int known_errorHandler = -1; | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8103 |     void *data; | 
 | 8104 |     int kind; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8105 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8106 |     if (PyUnicode_READY(unicode) == -1) | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8107 |         return NULL; | 
 | 8108 |     size = PyUnicode_GET_LENGTH(unicode); | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8109 |     data = PyUnicode_DATA(unicode); | 
 | 8110 |     kind = PyUnicode_KIND(unicode); | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8111 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8112 |     /* Default to Latin-1 */ | 
 | 8113 |     if (mapping == NULL) | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8114 |         return unicode_encode_ucs1(unicode, errors, 256); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8115 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8116 |     /* allocate enough for a simple encoding without | 
 | 8117 |        replacements, if we need more, we'll resize */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8118 |     res = PyBytes_FromStringAndSize(NULL, size); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8119 |     if (res == NULL) | 
 | 8120 |         goto onError; | 
| Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8121 |     if (size == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 |         return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8124 |     while (inpos<size) { | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8125 |         Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8126 |         /* try to encode it */ | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8127 |         charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8128 |         if (x==enc_EXCEPTION) /* error */ | 
 | 8129 |             goto onError; | 
 | 8130 |         if (x==enc_FAILED) { /* unencodable character */ | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8131 |             if (charmap_encoding_error(unicode, &inpos, mapping, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 |                                        &exc, | 
 | 8133 |                                        &known_errorHandler, &errorHandler, errors, | 
 | 8134 |                                        &res, &respos)) { | 
 | 8135 |                 goto onError; | 
 | 8136 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8137 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8138 |         else | 
 | 8139 |             /* done with this character => adjust input position */ | 
 | 8140 |             ++inpos; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8141 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8142 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8143 |     /* Resize if we allocated to much */ | 
| Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8144 |     if (respos<PyBytes_GET_SIZE(res)) | 
| Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8145 |         if (_PyBytes_Resize(&res, respos) < 0) | 
 | 8146 |             goto onError; | 
| Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8147 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8148 |     Py_XDECREF(exc); | 
 | 8149 |     Py_XDECREF(errorHandler); | 
 | 8150 |     return res; | 
 | 8151 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8152 |   onError: | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8153 |     Py_XDECREF(res); | 
 | 8154 |     Py_XDECREF(exc); | 
 | 8155 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8156 |     return NULL; | 
 | 8157 | } | 
 | 8158 |  | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8159 | /* Deprecated */ | 
 | 8160 | PyObject * | 
 | 8161 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, | 
 | 8162 |                         Py_ssize_t size, | 
 | 8163 |                         PyObject *mapping, | 
 | 8164 |                         const char *errors) | 
 | 8165 | { | 
 | 8166 |     PyObject *result; | 
 | 8167 |     PyObject *unicode = PyUnicode_FromUnicode(p, size); | 
 | 8168 |     if (unicode == NULL) | 
 | 8169 |         return NULL; | 
 | 8170 |     result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); | 
 | 8171 |     Py_DECREF(unicode); | 
| Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8172 |     return result; | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8173 | } | 
 | 8174 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8175 | PyObject * | 
 | 8176 | PyUnicode_AsCharmapString(PyObject *unicode, | 
 | 8177 |                           PyObject *mapping) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8178 | { | 
 | 8179 |     if (!PyUnicode_Check(unicode) || mapping == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 |         PyErr_BadArgument(); | 
 | 8181 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8182 |     } | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8183 |     return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8184 | } | 
 | 8185 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8186 | /* create or adjust a UnicodeTranslateError */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8187 | static void | 
 | 8188 | make_translate_exception(PyObject **exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8189 |                          PyObject *unicode, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8190 |                          Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 8191 |                          const char *reason) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8192 | { | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8193 |     if (*exceptionObject == NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8194 |         *exceptionObject = _PyUnicodeTranslateError_Create( | 
 | 8195 |             unicode, startpos, endpos, reason); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8196 |     } | 
 | 8197 |     else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8198 |         if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) | 
 | 8199 |             goto onError; | 
 | 8200 |         if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) | 
 | 8201 |             goto onError; | 
 | 8202 |         if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) | 
 | 8203 |             goto onError; | 
 | 8204 |         return; | 
 | 8205 |       onError: | 
 | 8206 |         Py_DECREF(*exceptionObject); | 
 | 8207 |         *exceptionObject = NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8208 |     } | 
 | 8209 | } | 
 | 8210 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8211 | /* error handling callback helper: | 
 | 8212 |    build arguments, call the callback and check the arguments, | 
 | 8213 |    put the result into newpos and return the replacement string, which | 
 | 8214 |    has to be freed by the caller */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8215 | static PyObject * | 
 | 8216 | unicode_translate_call_errorhandler(const char *errors, | 
 | 8217 |                                     PyObject **errorHandler, | 
 | 8218 |                                     const char *reason, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8219 |                                     PyObject *unicode, PyObject **exceptionObject, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8220 |                                     Py_ssize_t startpos, Py_ssize_t endpos, | 
 | 8221 |                                     Py_ssize_t *newpos) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8222 | { | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8223 |     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] | 8224 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8225 |     Py_ssize_t i_newpos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8226 |     PyObject *restuple; | 
 | 8227 |     PyObject *resunicode; | 
 | 8228 |  | 
 | 8229 |     if (*errorHandler == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 |         *errorHandler = PyCodec_LookupError(errors); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8231 |         if (*errorHandler == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8232 |             return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8233 |     } | 
 | 8234 |  | 
 | 8235 |     make_translate_exception(exceptionObject, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8236 |                              unicode, startpos, endpos, reason); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8237 |     if (*exceptionObject == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8238 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8239 |  | 
 | 8240 |     restuple = PyObject_CallFunctionObjArgs( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8241 |         *errorHandler, *exceptionObject, NULL); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8242 |     if (restuple == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8243 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 |     if (!PyTuple_Check(restuple)) { | 
| Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8245 |         PyErr_SetString(PyExc_TypeError, &argparse[4]); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8246 |         Py_DECREF(restuple); | 
 | 8247 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8248 |     } | 
 | 8249 |     if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8250 |                           &resunicode, &i_newpos)) { | 
 | 8251 |         Py_DECREF(restuple); | 
 | 8252 |         return NULL; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8253 |     } | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8254 |     if (i_newpos<0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8255 |         *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8256 |     else | 
 | 8257 |         *newpos = i_newpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8258 |     if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8259 |         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); | 
 | 8260 |         Py_DECREF(restuple); | 
 | 8261 |         return NULL; | 
| Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8262 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8263 |     Py_INCREF(resunicode); | 
 | 8264 |     Py_DECREF(restuple); | 
 | 8265 |     return resunicode; | 
 | 8266 | } | 
 | 8267 |  | 
 | 8268 | /* Lookup the character ch in the mapping and put the result in result, | 
 | 8269 |    which must be decrefed by the caller. | 
 | 8270 |    Return 0 on success, -1 on error */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8271 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8272 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8273 | { | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8274 |     PyObject *w = PyLong_FromLong((long)c); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8275 |     PyObject *x; | 
 | 8276 |  | 
 | 8277 |     if (w == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 |         return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8279 |     x = PyObject_GetItem(mapping, w); | 
 | 8280 |     Py_DECREF(w); | 
 | 8281 |     if (x == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8282 |         if (PyErr_ExceptionMatches(PyExc_LookupError)) { | 
 | 8283 |             /* No mapping found means: use 1:1 mapping. */ | 
 | 8284 |             PyErr_Clear(); | 
 | 8285 |             *result = NULL; | 
 | 8286 |             return 0; | 
 | 8287 |         } else | 
 | 8288 |             return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8289 |     } | 
 | 8290 |     else if (x == Py_None) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8291 |         *result = x; | 
 | 8292 |         return 0; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8293 |     } | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8294 |     else if (PyLong_Check(x)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8295 |         long value = PyLong_AS_LONG(x); | 
 | 8296 |         long max = PyUnicode_GetMax(); | 
 | 8297 |         if (value < 0 || value > max) { | 
 | 8298 |             PyErr_Format(PyExc_TypeError, | 
| Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8299 |                          "character mapping must be in range(0x%x)", max+1); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8300 |             Py_DECREF(x); | 
 | 8301 |             return -1; | 
 | 8302 |         } | 
 | 8303 |         *result = x; | 
 | 8304 |         return 0; | 
 | 8305 |     } | 
 | 8306 |     else if (PyUnicode_Check(x)) { | 
 | 8307 |         *result = x; | 
 | 8308 |         return 0; | 
 | 8309 |     } | 
 | 8310 |     else { | 
 | 8311 |         /* wrong return value */ | 
 | 8312 |         PyErr_SetString(PyExc_TypeError, | 
 | 8313 |                         "character mapping must return integer, None or str"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8314 |         Py_DECREF(x); | 
 | 8315 |         return -1; | 
 | 8316 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8317 | } | 
 | 8318 | /* ensure that *outobj is at least requiredsize characters long, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8319 |    if not reallocate and adjust various state variables. | 
 | 8320 |    Return 0 on success, -1 on error */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8321 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8322 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8323 |                                Py_ssize_t requiredsize) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8324 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8325 |     Py_ssize_t oldsize = *psize; | 
| Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8326 |     Py_UCS4 *new_outobj; | 
| Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8327 |     if (requiredsize > oldsize) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8328 |         /* exponentially overallocate to minimize reallocations */ | 
 | 8329 |         if (requiredsize < 2 * oldsize) | 
 | 8330 |             requiredsize = 2 * oldsize; | 
| Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8331 |         new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); | 
 | 8332 |         if (new_outobj == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 |             return -1; | 
| Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8334 |         *outobj = new_outobj; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8335 |         *psize = requiredsize; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8336 |     } | 
 | 8337 |     return 0; | 
 | 8338 | } | 
 | 8339 | /* lookup the character, put the result in the output string and adjust | 
 | 8340 |    various state variables. Return a new reference to the object that | 
 | 8341 |    was put in the output buffer in *result, or Py_None, if the mapping was | 
 | 8342 |    undefined (in which case no character was written). | 
 | 8343 |    The called must decref result. | 
 | 8344 |    Return 0 on success, -1 on error. */ | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8345 | static int | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8346 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, | 
 | 8347 |                         PyObject *mapping, Py_UCS4 **output, | 
 | 8348 |                         Py_ssize_t *osize, Py_ssize_t *opos, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8349 |                         PyObject **res) | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8350 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8351 |     Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); | 
 | 8352 |     if (charmaptranslate_lookup(curinp, mapping, res)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8353 |         return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8354 |     if (*res==NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8355 |         /* not found => default to 1:1 mapping */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8356 |         (*output)[(*opos)++] = curinp; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8357 |     } | 
 | 8358 |     else if (*res==Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8359 |         ; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8360 |     else if (PyLong_Check(*res)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8361 |         /* 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] | 8362 |         (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8363 |     } | 
 | 8364 |     else if (PyUnicode_Check(*res)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8365 |         Py_ssize_t repsize; | 
 | 8366 |         if (PyUnicode_READY(*res) == -1) | 
 | 8367 |             return -1; | 
 | 8368 |         repsize = PyUnicode_GET_LENGTH(*res); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8369 |         if (repsize==1) { | 
 | 8370 |             /* 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] | 8371 |             (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8372 |         } | 
 | 8373 |         else if (repsize!=0) { | 
 | 8374 |             /* more than one character */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8375 |             Py_ssize_t requiredsize = *opos + | 
 | 8376 |                 (PyUnicode_GET_LENGTH(input) - ipos) + | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8377 |                 repsize - 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8378 |             Py_ssize_t i; | 
 | 8379 |             if (charmaptranslate_makespace(output, osize, requiredsize)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 |                 return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8381 |             for(i = 0; i < repsize; i++) | 
 | 8382 |                 (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8383 |         } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8384 |     } | 
 | 8385 |     else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8386 |         return -1; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8387 |     return 0; | 
 | 8388 | } | 
 | 8389 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8390 | PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8391 | _PyUnicode_TranslateCharmap(PyObject *input, | 
 | 8392 |                             PyObject *mapping, | 
 | 8393 |                             const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8394 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8395 |     /* input object */ | 
 | 8396 |     char *idata; | 
 | 8397 |     Py_ssize_t size, i; | 
 | 8398 |     int kind; | 
 | 8399 |     /* output buffer */ | 
 | 8400 |     Py_UCS4 *output = NULL; | 
 | 8401 |     Py_ssize_t osize; | 
 | 8402 |     PyObject *res; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8403 |     /* current output position */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8404 |     Py_ssize_t opos; | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8405 |     char *reason = "character maps to <undefined>"; | 
 | 8406 |     PyObject *errorHandler = NULL; | 
 | 8407 |     PyObject *exc = NULL; | 
 | 8408 |     /* the following variable is used for caching string comparisons | 
 | 8409 |      * -1=not initialized, 0=unknown, 1=strict, 2=replace, | 
 | 8410 |      * 3=ignore, 4=xmlcharrefreplace */ | 
 | 8411 |     int known_errorHandler = -1; | 
 | 8412 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8413 |     if (mapping == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8414 |         PyErr_BadArgument(); | 
 | 8415 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8416 |     } | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8417 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8418 |     if (PyUnicode_READY(input) == -1) | 
 | 8419 |         return NULL; | 
 | 8420 |     idata = (char*)PyUnicode_DATA(input); | 
 | 8421 |     kind = PyUnicode_KIND(input); | 
 | 8422 |     size = PyUnicode_GET_LENGTH(input); | 
 | 8423 |     i = 0; | 
 | 8424 |  | 
 | 8425 |     if (size == 0) { | 
 | 8426 |         Py_INCREF(input); | 
 | 8427 |         return input; | 
 | 8428 |     } | 
 | 8429 |  | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8430 |     /* allocate enough for a simple 1:1 translation without | 
 | 8431 |        replacements, if we need more, we'll resize */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8432 |     osize = size; | 
 | 8433 |     output = PyMem_Malloc(osize * sizeof(Py_UCS4)); | 
 | 8434 |     opos = 0; | 
 | 8435 |     if (output == NULL) { | 
 | 8436 |         PyErr_NoMemory(); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 |         goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8438 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8439 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8440 |     while (i<size) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8441 |         /* try to encode it */ | 
 | 8442 |         PyObject *x = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8443 |         if (charmaptranslate_output(input, i, mapping, | 
 | 8444 |                                     &output, &osize, &opos, &x)) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8445 |             Py_XDECREF(x); | 
 | 8446 |             goto onError; | 
 | 8447 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8448 |         Py_XDECREF(x); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 |         if (x!=Py_None) /* it worked => adjust input pointer */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8450 |             ++i; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8451 |         else { /* untranslatable character */ | 
 | 8452 |             PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | 
 | 8453 |             Py_ssize_t repsize; | 
 | 8454 |             Py_ssize_t newpos; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8455 |             Py_ssize_t uni2; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 |             /* startpos for collecting untranslatable chars */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8457 |             Py_ssize_t collstart = i; | 
 | 8458 |             Py_ssize_t collend = i+1; | 
 | 8459 |             Py_ssize_t coll; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8460 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8461 |             /* find all untranslatable characters */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8462 |             while (collend < size) { | 
 | 8463 |                 if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8464 |                     goto onError; | 
 | 8465 |                 Py_XDECREF(x); | 
 | 8466 |                 if (x!=Py_None) | 
 | 8467 |                     break; | 
 | 8468 |                 ++collend; | 
 | 8469 |             } | 
 | 8470 |             /* cache callback name lookup | 
 | 8471 |              * (if not done yet, i.e. it's the first error) */ | 
 | 8472 |             if (known_errorHandler==-1) { | 
 | 8473 |                 if ((errors==NULL) || (!strcmp(errors, "strict"))) | 
 | 8474 |                     known_errorHandler = 1; | 
 | 8475 |                 else if (!strcmp(errors, "replace")) | 
 | 8476 |                     known_errorHandler = 2; | 
 | 8477 |                 else if (!strcmp(errors, "ignore")) | 
 | 8478 |                     known_errorHandler = 3; | 
 | 8479 |                 else if (!strcmp(errors, "xmlcharrefreplace")) | 
 | 8480 |                     known_errorHandler = 4; | 
 | 8481 |                 else | 
 | 8482 |                     known_errorHandler = 0; | 
 | 8483 |             } | 
 | 8484 |             switch (known_errorHandler) { | 
 | 8485 |             case 1: /* strict */ | 
| Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8486 |                 make_translate_exception(&exc, | 
 | 8487 |                                          input, collstart, collend, reason); | 
 | 8488 |                 if (exc != NULL) | 
 | 8489 |                     PyCodec_StrictErrors(exc); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8490 |                 goto onError; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8491 |             case 2: /* replace */ | 
 | 8492 |                 /* 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] | 8493 |                 for (coll = collstart; coll<collend; coll++) | 
 | 8494 |                     output[opos++] = '?'; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8495 |                 /* fall through */ | 
 | 8496 |             case 3: /* ignore */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8497 |                 i = collend; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8498 |                 break; | 
 | 8499 |             case 4: /* xmlcharrefreplace */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8500 |                 /* generate replacement (temporarily (mis)uses i) */ | 
 | 8501 |                 for (i = collstart; i < collend; ++i) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8502 |                     char buffer[2+29+1+1]; | 
 | 8503 |                     char *cp; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8504 |                     sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); | 
 | 8505 |                     if (charmaptranslate_makespace(&output, &osize, | 
 | 8506 |                                                    opos+strlen(buffer)+(size-collend))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8507 |                         goto onError; | 
 | 8508 |                     for (cp = buffer; *cp; ++cp) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8509 |                         output[opos++] = *cp; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8510 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8511 |                 i = collend; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8512 |                 break; | 
 | 8513 |             default: | 
 | 8514 |                 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8515 |                                                                  reason, input, &exc, | 
 | 8516 |                                                                  collstart, collend, &newpos); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8517 |                 if (repunicode == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8518 |                     goto onError; | 
| Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8519 |                 if (PyUnicode_READY(repunicode) == -1) { | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8520 |                     Py_DECREF(repunicode); | 
 | 8521 |                     goto onError; | 
 | 8522 |                 } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 |                 /* generate replacement  */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8524 |                 repsize = PyUnicode_GET_LENGTH(repunicode); | 
 | 8525 |                 if (charmaptranslate_makespace(&output, &osize, | 
 | 8526 |                                                opos+repsize+(size-collend))) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8527 |                     Py_DECREF(repunicode); | 
 | 8528 |                     goto onError; | 
 | 8529 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8530 |                 for (uni2 = 0; repsize-->0; ++uni2) | 
 | 8531 |                     output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); | 
 | 8532 |                 i = newpos; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8533 |                 Py_DECREF(repunicode); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8534 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8535 |         } | 
 | 8536 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8537 |     res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); | 
 | 8538 |     if (!res) | 
 | 8539 |         goto onError; | 
 | 8540 |     PyMem_Free(output); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8541 |     Py_XDECREF(exc); | 
 | 8542 |     Py_XDECREF(errorHandler); | 
 | 8543 |     return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8544 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8545 |   onError: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8546 |     PyMem_Free(output); | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8547 |     Py_XDECREF(exc); | 
 | 8548 |     Py_XDECREF(errorHandler); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8549 |     return NULL; | 
 | 8550 | } | 
 | 8551 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8552 | /* Deprecated. Use PyUnicode_Translate instead. */ | 
 | 8553 | PyObject * | 
 | 8554 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, | 
 | 8555 |                            Py_ssize_t size, | 
 | 8556 |                            PyObject *mapping, | 
 | 8557 |                            const char *errors) | 
 | 8558 | { | 
| Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8559 |     PyObject *result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8560 |     PyObject *unicode = PyUnicode_FromUnicode(p, size); | 
 | 8561 |     if (!unicode) | 
 | 8562 |         return NULL; | 
| Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8563 |     result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); | 
 | 8564 |     Py_DECREF(unicode); | 
 | 8565 |     return result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8566 | } | 
 | 8567 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8568 | PyObject * | 
 | 8569 | PyUnicode_Translate(PyObject *str, | 
 | 8570 |                     PyObject *mapping, | 
 | 8571 |                     const char *errors) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8572 | { | 
 | 8573 |     PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8574 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8575 |     str = PyUnicode_FromObject(str); | 
 | 8576 |     if (str == NULL) | 
| Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8577 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8578 |     result = _PyUnicode_TranslateCharmap(str, mapping, errors); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8579 |     Py_DECREF(str); | 
 | 8580 |     return result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8581 | } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8582 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8583 | static Py_UCS4 | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8584 | fix_decimal_and_space_to_ascii(PyObject *self) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8585 | { | 
 | 8586 |     /* No need to call PyUnicode_READY(self) because this function is only | 
 | 8587 |        called as a callback from fixup() which does it already. */ | 
 | 8588 |     const Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
 | 8589 |     const int kind = PyUnicode_KIND(self); | 
 | 8590 |     void *data = PyUnicode_DATA(self); | 
| Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8591 |     Py_UCS4 maxchar = 127, ch, fixed; | 
| Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8592 |     int modified = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8593 |     Py_ssize_t i; | 
 | 8594 |  | 
 | 8595 |     for (i = 0; i < len; ++i) { | 
 | 8596 |         ch = PyUnicode_READ(kind, data, i); | 
 | 8597 |         fixed = 0; | 
 | 8598 |         if (ch > 127) { | 
 | 8599 |             if (Py_UNICODE_ISSPACE(ch)) | 
 | 8600 |                 fixed = ' '; | 
 | 8601 |             else { | 
 | 8602 |                 const int decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 8603 |                 if (decimal >= 0) | 
 | 8604 |                     fixed = '0' + decimal; | 
 | 8605 |             } | 
 | 8606 |             if (fixed != 0) { | 
| Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8607 |                 modified = 1; | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8608 |                 maxchar = Py_MAX(maxchar, fixed); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8609 |                 PyUnicode_WRITE(kind, data, i, fixed); | 
 | 8610 |             } | 
| Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8611 |             else | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8612 |                 maxchar = Py_MAX(maxchar, ch); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8613 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8614 |     } | 
 | 8615 |  | 
| Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8616 |     return (modified) ? maxchar : 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8617 | } | 
 | 8618 |  | 
 | 8619 | PyObject * | 
 | 8620 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) | 
 | 8621 | { | 
 | 8622 |     if (!PyUnicode_Check(unicode)) { | 
 | 8623 |         PyErr_BadInternalCall(); | 
 | 8624 |         return NULL; | 
 | 8625 |     } | 
 | 8626 |     if (PyUnicode_READY(unicode) == -1) | 
 | 8627 |         return NULL; | 
 | 8628 |     if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { | 
 | 8629 |         /* If the string is already ASCII, just return the same string */ | 
 | 8630 |         Py_INCREF(unicode); | 
 | 8631 |         return unicode; | 
 | 8632 |     } | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8633 |     return fixup(unicode, fix_decimal_and_space_to_ascii); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8634 | } | 
 | 8635 |  | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8636 | PyObject * | 
 | 8637 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, | 
 | 8638 |                                   Py_ssize_t length) | 
 | 8639 | { | 
| Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8640 |     PyObject *decimal; | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8641 |     Py_ssize_t i; | 
| Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8642 |     Py_UCS4 maxchar; | 
 | 8643 |     enum PyUnicode_Kind kind; | 
 | 8644 |     void *data; | 
 | 8645 |  | 
| Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8646 |     maxchar = 127; | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8647 |     for (i = 0; i < length; i++) { | 
| Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8648 |         Py_UNICODE ch = s[i]; | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8649 |         if (ch > 127) { | 
 | 8650 |             int decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 8651 |             if (decimal >= 0) | 
| Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8652 |                 ch = '0' + decimal; | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8653 |             maxchar = Py_MAX(maxchar, ch); | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8654 |         } | 
 | 8655 |     } | 
| Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8656 |  | 
 | 8657 |     /* Copy to a new string */ | 
 | 8658 |     decimal = PyUnicode_New(length, maxchar); | 
 | 8659 |     if (decimal == NULL) | 
 | 8660 |         return decimal; | 
 | 8661 |     kind = PyUnicode_KIND(decimal); | 
 | 8662 |     data = PyUnicode_DATA(decimal); | 
 | 8663 |     /* Iterate over code points */ | 
 | 8664 |     for (i = 0; i < length; i++) { | 
 | 8665 |         Py_UNICODE ch = s[i]; | 
 | 8666 |         if (ch > 127) { | 
 | 8667 |             int decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 8668 |             if (decimal >= 0) | 
 | 8669 |                 ch = '0' + decimal; | 
 | 8670 |         } | 
 | 8671 |         PyUnicode_WRITE(kind, data, i, ch); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8672 |     } | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8673 |     return unicode_result(decimal); | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8674 | } | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8675 | /* --- Decimal Encoder ---------------------------------------------------- */ | 
 | 8676 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8677 | int | 
 | 8678 | PyUnicode_EncodeDecimal(Py_UNICODE *s, | 
 | 8679 |                         Py_ssize_t length, | 
 | 8680 |                         char *output, | 
 | 8681 |                         const char *errors) | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8682 | { | 
| Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8683 |     PyObject *unicode; | 
| Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8684 |     Py_ssize_t i; | 
| Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8685 |     enum PyUnicode_Kind kind; | 
 | 8686 |     void *data; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8687 |  | 
 | 8688 |     if (output == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8689 |         PyErr_BadArgument(); | 
 | 8690 |         return -1; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8691 |     } | 
 | 8692 |  | 
| Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8693 |     unicode = PyUnicode_FromUnicode(s, length); | 
 | 8694 |     if (unicode == NULL) | 
 | 8695 |         return -1; | 
 | 8696 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8697 |     if (PyUnicode_READY(unicode) == -1) { | 
| Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8698 |         Py_DECREF(unicode); | 
 | 8699 |         return -1; | 
 | 8700 |     } | 
| Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8701 |     kind = PyUnicode_KIND(unicode); | 
 | 8702 |     data = PyUnicode_DATA(unicode); | 
 | 8703 |  | 
| Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8704 |     for (i=0; i < length; ) { | 
| Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8705 |         PyObject *exc; | 
 | 8706 |         Py_UCS4 ch; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8707 |         int decimal; | 
| Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8708 |         Py_ssize_t startpos; | 
 | 8709 |  | 
 | 8710 |         ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8711 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8712 |         if (Py_UNICODE_ISSPACE(ch)) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8713 |             *output++ = ' '; | 
| Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8714 |             i++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8715 |             continue; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8716 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8717 |         decimal = Py_UNICODE_TODECIMAL(ch); | 
 | 8718 |         if (decimal >= 0) { | 
 | 8719 |             *output++ = '0' + decimal; | 
| Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8720 |             i++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8721 |             continue; | 
 | 8722 |         } | 
 | 8723 |         if (0 < ch && ch < 256) { | 
 | 8724 |             *output++ = (char)ch; | 
| Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8725 |             i++; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8726 |             continue; | 
 | 8727 |         } | 
| Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8728 |  | 
| Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8729 |         startpos = i; | 
| Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8730 |         exc = NULL; | 
 | 8731 |         raise_encode_exception(&exc, "decimal", unicode, | 
 | 8732 |                                startpos, startpos+1, | 
 | 8733 |                                "invalid decimal Unicode string"); | 
 | 8734 |         Py_XDECREF(exc); | 
 | 8735 |         Py_DECREF(unicode); | 
 | 8736 |         return -1; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8737 |     } | 
 | 8738 |     /* 0-terminate the output string */ | 
 | 8739 |     *output++ = '\0'; | 
| Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8740 |     Py_DECREF(unicode); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8741 |     return 0; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8742 | } | 
 | 8743 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8744 | /* --- Helpers ------------------------------------------------------------ */ | 
 | 8745 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | static Py_ssize_t | 
| Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8747 | any_find_slice(int direction, PyObject* s1, PyObject* s2, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8748 |                Py_ssize_t start, | 
 | 8749 |                Py_ssize_t end) | 
 | 8750 | { | 
 | 8751 |     int kind1, kind2, kind; | 
 | 8752 |     void *buf1, *buf2; | 
 | 8753 |     Py_ssize_t len1, len2, result; | 
 | 8754 |  | 
 | 8755 |     kind1 = PyUnicode_KIND(s1); | 
 | 8756 |     kind2 = PyUnicode_KIND(s2); | 
 | 8757 |     kind = kind1 > kind2 ? kind1 : kind2; | 
 | 8758 |     buf1 = PyUnicode_DATA(s1); | 
 | 8759 |     buf2 = PyUnicode_DATA(s2); | 
 | 8760 |     if (kind1 != kind) | 
 | 8761 |         buf1 = _PyUnicode_AsKind(s1, kind); | 
 | 8762 |     if (!buf1) | 
 | 8763 |         return -2; | 
 | 8764 |     if (kind2 != kind) | 
 | 8765 |         buf2 = _PyUnicode_AsKind(s2, kind); | 
 | 8766 |     if (!buf2) { | 
 | 8767 |         if (kind1 != kind) PyMem_Free(buf1); | 
 | 8768 |         return -2; | 
 | 8769 |     } | 
 | 8770 |     len1 = PyUnicode_GET_LENGTH(s1); | 
 | 8771 |     len2 = PyUnicode_GET_LENGTH(s2); | 
 | 8772 |  | 
| Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8773 |     if (direction > 0) { | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8774 |         switch (kind) { | 
| Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8775 |         case PyUnicode_1BYTE_KIND: | 
 | 8776 |             if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) | 
 | 8777 |                 result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); | 
 | 8778 |             else | 
 | 8779 |                 result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); | 
 | 8780 |             break; | 
 | 8781 |         case PyUnicode_2BYTE_KIND: | 
 | 8782 |             result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); | 
 | 8783 |             break; | 
 | 8784 |         case PyUnicode_4BYTE_KIND: | 
 | 8785 |             result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); | 
 | 8786 |             break; | 
 | 8787 |         default: | 
 | 8788 |             assert(0); result = -2; | 
 | 8789 |         } | 
 | 8790 |     } | 
 | 8791 |     else { | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8792 |         switch (kind) { | 
| Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8793 |         case PyUnicode_1BYTE_KIND: | 
 | 8794 |             if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) | 
 | 8795 |                 result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); | 
 | 8796 |             else | 
 | 8797 |                 result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); | 
 | 8798 |             break; | 
 | 8799 |         case PyUnicode_2BYTE_KIND: | 
 | 8800 |             result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); | 
 | 8801 |             break; | 
 | 8802 |         case PyUnicode_4BYTE_KIND: | 
 | 8803 |             result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); | 
 | 8804 |             break; | 
 | 8805 |         default: | 
 | 8806 |             assert(0); result = -2; | 
 | 8807 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8808 |     } | 
 | 8809 |  | 
 | 8810 |     if (kind1 != kind) | 
 | 8811 |         PyMem_Free(buf1); | 
 | 8812 |     if (kind2 != kind) | 
 | 8813 |         PyMem_Free(buf2); | 
 | 8814 |  | 
 | 8815 |     return result; | 
 | 8816 | } | 
 | 8817 |  | 
 | 8818 | Py_ssize_t | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8819 | _PyUnicode_InsertThousandsGrouping( | 
 | 8820 |     PyObject *unicode, Py_ssize_t index, | 
 | 8821 |     Py_ssize_t n_buffer, | 
 | 8822 |     void *digits, Py_ssize_t n_digits, | 
 | 8823 |     Py_ssize_t min_width, | 
 | 8824 |     const char *grouping, PyObject *thousands_sep, | 
 | 8825 |     Py_UCS4 *maxchar) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8826 | { | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8827 |     unsigned int kind, thousands_sep_kind; | 
| Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8828 |     char *data, *thousands_sep_data; | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8829 |     Py_ssize_t thousands_sep_len; | 
 | 8830 |     Py_ssize_t len; | 
 | 8831 |  | 
 | 8832 |     if (unicode != NULL) { | 
 | 8833 |         kind = PyUnicode_KIND(unicode); | 
| Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8834 |         data = (char *) PyUnicode_DATA(unicode) + index * kind; | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8835 |     } | 
 | 8836 |     else { | 
 | 8837 |         kind = PyUnicode_1BYTE_KIND; | 
 | 8838 |         data = NULL; | 
 | 8839 |     } | 
 | 8840 |     thousands_sep_kind = PyUnicode_KIND(thousands_sep); | 
 | 8841 |     thousands_sep_data = PyUnicode_DATA(thousands_sep); | 
 | 8842 |     thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); | 
 | 8843 |     if (unicode != NULL && thousands_sep_kind != kind) { | 
| Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8844 |         if (thousands_sep_kind < kind) { | 
 | 8845 |             thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); | 
 | 8846 |             if (!thousands_sep_data) | 
 | 8847 |                 return -1; | 
 | 8848 |         } | 
 | 8849 |         else { | 
 | 8850 |             data = _PyUnicode_AsKind(unicode, thousands_sep_kind); | 
 | 8851 |             if (!data) | 
 | 8852 |                 return -1; | 
 | 8853 |         } | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8854 |     } | 
 | 8855 |  | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8856 |     switch (kind) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8857 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8858 |         if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8859 |             len = asciilib_InsertThousandsGrouping( | 
| Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8860 |                 (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8861 |                 min_width, grouping, | 
| Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8862 |                 (Py_UCS1 *) thousands_sep_data, thousands_sep_len); | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8863 |         else | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8864 |             len = ucs1lib_InsertThousandsGrouping( | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8865 |                 (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8866 |                 min_width, grouping, | 
| Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8867 |                 (Py_UCS1 *) thousands_sep_data, thousands_sep_len); | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8868 |         break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8869 |     case PyUnicode_2BYTE_KIND: | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8870 |         len = ucs2lib_InsertThousandsGrouping( | 
| Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8871 |             (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8872 |             min_width, grouping, | 
| Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8873 |             (Py_UCS2 *) thousands_sep_data, thousands_sep_len); | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8874 |         break; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8875 |     case PyUnicode_4BYTE_KIND: | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8876 |         len = ucs4lib_InsertThousandsGrouping( | 
| Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8877 |             (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8878 |             min_width, grouping, | 
| Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8879 |             (Py_UCS4 *) thousands_sep_data, thousands_sep_len); | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8880 |         break; | 
 | 8881 |     default: | 
 | 8882 |         assert(0); | 
 | 8883 |         return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8884 |     } | 
| Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8885 |     if (unicode != NULL && thousands_sep_kind != kind) { | 
 | 8886 |         if (thousands_sep_kind < kind) | 
 | 8887 |             PyMem_Free(thousands_sep_data); | 
 | 8888 |         else | 
 | 8889 |             PyMem_Free(data); | 
 | 8890 |     } | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8891 |     if (unicode == NULL) { | 
 | 8892 |         *maxchar = 127; | 
 | 8893 |         if (len != n_digits) { | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8894 |             *maxchar = Py_MAX(*maxchar, | 
| Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8895 |                                    PyUnicode_MAX_CHAR_VALUE(thousands_sep)); | 
| Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8896 |         } | 
 | 8897 |     } | 
 | 8898 |     return len; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8899 | } | 
 | 8900 |  | 
 | 8901 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8902 | /* helper macro to fixup start/end slice values */ | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8903 | #define ADJUST_INDICES(start, end, len)         \ | 
 | 8904 |     if (end > len)                              \ | 
 | 8905 |         end = len;                              \ | 
 | 8906 |     else if (end < 0) {                         \ | 
 | 8907 |         end += len;                             \ | 
 | 8908 |         if (end < 0)                            \ | 
 | 8909 |             end = 0;                            \ | 
 | 8910 |     }                                           \ | 
 | 8911 |     if (start < 0) {                            \ | 
 | 8912 |         start += len;                           \ | 
 | 8913 |         if (start < 0)                          \ | 
 | 8914 |             start = 0;                          \ | 
 | 8915 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8916 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8917 | Py_ssize_t | 
 | 8918 | PyUnicode_Count(PyObject *str, | 
 | 8919 |                 PyObject *substr, | 
 | 8920 |                 Py_ssize_t start, | 
 | 8921 |                 Py_ssize_t end) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8922 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8923 |     Py_ssize_t result; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8924 |     PyObject* str_obj; | 
 | 8925 |     PyObject* sub_obj; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8926 |     int kind1, kind2, kind; | 
 | 8927 |     void *buf1 = NULL, *buf2 = NULL; | 
 | 8928 |     Py_ssize_t len1, len2; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8929 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8930 |     str_obj = PyUnicode_FromObject(str); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8931 |     if (!str_obj) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8932 |         return -1; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8933 |     sub_obj = PyUnicode_FromObject(substr); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8934 |     if (!sub_obj) { | 
 | 8935 |         Py_DECREF(str_obj); | 
 | 8936 |         return -1; | 
 | 8937 |     } | 
| Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8938 |     if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { | 
| Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8939 |         Py_DECREF(sub_obj); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8940 |         Py_DECREF(str_obj); | 
 | 8941 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8942 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8943 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8944 |     kind1 = PyUnicode_KIND(str_obj); | 
 | 8945 |     kind2 = PyUnicode_KIND(sub_obj); | 
| Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8946 |     kind = kind1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8947 |     buf1 = PyUnicode_DATA(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8948 |     buf2 = PyUnicode_DATA(sub_obj); | 
| Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8949 |     if (kind2 != kind) { | 
| Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8950 |         if (kind2 > kind) { | 
 | 8951 |             Py_DECREF(sub_obj); | 
 | 8952 |             Py_DECREF(str_obj); | 
| Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8953 |             return 0; | 
| Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8954 |         } | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8955 |         buf2 = _PyUnicode_AsKind(sub_obj, kind); | 
| Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8956 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8957 |     if (!buf2) | 
 | 8958 |         goto onError; | 
 | 8959 |     len1 = PyUnicode_GET_LENGTH(str_obj); | 
 | 8960 |     len2 = PyUnicode_GET_LENGTH(sub_obj); | 
 | 8961 |  | 
 | 8962 |     ADJUST_INDICES(start, end, len1); | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8963 |     switch (kind) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8964 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8965 |         if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) | 
 | 8966 |             result = asciilib_count( | 
 | 8967 |                 ((Py_UCS1*)buf1) + start, end - start, | 
 | 8968 |                 buf2, len2, PY_SSIZE_T_MAX | 
 | 8969 |                 ); | 
 | 8970 |         else | 
 | 8971 |             result = ucs1lib_count( | 
 | 8972 |                 ((Py_UCS1*)buf1) + start, end - start, | 
 | 8973 |                 buf2, len2, PY_SSIZE_T_MAX | 
 | 8974 |                 ); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8975 |         break; | 
 | 8976 |     case PyUnicode_2BYTE_KIND: | 
 | 8977 |         result = ucs2lib_count( | 
 | 8978 |             ((Py_UCS2*)buf1) + start, end - start, | 
 | 8979 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 8980 |             ); | 
 | 8981 |         break; | 
 | 8982 |     case PyUnicode_4BYTE_KIND: | 
 | 8983 |         result = ucs4lib_count( | 
 | 8984 |             ((Py_UCS4*)buf1) + start, end - start, | 
 | 8985 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 8986 |             ); | 
 | 8987 |         break; | 
 | 8988 |     default: | 
 | 8989 |         assert(0); result = 0; | 
 | 8990 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8991 |  | 
 | 8992 |     Py_DECREF(sub_obj); | 
 | 8993 |     Py_DECREF(str_obj); | 
 | 8994 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8995 |     if (kind2 != kind) | 
 | 8996 |         PyMem_Free(buf2); | 
 | 8997 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8998 |     return result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8999 |   onError: | 
 | 9000 |     Py_DECREF(sub_obj); | 
 | 9001 |     Py_DECREF(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9002 |     if (kind2 != kind && buf2) | 
 | 9003 |         PyMem_Free(buf2); | 
 | 9004 |     return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9005 | } | 
 | 9006 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9007 | Py_ssize_t | 
 | 9008 | PyUnicode_Find(PyObject *str, | 
 | 9009 |                PyObject *sub, | 
 | 9010 |                Py_ssize_t start, | 
 | 9011 |                Py_ssize_t end, | 
 | 9012 |                int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9013 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9014 |     Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9015 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9016 |     str = PyUnicode_FromObject(str); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9017 |     if (!str) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9018 |         return -2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9019 |     sub = PyUnicode_FromObject(sub); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9020 |     if (!sub) { | 
 | 9021 |         Py_DECREF(str); | 
 | 9022 |         return -2; | 
 | 9023 |     } | 
 | 9024 |     if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { | 
 | 9025 |         Py_DECREF(sub); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9026 |         Py_DECREF(str); | 
 | 9027 |         return -2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9029 |  | 
| Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9030 |     result = any_find_slice(direction, | 
 | 9031 |         str, sub, start, end | 
 | 9032 |         ); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9033 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 |     Py_DECREF(str); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9035 |     Py_DECREF(sub); | 
 | 9036 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9037 |     return result; | 
 | 9038 | } | 
 | 9039 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9040 | Py_ssize_t | 
 | 9041 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, | 
 | 9042 |                    Py_ssize_t start, Py_ssize_t end, | 
 | 9043 |                    int direction) | 
 | 9044 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9045 |     int kind; | 
| Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9046 |     Py_ssize_t result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9047 |     if (PyUnicode_READY(str) == -1) | 
 | 9048 |         return -2; | 
| Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9049 |     if (start < 0 || end < 0) { | 
 | 9050 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 9051 |         return -2; | 
 | 9052 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9053 |     if (end > PyUnicode_GET_LENGTH(str)) | 
 | 9054 |         end = PyUnicode_GET_LENGTH(str); | 
 | 9055 |     kind = PyUnicode_KIND(str); | 
| Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9056 |     result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, | 
 | 9057 |                       kind, end-start, ch, direction); | 
 | 9058 |     if (result == -1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9059 |         return -1; | 
| Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9060 |     else | 
 | 9061 |         return start + result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9062 | } | 
 | 9063 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9064 | static int | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9065 | tailmatch(PyObject *self, | 
 | 9066 |           PyObject *substring, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9067 |           Py_ssize_t start, | 
 | 9068 |           Py_ssize_t end, | 
 | 9069 |           int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9070 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9071 |     int kind_self; | 
 | 9072 |     int kind_sub; | 
 | 9073 |     void *data_self; | 
 | 9074 |     void *data_sub; | 
 | 9075 |     Py_ssize_t offset; | 
 | 9076 |     Py_ssize_t i; | 
 | 9077 |     Py_ssize_t end_sub; | 
 | 9078 |  | 
 | 9079 |     if (PyUnicode_READY(self) == -1 || | 
 | 9080 |         PyUnicode_READY(substring) == -1) | 
| Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9081 |         return -1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9082 |  | 
 | 9083 |     if (PyUnicode_GET_LENGTH(substring) == 0) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9084 |         return 1; | 
 | 9085 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9086 |     ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); | 
 | 9087 |     end -= PyUnicode_GET_LENGTH(substring); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9088 |     if (end < start) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9089 |         return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9091 |     kind_self = PyUnicode_KIND(self); | 
 | 9092 |     data_self = PyUnicode_DATA(self); | 
 | 9093 |     kind_sub = PyUnicode_KIND(substring); | 
 | 9094 |     data_sub = PyUnicode_DATA(substring); | 
 | 9095 |     end_sub = PyUnicode_GET_LENGTH(substring) - 1; | 
 | 9096 |  | 
 | 9097 |     if (direction > 0) | 
 | 9098 |         offset = end; | 
 | 9099 |     else | 
 | 9100 |         offset = start; | 
 | 9101 |  | 
 | 9102 |     if (PyUnicode_READ(kind_self, data_self, offset) == | 
 | 9103 |         PyUnicode_READ(kind_sub, data_sub, 0) && | 
 | 9104 |         PyUnicode_READ(kind_self, data_self, offset + end_sub) == | 
 | 9105 |         PyUnicode_READ(kind_sub, data_sub, end_sub)) { | 
 | 9106 |         /* If both are of the same kind, memcmp is sufficient */ | 
 | 9107 |         if (kind_self == kind_sub) { | 
 | 9108 |             return ! memcmp((char *)data_self + | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9109 |                                 (offset * PyUnicode_KIND(substring)), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9110 |                             data_sub, | 
 | 9111 |                             PyUnicode_GET_LENGTH(substring) * | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9112 |                                 PyUnicode_KIND(substring)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9113 |         } | 
 | 9114 |         /* otherwise we have to compare each character by first accesing it */ | 
 | 9115 |         else { | 
 | 9116 |             /* We do not need to compare 0 and len(substring)-1 because | 
 | 9117 |                the if statement above ensured already that they are equal | 
 | 9118 |                when we end up here. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9119 |             for (i = 1; i < end_sub; ++i) { | 
 | 9120 |                 if (PyUnicode_READ(kind_self, data_self, offset + i) != | 
 | 9121 |                     PyUnicode_READ(kind_sub, data_sub, i)) | 
 | 9122 |                     return 0; | 
 | 9123 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9124 |             return 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9125 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9126 |     } | 
 | 9127 |  | 
 | 9128 |     return 0; | 
 | 9129 | } | 
 | 9130 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9131 | Py_ssize_t | 
 | 9132 | PyUnicode_Tailmatch(PyObject *str, | 
 | 9133 |                     PyObject *substr, | 
 | 9134 |                     Py_ssize_t start, | 
 | 9135 |                     Py_ssize_t end, | 
 | 9136 |                     int direction) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9137 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9138 |     Py_ssize_t result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9139 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9140 |     str = PyUnicode_FromObject(str); | 
 | 9141 |     if (str == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9142 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9143 |     substr = PyUnicode_FromObject(substr); | 
 | 9144 |     if (substr == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9145 |         Py_DECREF(str); | 
 | 9146 |         return -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9147 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9148 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9149 |     result = tailmatch(str, substr, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9150 |                        start, end, direction); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9151 |     Py_DECREF(str); | 
 | 9152 |     Py_DECREF(substr); | 
 | 9153 |     return result; | 
 | 9154 | } | 
 | 9155 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9156 | /* Apply fixfct filter to the Unicode object self and return a | 
 | 9157 |    reference to the modified object */ | 
 | 9158 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9159 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9160 | fixup(PyObject *self, | 
 | 9161 |       Py_UCS4 (*fixfct)(PyObject *s)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9162 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9163 |     PyObject *u; | 
 | 9164 |     Py_UCS4 maxchar_old, maxchar_new = 0; | 
| Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9165 |     PyObject *v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9166 |  | 
| Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9167 |     u = _PyUnicode_Copy(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9168 |     if (u == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9169 |         return NULL; | 
| Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9170 |     maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9171 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9172 |     /* fix functions return the new maximum character in a string, | 
 | 9173 |        if the kind of the resulting unicode object does not change, | 
 | 9174 |        everything is fine.  Otherwise we need to change the string kind | 
 | 9175 |        and re-run the fix function. */ | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9176 |     maxchar_new = fixfct(u); | 
| Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9177 |  | 
 | 9178 |     if (maxchar_new == 0) { | 
 | 9179 |         /* no changes */; | 
 | 9180 |         if (PyUnicode_CheckExact(self)) { | 
 | 9181 |             Py_DECREF(u); | 
 | 9182 |             Py_INCREF(self); | 
 | 9183 |             return self; | 
 | 9184 |         } | 
 | 9185 |         else | 
 | 9186 |             return u; | 
 | 9187 |     } | 
 | 9188 |  | 
| Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9189 |     maxchar_new = align_maxchar(maxchar_new); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9190 |  | 
| Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9191 |     if (maxchar_new == maxchar_old) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9192 |         return u; | 
| Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9193 |  | 
 | 9194 |     /* In case the maximum character changed, we need to | 
 | 9195 |        convert the string to the new category. */ | 
 | 9196 |     v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); | 
 | 9197 |     if (v == NULL) { | 
 | 9198 |         Py_DECREF(u); | 
 | 9199 |         return NULL; | 
 | 9200 |     } | 
 | 9201 |     if (maxchar_new > maxchar_old) { | 
 | 9202 |         /* If the maxchar increased so that the kind changed, not all | 
 | 9203 |            characters are representable anymore and we need to fix the | 
 | 9204 |            string again. This only happens in very few cases. */ | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9205 |         _PyUnicode_FastCopyCharacters(v, 0, | 
 | 9206 |                                       self, 0, PyUnicode_GET_LENGTH(self)); | 
| Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9207 |         maxchar_old = fixfct(v); | 
 | 9208 |         assert(maxchar_old > 0 && maxchar_old <= maxchar_new); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9209 |     } | 
 | 9210 |     else { | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9211 |         _PyUnicode_FastCopyCharacters(v, 0, | 
 | 9212 |                                       u, 0, PyUnicode_GET_LENGTH(self)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9213 |     } | 
| Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9214 |     Py_DECREF(u); | 
 | 9215 |     assert(_PyUnicode_CheckConsistency(v, 1)); | 
 | 9216 |     return v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9217 | } | 
 | 9218 |  | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9219 | static PyObject * | 
 | 9220 | ascii_upper_or_lower(PyObject *self, int lower) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9221 | { | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9222 |     Py_ssize_t len = PyUnicode_GET_LENGTH(self); | 
 | 9223 |     char *resdata, *data = PyUnicode_DATA(self); | 
 | 9224 |     PyObject *res; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9225 |  | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9226 |     res = PyUnicode_New(len, 127); | 
 | 9227 |     if (res == NULL) | 
 | 9228 |         return NULL; | 
 | 9229 |     resdata = PyUnicode_DATA(res); | 
 | 9230 |     if (lower) | 
 | 9231 |         _Py_bytes_lower(resdata, data, len); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9232 |     else | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9233 |         _Py_bytes_upper(resdata, data, len); | 
 | 9234 |     return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9235 | } | 
 | 9236 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9237 | static Py_UCS4 | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9238 | handle_capital_sigma(int kind, void *data, Py_ssize_t length, Py_ssize_t i) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9239 | { | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9240 |     Py_ssize_t j; | 
 | 9241 |     int final_sigma; | 
 | 9242 |     Py_UCS4 c; | 
 | 9243 |     /* U+03A3 is in the Final_Sigma context when, it is found like this: | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9244 |  | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9245 |      \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) | 
 | 9246 |  | 
 | 9247 |     where ! is a negation and \p{xxx} is a character with property xxx. | 
 | 9248 |     */ | 
 | 9249 |     for (j = i - 1; j >= 0; j--) { | 
 | 9250 |         c = PyUnicode_READ(kind, data, j); | 
 | 9251 |         if (!_PyUnicode_IsCaseIgnorable(c)) | 
 | 9252 |             break; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9253 |     } | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9254 |     final_sigma = j >= 0 && _PyUnicode_IsCased(c); | 
 | 9255 |     if (final_sigma) { | 
 | 9256 |         for (j = i + 1; j < length; j++) { | 
 | 9257 |             c = PyUnicode_READ(kind, data, j); | 
 | 9258 |             if (!_PyUnicode_IsCaseIgnorable(c)) | 
 | 9259 |                 break; | 
 | 9260 |         } | 
 | 9261 |         final_sigma = j == length || !_PyUnicode_IsCased(c); | 
 | 9262 |     } | 
 | 9263 |     return (final_sigma) ? 0x3C2 : 0x3C3; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9264 | } | 
 | 9265 |  | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9266 | static int | 
 | 9267 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, | 
 | 9268 |            Py_UCS4 c, Py_UCS4 *mapped) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9269 | { | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9270 |     /* Obscure special case. */ | 
 | 9271 |     if (c == 0x3A3) { | 
 | 9272 |         mapped[0] = handle_capital_sigma(kind, data, length, i); | 
 | 9273 |         return 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9274 |     } | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9275 |     return _PyUnicode_ToLowerFull(c, mapped); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9276 | } | 
 | 9277 |  | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9278 | static Py_ssize_t | 
 | 9279 | do_capitalize(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9280 | { | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9281 |     Py_ssize_t i, k = 0; | 
 | 9282 |     int n_res, j; | 
 | 9283 |     Py_UCS4 c, mapped[3]; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9284 |  | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9285 |     c = PyUnicode_READ(kind, data, 0); | 
 | 9286 |     n_res = _PyUnicode_ToUpperFull(c, mapped); | 
 | 9287 |     for (j = 0; j < n_res; j++) { | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9288 |         *maxchar = Py_MAX(*maxchar, mapped[j]); | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9289 |         res[k++] = mapped[j]; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9290 |     } | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9291 |     for (i = 1; i < length; i++) { | 
 | 9292 |         c = PyUnicode_READ(kind, data, i); | 
 | 9293 |         n_res = lower_ucs4(kind, data, length, i, c, mapped); | 
 | 9294 |         for (j = 0; j < n_res; j++) { | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9295 |             *maxchar = Py_MAX(*maxchar, mapped[j]); | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9296 |             res[k++] = mapped[j]; | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9297 |         } | 
| Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9298 |     } | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9299 |     return k; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9300 | } | 
 | 9301 |  | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9302 | static Py_ssize_t | 
 | 9303 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { | 
 | 9304 |     Py_ssize_t i, k = 0; | 
 | 9305 |  | 
 | 9306 |     for (i = 0; i < length; i++) { | 
 | 9307 |         Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; | 
 | 9308 |         int n_res, j; | 
 | 9309 |         if (Py_UNICODE_ISUPPER(c)) { | 
 | 9310 |             n_res = lower_ucs4(kind, data, length, i, c, mapped); | 
 | 9311 |         } | 
 | 9312 |         else if (Py_UNICODE_ISLOWER(c)) { | 
 | 9313 |             n_res = _PyUnicode_ToUpperFull(c, mapped); | 
 | 9314 |         } | 
 | 9315 |         else { | 
 | 9316 |             n_res = 1; | 
 | 9317 |             mapped[0] = c; | 
 | 9318 |         } | 
 | 9319 |         for (j = 0; j < n_res; j++) { | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9320 |             *maxchar = Py_MAX(*maxchar, mapped[j]); | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9321 |             res[k++] = mapped[j]; | 
 | 9322 |         } | 
 | 9323 |     } | 
 | 9324 |     return k; | 
 | 9325 | } | 
 | 9326 |  | 
 | 9327 | static Py_ssize_t | 
 | 9328 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, | 
 | 9329 |                   Py_UCS4 *maxchar, int lower) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9330 | { | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9331 |     Py_ssize_t i, k = 0; | 
 | 9332 |  | 
 | 9333 |     for (i = 0; i < length; i++) { | 
 | 9334 |         Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; | 
 | 9335 |         int n_res, j; | 
 | 9336 |         if (lower) | 
 | 9337 |             n_res = lower_ucs4(kind, data, length, i, c, mapped); | 
 | 9338 |         else | 
 | 9339 |             n_res = _PyUnicode_ToUpperFull(c, mapped); | 
 | 9340 |         for (j = 0; j < n_res; j++) { | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9341 |             *maxchar = Py_MAX(*maxchar, mapped[j]); | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9342 |             res[k++] = mapped[j]; | 
 | 9343 |         } | 
 | 9344 |     } | 
 | 9345 |     return k; | 
 | 9346 | } | 
 | 9347 |  | 
 | 9348 | static Py_ssize_t | 
 | 9349 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) | 
 | 9350 | { | 
 | 9351 |     return do_upper_or_lower(kind, data, length, res, maxchar, 0); | 
 | 9352 | } | 
 | 9353 |  | 
 | 9354 | static Py_ssize_t | 
 | 9355 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) | 
 | 9356 | { | 
 | 9357 |     return do_upper_or_lower(kind, data, length, res, maxchar, 1); | 
 | 9358 | } | 
 | 9359 |  | 
| Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9360 | static Py_ssize_t | 
| Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9361 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) | 
 | 9362 | { | 
 | 9363 |     Py_ssize_t i, k = 0; | 
 | 9364 |  | 
 | 9365 |     for (i = 0; i < length; i++) { | 
 | 9366 |         Py_UCS4 c = PyUnicode_READ(kind, data, i); | 
 | 9367 |         Py_UCS4 mapped[3]; | 
 | 9368 |         int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); | 
 | 9369 |         for (j = 0; j < n_res; j++) { | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9370 |             *maxchar = Py_MAX(*maxchar, mapped[j]); | 
| Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9371 |             res[k++] = mapped[j]; | 
 | 9372 |         } | 
 | 9373 |     } | 
 | 9374 |     return k; | 
 | 9375 | } | 
 | 9376 |  | 
 | 9377 | static Py_ssize_t | 
| Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9378 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) | 
 | 9379 | { | 
 | 9380 |     Py_ssize_t i, k = 0; | 
 | 9381 |     int previous_is_cased; | 
 | 9382 |  | 
 | 9383 |     previous_is_cased = 0; | 
 | 9384 |     for (i = 0; i < length; i++) { | 
 | 9385 |         const Py_UCS4 c = PyUnicode_READ(kind, data, i); | 
 | 9386 |         Py_UCS4 mapped[3]; | 
 | 9387 |         int n_res, j; | 
 | 9388 |  | 
 | 9389 |         if (previous_is_cased) | 
 | 9390 |             n_res = lower_ucs4(kind, data, length, i, c, mapped); | 
 | 9391 |         else | 
 | 9392 |             n_res = _PyUnicode_ToTitleFull(c, mapped); | 
 | 9393 |  | 
 | 9394 |         for (j = 0; j < n_res; j++) { | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9395 |             *maxchar = Py_MAX(*maxchar, mapped[j]); | 
| Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9396 |             res[k++] = mapped[j]; | 
 | 9397 |         } | 
 | 9398 |  | 
 | 9399 |         previous_is_cased = _PyUnicode_IsCased(c); | 
 | 9400 |     } | 
 | 9401 |     return k; | 
 | 9402 | } | 
 | 9403 |  | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9404 | static PyObject * | 
 | 9405 | case_operation(PyObject *self, | 
 | 9406 |                Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) | 
 | 9407 | { | 
 | 9408 |     PyObject *res = NULL; | 
 | 9409 |     Py_ssize_t length, newlength = 0; | 
 | 9410 |     int kind, outkind; | 
 | 9411 |     void *data, *outdata; | 
 | 9412 |     Py_UCS4 maxchar = 0, *tmp, *tmpend; | 
 | 9413 |  | 
| Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9414 |     assert(PyUnicode_IS_READY(self)); | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9415 |  | 
 | 9416 |     kind = PyUnicode_KIND(self); | 
 | 9417 |     data = PyUnicode_DATA(self); | 
 | 9418 |     length = PyUnicode_GET_LENGTH(self); | 
 | 9419 |     tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); | 
 | 9420 |     if (tmp == NULL) | 
 | 9421 |         return PyErr_NoMemory(); | 
 | 9422 |     newlength = perform(kind, data, length, tmp, &maxchar); | 
 | 9423 |     res = PyUnicode_New(newlength, maxchar); | 
 | 9424 |     if (res == NULL) | 
 | 9425 |         goto leave; | 
 | 9426 |     tmpend = tmp + newlength; | 
 | 9427 |     outdata = PyUnicode_DATA(res); | 
 | 9428 |     outkind = PyUnicode_KIND(res); | 
 | 9429 |     switch (outkind) { | 
 | 9430 |     case PyUnicode_1BYTE_KIND: | 
 | 9431 |         _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); | 
 | 9432 |         break; | 
 | 9433 |     case PyUnicode_2BYTE_KIND: | 
 | 9434 |         _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); | 
 | 9435 |         break; | 
 | 9436 |     case PyUnicode_4BYTE_KIND: | 
 | 9437 |         memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); | 
 | 9438 |         break; | 
 | 9439 |     default: | 
 | 9440 |         assert(0); | 
 | 9441 |         break; | 
 | 9442 |     } | 
 | 9443 |   leave: | 
 | 9444 |     PyMem_FREE(tmp); | 
 | 9445 |     return res; | 
 | 9446 | } | 
 | 9447 |  | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9448 | PyObject * | 
 | 9449 | PyUnicode_Join(PyObject *separator, PyObject *seq) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9450 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9451 |     PyObject *sep = NULL; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9452 |     Py_ssize_t seplen; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9453 |     PyObject *res = NULL; /* the result */ | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9454 |     PyObject *fseq;          /* PySequence_Fast(seq) */ | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9455 |     Py_ssize_t seqlen;       /* len(fseq) -- number of items in sequence */ | 
 | 9456 |     PyObject **items; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9457 |     PyObject *item; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9458 |     Py_ssize_t sz, i, res_offset; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9459 |     Py_UCS4 maxchar; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9460 |     Py_UCS4 item_maxchar; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9461 |     int use_memcpy; | 
 | 9462 |     unsigned char *res_data = NULL, *sep_data = NULL; | 
 | 9463 |     PyObject *last_obj; | 
 | 9464 |     unsigned int kind = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9465 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9466 |     fseq = PySequence_Fast(seq, ""); | 
 | 9467 |     if (fseq == NULL) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9468 |         return NULL; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9469 |     } | 
 | 9470 |  | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9471 |     /* NOTE: the following code can't call back into Python code, | 
 | 9472 |      * so we are sure that fseq won't be mutated. | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9473 |      */ | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9474 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9475 |     seqlen = PySequence_Fast_GET_SIZE(fseq); | 
 | 9476 |     /* If empty sequence, return u"". */ | 
 | 9477 |     if (seqlen == 0) { | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9478 |         Py_DECREF(fseq); | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9479 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9480 |     } | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9481 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9482 |     /* If singleton sequence with an exact Unicode, return that. */ | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9483 |     last_obj = NULL; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9484 |     items = PySequence_Fast_ITEMS(fseq); | 
| Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9485 |     if (seqlen == 1) { | 
 | 9486 |         if (PyUnicode_CheckExact(items[0])) { | 
 | 9487 |             res = items[0]; | 
 | 9488 |             Py_INCREF(res); | 
 | 9489 |             Py_DECREF(fseq); | 
 | 9490 |             return res; | 
 | 9491 |         } | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9492 |         seplen = 0; | 
| Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9493 |         maxchar = 0; | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9494 |     } | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9495 |     else { | 
| Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9496 |         /* Set up sep and seplen */ | 
 | 9497 |         if (separator == NULL) { | 
 | 9498 |             /* fall back to a blank space separator */ | 
 | 9499 |             sep = PyUnicode_FromOrdinal(' '); | 
 | 9500 |             if (!sep) | 
 | 9501 |                 goto onError; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9502 |             seplen = 1; | 
| Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9503 |             maxchar = 32; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9504 |         } | 
| Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9505 |         else { | 
 | 9506 |             if (!PyUnicode_Check(separator)) { | 
 | 9507 |                 PyErr_Format(PyExc_TypeError, | 
 | 9508 |                              "separator: expected str instance," | 
 | 9509 |                              " %.80s found", | 
 | 9510 |                              Py_TYPE(separator)->tp_name); | 
 | 9511 |                 goto onError; | 
 | 9512 |             } | 
 | 9513 |             if (PyUnicode_READY(separator)) | 
 | 9514 |                 goto onError; | 
 | 9515 |             sep = separator; | 
 | 9516 |             seplen = PyUnicode_GET_LENGTH(separator); | 
 | 9517 |             maxchar = PyUnicode_MAX_CHAR_VALUE(separator); | 
 | 9518 |             /* inc refcount to keep this code path symmetric with the | 
 | 9519 |                above case of a blank separator */ | 
 | 9520 |             Py_INCREF(sep); | 
 | 9521 |         } | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9522 |         last_obj = sep; | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9523 |     } | 
 | 9524 |  | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9525 |     /* There are at least two things to join, or else we have a subclass | 
 | 9526 |      * of str in the sequence. | 
 | 9527 |      * Do a pre-pass to figure out the total amount of space we'll | 
 | 9528 |      * need (sz), and see whether all argument are strings. | 
 | 9529 |      */ | 
 | 9530 |     sz = 0; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9531 | #ifdef Py_DEBUG | 
 | 9532 |     use_memcpy = 0; | 
 | 9533 | #else | 
 | 9534 |     use_memcpy = 1; | 
 | 9535 | #endif | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9536 |     for (i = 0; i < seqlen; i++) { | 
 | 9537 |         const Py_ssize_t old_sz = sz; | 
 | 9538 |         item = items[i]; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9539 |         if (!PyUnicode_Check(item)) { | 
 | 9540 |             PyErr_Format(PyExc_TypeError, | 
 | 9541 |                          "sequence item %zd: expected str instance," | 
 | 9542 |                          " %.80s found", | 
 | 9543 |                          i, Py_TYPE(item)->tp_name); | 
 | 9544 |             goto onError; | 
 | 9545 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9546 |         if (PyUnicode_READY(item) == -1) | 
 | 9547 |             goto onError; | 
 | 9548 |         sz += PyUnicode_GET_LENGTH(item); | 
 | 9549 |         item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9550 |         maxchar = Py_MAX(maxchar, item_maxchar); | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9551 |         if (i != 0) | 
 | 9552 |             sz += seplen; | 
 | 9553 |         if (sz < old_sz || sz > PY_SSIZE_T_MAX) { | 
 | 9554 |             PyErr_SetString(PyExc_OverflowError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9555 |                             "join() result is too long for a Python string"); | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9556 |             goto onError; | 
 | 9557 |         } | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9558 |         if (use_memcpy && last_obj != NULL) { | 
 | 9559 |             if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) | 
 | 9560 |                 use_memcpy = 0; | 
 | 9561 |         } | 
 | 9562 |         last_obj = item; | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9563 |     } | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9564 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9565 |     res = PyUnicode_New(sz, maxchar); | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9566 |     if (res == NULL) | 
 | 9567 |         goto onError; | 
| Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9568 |  | 
| Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9569 |     /* Catenate everything. */ | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9570 | #ifdef Py_DEBUG | 
 | 9571 |     use_memcpy = 0; | 
 | 9572 | #else | 
 | 9573 |     if (use_memcpy) { | 
 | 9574 |         res_data = PyUnicode_1BYTE_DATA(res); | 
 | 9575 |         kind = PyUnicode_KIND(res); | 
 | 9576 |         if (seplen != 0) | 
 | 9577 |             sep_data = PyUnicode_1BYTE_DATA(sep); | 
 | 9578 |     } | 
 | 9579 | #endif | 
| Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9580 |     if (use_memcpy) { | 
 | 9581 |         for (i = 0; i < seqlen; ++i) { | 
 | 9582 |             Py_ssize_t itemlen; | 
 | 9583 |             item = items[i]; | 
 | 9584 |  | 
 | 9585 |             /* Copy item, and maybe the separator. */ | 
 | 9586 |             if (i && seplen != 0) { | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9587 |                 Py_MEMCPY(res_data, | 
 | 9588 |                           sep_data, | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9589 |                           kind * seplen); | 
 | 9590 |                 res_data += kind * seplen; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9591 |             } | 
| Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9592 |  | 
 | 9593 |             itemlen = PyUnicode_GET_LENGTH(item); | 
 | 9594 |             if (itemlen != 0) { | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9595 |                 Py_MEMCPY(res_data, | 
 | 9596 |                           PyUnicode_DATA(item), | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9597 |                           kind * itemlen); | 
 | 9598 |                 res_data += kind * itemlen; | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9599 |             } | 
| Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9600 |         } | 
 | 9601 |         assert(res_data == PyUnicode_1BYTE_DATA(res) | 
 | 9602 |                            + kind * PyUnicode_GET_LENGTH(res)); | 
 | 9603 |     } | 
 | 9604 |     else { | 
 | 9605 |         for (i = 0, res_offset = 0; i < seqlen; ++i) { | 
 | 9606 |             Py_ssize_t itemlen; | 
 | 9607 |             item = items[i]; | 
 | 9608 |  | 
 | 9609 |             /* Copy item, and maybe the separator. */ | 
 | 9610 |             if (i && seplen != 0) { | 
 | 9611 |                 _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); | 
 | 9612 |                 res_offset += seplen; | 
 | 9613 |             } | 
 | 9614 |  | 
 | 9615 |             itemlen = PyUnicode_GET_LENGTH(item); | 
 | 9616 |             if (itemlen != 0) { | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9617 |                 _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9618 |                 res_offset += itemlen; | 
 | 9619 |             } | 
| Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9620 |         } | 
| Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9621 |         assert(res_offset == PyUnicode_GET_LENGTH(res)); | 
| Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9622 |     } | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9623 |  | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9624 |     Py_DECREF(fseq); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9625 |     Py_XDECREF(sep); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9626 |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9627 |     return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9628 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9629 |   onError: | 
| Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9630 |     Py_DECREF(fseq); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9631 |     Py_XDECREF(sep); | 
| Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9632 |     Py_XDECREF(res); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9633 |     return NULL; | 
 | 9634 | } | 
 | 9635 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9636 | #define FILL(kind, data, value, start, length) \ | 
 | 9637 |     do { \ | 
 | 9638 |         Py_ssize_t i_ = 0; \ | 
 | 9639 |         assert(kind != PyUnicode_WCHAR_KIND); \ | 
 | 9640 |         switch ((kind)) { \ | 
 | 9641 |         case PyUnicode_1BYTE_KIND: { \ | 
 | 9642 |             unsigned char * to_ = (unsigned char *)((data)) + (start); \ | 
| Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9643 |             memset(to_, (unsigned char)value, (length)); \ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9644 |             break; \ | 
 | 9645 |         } \ | 
 | 9646 |         case PyUnicode_2BYTE_KIND: { \ | 
 | 9647 |             Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ | 
 | 9648 |             for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ | 
 | 9649 |             break; \ | 
 | 9650 |         } \ | 
| Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9651 |         case PyUnicode_4BYTE_KIND: { \ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9652 |             Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ | 
 | 9653 |             for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ | 
 | 9654 |             break; \ | 
| Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9655 |         default: assert(0); \ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9656 |         } \ | 
 | 9657 |         } \ | 
 | 9658 |     } while (0) | 
 | 9659 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9660 | void | 
 | 9661 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, | 
 | 9662 |                     Py_UCS4 fill_char) | 
 | 9663 | { | 
 | 9664 |     const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); | 
 | 9665 |     const void *data = PyUnicode_DATA(unicode); | 
 | 9666 |     assert(PyUnicode_IS_READY(unicode)); | 
 | 9667 |     assert(unicode_modifiable(unicode)); | 
 | 9668 |     assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); | 
 | 9669 |     assert(start >= 0); | 
 | 9670 |     assert(start + length <= PyUnicode_GET_LENGTH(unicode)); | 
 | 9671 |     FILL(kind, data, fill_char, start, length); | 
 | 9672 | } | 
 | 9673 |  | 
| Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9674 | Py_ssize_t | 
 | 9675 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, | 
 | 9676 |                Py_UCS4 fill_char) | 
 | 9677 | { | 
 | 9678 |     Py_ssize_t maxlen; | 
| Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9679 |  | 
 | 9680 |     if (!PyUnicode_Check(unicode)) { | 
 | 9681 |         PyErr_BadInternalCall(); | 
 | 9682 |         return -1; | 
 | 9683 |     } | 
 | 9684 |     if (PyUnicode_READY(unicode) == -1) | 
 | 9685 |         return -1; | 
 | 9686 |     if (unicode_check_modifiable(unicode)) | 
 | 9687 |         return -1; | 
 | 9688 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9689 |     if (start < 0) { | 
 | 9690 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 9691 |         return -1; | 
 | 9692 |     } | 
| Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9693 |     if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { | 
 | 9694 |         PyErr_SetString(PyExc_ValueError, | 
 | 9695 |                          "fill character is bigger than " | 
 | 9696 |                          "the string maximum character"); | 
 | 9697 |         return -1; | 
 | 9698 |     } | 
 | 9699 |  | 
 | 9700 |     maxlen = PyUnicode_GET_LENGTH(unicode) - start; | 
 | 9701 |     length = Py_MIN(maxlen, length); | 
 | 9702 |     if (length <= 0) | 
 | 9703 |         return 0; | 
 | 9704 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9705 |     _PyUnicode_FastFill(unicode, start, length, fill_char); | 
| Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9706 |     return length; | 
 | 9707 | } | 
 | 9708 |  | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9709 | static PyObject * | 
 | 9710 | pad(PyObject *self, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9711 |     Py_ssize_t left, | 
 | 9712 |     Py_ssize_t right, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9713 |     Py_UCS4 fill) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9714 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9715 |     PyObject *u; | 
 | 9716 |     Py_UCS4 maxchar; | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9717 |     int kind; | 
 | 9718 |     void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9719 |  | 
 | 9720 |     if (left < 0) | 
 | 9721 |         left = 0; | 
 | 9722 |     if (right < 0) | 
 | 9723 |         right = 0; | 
 | 9724 |  | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9725 |     if (left == 0 && right == 0) | 
 | 9726 |         return unicode_result_unchanged(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9727 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9728 |     if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || | 
 | 9729 |         right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { | 
| Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9730 |         PyErr_SetString(PyExc_OverflowError, "padded string is too long"); | 
 | 9731 |         return NULL; | 
 | 9732 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9733 |     maxchar = PyUnicode_MAX_CHAR_VALUE(self); | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9734 |     maxchar = Py_MAX(maxchar, fill); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9735 |     u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); | 
| Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9736 |     if (!u) | 
 | 9737 |         return NULL; | 
 | 9738 |  | 
 | 9739 |     kind = PyUnicode_KIND(u); | 
 | 9740 |     data = PyUnicode_DATA(u); | 
 | 9741 |     if (left) | 
 | 9742 |         FILL(kind, data, fill, 0, left); | 
 | 9743 |     if (right) | 
 | 9744 |         FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9745 |     _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9746 |     assert(_PyUnicode_CheckConsistency(u, 1)); | 
 | 9747 |     return u; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9748 | } | 
 | 9749 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9750 | PyObject * | 
 | 9751 | PyUnicode_Splitlines(PyObject *string, int keepends) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9752 | { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9753 |     PyObject *list; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9754 |  | 
 | 9755 |     string = PyUnicode_FromObject(string); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9756 |     if (string == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9757 |         return NULL; | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9758 |     if (PyUnicode_READY(string) == -1) { | 
 | 9759 |         Py_DECREF(string); | 
 | 9760 |         return NULL; | 
 | 9761 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9762 |  | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9763 |     switch (PyUnicode_KIND(string)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9764 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9765 |         if (PyUnicode_IS_ASCII(string)) | 
 | 9766 |             list = asciilib_splitlines( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9767 |                 string, PyUnicode_1BYTE_DATA(string), | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9768 |                 PyUnicode_GET_LENGTH(string), keepends); | 
 | 9769 |         else | 
 | 9770 |             list = ucs1lib_splitlines( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9771 |                 string, PyUnicode_1BYTE_DATA(string), | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9772 |                 PyUnicode_GET_LENGTH(string), keepends); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9773 |         break; | 
 | 9774 |     case PyUnicode_2BYTE_KIND: | 
 | 9775 |         list = ucs2lib_splitlines( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9776 |             string, PyUnicode_2BYTE_DATA(string), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9777 |             PyUnicode_GET_LENGTH(string), keepends); | 
 | 9778 |         break; | 
 | 9779 |     case PyUnicode_4BYTE_KIND: | 
 | 9780 |         list = ucs4lib_splitlines( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9781 |             string, PyUnicode_4BYTE_DATA(string), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9782 |             PyUnicode_GET_LENGTH(string), keepends); | 
 | 9783 |         break; | 
 | 9784 |     default: | 
 | 9785 |         assert(0); | 
 | 9786 |         list = 0; | 
 | 9787 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9788 |     Py_DECREF(string); | 
 | 9789 |     return list; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9790 | } | 
 | 9791 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9792 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9793 | split(PyObject *self, | 
 | 9794 |       PyObject *substring, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9795 |       Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9796 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9797 |     int kind1, kind2, kind; | 
 | 9798 |     void *buf1, *buf2; | 
 | 9799 |     Py_ssize_t len1, len2; | 
 | 9800 |     PyObject* out; | 
 | 9801 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9802 |     if (maxcount < 0) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9803 |         maxcount = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9804 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9805 |     if (PyUnicode_READY(self) == -1) | 
 | 9806 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9807 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9808 |     if (substring == NULL) | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9809 |         switch (PyUnicode_KIND(self)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9810 |         case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9811 |             if (PyUnicode_IS_ASCII(self)) | 
 | 9812 |                 return asciilib_split_whitespace( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9813 |                     self,  PyUnicode_1BYTE_DATA(self), | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9814 |                     PyUnicode_GET_LENGTH(self), maxcount | 
 | 9815 |                     ); | 
 | 9816 |             else | 
 | 9817 |                 return ucs1lib_split_whitespace( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9818 |                     self,  PyUnicode_1BYTE_DATA(self), | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9819 |                     PyUnicode_GET_LENGTH(self), maxcount | 
 | 9820 |                     ); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9821 |         case PyUnicode_2BYTE_KIND: | 
 | 9822 |             return ucs2lib_split_whitespace( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9823 |                 self,  PyUnicode_2BYTE_DATA(self), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9824 |                 PyUnicode_GET_LENGTH(self), maxcount | 
 | 9825 |                 ); | 
 | 9826 |         case PyUnicode_4BYTE_KIND: | 
 | 9827 |             return ucs4lib_split_whitespace( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9828 |                 self,  PyUnicode_4BYTE_DATA(self), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9829 |                 PyUnicode_GET_LENGTH(self), maxcount | 
 | 9830 |                 ); | 
 | 9831 |         default: | 
 | 9832 |             assert(0); | 
 | 9833 |             return NULL; | 
 | 9834 |         } | 
 | 9835 |  | 
 | 9836 |     if (PyUnicode_READY(substring) == -1) | 
 | 9837 |         return NULL; | 
 | 9838 |  | 
 | 9839 |     kind1 = PyUnicode_KIND(self); | 
 | 9840 |     kind2 = PyUnicode_KIND(substring); | 
 | 9841 |     kind = kind1 > kind2 ? kind1 : kind2; | 
 | 9842 |     buf1 = PyUnicode_DATA(self); | 
 | 9843 |     buf2 = PyUnicode_DATA(substring); | 
 | 9844 |     if (kind1 != kind) | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9845 |         buf1 = _PyUnicode_AsKind(self, kind); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9846 |     if (!buf1) | 
 | 9847 |         return NULL; | 
 | 9848 |     if (kind2 != kind) | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9849 |         buf2 = _PyUnicode_AsKind(substring, kind); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9850 |     if (!buf2) { | 
 | 9851 |         if (kind1 != kind) PyMem_Free(buf1); | 
 | 9852 |         return NULL; | 
 | 9853 |     } | 
 | 9854 |     len1 = PyUnicode_GET_LENGTH(self); | 
 | 9855 |     len2 = PyUnicode_GET_LENGTH(substring); | 
 | 9856 |  | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9857 |     switch (kind) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9858 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9859 |         if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) | 
 | 9860 |             out = asciilib_split( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9861 |                 self,  buf1, len1, buf2, len2, maxcount); | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9862 |         else | 
 | 9863 |             out = ucs1lib_split( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9864 |                 self,  buf1, len1, buf2, len2, maxcount); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9865 |         break; | 
 | 9866 |     case PyUnicode_2BYTE_KIND: | 
 | 9867 |         out = ucs2lib_split( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9868 |             self,  buf1, len1, buf2, len2, maxcount); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9869 |         break; | 
 | 9870 |     case PyUnicode_4BYTE_KIND: | 
 | 9871 |         out = ucs4lib_split( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9872 |             self,  buf1, len1, buf2, len2, maxcount); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9873 |         break; | 
 | 9874 |     default: | 
 | 9875 |         out = NULL; | 
 | 9876 |     } | 
 | 9877 |     if (kind1 != kind) | 
 | 9878 |         PyMem_Free(buf1); | 
 | 9879 |     if (kind2 != kind) | 
 | 9880 |         PyMem_Free(buf2); | 
 | 9881 |     return out; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9882 | } | 
 | 9883 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9884 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9885 | rsplit(PyObject *self, | 
 | 9886 |        PyObject *substring, | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9887 |        Py_ssize_t maxcount) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9888 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9889 |     int kind1, kind2, kind; | 
 | 9890 |     void *buf1, *buf2; | 
 | 9891 |     Py_ssize_t len1, len2; | 
 | 9892 |     PyObject* out; | 
 | 9893 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9894 |     if (maxcount < 0) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9895 |         maxcount = PY_SSIZE_T_MAX; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9896 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9897 |     if (PyUnicode_READY(self) == -1) | 
 | 9898 |         return NULL; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9899 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9900 |     if (substring == NULL) | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9901 |         switch (PyUnicode_KIND(self)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9902 |         case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9903 |             if (PyUnicode_IS_ASCII(self)) | 
 | 9904 |                 return asciilib_rsplit_whitespace( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9905 |                     self,  PyUnicode_1BYTE_DATA(self), | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9906 |                     PyUnicode_GET_LENGTH(self), maxcount | 
 | 9907 |                     ); | 
 | 9908 |             else | 
 | 9909 |                 return ucs1lib_rsplit_whitespace( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9910 |                     self,  PyUnicode_1BYTE_DATA(self), | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9911 |                     PyUnicode_GET_LENGTH(self), maxcount | 
 | 9912 |                     ); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9913 |         case PyUnicode_2BYTE_KIND: | 
 | 9914 |             return ucs2lib_rsplit_whitespace( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9915 |                 self,  PyUnicode_2BYTE_DATA(self), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9916 |                 PyUnicode_GET_LENGTH(self), maxcount | 
 | 9917 |                 ); | 
 | 9918 |         case PyUnicode_4BYTE_KIND: | 
 | 9919 |             return ucs4lib_rsplit_whitespace( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9920 |                 self,  PyUnicode_4BYTE_DATA(self), | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 |                 PyUnicode_GET_LENGTH(self), maxcount | 
 | 9922 |                 ); | 
 | 9923 |         default: | 
 | 9924 |             assert(0); | 
 | 9925 |             return NULL; | 
 | 9926 |         } | 
 | 9927 |  | 
 | 9928 |     if (PyUnicode_READY(substring) == -1) | 
 | 9929 |         return NULL; | 
 | 9930 |  | 
 | 9931 |     kind1 = PyUnicode_KIND(self); | 
 | 9932 |     kind2 = PyUnicode_KIND(substring); | 
 | 9933 |     kind = kind1 > kind2 ? kind1 : kind2; | 
 | 9934 |     buf1 = PyUnicode_DATA(self); | 
 | 9935 |     buf2 = PyUnicode_DATA(substring); | 
 | 9936 |     if (kind1 != kind) | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9937 |         buf1 = _PyUnicode_AsKind(self, kind); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9938 |     if (!buf1) | 
 | 9939 |         return NULL; | 
 | 9940 |     if (kind2 != kind) | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9941 |         buf2 = _PyUnicode_AsKind(substring, kind); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9942 |     if (!buf2) { | 
 | 9943 |         if (kind1 != kind) PyMem_Free(buf1); | 
 | 9944 |         return NULL; | 
 | 9945 |     } | 
 | 9946 |     len1 = PyUnicode_GET_LENGTH(self); | 
 | 9947 |     len2 = PyUnicode_GET_LENGTH(substring); | 
 | 9948 |  | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9949 |     switch (kind) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9950 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9951 |         if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) | 
 | 9952 |             out = asciilib_rsplit( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9953 |                 self,  buf1, len1, buf2, len2, maxcount); | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9954 |         else | 
 | 9955 |             out = ucs1lib_rsplit( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9956 |                 self,  buf1, len1, buf2, len2, maxcount); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9957 |         break; | 
 | 9958 |     case PyUnicode_2BYTE_KIND: | 
 | 9959 |         out = ucs2lib_rsplit( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9960 |             self,  buf1, len1, buf2, len2, maxcount); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9961 |         break; | 
 | 9962 |     case PyUnicode_4BYTE_KIND: | 
 | 9963 |         out = ucs4lib_rsplit( | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9964 |             self,  buf1, len1, buf2, len2, maxcount); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9965 |         break; | 
 | 9966 |     default: | 
 | 9967 |         out = NULL; | 
 | 9968 |     } | 
 | 9969 |     if (kind1 != kind) | 
 | 9970 |         PyMem_Free(buf1); | 
 | 9971 |     if (kind2 != kind) | 
 | 9972 |         PyMem_Free(buf2); | 
 | 9973 |     return out; | 
 | 9974 | } | 
 | 9975 |  | 
 | 9976 | static Py_ssize_t | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9977 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, | 
 | 9978 |             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] | 9979 | { | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9980 |     switch (kind) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9981 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9982 |         if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) | 
 | 9983 |             return asciilib_find(buf1, len1, buf2, len2, offset); | 
 | 9984 |         else | 
 | 9985 |             return ucs1lib_find(buf1, len1, buf2, len2, offset); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9986 |     case PyUnicode_2BYTE_KIND: | 
 | 9987 |         return ucs2lib_find(buf1, len1, buf2, len2, offset); | 
 | 9988 |     case PyUnicode_4BYTE_KIND: | 
 | 9989 |         return ucs4lib_find(buf1, len1, buf2, len2, offset); | 
 | 9990 |     } | 
 | 9991 |     assert(0); | 
 | 9992 |     return -1; | 
 | 9993 | } | 
 | 9994 |  | 
 | 9995 | static Py_ssize_t | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9996 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, | 
 | 9997 |              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] | 9998 | { | 
| Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 9999 |     switch (kind) { | 
 | 10000 |     case PyUnicode_1BYTE_KIND: | 
 | 10001 |         if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) | 
 | 10002 |             return asciilib_count(sbuf, slen, buf1, len1, maxcount); | 
 | 10003 |         else | 
 | 10004 |             return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); | 
 | 10005 |     case PyUnicode_2BYTE_KIND: | 
 | 10006 |         return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); | 
 | 10007 |     case PyUnicode_4BYTE_KIND: | 
 | 10008 |         return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); | 
 | 10009 |     } | 
 | 10010 |     assert(0); | 
 | 10011 |     return 0; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10012 | } | 
 | 10013 |  | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10014 | static void | 
 | 10015 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, | 
 | 10016 |                       Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) | 
 | 10017 | { | 
 | 10018 |     int kind = PyUnicode_KIND(u); | 
 | 10019 |     void *data = PyUnicode_DATA(u); | 
 | 10020 |     Py_ssize_t len = PyUnicode_GET_LENGTH(u); | 
 | 10021 |     if (kind == PyUnicode_1BYTE_KIND) { | 
 | 10022 |         ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, | 
 | 10023 |                                       (Py_UCS1 *)data + len, | 
 | 10024 |                                       u1, u2, maxcount); | 
 | 10025 |     } | 
 | 10026 |     else if (kind == PyUnicode_2BYTE_KIND) { | 
 | 10027 |         ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, | 
 | 10028 |                                       (Py_UCS2 *)data + len, | 
 | 10029 |                                       u1, u2, maxcount); | 
 | 10030 |     } | 
 | 10031 |     else { | 
 | 10032 |         assert(kind == PyUnicode_4BYTE_KIND); | 
 | 10033 |         ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, | 
 | 10034 |                                       (Py_UCS4 *)data + len, | 
 | 10035 |                                       u1, u2, maxcount); | 
 | 10036 |     } | 
 | 10037 | } | 
 | 10038 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10039 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10040 | replace(PyObject *self, PyObject *str1, | 
 | 10041 |         PyObject *str2, Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10042 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10043 |     PyObject *u; | 
 | 10044 |     char *sbuf = PyUnicode_DATA(self); | 
 | 10045 |     char *buf1 = PyUnicode_DATA(str1); | 
 | 10046 |     char *buf2 = PyUnicode_DATA(str2); | 
 | 10047 |     int srelease = 0, release1 = 0, release2 = 0; | 
 | 10048 |     int skind = PyUnicode_KIND(self); | 
 | 10049 |     int kind1 = PyUnicode_KIND(str1); | 
 | 10050 |     int kind2 = PyUnicode_KIND(str2); | 
 | 10051 |     Py_ssize_t slen = PyUnicode_GET_LENGTH(self); | 
 | 10052 |     Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); | 
 | 10053 |     Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10054 |     int mayshrink; | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10055 |     Py_UCS4 maxchar, maxchar_str1, maxchar_str2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10056 |  | 
 | 10057 |     if (maxcount < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10058 |         maxcount = PY_SSIZE_T_MAX; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 |     else if (maxcount == 0 || slen == 0) | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10060 |         goto nothing; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10061 |  | 
| Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10062 |     if (str1 == str2) | 
 | 10063 |         goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10064 |  | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10065 |     maxchar = PyUnicode_MAX_CHAR_VALUE(self); | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10066 |     maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); | 
 | 10067 |     if (maxchar < maxchar_str1) | 
 | 10068 |         /* substring too wide to be present */ | 
 | 10069 |         goto nothing; | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10070 |     maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); | 
 | 10071 |     /* Replacing str1 with str2 may cause a maxchar reduction in the | 
 | 10072 |        result string. */ | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10073 |     mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10074 |     maxchar = Py_MAX(maxchar, maxchar_str2); | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10075 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10076 |     if (len1 == len2) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10077 |         /* same length */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10078 |         if (len1 == 0) | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10079 |             goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 |         if (len1 == 1) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10081 |             /* replace characters */ | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10082 |             Py_UCS4 u1, u2; | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10083 |             Py_ssize_t pos; | 
| Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10084 |  | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10085 |             u1 = PyUnicode_READ(kind1, buf1, 0); | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10086 |             pos = findchar(sbuf, skind, slen, u1, 1); | 
| Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10087 |             if (pos < 0) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10088 |                 goto nothing; | 
| Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10089 |             u2 = PyUnicode_READ(kind2, buf2, 0); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10090 |             u = PyUnicode_New(slen, maxchar); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10091 |             if (!u) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10092 |                 goto error; | 
| Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10093 |  | 
| Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10094 |             _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); | 
 | 10095 |             replace_1char_inplace(u, pos, u1, u2, maxcount); | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10096 |         } | 
 | 10097 |         else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10098 |             int rkind = skind; | 
 | 10099 |             char *res; | 
| Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10100 |             Py_ssize_t i; | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10101 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10102 |             if (kind1 < rkind) { | 
 | 10103 |                 /* widen substring */ | 
 | 10104 |                 buf1 = _PyUnicode_AsKind(str1, rkind); | 
 | 10105 |                 if (!buf1) goto error; | 
 | 10106 |                 release1 = 1; | 
 | 10107 |             } | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10108 |             i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10109 |             if (i < 0) | 
 | 10110 |                 goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10111 |             if (rkind > kind2) { | 
 | 10112 |                 /* widen replacement */ | 
 | 10113 |                 buf2 = _PyUnicode_AsKind(str2, rkind); | 
 | 10114 |                 if (!buf2) goto error; | 
 | 10115 |                 release2 = 1; | 
 | 10116 |             } | 
 | 10117 |             else if (rkind < kind2) { | 
 | 10118 |                 /* widen self and buf1 */ | 
 | 10119 |                 rkind = kind2; | 
 | 10120 |                 if (release1) PyMem_Free(buf1); | 
| Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10121 |                 release1 = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10122 |                 sbuf = _PyUnicode_AsKind(self, rkind); | 
 | 10123 |                 if (!sbuf) goto error; | 
 | 10124 |                 srelease = 1; | 
 | 10125 |                 buf1 = _PyUnicode_AsKind(str1, rkind); | 
 | 10126 |                 if (!buf1) goto error; | 
 | 10127 |                 release1 = 1; | 
 | 10128 |             } | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10129 |             u = PyUnicode_New(slen, maxchar); | 
 | 10130 |             if (!u) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10131 |                 goto error; | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10132 |             assert(PyUnicode_KIND(u) == rkind); | 
 | 10133 |             res = PyUnicode_DATA(u); | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10134 |  | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10135 |             memcpy(res, sbuf, rkind * slen); | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10136 |             /* change everything in-place, starting with this one */ | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10137 |             memcpy(res + rkind * i, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10138 |                    buf2, | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10139 |                    rkind * len2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10140 |             i += len1; | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10141 |  | 
 | 10142 |             while ( --maxcount > 0) { | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10143 |                 i = anylib_find(rkind, self, | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10144 |                                 sbuf+rkind*i, slen-i, | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10145 |                                 str1, buf1, len1, i); | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10146 |                 if (i == -1) | 
 | 10147 |                     break; | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10148 |                 memcpy(res + rkind * i, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10149 |                        buf2, | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10150 |                        rkind * len2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 |                 i += len1; | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10152 |             } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10153 |         } | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10154 |     } | 
 | 10155 |     else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10156 |         Py_ssize_t n, i, j, ires; | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10157 |         Py_ssize_t new_size; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10158 |         int rkind = skind; | 
 | 10159 |         char *res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10160 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10161 |         if (kind1 < rkind) { | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10162 |             /* widen substring */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10163 |             buf1 = _PyUnicode_AsKind(str1, rkind); | 
 | 10164 |             if (!buf1) goto error; | 
 | 10165 |             release1 = 1; | 
 | 10166 |         } | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10167 |         n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10168 |         if (n == 0) | 
 | 10169 |             goto nothing; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10170 |         if (kind2 < rkind) { | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10171 |             /* widen replacement */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 |             buf2 = _PyUnicode_AsKind(str2, rkind); | 
 | 10173 |             if (!buf2) goto error; | 
 | 10174 |             release2 = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10175 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10176 |         else if (kind2 > rkind) { | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10177 |             /* widen self and buf1 */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10178 |             rkind = kind2; | 
 | 10179 |             sbuf = _PyUnicode_AsKind(self, rkind); | 
 | 10180 |             if (!sbuf) goto error; | 
 | 10181 |             srelease = 1; | 
 | 10182 |             if (release1) PyMem_Free(buf1); | 
| Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10183 |             release1 = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10184 |             buf1 = _PyUnicode_AsKind(str1, rkind); | 
 | 10185 |             if (!buf1) goto error; | 
 | 10186 |             release1 = 1; | 
 | 10187 |         } | 
 | 10188 |         /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - | 
 | 10189 |            PyUnicode_GET_LENGTH(str1))); */ | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10190 |         if (len2 > len1 && len2 - len1 > (PY_SSIZE_T_MAX - slen) / n) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 |                 PyErr_SetString(PyExc_OverflowError, | 
 | 10192 |                                 "replace string is too long"); | 
 | 10193 |                 goto error; | 
 | 10194 |         } | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10195 |         new_size = slen + n * (len2 - len1); | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10196 |         if (new_size == 0) { | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10197 |             _Py_INCREF_UNICODE_EMPTY(); | 
 | 10198 |             if (!unicode_empty) | 
 | 10199 |                 goto error; | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10200 |             u = unicode_empty; | 
 | 10201 |             goto done; | 
 | 10202 |         } | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10203 |         if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10204 |             PyErr_SetString(PyExc_OverflowError, | 
 | 10205 |                             "replace string is too long"); | 
 | 10206 |             goto error; | 
 | 10207 |         } | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10208 |         u = PyUnicode_New(new_size, maxchar); | 
 | 10209 |         if (!u) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10210 |             goto error; | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10211 |         assert(PyUnicode_KIND(u) == rkind); | 
 | 10212 |         res = PyUnicode_DATA(u); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 |         ires = i = 0; | 
 | 10214 |         if (len1 > 0) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10215 |             while (n-- > 0) { | 
 | 10216 |                 /* look for next match */ | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10217 |                 j = anylib_find(rkind, self, | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10218 |                                 sbuf + rkind * i, slen-i, | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10219 |                                 str1, buf1, len1, i); | 
| Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10220 |                 if (j == -1) | 
 | 10221 |                     break; | 
 | 10222 |                 else if (j > i) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10223 |                     /* copy unchanged part [i:j] */ | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10224 |                     memcpy(res + rkind * ires, | 
 | 10225 |                            sbuf + rkind * i, | 
 | 10226 |                            rkind * (j-i)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10227 |                     ires += j - i; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10228 |                 } | 
 | 10229 |                 /* copy substitution string */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10230 |                 if (len2 > 0) { | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10231 |                     memcpy(res + rkind * ires, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10232 |                            buf2, | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10233 |                            rkind * len2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10234 |                     ires += len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10235 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10236 |                 i = j + len1; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10237 |             } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10238 |             if (i < slen) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10239 |                 /* copy tail [i:] */ | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10240 |                 memcpy(res + rkind * ires, | 
 | 10241 |                        sbuf + rkind * i, | 
 | 10242 |                        rkind * (slen-i)); | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10243 |         } | 
 | 10244 |         else { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10245 |             /* interleave */ | 
 | 10246 |             while (n > 0) { | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10247 |                 memcpy(res + rkind * ires, | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10248 |                        buf2, | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10249 |                        rkind * len2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10250 |                 ires += len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10251 |                 if (--n <= 0) | 
 | 10252 |                     break; | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10253 |                 memcpy(res + rkind * ires, | 
 | 10254 |                        sbuf + rkind * i, | 
 | 10255 |                        rkind); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10256 |                 ires++; | 
 | 10257 |                 i++; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10258 |             } | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10259 |             memcpy(res + rkind * ires, | 
 | 10260 |                    sbuf + rkind * i, | 
 | 10261 |                    rkind * (slen-i)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10262 |         } | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10263 |     } | 
 | 10264 |  | 
 | 10265 |     if (mayshrink) { | 
| Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10266 |         unicode_adjust_maxchar(&u); | 
 | 10267 |         if (u == NULL) | 
 | 10268 |             goto error; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10269 |     } | 
| Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10270 |  | 
 | 10271 |   done: | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10272 |     if (srelease) | 
 | 10273 |         PyMem_FREE(sbuf); | 
 | 10274 |     if (release1) | 
 | 10275 |         PyMem_FREE(buf1); | 
 | 10276 |     if (release2) | 
 | 10277 |         PyMem_FREE(buf2); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10278 |     assert(_PyUnicode_CheckConsistency(u, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10279 |     return u; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10280 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10281 |   nothing: | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10282 |     /* nothing to replace; return original string (when possible) */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10283 |     if (srelease) | 
 | 10284 |         PyMem_FREE(sbuf); | 
 | 10285 |     if (release1) | 
 | 10286 |         PyMem_FREE(buf1); | 
 | 10287 |     if (release2) | 
 | 10288 |         PyMem_FREE(buf2); | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10289 |     return unicode_result_unchanged(self); | 
 | 10290 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10291 |   error: | 
 | 10292 |     if (srelease && sbuf) | 
 | 10293 |         PyMem_FREE(sbuf); | 
 | 10294 |     if (release1 && buf1) | 
 | 10295 |         PyMem_FREE(buf1); | 
 | 10296 |     if (release2 && buf2) | 
 | 10297 |         PyMem_FREE(buf2); | 
 | 10298 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10299 | } | 
 | 10300 |  | 
 | 10301 | /* --- Unicode Object Methods --------------------------------------------- */ | 
 | 10302 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10303 | PyDoc_STRVAR(title__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10304 |              "S.title() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10305 | \n\ | 
 | 10306 | 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] | 10307 | characters, all remaining cased characters have lower case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10308 |  | 
 | 10309 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10310 | unicode_title(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10311 | { | 
| Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10312 |     if (PyUnicode_READY(self) == -1) | 
 | 10313 |         return NULL; | 
| Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10314 |     return case_operation(self, do_title); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10315 | } | 
 | 10316 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10317 | PyDoc_STRVAR(capitalize__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10318 |              "S.capitalize() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10319 | \n\ | 
 | 10320 | 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] | 10321 | have upper case and the rest lower case."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10322 |  | 
 | 10323 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10324 | unicode_capitalize(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10325 | { | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10326 |     if (PyUnicode_READY(self) == -1) | 
 | 10327 |         return NULL; | 
 | 10328 |     if (PyUnicode_GET_LENGTH(self) == 0) | 
 | 10329 |         return unicode_result_unchanged(self); | 
| Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10330 |     return case_operation(self, do_capitalize); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10331 | } | 
 | 10332 |  | 
| Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10333 | PyDoc_STRVAR(casefold__doc__, | 
 | 10334 |              "S.casefold() -> str\n\ | 
 | 10335 | \n\ | 
 | 10336 | Return a version of S suitable for caseless comparisons."); | 
 | 10337 |  | 
 | 10338 | static PyObject * | 
 | 10339 | unicode_casefold(PyObject *self) | 
 | 10340 | { | 
 | 10341 |     if (PyUnicode_READY(self) == -1) | 
 | 10342 |         return NULL; | 
 | 10343 |     if (PyUnicode_IS_ASCII(self)) | 
 | 10344 |         return ascii_upper_or_lower(self, 1); | 
| Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10345 |     return case_operation(self, do_casefold); | 
| Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10346 | } | 
 | 10347 |  | 
 | 10348 |  | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10349 | /* Argument converter.  Coerces to a single unicode character */ | 
 | 10350 |  | 
 | 10351 | static int | 
 | 10352 | convert_uc(PyObject *obj, void *addr) | 
 | 10353 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10354 |     Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10355 |     PyObject *uniobj; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10356 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10357 |     uniobj = PyUnicode_FromObject(obj); | 
 | 10358 |     if (uniobj == NULL) { | 
 | 10359 |         PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10360 |                         "The fill character cannot be converted to Unicode"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10361 |         return 0; | 
 | 10362 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10363 |     if (PyUnicode_GET_LENGTH(uniobj) != 1) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10364 |         PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10365 |                         "The fill character must be exactly one character long"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10366 |         Py_DECREF(uniobj); | 
 | 10367 |         return 0; | 
 | 10368 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10369 |     *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10370 |     Py_DECREF(uniobj); | 
 | 10371 |     return 1; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10372 | } | 
 | 10373 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10374 | PyDoc_STRVAR(center__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10375 |              "S.center(width[, fillchar]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10376 | \n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10377 | Return S centered in a string of length width. Padding is\n\ | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10378 | done using the specified fill character (default is a space)"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10379 |  | 
 | 10380 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10381 | unicode_center(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10382 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10383 |     Py_ssize_t marg, left; | 
 | 10384 |     Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10385 |     Py_UCS4 fillchar = ' '; | 
 | 10386 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10387 |     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] | 10388 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10389 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10390 |     if (PyUnicode_READY(self) == -1) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10391 |         return NULL; | 
 | 10392 |  | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10393 |     if (PyUnicode_GET_LENGTH(self) >= width) | 
 | 10394 |         return unicode_result_unchanged(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10395 |  | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10396 |     marg = width - PyUnicode_GET_LENGTH(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10397 |     left = marg / 2 + (marg & width & 1); | 
 | 10398 |  | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10399 |     return pad(self, left, marg - left, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10400 | } | 
 | 10401 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10402 | /* This function assumes that str1 and str2 are readied by the caller. */ | 
 | 10403 |  | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10404 | static int | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10405 | unicode_compare(PyObject *str1, PyObject *str2) | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10406 | { | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10407 | #define COMPARE(TYPE1, TYPE2) \ | 
 | 10408 |     do { \ | 
 | 10409 |         TYPE1* p1 = (TYPE1 *)data1; \ | 
 | 10410 |         TYPE2* p2 = (TYPE2 *)data2; \ | 
 | 10411 |         TYPE1* end = p1 + len; \ | 
 | 10412 |         Py_UCS4 c1, c2; \ | 
 | 10413 |         for (; p1 != end; p1++, p2++) { \ | 
 | 10414 |             c1 = *p1; \ | 
 | 10415 |             c2 = *p2; \ | 
 | 10416 |             if (c1 != c2) \ | 
 | 10417 |                 return (c1 < c2) ? -1 : 1; \ | 
 | 10418 |         } \ | 
 | 10419 |     } \ | 
 | 10420 |     while (0) | 
 | 10421 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10422 |     int kind1, kind2; | 
 | 10423 |     void *data1, *data2; | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10424 |     Py_ssize_t len1, len2, len; | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10425 |  | 
| Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10426 |     /* a string is equal to itself */ | 
 | 10427 |     if (str1 == str2) | 
 | 10428 |         return 0; | 
 | 10429 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10430 |     kind1 = PyUnicode_KIND(str1); | 
 | 10431 |     kind2 = PyUnicode_KIND(str2); | 
 | 10432 |     data1 = PyUnicode_DATA(str1); | 
 | 10433 |     data2 = PyUnicode_DATA(str2); | 
 | 10434 |     len1 = PyUnicode_GET_LENGTH(str1); | 
 | 10435 |     len2 = PyUnicode_GET_LENGTH(str2); | 
| Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10436 |     len = Py_MIN(len1, len2); | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10437 |  | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10438 |     switch(kind1) { | 
 | 10439 |     case PyUnicode_1BYTE_KIND: | 
 | 10440 |     { | 
 | 10441 |         switch(kind2) { | 
 | 10442 |         case PyUnicode_1BYTE_KIND: | 
 | 10443 |         { | 
 | 10444 |             int cmp = memcmp(data1, data2, len); | 
 | 10445 |             /* normalize result of memcmp() into the range [-1; 1] */ | 
 | 10446 |             if (cmp < 0) | 
 | 10447 |                 return -1; | 
 | 10448 |             if (cmp > 0) | 
 | 10449 |                 return 1; | 
 | 10450 |             break; | 
| Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10451 |         } | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10452 |         case PyUnicode_2BYTE_KIND: | 
 | 10453 |             COMPARE(Py_UCS1, Py_UCS2); | 
 | 10454 |             break; | 
 | 10455 |         case PyUnicode_4BYTE_KIND: | 
 | 10456 |             COMPARE(Py_UCS1, Py_UCS4); | 
 | 10457 |             break; | 
 | 10458 |         default: | 
 | 10459 |             assert(0); | 
 | 10460 |         } | 
 | 10461 |         break; | 
 | 10462 |     } | 
 | 10463 |     case PyUnicode_2BYTE_KIND: | 
 | 10464 |     { | 
 | 10465 |         switch(kind2) { | 
 | 10466 |         case PyUnicode_1BYTE_KIND: | 
 | 10467 |             COMPARE(Py_UCS2, Py_UCS1); | 
 | 10468 |             break; | 
 | 10469 |         case PyUnicode_2BYTE_KIND: | 
| Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10470 |         { | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10471 |             COMPARE(Py_UCS2, Py_UCS2); | 
 | 10472 |             break; | 
| Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10473 |         } | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10474 |         case PyUnicode_4BYTE_KIND: | 
 | 10475 |             COMPARE(Py_UCS2, Py_UCS4); | 
 | 10476 |             break; | 
 | 10477 |         default: | 
 | 10478 |             assert(0); | 
 | 10479 |         } | 
 | 10480 |         break; | 
 | 10481 |     } | 
 | 10482 |     case PyUnicode_4BYTE_KIND: | 
 | 10483 |     { | 
 | 10484 |         switch(kind2) { | 
 | 10485 |         case PyUnicode_1BYTE_KIND: | 
 | 10486 |             COMPARE(Py_UCS4, Py_UCS1); | 
 | 10487 |             break; | 
 | 10488 |         case PyUnicode_2BYTE_KIND: | 
 | 10489 |             COMPARE(Py_UCS4, Py_UCS2); | 
 | 10490 |             break; | 
 | 10491 |         case PyUnicode_4BYTE_KIND: | 
| Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10492 |         { | 
 | 10493 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 | 
 | 10494 |             int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); | 
 | 10495 |             /* normalize result of wmemcmp() into the range [-1; 1] */ | 
 | 10496 |             if (cmp < 0) | 
 | 10497 |                 return -1; | 
 | 10498 |             if (cmp > 0) | 
 | 10499 |                 return 1; | 
 | 10500 | #else | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10501 |             COMPARE(Py_UCS4, Py_UCS4); | 
| Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10502 | #endif | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10503 |             break; | 
| Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10504 |         } | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10505 |         default: | 
 | 10506 |             assert(0); | 
 | 10507 |         } | 
 | 10508 |         break; | 
 | 10509 |     } | 
 | 10510 |     default: | 
 | 10511 |         assert(0); | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10512 |     } | 
 | 10513 |  | 
| Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10514 |     if (len1 == len2) | 
 | 10515 |         return 0; | 
 | 10516 |     if (len1 < len2) | 
 | 10517 |         return -1; | 
 | 10518 |     else | 
 | 10519 |         return 1; | 
| Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10520 |  | 
 | 10521 | #undef COMPARE | 
| Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10522 | } | 
 | 10523 |  | 
| Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10524 | static int | 
 | 10525 | unicode_compare_eq(PyObject *str1, PyObject *str2) | 
 | 10526 | { | 
 | 10527 |     int kind; | 
 | 10528 |     void *data1, *data2; | 
 | 10529 |     Py_ssize_t len; | 
 | 10530 |     int cmp; | 
 | 10531 |  | 
 | 10532 |     /* a string is equal to itself */ | 
 | 10533 |     if (str1 == str2) | 
 | 10534 |         return 1; | 
 | 10535 |  | 
 | 10536 |     len = PyUnicode_GET_LENGTH(str1); | 
 | 10537 |     if (PyUnicode_GET_LENGTH(str2) != len) | 
 | 10538 |         return 0; | 
 | 10539 |     kind = PyUnicode_KIND(str1); | 
 | 10540 |     if (PyUnicode_KIND(str2) != kind) | 
 | 10541 |         return 0; | 
 | 10542 |     data1 = PyUnicode_DATA(str1); | 
 | 10543 |     data2 = PyUnicode_DATA(str2); | 
 | 10544 |  | 
 | 10545 |     cmp = memcmp(data1, data2, len * kind); | 
 | 10546 |     return (cmp == 0); | 
 | 10547 | } | 
 | 10548 |  | 
 | 10549 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10550 | int | 
 | 10551 | PyUnicode_Compare(PyObject *left, PyObject *right) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10552 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10553 |     if (PyUnicode_Check(left) && PyUnicode_Check(right)) { | 
 | 10554 |         if (PyUnicode_READY(left) == -1 || | 
 | 10555 |             PyUnicode_READY(right) == -1) | 
 | 10556 |             return -1; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10557 |         return unicode_compare(left, right); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10558 |     } | 
| Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10559 |     PyErr_Format(PyExc_TypeError, | 
 | 10560 |                  "Can't compare %.100s and %.100s", | 
 | 10561 |                  left->ob_type->tp_name, | 
 | 10562 |                  right->ob_type->tp_name); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10563 |     return -1; | 
 | 10564 | } | 
 | 10565 |  | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10566 | int | 
 | 10567 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) | 
 | 10568 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10569 |     Py_ssize_t i; | 
 | 10570 |     int kind; | 
 | 10571 |     void *data; | 
 | 10572 |     Py_UCS4 chr; | 
 | 10573 |  | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10574 |     assert(_PyUnicode_CHECK(uni)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10575 |     if (PyUnicode_READY(uni) == -1) | 
 | 10576 |         return -1; | 
 | 10577 |     kind = PyUnicode_KIND(uni); | 
 | 10578 |     data = PyUnicode_DATA(uni); | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10579 |     /* Compare Unicode string and source character set string */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10580 |     for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) | 
 | 10581 |         if (chr != str[i]) | 
 | 10582 |             return (chr < (unsigned char)(str[i])) ? -1 : 1; | 
| Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10583 |     /* This check keeps Python strings that end in '\0' from comparing equal | 
 | 10584 |      to C strings identical up to that point. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10585 |     if (PyUnicode_GET_LENGTH(uni) != i || chr) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10586 |         return 1; /* uni is longer */ | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10587 |     if (str[i]) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10588 |         return -1; /* str is longer */ | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10589 |     return 0; | 
 | 10590 | } | 
 | 10591 |  | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10592 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10593 | #define TEST_COND(cond)                         \ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10594 |     ((cond) ? Py_True : Py_False) | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10595 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10596 | PyObject * | 
 | 10597 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10598 | { | 
 | 10599 |     int result; | 
| Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10600 |     PyObject *v; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10601 |  | 
| Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10602 |     if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) | 
 | 10603 |         Py_RETURN_NOTIMPLEMENTED; | 
 | 10604 |  | 
 | 10605 |     if (PyUnicode_READY(left) == -1 || | 
 | 10606 |         PyUnicode_READY(right) == -1) | 
 | 10607 |         return NULL; | 
 | 10608 |  | 
 | 10609 |     if (op == Py_EQ || op == Py_NE) { | 
 | 10610 |         result = unicode_compare_eq(left, right); | 
 | 10611 |         if (op == Py_EQ) | 
 | 10612 |             v = TEST_COND(result); | 
 | 10613 |         else | 
 | 10614 |             v = TEST_COND(!result); | 
 | 10615 |     } | 
 | 10616 |     else { | 
| Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10617 |         result = unicode_compare(left, right); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10618 |  | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10619 |         /* Convert the return value to a Boolean */ | 
 | 10620 |         switch (op) { | 
| Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10621 |         case Py_LE: | 
 | 10622 |             v = TEST_COND(result <= 0); | 
 | 10623 |             break; | 
 | 10624 |         case Py_GE: | 
 | 10625 |             v = TEST_COND(result >= 0); | 
 | 10626 |             break; | 
 | 10627 |         case Py_LT: | 
 | 10628 |             v = TEST_COND(result == -1); | 
 | 10629 |             break; | 
 | 10630 |         case Py_GT: | 
 | 10631 |             v = TEST_COND(result == 1); | 
 | 10632 |             break; | 
 | 10633 |         default: | 
 | 10634 |             PyErr_BadArgument(); | 
 | 10635 |             return NULL; | 
 | 10636 |         } | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10637 |     } | 
| Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10638 |     Py_INCREF(v); | 
 | 10639 |     return v; | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10640 | } | 
 | 10641 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10642 | int | 
 | 10643 | PyUnicode_Contains(PyObject *container, PyObject *element) | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10644 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10645 |     PyObject *str, *sub; | 
| Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10646 |     int kind1, kind2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10647 |     void *buf1, *buf2; | 
 | 10648 |     Py_ssize_t len1, len2; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10649 |     int result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10650 |  | 
 | 10651 |     /* Coerce the two arguments */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10652 |     sub = PyUnicode_FromObject(element); | 
 | 10653 |     if (!sub) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10654 |         PyErr_Format(PyExc_TypeError, | 
 | 10655 |                      "'in <string>' requires string as left operand, not %s", | 
 | 10656 |                      element->ob_type->tp_name); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10657 |         return -1; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10658 |     } | 
 | 10659 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10660 |     str = PyUnicode_FromObject(container); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10661 |     if (!str) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10662 |         Py_DECREF(sub); | 
 | 10663 |         return -1; | 
 | 10664 |     } | 
 | 10665 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10666 |     kind1 = PyUnicode_KIND(str); | 
 | 10667 |     kind2 = PyUnicode_KIND(sub); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10668 |     buf1 = PyUnicode_DATA(str); | 
 | 10669 |     buf2 = PyUnicode_DATA(sub); | 
| Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10670 |     if (kind2 != kind1) { | 
 | 10671 |         if (kind2 > kind1) { | 
| Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10672 |             Py_DECREF(sub); | 
 | 10673 |             Py_DECREF(str); | 
| Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10674 |             return 0; | 
| Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10675 |         } | 
| Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10676 |         buf2 = _PyUnicode_AsKind(sub, kind1); | 
| Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10677 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10678 |     if (!buf2) { | 
 | 10679 |         Py_DECREF(sub); | 
| Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10680 |         Py_DECREF(str); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10681 |         return -1; | 
 | 10682 |     } | 
 | 10683 |     len1 = PyUnicode_GET_LENGTH(str); | 
 | 10684 |     len2 = PyUnicode_GET_LENGTH(sub); | 
 | 10685 |  | 
| Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10686 |     switch (kind1) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10687 |     case PyUnicode_1BYTE_KIND: | 
 | 10688 |         result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; | 
 | 10689 |         break; | 
 | 10690 |     case PyUnicode_2BYTE_KIND: | 
 | 10691 |         result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; | 
 | 10692 |         break; | 
 | 10693 |     case PyUnicode_4BYTE_KIND: | 
 | 10694 |         result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; | 
 | 10695 |         break; | 
 | 10696 |     default: | 
 | 10697 |         result = -1; | 
 | 10698 |         assert(0); | 
 | 10699 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10700 |  | 
 | 10701 |     Py_DECREF(str); | 
 | 10702 |     Py_DECREF(sub); | 
 | 10703 |  | 
| Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10704 |     if (kind2 != kind1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10705 |         PyMem_Free(buf2); | 
 | 10706 |  | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10707 |     return result; | 
| Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10708 | } | 
 | 10709 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10710 | /* Concat to string or Unicode object giving a new Unicode object. */ | 
 | 10711 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10712 | PyObject * | 
 | 10713 | PyUnicode_Concat(PyObject *left, PyObject *right) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10714 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10715 |     PyObject *u = NULL, *v = NULL, *w; | 
| Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10716 |     Py_UCS4 maxchar, maxchar2; | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10717 |     Py_ssize_t u_len, v_len, new_len; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10718 |  | 
 | 10719 |     /* Coerce the two arguments */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10720 |     u = PyUnicode_FromObject(left); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10721 |     if (u == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10722 |         goto onError; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10723 |     v = PyUnicode_FromObject(right); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10724 |     if (v == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10725 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10726 |  | 
 | 10727 |     /* Shortcuts */ | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10728 |     if (v == unicode_empty) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10729 |         Py_DECREF(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10730 |         return u; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10731 |     } | 
| Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10732 |     if (u == unicode_empty) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10733 |         Py_DECREF(u); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10734 |         return v; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10735 |     } | 
 | 10736 |  | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10737 |     u_len = PyUnicode_GET_LENGTH(u); | 
 | 10738 |     v_len = PyUnicode_GET_LENGTH(v); | 
 | 10739 |     if (u_len > PY_SSIZE_T_MAX - v_len) { | 
 | 10740 |         PyErr_SetString(PyExc_OverflowError, | 
 | 10741 |                         "strings are too large to concat"); | 
 | 10742 |         goto onError; | 
 | 10743 |     } | 
 | 10744 |     new_len = u_len + v_len; | 
 | 10745 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10746 |     maxchar = PyUnicode_MAX_CHAR_VALUE(u); | 
| Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10747 |     maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10748 |     maxchar = Py_MAX(maxchar, maxchar2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10749 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10750 |     /* Concat the two Unicode strings */ | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10751 |     w = PyUnicode_New(new_len, maxchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10752 |     if (w == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10753 |         goto onError; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10754 |     _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); | 
 | 10755 |     _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10756 |     Py_DECREF(u); | 
 | 10757 |     Py_DECREF(v); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10758 |     assert(_PyUnicode_CheckConsistency(w, 1)); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10759 |     return w; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10760 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10761 |   onError: | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10762 |     Py_XDECREF(u); | 
 | 10763 |     Py_XDECREF(v); | 
 | 10764 |     return NULL; | 
 | 10765 | } | 
 | 10766 |  | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10767 | void | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10768 | PyUnicode_Append(PyObject **p_left, PyObject *right) | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10769 | { | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10770 |     PyObject *left, *res; | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10771 |     Py_UCS4 maxchar, maxchar2; | 
 | 10772 |     Py_ssize_t left_len, right_len, new_len; | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10773 |  | 
 | 10774 |     if (p_left == NULL) { | 
 | 10775 |         if (!PyErr_Occurred()) | 
 | 10776 |             PyErr_BadInternalCall(); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10777 |         return; | 
 | 10778 |     } | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10779 |     left = *p_left; | 
| Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 10780 |     if (right == NULL || left == NULL | 
 | 10781 |         || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10782 |         if (!PyErr_Occurred()) | 
 | 10783 |             PyErr_BadInternalCall(); | 
 | 10784 |         goto error; | 
 | 10785 |     } | 
 | 10786 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10787 |     if (PyUnicode_READY(left) == -1) | 
| Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10788 |         goto error; | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10789 |     if (PyUnicode_READY(right) == -1) | 
| Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10790 |         goto error; | 
 | 10791 |  | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10792 |     /* Shortcuts */ | 
 | 10793 |     if (left == unicode_empty) { | 
 | 10794 |         Py_DECREF(left); | 
 | 10795 |         Py_INCREF(right); | 
 | 10796 |         *p_left = right; | 
 | 10797 |         return; | 
 | 10798 |     } | 
 | 10799 |     if (right == unicode_empty) | 
 | 10800 |         return; | 
 | 10801 |  | 
 | 10802 |     left_len = PyUnicode_GET_LENGTH(left); | 
 | 10803 |     right_len = PyUnicode_GET_LENGTH(right); | 
 | 10804 |     if (left_len > PY_SSIZE_T_MAX - right_len) { | 
 | 10805 |         PyErr_SetString(PyExc_OverflowError, | 
 | 10806 |                         "strings are too large to concat"); | 
 | 10807 |         goto error; | 
 | 10808 |     } | 
 | 10809 |     new_len = left_len + right_len; | 
 | 10810 |  | 
 | 10811 |     if (unicode_modifiable(left) | 
 | 10812 |         && PyUnicode_CheckExact(right) | 
 | 10813 |         && PyUnicode_KIND(right) <= PyUnicode_KIND(left) | 
| Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10814 |         /* Don't resize for ascii += latin1. Convert ascii to latin1 requires | 
 | 10815 |            to change the structure size, but characters are stored just after | 
| Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10816 |            the structure, and so it requires to move all characters which is | 
| Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10817 |            not so different than duplicating the string. */ | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10818 |         && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) | 
 | 10819 |     { | 
 | 10820 |         /* append inplace */ | 
| Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 10821 |         if (unicode_resize(p_left, new_len) != 0) | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10822 |             goto error; | 
| Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 10823 |  | 
| Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 10824 |         /* copy 'right' into the newly allocated area of 'left' */ | 
 | 10825 |         _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10826 |     } | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10827 |     else { | 
 | 10828 |         maxchar = PyUnicode_MAX_CHAR_VALUE(left); | 
 | 10829 |         maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); | 
| Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10830 |         maxchar = Py_MAX(maxchar, maxchar2); | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10831 |  | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10832 |         /* Concat the two Unicode strings */ | 
 | 10833 |         res = PyUnicode_New(new_len, maxchar); | 
 | 10834 |         if (res == NULL) | 
 | 10835 |             goto error; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10836 |         _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); | 
 | 10837 |         _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10838 |         Py_DECREF(left); | 
| Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 10839 |         *p_left = res; | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10840 |     } | 
 | 10841 |     assert(_PyUnicode_CheckConsistency(*p_left, 1)); | 
| Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10842 |     return; | 
 | 10843 |  | 
 | 10844 | error: | 
| Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10845 |     Py_CLEAR(*p_left); | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10846 | } | 
 | 10847 |  | 
 | 10848 | void | 
 | 10849 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) | 
 | 10850 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10851 |     PyUnicode_Append(pleft, right); | 
 | 10852 |     Py_XDECREF(right); | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10853 | } | 
 | 10854 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10855 | PyDoc_STRVAR(count__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10856 |              "S.count(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10857 | \n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10858 | Return the number of non-overlapping occurrences of substring sub in\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10859 | 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] | 10860 | interpreted as in slice notation."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10861 |  | 
 | 10862 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10863 | unicode_count(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10864 | { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10865 |     PyObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10866 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10867 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10868 |     PyObject *result; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10869 |     int kind1, kind2, kind; | 
 | 10870 |     void *buf1, *buf2; | 
 | 10871 |     Py_ssize_t len1, len2, iresult; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10872 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10873 |     if (!stringlib_parse_args_finds_unicode("count", args, &substring, | 
 | 10874 |                                             &start, &end)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10875 |         return NULL; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10876 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10877 |     kind1 = PyUnicode_KIND(self); | 
 | 10878 |     kind2 = PyUnicode_KIND(substring); | 
| Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 10879 |     if (kind2 > kind1) { | 
 | 10880 |         Py_DECREF(substring); | 
| Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10881 |         return PyLong_FromLong(0); | 
| Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 10882 |     } | 
| Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10883 |     kind = kind1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10884 |     buf1 = PyUnicode_DATA(self); | 
 | 10885 |     buf2 = PyUnicode_DATA(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10886 |     if (kind2 != kind) | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10887 |         buf2 = _PyUnicode_AsKind(substring, kind); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10888 |     if (!buf2) { | 
 | 10889 |         Py_DECREF(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10890 |         return NULL; | 
 | 10891 |     } | 
 | 10892 |     len1 = PyUnicode_GET_LENGTH(self); | 
 | 10893 |     len2 = PyUnicode_GET_LENGTH(substring); | 
 | 10894 |  | 
 | 10895 |     ADJUST_INDICES(start, end, len1); | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10896 |     switch (kind) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10897 |     case PyUnicode_1BYTE_KIND: | 
 | 10898 |         iresult = ucs1lib_count( | 
 | 10899 |             ((Py_UCS1*)buf1) + start, end - start, | 
 | 10900 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 10901 |             ); | 
 | 10902 |         break; | 
 | 10903 |     case PyUnicode_2BYTE_KIND: | 
 | 10904 |         iresult = ucs2lib_count( | 
 | 10905 |             ((Py_UCS2*)buf1) + start, end - start, | 
 | 10906 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 10907 |             ); | 
 | 10908 |         break; | 
 | 10909 |     case PyUnicode_4BYTE_KIND: | 
 | 10910 |         iresult = ucs4lib_count( | 
 | 10911 |             ((Py_UCS4*)buf1) + start, end - start, | 
 | 10912 |             buf2, len2, PY_SSIZE_T_MAX | 
 | 10913 |             ); | 
 | 10914 |         break; | 
 | 10915 |     default: | 
 | 10916 |         assert(0); iresult = 0; | 
 | 10917 |     } | 
 | 10918 |  | 
 | 10919 |     result = PyLong_FromSsize_t(iresult); | 
 | 10920 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10921 |     if (kind2 != kind) | 
 | 10922 |         PyMem_Free(buf2); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10923 |  | 
 | 10924 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10925 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10926 |     return result; | 
 | 10927 | } | 
 | 10928 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10929 | PyDoc_STRVAR(encode__doc__, | 
| Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10930 |              "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10931 | \n\ | 
| Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10932 | Encode S using the codec registered for encoding. Default encoding\n\ | 
 | 10933 | 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] | 10934 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ | 
| Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10935 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ | 
 | 10936 | 'xmlcharrefreplace' as well as any other name registered with\n\ | 
 | 10937 | codecs.register_error that can handle UnicodeEncodeErrors."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10938 |  | 
 | 10939 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10940 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10941 | { | 
| Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10942 |     static char *kwlist[] = {"encoding", "errors", 0}; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10943 |     char *encoding = NULL; | 
 | 10944 |     char *errors = NULL; | 
| Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10945 |  | 
| Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10946 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", | 
 | 10947 |                                      kwlist, &encoding, &errors)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10948 |         return NULL; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10949 |     return PyUnicode_AsEncodedString(self, encoding, errors); | 
| Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10950 | } | 
 | 10951 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10952 | PyDoc_STRVAR(expandtabs__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10953 |              "S.expandtabs([tabsize]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10954 | \n\ | 
 | 10955 | 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] | 10956 | 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] | 10957 |  | 
 | 10958 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10959 | unicode_expandtabs(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10960 | { | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10961 |     Py_ssize_t i, j, line_pos, src_len, incr; | 
 | 10962 |     Py_UCS4 ch; | 
 | 10963 |     PyObject *u; | 
 | 10964 |     void *src_data, *dest_data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10965 |     int tabsize = 8; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10966 |     int kind; | 
| Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10967 |     int found; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10968 |  | 
 | 10969 |     if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10970 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10971 |  | 
| Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10972 |     if (PyUnicode_READY(self) == -1) | 
 | 10973 |         return NULL; | 
 | 10974 |  | 
| Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10975 |     /* First pass: determine size of output string */ | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10976 |     src_len = PyUnicode_GET_LENGTH(self); | 
 | 10977 |     i = j = line_pos = 0; | 
 | 10978 |     kind = PyUnicode_KIND(self); | 
 | 10979 |     src_data = PyUnicode_DATA(self); | 
| Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10980 |     found = 0; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10981 |     for (; i < src_len; i++) { | 
 | 10982 |         ch = PyUnicode_READ(kind, src_data, i); | 
 | 10983 |         if (ch == '\t') { | 
| Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10984 |             found = 1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10985 |             if (tabsize > 0) { | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10986 |                 incr = tabsize - (line_pos % tabsize); /* cannot overflow */ | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10987 |                 if (j > PY_SSIZE_T_MAX - incr) | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10988 |                     goto overflow; | 
 | 10989 |                 line_pos += incr; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10990 |                 j += incr; | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10991 |             } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10992 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10993 |         else { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10994 |             if (j > PY_SSIZE_T_MAX - 1) | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10995 |                 goto overflow; | 
 | 10996 |             line_pos++; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10997 |             j++; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10998 |             if (ch == '\n' || ch == '\r') | 
 | 10999 |                 line_pos = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11000 |         } | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11001 |     } | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11002 |     if (!found) | 
 | 11003 |         return unicode_result_unchanged(self); | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11004 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11005 |     /* Second pass: create output string and fill it */ | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11006 |     u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11007 |     if (!u) | 
 | 11008 |         return NULL; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11009 |     dest_data = PyUnicode_DATA(u); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11010 |  | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11011 |     i = j = line_pos = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 |  | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11013 |     for (; i < src_len; i++) { | 
 | 11014 |         ch = PyUnicode_READ(kind, src_data, i); | 
 | 11015 |         if (ch == '\t') { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11016 |             if (tabsize > 0) { | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11017 |                 incr = tabsize - (line_pos % tabsize); | 
 | 11018 |                 line_pos += incr; | 
| Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11019 |                 FILL(kind, dest_data, ' ', j, incr); | 
 | 11020 |                 j += incr; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11021 |             } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11022 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11023 |         else { | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11024 |             line_pos++; | 
 | 11025 |             PyUnicode_WRITE(kind, dest_data, j, ch); | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11026 |             j++; | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11027 |             if (ch == '\n' || ch == '\r') | 
 | 11028 |                 line_pos = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11029 |         } | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11030 |     } | 
 | 11031 |     assert (j == PyUnicode_GET_LENGTH(u)); | 
| Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11032 |     return unicode_result(u); | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11033 |  | 
| Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11034 |   overflow: | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11035 |     PyErr_SetString(PyExc_OverflowError, "new string is too long"); | 
 | 11036 |     return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11037 | } | 
 | 11038 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11039 | PyDoc_STRVAR(find__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11040 |              "S.find(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11041 | \n\ | 
 | 11042 | Return the lowest index in S where substring sub is found,\n\ | 
| Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11043 | such that sub is contained within S[start:end].  Optional\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11044 | arguments start and end are interpreted as in slice notation.\n\ | 
 | 11045 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11046 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11047 |  | 
 | 11048 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11049 | unicode_find(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11050 | { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11051 |     PyObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11052 |     Py_ssize_t start; | 
 | 11053 |     Py_ssize_t end; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11054 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11055 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11056 |     if (!stringlib_parse_args_finds_unicode("find", args, &substring, | 
 | 11057 |                                             &start, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11058 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11059 |  | 
| Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11060 |     if (PyUnicode_READY(self) == -1) { | 
 | 11061 |         Py_DECREF(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11062 |         return NULL; | 
| Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11063 |     } | 
 | 11064 |     if (PyUnicode_READY(substring) == -1) { | 
 | 11065 |         Py_DECREF(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11066 |         return NULL; | 
| Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11067 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11068 |  | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11069 |     result = any_find_slice(1, self, substring, start, end); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11070 |  | 
 | 11071 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11072 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11073 |     if (result == -2) | 
 | 11074 |         return NULL; | 
 | 11075 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11076 |     return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11077 | } | 
 | 11078 |  | 
 | 11079 | static PyObject * | 
| Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11080 | unicode_getitem(PyObject *self, Py_ssize_t index) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11081 | { | 
| Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11082 |     void *data; | 
 | 11083 |     enum PyUnicode_Kind kind; | 
 | 11084 |     Py_UCS4 ch; | 
 | 11085 |     PyObject *res; | 
 | 11086 |  | 
 | 11087 |     if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { | 
 | 11088 |         PyErr_BadArgument(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11089 |         return NULL; | 
| Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11090 |     } | 
 | 11091 |     if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { | 
 | 11092 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 11093 |         return NULL; | 
 | 11094 |     } | 
 | 11095 |     kind = PyUnicode_KIND(self); | 
 | 11096 |     data = PyUnicode_DATA(self); | 
 | 11097 |     ch = PyUnicode_READ(kind, data, index); | 
 | 11098 |     if (ch < 256) | 
 | 11099 |         return get_latin1_char(ch); | 
 | 11100 |  | 
 | 11101 |     res = PyUnicode_New(1, ch); | 
 | 11102 |     if (res == NULL) | 
 | 11103 |         return NULL; | 
 | 11104 |     kind = PyUnicode_KIND(res); | 
 | 11105 |     data = PyUnicode_DATA(res); | 
 | 11106 |     PyUnicode_WRITE(kind, data, 0, ch); | 
 | 11107 |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
 | 11108 |     return res; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11109 | } | 
 | 11110 |  | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11111 | /* Believe it or not, this produces the same value for ASCII strings | 
| Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11112 |    as bytes_hash(). */ | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11113 | static Py_hash_t | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11114 | unicode_hash(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 | { | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11116 |     Py_ssize_t len; | 
| Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11117 |     Py_uhash_t x;  /* Unsigned for defined overflow behavior. */ | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11118 |  | 
| Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11119 | #ifdef Py_DEBUG | 
| Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11120 |     assert(_Py_HashSecret_Initialized); | 
| Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11121 | #endif | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11122 |     if (_PyUnicode_HASH(self) != -1) | 
 | 11123 |         return _PyUnicode_HASH(self); | 
 | 11124 |     if (PyUnicode_READY(self) == -1) | 
 | 11125 |         return -1; | 
 | 11126 |     len = PyUnicode_GET_LENGTH(self); | 
| Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11127 |     /* | 
 | 11128 |       We make the hash of the empty string be 0, rather than using | 
 | 11129 |       (prefix ^ suffix), since this slightly obfuscates the hash secret | 
 | 11130 |     */ | 
 | 11131 |     if (len == 0) { | 
 | 11132 |         _PyUnicode_HASH(self) = 0; | 
 | 11133 |         return 0; | 
 | 11134 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11135 |  | 
 | 11136 |     /* The hash function as a macro, gets expanded three times below. */ | 
| Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11137 | #define HASH(P)                                            \ | 
 | 11138 |     x ^= (Py_uhash_t) *P << 7;                             \ | 
 | 11139 |     while (--len >= 0)                                     \ | 
 | 11140 |         x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++;  \ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11141 |  | 
| Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11142 |     x = (Py_uhash_t) _Py_HashSecret.prefix; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 |     switch (PyUnicode_KIND(self)) { | 
 | 11144 |     case PyUnicode_1BYTE_KIND: { | 
 | 11145 |         const unsigned char *c = PyUnicode_1BYTE_DATA(self); | 
 | 11146 |         HASH(c); | 
 | 11147 |         break; | 
 | 11148 |     } | 
 | 11149 |     case PyUnicode_2BYTE_KIND: { | 
 | 11150 |         const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); | 
 | 11151 |         HASH(s); | 
 | 11152 |         break; | 
 | 11153 |     } | 
 | 11154 |     default: { | 
 | 11155 |         Py_UCS4 *l; | 
 | 11156 |         assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && | 
 | 11157 |                "Impossible switch case in unicode_hash"); | 
 | 11158 |         l = PyUnicode_4BYTE_DATA(self); | 
 | 11159 |         HASH(l); | 
 | 11160 |         break; | 
 | 11161 |     } | 
 | 11162 |     } | 
| Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11163 |     x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); | 
 | 11164 |     x ^= (Py_uhash_t) _Py_HashSecret.suffix; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11165 |  | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11166 |     if (x == -1) | 
 | 11167 |         x = -2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11168 |     _PyUnicode_HASH(self) = x; | 
| Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11169 |     return x; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11170 | } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11171 | #undef HASH | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11173 | PyDoc_STRVAR(index__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11174 |              "S.index(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11176 | 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] | 11177 |  | 
 | 11178 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11179 | unicode_index(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11181 |     Py_ssize_t result; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11182 |     PyObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11183 |     Py_ssize_t start; | 
 | 11184 |     Py_ssize_t end; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11186 |     if (!stringlib_parse_args_finds_unicode("index", args, &substring, | 
 | 11187 |                                             &start, &end)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11188 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11189 |  | 
| Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11190 |     if (PyUnicode_READY(self) == -1) { | 
 | 11191 |         Py_DECREF(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11192 |         return NULL; | 
| Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11193 |     } | 
 | 11194 |     if (PyUnicode_READY(substring) == -1) { | 
 | 11195 |         Py_DECREF(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11196 |         return NULL; | 
| Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11197 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11198 |  | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11199 |     result = any_find_slice(1, self, substring, start, end); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11200 |  | 
 | 11201 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11202 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11203 |     if (result == -2) | 
 | 11204 |         return NULL; | 
 | 11205 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11206 |     if (result < 0) { | 
 | 11207 |         PyErr_SetString(PyExc_ValueError, "substring not found"); | 
 | 11208 |         return NULL; | 
 | 11209 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11210 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11211 |     return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | } | 
 | 11213 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11214 | PyDoc_STRVAR(islower__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11215 |              "S.islower() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11216 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11217 | 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] | 11218 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11219 |  | 
 | 11220 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11221 | unicode_islower(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11222 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11223 |     Py_ssize_t i, length; | 
 | 11224 |     int kind; | 
 | 11225 |     void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11226 |     int cased; | 
 | 11227 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11228 |     if (PyUnicode_READY(self) == -1) | 
 | 11229 |         return NULL; | 
 | 11230 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11231 |     kind = PyUnicode_KIND(self); | 
 | 11232 |     data = PyUnicode_DATA(self); | 
 | 11233 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11234 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11235 |     if (length == 1) | 
 | 11236 |         return PyBool_FromLong( | 
 | 11237 |             Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11239 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11240 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11241 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11242 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11243 |     cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11244 |     for (i = 0; i < length; i++) { | 
 | 11245 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11246 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11247 |         if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) | 
 | 11248 |             return PyBool_FromLong(0); | 
 | 11249 |         else if (!cased && Py_UNICODE_ISLOWER(ch)) | 
 | 11250 |             cased = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11252 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 | } | 
 | 11254 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11255 | PyDoc_STRVAR(isupper__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11256 |              "S.isupper() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11258 | 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] | 11259 | at least one cased character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11260 |  | 
 | 11261 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11262 | unicode_isupper(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11263 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11264 |     Py_ssize_t i, length; | 
 | 11265 |     int kind; | 
 | 11266 |     void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11267 |     int cased; | 
 | 11268 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11269 |     if (PyUnicode_READY(self) == -1) | 
 | 11270 |         return NULL; | 
 | 11271 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11272 |     kind = PyUnicode_KIND(self); | 
 | 11273 |     data = PyUnicode_DATA(self); | 
 | 11274 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11276 |     if (length == 1) | 
 | 11277 |         return PyBool_FromLong( | 
 | 11278 |             Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11280 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11281 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11282 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11283 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11284 |     cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11285 |     for (i = 0; i < length; i++) { | 
 | 11286 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11287 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11288 |         if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) | 
 | 11289 |             return PyBool_FromLong(0); | 
 | 11290 |         else if (!cased && Py_UNICODE_ISUPPER(ch)) | 
 | 11291 |             cased = 1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11292 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11293 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | } | 
 | 11295 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11296 | PyDoc_STRVAR(istitle__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11297 |              "S.istitle() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11298 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11299 | Return True if S is a titlecased string and there is at least one\n\ | 
 | 11300 | character in S, i.e. upper- and titlecase characters may only\n\ | 
 | 11301 | follow uncased characters and lowercase characters only cased ones.\n\ | 
 | 11302 | Return False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 |  | 
 | 11304 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11305 | unicode_istitle(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11307 |     Py_ssize_t i, length; | 
 | 11308 |     int kind; | 
 | 11309 |     void *data; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11310 |     int cased, previous_is_cased; | 
 | 11311 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11312 |     if (PyUnicode_READY(self) == -1) | 
 | 11313 |         return NULL; | 
 | 11314 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11315 |     kind = PyUnicode_KIND(self); | 
 | 11316 |     data = PyUnicode_DATA(self); | 
 | 11317 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11318 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11319 |     if (length == 1) { | 
 | 11320 |         Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
 | 11321 |         return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || | 
 | 11322 |                                (Py_UNICODE_ISUPPER(ch) != 0)); | 
 | 11323 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11324 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11325 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11326 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11327 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11328 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11329 |     cased = 0; | 
 | 11330 |     previous_is_cased = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11331 |     for (i = 0; i < length; i++) { | 
 | 11332 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11333 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11334 |         if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { | 
 | 11335 |             if (previous_is_cased) | 
 | 11336 |                 return PyBool_FromLong(0); | 
 | 11337 |             previous_is_cased = 1; | 
 | 11338 |             cased = 1; | 
 | 11339 |         } | 
 | 11340 |         else if (Py_UNICODE_ISLOWER(ch)) { | 
 | 11341 |             if (!previous_is_cased) | 
 | 11342 |                 return PyBool_FromLong(0); | 
 | 11343 |             previous_is_cased = 1; | 
 | 11344 |             cased = 1; | 
 | 11345 |         } | 
 | 11346 |         else | 
 | 11347 |             previous_is_cased = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11348 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11349 |     return PyBool_FromLong(cased); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | } | 
 | 11351 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11352 | PyDoc_STRVAR(isspace__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11353 |              "S.isspace() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11354 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11355 | Return True if all characters in S are whitespace\n\ | 
 | 11356 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11357 |  | 
 | 11358 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11359 | unicode_isspace(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11360 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11361 |     Py_ssize_t i, length; | 
 | 11362 |     int kind; | 
 | 11363 |     void *data; | 
 | 11364 |  | 
 | 11365 |     if (PyUnicode_READY(self) == -1) | 
 | 11366 |         return NULL; | 
 | 11367 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11368 |     kind = PyUnicode_KIND(self); | 
 | 11369 |     data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11370 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11371 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11372 |     if (length == 1) | 
 | 11373 |         return PyBool_FromLong( | 
 | 11374 |             Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11376 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11377 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11378 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11379 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11380 |     for (i = 0; i < length; i++) { | 
 | 11381 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11382 |         if (!Py_UNICODE_ISSPACE(ch)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11383 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11384 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11385 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | } | 
 | 11387 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11388 | PyDoc_STRVAR(isalpha__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11389 |              "S.isalpha() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11390 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11391 | Return True if all characters in S are alphabetic\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11392 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11393 |  | 
 | 11394 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11395 | unicode_isalpha(PyObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11396 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11397 |     Py_ssize_t i, length; | 
 | 11398 |     int kind; | 
 | 11399 |     void *data; | 
 | 11400 |  | 
 | 11401 |     if (PyUnicode_READY(self) == -1) | 
 | 11402 |         return NULL; | 
 | 11403 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11404 |     kind = PyUnicode_KIND(self); | 
 | 11405 |     data = PyUnicode_DATA(self); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11406 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11407 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11408 |     if (length == 1) | 
 | 11409 |         return PyBool_FromLong( | 
 | 11410 |             Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11411 |  | 
 | 11412 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11413 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11414 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11415 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11416 |     for (i = 0; i < length; i++) { | 
 | 11417 |         if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11418 |             return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11419 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11420 |     return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11421 | } | 
 | 11422 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11423 | PyDoc_STRVAR(isalnum__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11424 |              "S.isalnum() -> bool\n\ | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11425 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11426 | Return True if all characters in S are alphanumeric\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11427 | and there is at least one character in S, False otherwise."); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11428 |  | 
 | 11429 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11430 | unicode_isalnum(PyObject *self) | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11431 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11432 |     int kind; | 
 | 11433 |     void *data; | 
 | 11434 |     Py_ssize_t len, i; | 
 | 11435 |  | 
 | 11436 |     if (PyUnicode_READY(self) == -1) | 
 | 11437 |         return NULL; | 
 | 11438 |  | 
 | 11439 |     kind = PyUnicode_KIND(self); | 
 | 11440 |     data = PyUnicode_DATA(self); | 
 | 11441 |     len = PyUnicode_GET_LENGTH(self); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11442 |  | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11443 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11444 |     if (len == 1) { | 
 | 11445 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
 | 11446 |         return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); | 
 | 11447 |     } | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11448 |  | 
 | 11449 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11450 |     if (len == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11451 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11452 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11453 |     for (i = 0; i < len; i++) { | 
 | 11454 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
| Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11455 |         if (!Py_UNICODE_ISALNUM(ch)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11456 |             return PyBool_FromLong(0); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11457 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11458 |     return PyBool_FromLong(1); | 
| Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11459 | } | 
 | 11460 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11461 | PyDoc_STRVAR(isdecimal__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11462 |              "S.isdecimal() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11463 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11464 | 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] | 11465 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11466 |  | 
 | 11467 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11468 | unicode_isdecimal(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11469 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11470 |     Py_ssize_t i, length; | 
 | 11471 |     int kind; | 
 | 11472 |     void *data; | 
 | 11473 |  | 
 | 11474 |     if (PyUnicode_READY(self) == -1) | 
 | 11475 |         return NULL; | 
 | 11476 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11477 |     kind = PyUnicode_KIND(self); | 
 | 11478 |     data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11479 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11481 |     if (length == 1) | 
 | 11482 |         return PyBool_FromLong( | 
 | 11483 |             Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11485 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11486 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11487 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11488 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11489 |     for (i = 0; i < length; i++) { | 
 | 11490 |         if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11491 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11492 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11493 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11494 | } | 
 | 11495 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11496 | PyDoc_STRVAR(isdigit__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11497 |              "S.isdigit() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | \n\ | 
| Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11499 | Return True if all characters in S are digits\n\ | 
 | 11500 | and there is at least one character in S, False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 |  | 
 | 11502 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11503 | unicode_isdigit(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11504 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 |     Py_ssize_t i, length; | 
 | 11506 |     int kind; | 
 | 11507 |     void *data; | 
 | 11508 |  | 
 | 11509 |     if (PyUnicode_READY(self) == -1) | 
 | 11510 |         return NULL; | 
 | 11511 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11512 |     kind = PyUnicode_KIND(self); | 
 | 11513 |     data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11514 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11515 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11516 |     if (length == 1) { | 
 | 11517 |         const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); | 
 | 11518 |         return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); | 
 | 11519 |     } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11521 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11522 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11523 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11524 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11525 |     for (i = 0; i < length; i++) { | 
 | 11526 |         if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11527 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11528 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11529 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11530 | } | 
 | 11531 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11532 | PyDoc_STRVAR(isnumeric__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11533 |              "S.isnumeric() -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11534 | \n\ | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11535 | 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] | 11536 | False otherwise."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11537 |  | 
 | 11538 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11539 | unicode_isnumeric(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11540 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11541 |     Py_ssize_t i, length; | 
 | 11542 |     int kind; | 
 | 11543 |     void *data; | 
 | 11544 |  | 
 | 11545 |     if (PyUnicode_READY(self) == -1) | 
 | 11546 |         return NULL; | 
 | 11547 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11548 |     kind = PyUnicode_KIND(self); | 
 | 11549 |     data = PyUnicode_DATA(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11550 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11551 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11552 |     if (length == 1) | 
 | 11553 |         return PyBool_FromLong( | 
 | 11554 |             Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 |  | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11556 |     /* Special case for empty strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11557 |     if (length == 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11558 |         return PyBool_FromLong(0); | 
| Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11559 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11560 |     for (i = 0; i < length; i++) { | 
 | 11561 |         if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11562 |             return PyBool_FromLong(0); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11563 |     } | 
| Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11564 |     return PyBool_FromLong(1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | } | 
 | 11566 |  | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11567 | int | 
 | 11568 | PyUnicode_IsIdentifier(PyObject *self) | 
 | 11569 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11570 |     int kind; | 
 | 11571 |     void *data; | 
 | 11572 |     Py_ssize_t i; | 
| Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11573 |     Py_UCS4 first; | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11574 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11575 |     if (PyUnicode_READY(self) == -1) { | 
 | 11576 |         Py_FatalError("identifier not ready"); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11577 |         return 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11578 |     } | 
 | 11579 |  | 
 | 11580 |     /* Special case for empty strings */ | 
 | 11581 |     if (PyUnicode_GET_LENGTH(self) == 0) | 
 | 11582 |         return 0; | 
 | 11583 |     kind = PyUnicode_KIND(self); | 
 | 11584 |     data = PyUnicode_DATA(self); | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11585 |  | 
 | 11586 |     /* PEP 3131 says that the first character must be in | 
 | 11587 |        XID_Start and subsequent characters in XID_Continue, | 
 | 11588 |        and for the ASCII range, the 2.x rules apply (i.e | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11589 |        start with letters and underscore, continue with | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11590 |        letters, digits, underscore). However, given the current | 
 | 11591 |        definition of XID_Start and XID_Continue, it is sufficient | 
 | 11592 |        to check just for these, except that _ must be allowed | 
 | 11593 |        as starting an identifier.  */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11594 |     first = PyUnicode_READ(kind, data, 0); | 
| Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11595 |     if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11596 |         return 0; | 
 | 11597 |  | 
| Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11598 |     for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11599 |         if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11600 |             return 0; | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11601 |     return 1; | 
 | 11602 | } | 
 | 11603 |  | 
 | 11604 | PyDoc_STRVAR(isidentifier__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11605 |              "S.isidentifier() -> bool\n\ | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11606 | \n\ | 
 | 11607 | Return True if S is a valid identifier according\n\ | 
| Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11608 | to the language definition.\n\ | 
 | 11609 | \n\ | 
 | 11610 | Use keyword.iskeyword() to test for reserved identifiers\n\ | 
 | 11611 | such as \"def\" and \"class\".\n"); | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11612 |  | 
 | 11613 | static PyObject* | 
 | 11614 | unicode_isidentifier(PyObject *self) | 
 | 11615 | { | 
 | 11616 |     return PyBool_FromLong(PyUnicode_IsIdentifier(self)); | 
 | 11617 | } | 
 | 11618 |  | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11619 | PyDoc_STRVAR(isprintable__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11620 |              "S.isprintable() -> bool\n\ | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11621 | \n\ | 
 | 11622 | Return True if all characters in S are considered\n\ | 
 | 11623 | printable in repr() or S is empty, False otherwise."); | 
 | 11624 |  | 
 | 11625 | static PyObject* | 
 | 11626 | unicode_isprintable(PyObject *self) | 
 | 11627 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11628 |     Py_ssize_t i, length; | 
 | 11629 |     int kind; | 
 | 11630 |     void *data; | 
 | 11631 |  | 
 | 11632 |     if (PyUnicode_READY(self) == -1) | 
 | 11633 |         return NULL; | 
 | 11634 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11635 |     kind = PyUnicode_KIND(self); | 
 | 11636 |     data = PyUnicode_DATA(self); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11637 |  | 
 | 11638 |     /* Shortcut for single character strings */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11639 |     if (length == 1) | 
 | 11640 |         return PyBool_FromLong( | 
 | 11641 |             Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11642 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11643 |     for (i = 0; i < length; i++) { | 
 | 11644 |         if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11645 |             Py_RETURN_FALSE; | 
 | 11646 |         } | 
 | 11647 |     } | 
 | 11648 |     Py_RETURN_TRUE; | 
 | 11649 | } | 
 | 11650 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11651 | PyDoc_STRVAR(join__doc__, | 
| Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11652 |              "S.join(iterable) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11653 | \n\ | 
 | 11654 | 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] | 11655 | iterable.  The separator between elements is S."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11656 |  | 
 | 11657 | static PyObject* | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11658 | unicode_join(PyObject *self, PyObject *data) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11659 | { | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11660 |     return PyUnicode_Join(self, data); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11661 | } | 
 | 11662 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11663 | static Py_ssize_t | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11664 | unicode_length(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11665 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11666 |     if (PyUnicode_READY(self) == -1) | 
 | 11667 |         return -1; | 
 | 11668 |     return PyUnicode_GET_LENGTH(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11669 | } | 
 | 11670 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11671 | PyDoc_STRVAR(ljust__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11672 |              "S.ljust(width[, fillchar]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11673 | \n\ | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11674 | 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] | 11675 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11676 |  | 
 | 11677 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11678 | unicode_ljust(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11679 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11680 |     Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11681 |     Py_UCS4 fillchar = ' '; | 
 | 11682 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11683 |     if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11684 |         return NULL; | 
 | 11685 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11686 |     if (PyUnicode_READY(self) == -1) | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11687 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11688 |  | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11689 |     if (PyUnicode_GET_LENGTH(self) >= width) | 
 | 11690 |         return unicode_result_unchanged(self); | 
 | 11691 |  | 
 | 11692 |     return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11693 | } | 
 | 11694 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11695 | PyDoc_STRVAR(lower__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11696 |              "S.lower() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11697 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11698 | Return a copy of the string S converted to lowercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11699 |  | 
 | 11700 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11701 | unicode_lower(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11702 | { | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11703 |     if (PyUnicode_READY(self) == -1) | 
 | 11704 |         return NULL; | 
 | 11705 |     if (PyUnicode_IS_ASCII(self)) | 
 | 11706 |         return ascii_upper_or_lower(self, 1); | 
| Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11707 |     return case_operation(self, do_lower); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11708 | } | 
 | 11709 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11710 | #define LEFTSTRIP 0 | 
 | 11711 | #define RIGHTSTRIP 1 | 
 | 11712 | #define BOTHSTRIP 2 | 
 | 11713 |  | 
 | 11714 | /* Arrays indexed by above */ | 
 | 11715 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; | 
 | 11716 |  | 
 | 11717 | #define STRIPNAME(i) (stripformat[i]+3) | 
 | 11718 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11719 | /* externally visible for str.strip(unicode) */ | 
 | 11720 | PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11721 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11722 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11723 |     void *data; | 
 | 11724 |     int kind; | 
 | 11725 |     Py_ssize_t i, j, len; | 
 | 11726 |     BLOOM_MASK sepmask; | 
| Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11727 |     Py_ssize_t seplen; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11728 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11729 |     if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) | 
 | 11730 |         return NULL; | 
 | 11731 |  | 
 | 11732 |     kind = PyUnicode_KIND(self); | 
 | 11733 |     data = PyUnicode_DATA(self); | 
 | 11734 |     len = PyUnicode_GET_LENGTH(self); | 
| Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11735 |     seplen = PyUnicode_GET_LENGTH(sepobj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11736 |     sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), | 
 | 11737 |                               PyUnicode_DATA(sepobj), | 
| Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11738 |                               seplen); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11739 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11740 |     i = 0; | 
 | 11741 |     if (striptype != RIGHTSTRIP) { | 
| Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11742 |         while (i < len) { | 
 | 11743 |             Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 11744 |             if (!BLOOM(sepmask, ch)) | 
 | 11745 |                 break; | 
 | 11746 |             if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) | 
 | 11747 |                 break; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11748 |             i++; | 
 | 11749 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11750 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11751 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11752 |     j = len; | 
 | 11753 |     if (striptype != LEFTSTRIP) { | 
| Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11754 |         j--; | 
 | 11755 |         while (j >= i) { | 
 | 11756 |             Py_UCS4 ch = PyUnicode_READ(kind, data, j); | 
 | 11757 |             if (!BLOOM(sepmask, ch)) | 
 | 11758 |                 break; | 
 | 11759 |             if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) | 
 | 11760 |                 break; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11761 |             j--; | 
| Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11762 |         } | 
 | 11763 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11764 |         j++; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11765 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11766 |  | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11767 |     return PyUnicode_Substring(self, i, j); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11768 | } | 
 | 11769 |  | 
 | 11770 | PyObject* | 
 | 11771 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) | 
 | 11772 | { | 
 | 11773 |     unsigned char *data; | 
 | 11774 |     int kind; | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11775 |     Py_ssize_t length; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11776 |  | 
| Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11777 |     if (PyUnicode_READY(self) == -1) | 
 | 11778 |         return NULL; | 
 | 11779 |  | 
| Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11780 |     length = PyUnicode_GET_LENGTH(self); | 
 | 11781 |     end = Py_MIN(end, length); | 
| Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11782 |  | 
| Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11783 |     if (start == 0 && end == length) | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11784 |         return unicode_result_unchanged(self); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11785 |  | 
| Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11786 |     if (start < 0 || end < 0) { | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11787 |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | 
 | 11788 |         return NULL; | 
 | 11789 |     } | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11790 |     if (start >= length || end < start) | 
 | 11791 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11792 |  | 
| Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11793 |     length = end - start; | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11794 |     if (PyUnicode_IS_ASCII(self)) { | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11795 |         data = PyUnicode_1BYTE_DATA(self); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11796 |         return _PyUnicode_FromASCII((char*)(data + start), length); | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11797 |     } | 
 | 11798 |     else { | 
 | 11799 |         kind = PyUnicode_KIND(self); | 
 | 11800 |         data = PyUnicode_1BYTE_DATA(self); | 
 | 11801 |         return PyUnicode_FromKindAndData(kind, | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11802 |                                          data + kind * start, | 
| Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11803 |                                          length); | 
 | 11804 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11805 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11806 |  | 
 | 11807 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11808 | do_strip(PyObject *self, int striptype) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11809 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11810 |     Py_ssize_t len, i, j; | 
 | 11811 |  | 
 | 11812 |     if (PyUnicode_READY(self) == -1) | 
 | 11813 |         return NULL; | 
 | 11814 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11815 |     len = PyUnicode_GET_LENGTH(self); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11816 |  | 
| Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11817 |     if (PyUnicode_IS_ASCII(self)) { | 
 | 11818 |         Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); | 
 | 11819 |  | 
 | 11820 |         i = 0; | 
 | 11821 |         if (striptype != RIGHTSTRIP) { | 
 | 11822 |             while (i < len) { | 
| Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 11823 |                 Py_UCS1 ch = data[i]; | 
| Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11824 |                 if (!_Py_ascii_whitespace[ch]) | 
 | 11825 |                     break; | 
 | 11826 |                 i++; | 
 | 11827 |             } | 
 | 11828 |         } | 
 | 11829 |  | 
 | 11830 |         j = len; | 
 | 11831 |         if (striptype != LEFTSTRIP) { | 
 | 11832 |             j--; | 
 | 11833 |             while (j >= i) { | 
| Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 11834 |                 Py_UCS1 ch = data[j]; | 
| Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11835 |                 if (!_Py_ascii_whitespace[ch]) | 
 | 11836 |                     break; | 
 | 11837 |                 j--; | 
 | 11838 |             } | 
 | 11839 |             j++; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11840 |         } | 
 | 11841 |     } | 
| Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11842 |     else { | 
 | 11843 |         int kind = PyUnicode_KIND(self); | 
 | 11844 |         void *data = PyUnicode_DATA(self); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11845 |  | 
| Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11846 |         i = 0; | 
 | 11847 |         if (striptype != RIGHTSTRIP) { | 
 | 11848 |             while (i < len) { | 
 | 11849 |                 Py_UCS4 ch = PyUnicode_READ(kind, data, i); | 
 | 11850 |                 if (!Py_UNICODE_ISSPACE(ch)) | 
 | 11851 |                     break; | 
 | 11852 |                 i++; | 
 | 11853 |             } | 
| Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 11854 |         } | 
| Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11855 |  | 
 | 11856 |         j = len; | 
 | 11857 |         if (striptype != LEFTSTRIP) { | 
 | 11858 |             j--; | 
 | 11859 |             while (j >= i) { | 
 | 11860 |                 Py_UCS4 ch = PyUnicode_READ(kind, data, j); | 
 | 11861 |                 if (!Py_UNICODE_ISSPACE(ch)) | 
 | 11862 |                     break; | 
 | 11863 |                 j--; | 
 | 11864 |             } | 
 | 11865 |             j++; | 
 | 11866 |         } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11867 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11868 |  | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11869 |     return PyUnicode_Substring(self, i, j); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | } | 
 | 11871 |  | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11872 |  | 
 | 11873 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11874 | do_argstrip(PyObject *self, int striptype, PyObject *args) | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11875 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11876 |     PyObject *sep = NULL; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11877 |  | 
| Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 11878 |     if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11879 |         return NULL; | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11880 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11881 |     if (sep != NULL && sep != Py_None) { | 
 | 11882 |         if (PyUnicode_Check(sep)) | 
 | 11883 |             return _PyUnicode_XStrip(self, striptype, sep); | 
 | 11884 |         else { | 
 | 11885 |             PyErr_Format(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11886 |                          "%s arg must be None or str", | 
 | 11887 |                          STRIPNAME(striptype)); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11888 |             return NULL; | 
 | 11889 |         } | 
 | 11890 |     } | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11891 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11892 |     return do_strip(self, striptype); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11893 | } | 
 | 11894 |  | 
 | 11895 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11896 | PyDoc_STRVAR(strip__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11897 |              "S.strip([chars]) -> str\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11898 | \n\ | 
 | 11899 | Return a copy of the string S with leading and trailing\n\ | 
 | 11900 | whitespace removed.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11901 | 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] | 11902 |  | 
 | 11903 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11904 | unicode_strip(PyObject *self, PyObject *args) | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11905 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11906 |     if (PyTuple_GET_SIZE(args) == 0) | 
 | 11907 |         return do_strip(self, BOTHSTRIP); /* Common case */ | 
 | 11908 |     else | 
 | 11909 |         return do_argstrip(self, BOTHSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11910 | } | 
 | 11911 |  | 
 | 11912 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11913 | PyDoc_STRVAR(lstrip__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11914 |              "S.lstrip([chars]) -> str\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11915 | \n\ | 
 | 11916 | Return a copy of the string S with leading whitespace removed.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11917 | 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] | 11918 |  | 
 | 11919 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11920 | unicode_lstrip(PyObject *self, PyObject *args) | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11921 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11922 |     if (PyTuple_GET_SIZE(args) == 0) | 
 | 11923 |         return do_strip(self, LEFTSTRIP); /* Common case */ | 
 | 11924 |     else | 
 | 11925 |         return do_argstrip(self, LEFTSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11926 | } | 
 | 11927 |  | 
 | 11928 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11929 | PyDoc_STRVAR(rstrip__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11930 |              "S.rstrip([chars]) -> str\n\ | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11931 | \n\ | 
 | 11932 | Return a copy of the string S with trailing whitespace removed.\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11933 | 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] | 11934 |  | 
 | 11935 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11936 | unicode_rstrip(PyObject *self, PyObject *args) | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11937 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11938 |     if (PyTuple_GET_SIZE(args) == 0) | 
 | 11939 |         return do_strip(self, RIGHTSTRIP); /* Common case */ | 
 | 11940 |     else | 
 | 11941 |         return do_argstrip(self, RIGHTSTRIP, args); | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11942 | } | 
 | 11943 |  | 
 | 11944 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11945 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11946 | unicode_repeat(PyObject *str, Py_ssize_t len) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11947 | { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11948 |     PyObject *u; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11949 |     Py_ssize_t nchars, n; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11950 |  | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 11951 |     if (len < 1) | 
 | 11952 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11953 |  | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11954 |     /* no repeat, return original string */ | 
 | 11955 |     if (len == 1) | 
 | 11956 |         return unicode_result_unchanged(str); | 
| Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11957 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11958 |     if (PyUnicode_READY(str) == -1) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11959 |         return NULL; | 
 | 11960 |  | 
| Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11961 |     if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11962 |         PyErr_SetString(PyExc_OverflowError, | 
 | 11963 |                         "repeated string is too long"); | 
 | 11964 |         return NULL; | 
 | 11965 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11966 |     nchars = len * PyUnicode_GET_LENGTH(str); | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11967 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11968 |     u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11969 |     if (!u) | 
 | 11970 |         return NULL; | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11971 |     assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11972 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11973 |     if (PyUnicode_GET_LENGTH(str) == 1) { | 
 | 11974 |         const int kind = PyUnicode_KIND(str); | 
 | 11975 |         const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); | 
| Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11976 |         if (kind == PyUnicode_1BYTE_KIND) { | 
 | 11977 |             void *to = PyUnicode_DATA(u); | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11978 |             memset(to, (unsigned char)fill_char, len); | 
| Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11979 |         } | 
 | 11980 |         else if (kind == PyUnicode_2BYTE_KIND) { | 
 | 11981 |             Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11982 |             for (n = 0; n < len; ++n) | 
| Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11983 |                 ucs2[n] = fill_char; | 
 | 11984 |         } else { | 
 | 11985 |             Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); | 
 | 11986 |             assert(kind == PyUnicode_4BYTE_KIND); | 
 | 11987 |             for (n = 0; n < len; ++n) | 
 | 11988 |                 ucs4[n] = fill_char; | 
| Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11989 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11990 |     } | 
 | 11991 |     else { | 
 | 11992 |         /* number of characters copied this far */ | 
 | 11993 |         Py_ssize_t done = PyUnicode_GET_LENGTH(str); | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11994 |         const Py_ssize_t char_size = PyUnicode_KIND(str); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11995 |         char *to = (char *) PyUnicode_DATA(u); | 
 | 11996 |         Py_MEMCPY(to, PyUnicode_DATA(str), | 
 | 11997 |                   PyUnicode_GET_LENGTH(str) * char_size); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11998 |         while (done < nchars) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11999 |             n = (done <= nchars-done) ? done : nchars-done; | 
 | 12000 |             Py_MEMCPY(to + (done * char_size), to, n * char_size); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12001 |             done += n; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12002 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12003 |     } | 
 | 12004 |  | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12005 |     assert(_PyUnicode_CheckConsistency(u, 1)); | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12006 |     return u; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12007 | } | 
 | 12008 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12009 | PyObject * | 
 | 12010 | PyUnicode_Replace(PyObject *obj, | 
 | 12011 |                   PyObject *subobj, | 
 | 12012 |                   PyObject *replobj, | 
 | 12013 |                   Py_ssize_t maxcount) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12014 | { | 
 | 12015 |     PyObject *self; | 
 | 12016 |     PyObject *str1; | 
 | 12017 |     PyObject *str2; | 
 | 12018 |     PyObject *result; | 
 | 12019 |  | 
 | 12020 |     self = PyUnicode_FromObject(obj); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12021 |     if (self == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12022 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12023 |     str1 = PyUnicode_FromObject(subobj); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12024 |     if (str1 == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12025 |         Py_DECREF(self); | 
 | 12026 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12027 |     } | 
 | 12028 |     str2 = PyUnicode_FromObject(replobj); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12029 |     if (str2 == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12030 |         Py_DECREF(self); | 
 | 12031 |         Py_DECREF(str1); | 
 | 12032 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12033 |     } | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12034 |     if (PyUnicode_READY(self) == -1 || | 
 | 12035 |         PyUnicode_READY(str1) == -1 || | 
 | 12036 |         PyUnicode_READY(str2) == -1) | 
 | 12037 |         result = NULL; | 
 | 12038 |     else | 
 | 12039 |         result = replace(self, str1, str2, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12040 |     Py_DECREF(self); | 
 | 12041 |     Py_DECREF(str1); | 
 | 12042 |     Py_DECREF(str2); | 
 | 12043 |     return result; | 
 | 12044 | } | 
 | 12045 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12046 | PyDoc_STRVAR(replace__doc__, | 
| Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12047 |              "S.replace(old, new[, count]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12048 | \n\ | 
 | 12049 | Return a copy of S with all occurrences of substring\n\ | 
| Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12050 | old replaced by new.  If the optional argument count is\n\ | 
 | 12051 | given, only the first count occurrences are replaced."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12052 |  | 
 | 12053 | static PyObject* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12054 | unicode_replace(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12055 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12056 |     PyObject *str1; | 
 | 12057 |     PyObject *str2; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12058 |     Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12059 |     PyObject *result; | 
 | 12060 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12061 |     if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12062 |         return NULL; | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12063 |     if (PyUnicode_READY(self) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12064 |         return NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12065 |     str1 = PyUnicode_FromObject(str1); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12066 |     if (str1 == NULL) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12067 |         return NULL; | 
 | 12068 |     str2 = PyUnicode_FromObject(str2); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12069 |     if (str2 == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12070 |         Py_DECREF(str1); | 
 | 12071 |         return NULL; | 
| Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12072 |     } | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12073 |     if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) | 
 | 12074 |         result = NULL; | 
 | 12075 |     else | 
 | 12076 |         result = replace(self, str1, str2, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12077 |  | 
 | 12078 |     Py_DECREF(str1); | 
 | 12079 |     Py_DECREF(str2); | 
 | 12080 |     return result; | 
 | 12081 | } | 
 | 12082 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12083 | static PyObject * | 
 | 12084 | unicode_repr(PyObject *unicode) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12085 | { | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12086 |     PyObject *repr; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12087 |     Py_ssize_t isize; | 
 | 12088 |     Py_ssize_t osize, squote, dquote, i, o; | 
 | 12089 |     Py_UCS4 max, quote; | 
| Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12090 |     int ikind, okind, unchanged; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12091 |     void *idata, *odata; | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12092 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12093 |     if (PyUnicode_READY(unicode) == -1) | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12094 |         return NULL; | 
 | 12095 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12096 |     isize = PyUnicode_GET_LENGTH(unicode); | 
 | 12097 |     idata = PyUnicode_DATA(unicode); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12098 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12099 |     /* Compute length of output, quote characters, and | 
 | 12100 |        maximum character */ | 
| Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12101 |     osize = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12102 |     max = 127; | 
 | 12103 |     squote = dquote = 0; | 
 | 12104 |     ikind = PyUnicode_KIND(unicode); | 
 | 12105 |     for (i = 0; i < isize; i++) { | 
 | 12106 |         Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); | 
 | 12107 |         switch (ch) { | 
 | 12108 |         case '\'': squote++; osize++; break; | 
 | 12109 |         case '"':  dquote++; osize++; break; | 
 | 12110 |         case '\\': case '\t': case '\r': case '\n': | 
 | 12111 |             osize += 2; break; | 
 | 12112 |         default: | 
 | 12113 |             /* Fast-path ASCII */ | 
 | 12114 |             if (ch < ' ' || ch == 0x7f) | 
 | 12115 |                 osize += 4; /* \xHH */ | 
 | 12116 |             else if (ch < 0x7f) | 
 | 12117 |                 osize++; | 
 | 12118 |             else if (Py_UNICODE_ISPRINTABLE(ch)) { | 
 | 12119 |                 osize++; | 
 | 12120 |                 max = ch > max ? ch : max; | 
 | 12121 |             } | 
 | 12122 |             else if (ch < 0x100) | 
 | 12123 |                 osize += 4; /* \xHH */ | 
 | 12124 |             else if (ch < 0x10000) | 
 | 12125 |                 osize += 6; /* \uHHHH */ | 
 | 12126 |             else | 
 | 12127 |                 osize += 10; /* \uHHHHHHHH */ | 
 | 12128 |         } | 
 | 12129 |     } | 
 | 12130 |  | 
 | 12131 |     quote = '\''; | 
| Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12132 |     unchanged = (osize == isize); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12133 |     if (squote) { | 
| Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12134 |         unchanged = 0; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12135 |         if (dquote) | 
 | 12136 |             /* Both squote and dquote present. Use squote, | 
 | 12137 |                and escape them */ | 
 | 12138 |             osize += squote; | 
 | 12139 |         else | 
 | 12140 |             quote = '"'; | 
 | 12141 |     } | 
| Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12142 |     osize += 2;   /* quotes */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12143 |  | 
 | 12144 |     repr = PyUnicode_New(osize, max); | 
 | 12145 |     if (repr == NULL) | 
 | 12146 |         return NULL; | 
 | 12147 |     okind = PyUnicode_KIND(repr); | 
 | 12148 |     odata = PyUnicode_DATA(repr); | 
 | 12149 |  | 
 | 12150 |     PyUnicode_WRITE(okind, odata, 0, quote); | 
 | 12151 |     PyUnicode_WRITE(okind, odata, osize-1, quote); | 
| Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12152 |     if (unchanged) { | 
 | 12153 |         _PyUnicode_FastCopyCharacters(repr, 1, | 
 | 12154 |                                       unicode, 0, | 
 | 12155 |                                       isize); | 
 | 12156 |     } | 
 | 12157 |     else { | 
 | 12158 |         for (i = 0, o = 1; i < isize; i++) { | 
 | 12159 |             Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12160 |  | 
| Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12161 |             /* Escape quotes and backslashes */ | 
 | 12162 |             if ((ch == quote) || (ch == '\\')) { | 
| Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12163 |                 PyUnicode_WRITE(okind, odata, o++, '\\'); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12164 |                 PyUnicode_WRITE(okind, odata, o++, ch); | 
| Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12165 |                 continue; | 
 | 12166 |             } | 
 | 12167 |  | 
 | 12168 |             /* Map special whitespace to '\t', \n', '\r' */ | 
 | 12169 |             if (ch == '\t') { | 
 | 12170 |                 PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 12171 |                 PyUnicode_WRITE(okind, odata, o++, 't'); | 
 | 12172 |             } | 
 | 12173 |             else if (ch == '\n') { | 
 | 12174 |                 PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 12175 |                 PyUnicode_WRITE(okind, odata, o++, 'n'); | 
 | 12176 |             } | 
 | 12177 |             else if (ch == '\r') { | 
 | 12178 |                 PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 12179 |                 PyUnicode_WRITE(okind, odata, o++, 'r'); | 
 | 12180 |             } | 
 | 12181 |  | 
 | 12182 |             /* Map non-printable US ASCII to '\xhh' */ | 
 | 12183 |             else if (ch < ' ' || ch == 0x7F) { | 
 | 12184 |                 PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 12185 |                 PyUnicode_WRITE(okind, odata, o++, 'x'); | 
 | 12186 |                 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); | 
 | 12187 |                 PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); | 
 | 12188 |             } | 
 | 12189 |  | 
 | 12190 |             /* Copy ASCII characters as-is */ | 
 | 12191 |             else if (ch < 0x7F) { | 
 | 12192 |                 PyUnicode_WRITE(okind, odata, o++, ch); | 
 | 12193 |             } | 
 | 12194 |  | 
 | 12195 |             /* Non-ASCII characters */ | 
 | 12196 |             else { | 
 | 12197 |                 /* Map Unicode whitespace and control characters | 
 | 12198 |                    (categories Z* and C* except ASCII space) | 
 | 12199 |                 */ | 
 | 12200 |                 if (!Py_UNICODE_ISPRINTABLE(ch)) { | 
 | 12201 |                     PyUnicode_WRITE(okind, odata, o++, '\\'); | 
 | 12202 |                     /* Map 8-bit characters to '\xhh' */ | 
 | 12203 |                     if (ch <= 0xff) { | 
 | 12204 |                         PyUnicode_WRITE(okind, odata, o++, 'x'); | 
 | 12205 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); | 
 | 12206 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); | 
 | 12207 |                     } | 
 | 12208 |                     /* Map 16-bit characters to '\uxxxx' */ | 
 | 12209 |                     else if (ch <= 0xffff) { | 
 | 12210 |                         PyUnicode_WRITE(okind, odata, o++, 'u'); | 
 | 12211 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); | 
 | 12212 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); | 
 | 12213 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); | 
 | 12214 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); | 
 | 12215 |                     } | 
 | 12216 |                     /* Map 21-bit characters to '\U00xxxxxx' */ | 
 | 12217 |                     else { | 
 | 12218 |                         PyUnicode_WRITE(okind, odata, o++, 'U'); | 
 | 12219 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); | 
 | 12220 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); | 
 | 12221 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); | 
 | 12222 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); | 
 | 12223 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); | 
 | 12224 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); | 
 | 12225 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); | 
 | 12226 |                         PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); | 
 | 12227 |                     } | 
 | 12228 |                 } | 
 | 12229 |                 /* Copy characters as-is */ | 
 | 12230 |                 else { | 
 | 12231 |                     PyUnicode_WRITE(okind, odata, o++, ch); | 
 | 12232 |                 } | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12233 |             } | 
 | 12234 |         } | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12235 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12236 |     /* Closing quote already added at the beginning */ | 
| Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12237 |     assert(_PyUnicode_CheckConsistency(repr, 1)); | 
| Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12238 |     return repr; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12239 | } | 
 | 12240 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12241 | PyDoc_STRVAR(rfind__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12242 |              "S.rfind(sub[, start[, end]]) -> int\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12243 | \n\ | 
 | 12244 | Return the highest index in S where substring sub is found,\n\ | 
| Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12245 | such that sub is contained within S[start:end].  Optional\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12246 | arguments start and end are interpreted as in slice notation.\n\ | 
 | 12247 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12248 | Return -1 on failure."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12249 |  | 
 | 12250 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12251 | unicode_rfind(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12252 | { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12253 |     PyObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12254 |     Py_ssize_t start; | 
 | 12255 |     Py_ssize_t end; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12256 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12258 |     if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, | 
 | 12259 |                                             &start, &end)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12260 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12261 |  | 
| Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12262 |     if (PyUnicode_READY(self) == -1) { | 
 | 12263 |         Py_DECREF(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12264 |         return NULL; | 
| Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12265 |     } | 
 | 12266 |     if (PyUnicode_READY(substring) == -1) { | 
 | 12267 |         Py_DECREF(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12268 |         return NULL; | 
| Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12269 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12270 |  | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12271 |     result = any_find_slice(-1, self, substring, start, end); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12272 |  | 
 | 12273 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12274 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12275 |     if (result == -2) | 
 | 12276 |         return NULL; | 
 | 12277 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12278 |     return PyLong_FromSsize_t(result); | 
| 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(rindex__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12282 |              "S.rindex(sub[, start[, end]]) -> int\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 | 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] | 12285 |  | 
 | 12286 | static PyObject * | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12287 | unicode_rindex(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12288 | { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12289 |     PyObject *substring; | 
| Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12290 |     Py_ssize_t start; | 
 | 12291 |     Py_ssize_t end; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12292 |     Py_ssize_t result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12293 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12294 |     if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, | 
 | 12295 |                                             &start, &end)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12296 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12297 |  | 
| Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12298 |     if (PyUnicode_READY(self) == -1) { | 
 | 12299 |         Py_DECREF(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12300 |         return NULL; | 
| Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12301 |     } | 
 | 12302 |     if (PyUnicode_READY(substring) == -1) { | 
 | 12303 |         Py_DECREF(substring); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12304 |         return NULL; | 
| Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12305 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12306 |  | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12307 |     result = any_find_slice(-1, self, substring, start, end); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12308 |  | 
 | 12309 |     Py_DECREF(substring); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12310 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12311 |     if (result == -2) | 
 | 12312 |         return NULL; | 
 | 12313 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12314 |     if (result < 0) { | 
 | 12315 |         PyErr_SetString(PyExc_ValueError, "substring not found"); | 
 | 12316 |         return NULL; | 
 | 12317 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12318 |  | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12319 |     return PyLong_FromSsize_t(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12320 | } | 
 | 12321 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12322 | PyDoc_STRVAR(rjust__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12323 |              "S.rjust(width[, fillchar]) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12324 | \n\ | 
| Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12325 | 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] | 12326 | done using the specified fill character (default is a space)."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12327 |  | 
 | 12328 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12329 | unicode_rjust(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12330 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12331 |     Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12332 |     Py_UCS4 fillchar = ' '; | 
 | 12333 |  | 
| Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12334 |     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] | 12335 |         return NULL; | 
| Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12336 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12337 |     if (PyUnicode_READY(self) == -1) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12338 |         return NULL; | 
 | 12339 |  | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12340 |     if (PyUnicode_GET_LENGTH(self) >= width) | 
 | 12341 |         return unicode_result_unchanged(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12342 |  | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12343 |     return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12344 | } | 
 | 12345 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12346 | PyObject * | 
 | 12347 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12348 | { | 
 | 12349 |     PyObject *result; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12350 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12351 |     s = PyUnicode_FromObject(s); | 
 | 12352 |     if (s == NULL) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12353 |         return NULL; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12354 |     if (sep != NULL) { | 
 | 12355 |         sep = PyUnicode_FromObject(sep); | 
 | 12356 |         if (sep == NULL) { | 
 | 12357 |             Py_DECREF(s); | 
 | 12358 |             return NULL; | 
 | 12359 |         } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12360 |     } | 
 | 12361 |  | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12362 |     result = split(s, sep, maxsplit); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12363 |  | 
 | 12364 |     Py_DECREF(s); | 
 | 12365 |     Py_XDECREF(sep); | 
 | 12366 |     return result; | 
 | 12367 | } | 
 | 12368 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12369 | PyDoc_STRVAR(split__doc__, | 
| Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12370 |              "S.split(sep=None, maxsplit=-1) -> list of strings\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12371 | \n\ | 
 | 12372 | Return a list of the words in S, using sep as the\n\ | 
 | 12373 | delimiter string.  If maxsplit is given, at most maxsplit\n\ | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12374 | 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] | 12375 | whitespace string is a separator and empty strings are\n\ | 
 | 12376 | removed from the result."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12377 |  | 
 | 12378 | static PyObject* | 
| Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12379 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12380 | { | 
| Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12381 |     static char *kwlist[] = {"sep", "maxsplit", 0}; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12382 |     PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12383 |     Py_ssize_t maxcount = -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12384 |  | 
| Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12385 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", | 
 | 12386 |                                      kwlist, &substring, &maxcount)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12387 |         return NULL; | 
 | 12388 |  | 
 | 12389 |     if (substring == Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12390 |         return split(self, NULL, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12391 |     else if (PyUnicode_Check(substring)) | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12392 |         return split(self, substring, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12393 |     else | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12394 |         return PyUnicode_Split(self, substring, maxcount); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12395 | } | 
 | 12396 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12397 | PyObject * | 
 | 12398 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) | 
 | 12399 | { | 
 | 12400 |     PyObject* str_obj; | 
 | 12401 |     PyObject* sep_obj; | 
 | 12402 |     PyObject* out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12403 |     int kind1, kind2, kind; | 
 | 12404 |     void *buf1 = NULL, *buf2 = NULL; | 
 | 12405 |     Py_ssize_t len1, len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12406 |  | 
 | 12407 |     str_obj = PyUnicode_FromObject(str_in); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12408 |     if (!str_obj) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12409 |         return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12410 |     sep_obj = PyUnicode_FromObject(sep_in); | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12411 |     if (!sep_obj) { | 
 | 12412 |         Py_DECREF(str_obj); | 
 | 12413 |         return NULL; | 
 | 12414 |     } | 
 | 12415 |     if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { | 
 | 12416 |         Py_DECREF(sep_obj); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12417 |         Py_DECREF(str_obj); | 
 | 12418 |         return NULL; | 
 | 12419 |     } | 
 | 12420 |  | 
| Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12421 |     kind1 = PyUnicode_KIND(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12422 |     kind2 = PyUnicode_KIND(sep_obj); | 
| Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12423 |     kind = Py_MAX(kind1, kind2); | 
 | 12424 |     buf1 = PyUnicode_DATA(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12425 |     if (kind1 != kind) | 
| Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12426 |         buf1 = _PyUnicode_AsKind(str_obj, kind); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12427 |     if (!buf1) | 
 | 12428 |         goto onError; | 
 | 12429 |     buf2 = PyUnicode_DATA(sep_obj); | 
 | 12430 |     if (kind2 != kind) | 
 | 12431 |         buf2 = _PyUnicode_AsKind(sep_obj, kind); | 
 | 12432 |     if (!buf2) | 
 | 12433 |         goto onError; | 
 | 12434 |     len1 = PyUnicode_GET_LENGTH(str_obj); | 
 | 12435 |     len2 = PyUnicode_GET_LENGTH(sep_obj); | 
 | 12436 |  | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12437 |     switch (PyUnicode_KIND(str_obj)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12438 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12439 |         if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) | 
 | 12440 |             out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 12441 |         else | 
 | 12442 |             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] | 12443 |         break; | 
 | 12444 |     case PyUnicode_2BYTE_KIND: | 
 | 12445 |         out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 12446 |         break; | 
 | 12447 |     case PyUnicode_4BYTE_KIND: | 
 | 12448 |         out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 12449 |         break; | 
 | 12450 |     default: | 
 | 12451 |         assert(0); | 
 | 12452 |         out = 0; | 
 | 12453 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12454 |  | 
 | 12455 |     Py_DECREF(sep_obj); | 
 | 12456 |     Py_DECREF(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12457 |     if (kind1 != kind) | 
 | 12458 |         PyMem_Free(buf1); | 
 | 12459 |     if (kind2 != kind) | 
 | 12460 |         PyMem_Free(buf2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12461 |  | 
 | 12462 |     return out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12463 |   onError: | 
 | 12464 |     Py_DECREF(sep_obj); | 
 | 12465 |     Py_DECREF(str_obj); | 
 | 12466 |     if (kind1 != kind && buf1) | 
 | 12467 |         PyMem_Free(buf1); | 
 | 12468 |     if (kind2 != kind && buf2) | 
 | 12469 |         PyMem_Free(buf2); | 
 | 12470 |     return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12471 | } | 
 | 12472 |  | 
 | 12473 |  | 
 | 12474 | PyObject * | 
 | 12475 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) | 
 | 12476 | { | 
 | 12477 |     PyObject* str_obj; | 
 | 12478 |     PyObject* sep_obj; | 
 | 12479 |     PyObject* out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12480 |     int kind1, kind2, kind; | 
 | 12481 |     void *buf1 = NULL, *buf2 = NULL; | 
 | 12482 |     Py_ssize_t len1, len2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12483 |  | 
 | 12484 |     str_obj = PyUnicode_FromObject(str_in); | 
 | 12485 |     if (!str_obj) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12486 |         return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12487 |     sep_obj = PyUnicode_FromObject(sep_in); | 
 | 12488 |     if (!sep_obj) { | 
 | 12489 |         Py_DECREF(str_obj); | 
 | 12490 |         return NULL; | 
 | 12491 |     } | 
 | 12492 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12493 |     kind1 = PyUnicode_KIND(str_in); | 
 | 12494 |     kind2 = PyUnicode_KIND(sep_obj); | 
| Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12495 |     kind = Py_MAX(kind1, kind2); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12496 |     buf1 = PyUnicode_DATA(str_in); | 
 | 12497 |     if (kind1 != kind) | 
 | 12498 |         buf1 = _PyUnicode_AsKind(str_in, kind); | 
 | 12499 |     if (!buf1) | 
 | 12500 |         goto onError; | 
 | 12501 |     buf2 = PyUnicode_DATA(sep_obj); | 
 | 12502 |     if (kind2 != kind) | 
 | 12503 |         buf2 = _PyUnicode_AsKind(sep_obj, kind); | 
 | 12504 |     if (!buf2) | 
 | 12505 |         goto onError; | 
 | 12506 |     len1 = PyUnicode_GET_LENGTH(str_obj); | 
 | 12507 |     len2 = PyUnicode_GET_LENGTH(sep_obj); | 
 | 12508 |  | 
| Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12509 |     switch (PyUnicode_KIND(str_in)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12510 |     case PyUnicode_1BYTE_KIND: | 
| Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12511 |         if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) | 
 | 12512 |             out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 12513 |         else | 
 | 12514 |             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] | 12515 |         break; | 
 | 12516 |     case PyUnicode_2BYTE_KIND: | 
 | 12517 |         out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 12518 |         break; | 
 | 12519 |     case PyUnicode_4BYTE_KIND: | 
 | 12520 |         out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); | 
 | 12521 |         break; | 
 | 12522 |     default: | 
 | 12523 |         assert(0); | 
 | 12524 |         out = 0; | 
 | 12525 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12526 |  | 
 | 12527 |     Py_DECREF(sep_obj); | 
 | 12528 |     Py_DECREF(str_obj); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12529 |     if (kind1 != kind) | 
 | 12530 |         PyMem_Free(buf1); | 
 | 12531 |     if (kind2 != kind) | 
 | 12532 |         PyMem_Free(buf2); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12533 |  | 
 | 12534 |     return out; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12535 |   onError: | 
 | 12536 |     Py_DECREF(sep_obj); | 
 | 12537 |     Py_DECREF(str_obj); | 
 | 12538 |     if (kind1 != kind && buf1) | 
 | 12539 |         PyMem_Free(buf1); | 
 | 12540 |     if (kind2 != kind && buf2) | 
 | 12541 |         PyMem_Free(buf2); | 
 | 12542 |     return NULL; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12543 | } | 
 | 12544 |  | 
 | 12545 | PyDoc_STRVAR(partition__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12546 |              "S.partition(sep) -> (head, sep, tail)\n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12547 | \n\ | 
| Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12548 | 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] | 12549 | 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] | 12550 | found, return S and two empty strings."); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12551 |  | 
 | 12552 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12553 | unicode_partition(PyObject *self, PyObject *separator) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12554 | { | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12555 |     return PyUnicode_Partition(self, separator); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12556 | } | 
 | 12557 |  | 
 | 12558 | PyDoc_STRVAR(rpartition__doc__, | 
| Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12559 |              "S.rpartition(sep) -> (head, sep, tail)\n\ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12560 | \n\ | 
| Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12561 | 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] | 12562 | 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] | 12563 | separator is not found, return two empty strings and S."); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12564 |  | 
 | 12565 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12566 | unicode_rpartition(PyObject *self, PyObject *separator) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12567 | { | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12568 |     return PyUnicode_RPartition(self, separator); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12569 | } | 
 | 12570 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12571 | PyObject * | 
 | 12572 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12573 | { | 
 | 12574 |     PyObject *result; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12575 |  | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12576 |     s = PyUnicode_FromObject(s); | 
 | 12577 |     if (s == NULL) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12578 |         return NULL; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12579 |     if (sep != NULL) { | 
 | 12580 |         sep = PyUnicode_FromObject(sep); | 
 | 12581 |         if (sep == NULL) { | 
 | 12582 |             Py_DECREF(s); | 
 | 12583 |             return NULL; | 
 | 12584 |         } | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12585 |     } | 
 | 12586 |  | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12587 |     result = rsplit(s, sep, maxsplit); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12588 |  | 
 | 12589 |     Py_DECREF(s); | 
 | 12590 |     Py_XDECREF(sep); | 
 | 12591 |     return result; | 
 | 12592 | } | 
 | 12593 |  | 
 | 12594 | PyDoc_STRVAR(rsplit__doc__, | 
| Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12595 |              "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12596 | \n\ | 
 | 12597 | Return a list of the words in S, using sep as the\n\ | 
 | 12598 | delimiter string, starting at the end of the string and\n\ | 
 | 12599 | working to the front.  If maxsplit is given, at most maxsplit\n\ | 
 | 12600 | splits are done. If sep is not specified, any whitespace string\n\ | 
 | 12601 | is a separator."); | 
 | 12602 |  | 
 | 12603 | static PyObject* | 
| Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12604 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12605 | { | 
| Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12606 |     static char *kwlist[] = {"sep", "maxsplit", 0}; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12607 |     PyObject *substring = Py_None; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12608 |     Py_ssize_t maxcount = -1; | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12609 |  | 
| Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12610 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", | 
 | 12611 |                                      kwlist, &substring, &maxcount)) | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12612 |         return NULL; | 
 | 12613 |  | 
 | 12614 |     if (substring == Py_None) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12615 |         return rsplit(self, NULL, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12616 |     else if (PyUnicode_Check(substring)) | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12617 |         return rsplit(self, substring, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12618 |     else | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12619 |         return PyUnicode_RSplit(self, substring, maxcount); | 
| Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12620 | } | 
 | 12621 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12622 | PyDoc_STRVAR(splitlines__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12623 |              "S.splitlines([keepends]) -> list of strings\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12624 | \n\ | 
 | 12625 | 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] | 12626 | 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] | 12627 | is given and true."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12628 |  | 
 | 12629 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12630 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12631 | { | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12632 |     static char *kwlist[] = {"keepends", 0}; | 
| Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12633 |     int keepends = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12634 |  | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12635 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", | 
 | 12636 |                                      kwlist, &keepends)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12637 |         return NULL; | 
 | 12638 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12639 |     return PyUnicode_Splitlines(self, keepends); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12640 | } | 
 | 12641 |  | 
 | 12642 | static | 
| Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12643 | PyObject *unicode_str(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12644 | { | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12645 |     return unicode_result_unchanged(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12646 | } | 
 | 12647 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12648 | PyDoc_STRVAR(swapcase__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12649 |              "S.swapcase() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12650 | \n\ | 
 | 12651 | 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] | 12652 | and vice versa."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12653 |  | 
 | 12654 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12655 | unicode_swapcase(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12656 | { | 
| Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12657 |     if (PyUnicode_READY(self) == -1) | 
 | 12658 |         return NULL; | 
| Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12659 |     return case_operation(self, do_swapcase); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12660 | } | 
 | 12661 |  | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12662 | /*[clinic] | 
 | 12663 | module str | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12664 |  | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12665 | @staticmethod | 
 | 12666 | str.maketrans as unicode_maketrans | 
 | 12667 |  | 
 | 12668 |   x: object | 
 | 12669 |  | 
 | 12670 |   y: unicode=NULL | 
 | 12671 |  | 
 | 12672 |   z: unicode=NULL | 
 | 12673 |  | 
 | 12674 |   / | 
 | 12675 |  | 
 | 12676 | Return a translation table usable for str.translate(). | 
 | 12677 |  | 
 | 12678 | If there is only one argument, it must be a dictionary mapping Unicode | 
 | 12679 | ordinals (integers) or characters to Unicode ordinals, strings or None. | 
 | 12680 | Character keys will be then converted to ordinals. | 
 | 12681 | If there are two arguments, they must be strings of equal length, and | 
 | 12682 | in the resulting dictionary, each character in x will be mapped to the | 
 | 12683 | character at the same position in y. If there is a third argument, it | 
 | 12684 | must be a string, whose characters will be mapped to None in the result. | 
 | 12685 | [clinic]*/ | 
 | 12686 |  | 
 | 12687 | PyDoc_STRVAR(unicode_maketrans__doc__, | 
 | 12688 | "Return a translation table usable for str.translate().\n" | 
 | 12689 | "\n" | 
 | 12690 | "str.maketrans(x, y=None, z=None)\n" | 
 | 12691 | "\n" | 
 | 12692 | "If there is only one argument, it must be a dictionary mapping Unicode\n" | 
 | 12693 | "ordinals (integers) or characters to Unicode ordinals, strings or None.\n" | 
 | 12694 | "Character keys will be then converted to ordinals.\n" | 
 | 12695 | "If there are two arguments, they must be strings of equal length, and\n" | 
 | 12696 | "in the resulting dictionary, each character in x will be mapped to the\n" | 
 | 12697 | "character at the same position in y. If there is a third argument, it\n" | 
 | 12698 | "must be a string, whose characters will be mapped to None in the result."); | 
 | 12699 |  | 
 | 12700 | #define UNICODE_MAKETRANS_METHODDEF    \ | 
 | 12701 |     {"maketrans", (PyCFunction)unicode_maketrans, METH_VARARGS|METH_STATIC, unicode_maketrans__doc__}, | 
 | 12702 |  | 
 | 12703 | static PyObject * | 
 | 12704 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z); | 
 | 12705 |  | 
 | 12706 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12707 | unicode_maketrans(PyObject *null, PyObject *args) | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12708 | { | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12709 |     PyObject *return_value = NULL; | 
 | 12710 |     PyObject *x; | 
 | 12711 |     PyObject *y = NULL; | 
 | 12712 |     PyObject *z = NULL; | 
 | 12713 |  | 
 | 12714 |     if (!PyArg_ParseTuple(args, | 
 | 12715 |         "O|UU:maketrans", | 
 | 12716 |         &x, &y, &z)) | 
 | 12717 |         goto exit; | 
 | 12718 |     return_value = unicode_maketrans_impl(x, y, z); | 
 | 12719 |  | 
 | 12720 | exit: | 
 | 12721 |     return return_value; | 
 | 12722 | } | 
 | 12723 |  | 
 | 12724 | static PyObject * | 
 | 12725 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) | 
 | 12726 | /*[clinic checksum: 137db9c3199e7906b7967009f511c24fa3235b5f]*/ | 
 | 12727 | { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12728 |     PyObject *new = NULL, *key, *value; | 
 | 12729 |     Py_ssize_t i = 0; | 
 | 12730 |     int res; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12731 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12732 |     new = PyDict_New(); | 
 | 12733 |     if (!new) | 
 | 12734 |         return NULL; | 
 | 12735 |     if (y != NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12736 |         int x_kind, y_kind, z_kind; | 
 | 12737 |         void *x_data, *y_data, *z_data; | 
 | 12738 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12739 |         /* x must be a string too, of equal length */ | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12740 |         if (!PyUnicode_Check(x)) { | 
 | 12741 |             PyErr_SetString(PyExc_TypeError, "first maketrans argument must " | 
 | 12742 |                             "be a string if there is a second argument"); | 
 | 12743 |             goto err; | 
 | 12744 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12745 |         if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12746 |             PyErr_SetString(PyExc_ValueError, "the first two maketrans " | 
 | 12747 |                             "arguments must have equal length"); | 
 | 12748 |             goto err; | 
 | 12749 |         } | 
 | 12750 |         /* 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] | 12751 |         x_kind = PyUnicode_KIND(x); | 
 | 12752 |         y_kind = PyUnicode_KIND(y); | 
 | 12753 |         x_data = PyUnicode_DATA(x); | 
 | 12754 |         y_data = PyUnicode_DATA(y); | 
 | 12755 |         for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { | 
 | 12756 |             key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); | 
| Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12757 |             if (!key) | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12758 |                 goto err; | 
| Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12759 |             value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); | 
| Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12760 |             if (!value) { | 
 | 12761 |                 Py_DECREF(key); | 
 | 12762 |                 goto err; | 
 | 12763 |             } | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12764 |             res = PyDict_SetItem(new, key, value); | 
 | 12765 |             Py_DECREF(key); | 
 | 12766 |             Py_DECREF(value); | 
 | 12767 |             if (res < 0) | 
 | 12768 |                 goto err; | 
 | 12769 |         } | 
 | 12770 |         /* create entries for deleting chars in z */ | 
 | 12771 |         if (z != NULL) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12772 |             z_kind = PyUnicode_KIND(z); | 
 | 12773 |             z_data = PyUnicode_DATA(z); | 
| Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12774 |             for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12775 |                 key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12776 |                 if (!key) | 
 | 12777 |                     goto err; | 
 | 12778 |                 res = PyDict_SetItem(new, key, Py_None); | 
 | 12779 |                 Py_DECREF(key); | 
 | 12780 |                 if (res < 0) | 
 | 12781 |                     goto err; | 
 | 12782 |             } | 
 | 12783 |         } | 
 | 12784 |     } else { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12785 |         int kind; | 
 | 12786 |         void *data; | 
 | 12787 |  | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12788 |         /* x must be a dict */ | 
| Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12789 |         if (!PyDict_CheckExact(x)) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12790 |             PyErr_SetString(PyExc_TypeError, "if you give only one argument " | 
 | 12791 |                             "to maketrans it must be a dict"); | 
 | 12792 |             goto err; | 
 | 12793 |         } | 
 | 12794 |         /* copy entries into the new dict, converting string keys to int keys */ | 
 | 12795 |         while (PyDict_Next(x, &i, &key, &value)) { | 
 | 12796 |             if (PyUnicode_Check(key)) { | 
 | 12797 |                 /* convert string keys to integer keys */ | 
 | 12798 |                 PyObject *newkey; | 
| Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12799 |                 if (PyUnicode_GET_LENGTH(key) != 1) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12800 |                     PyErr_SetString(PyExc_ValueError, "string keys in translate " | 
 | 12801 |                                     "table must be of length 1"); | 
 | 12802 |                     goto err; | 
 | 12803 |                 } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12804 |                 kind = PyUnicode_KIND(key); | 
 | 12805 |                 data = PyUnicode_DATA(key); | 
 | 12806 |                 newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12807 |                 if (!newkey) | 
 | 12808 |                     goto err; | 
 | 12809 |                 res = PyDict_SetItem(new, newkey, value); | 
 | 12810 |                 Py_DECREF(newkey); | 
 | 12811 |                 if (res < 0) | 
 | 12812 |                     goto err; | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12813 |             } else if (PyLong_Check(key)) { | 
| Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12814 |                 /* just keep integer keys */ | 
 | 12815 |                 if (PyDict_SetItem(new, key, value) < 0) | 
 | 12816 |                     goto err; | 
 | 12817 |             } else { | 
 | 12818 |                 PyErr_SetString(PyExc_TypeError, "keys in translate table must " | 
 | 12819 |                                 "be strings or integers"); | 
 | 12820 |                 goto err; | 
 | 12821 |             } | 
 | 12822 |         } | 
 | 12823 |     } | 
 | 12824 |     return new; | 
 | 12825 |   err: | 
 | 12826 |     Py_DECREF(new); | 
 | 12827 |     return NULL; | 
 | 12828 | } | 
 | 12829 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12830 | PyDoc_STRVAR(translate__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12831 |              "S.translate(table) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12832 | \n\ | 
 | 12833 | Return a copy of the string S, where all characters have been mapped\n\ | 
 | 12834 | through the given translation table, which must be a mapping of\n\ | 
| Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12835 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ | 
| Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12836 | Unmapped characters are left untouched. Characters mapped to None\n\ | 
 | 12837 | are deleted."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12838 |  | 
 | 12839 | static PyObject* | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12840 | unicode_translate(PyObject *self, PyObject *table) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12841 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12842 |     return _PyUnicode_TranslateCharmap(self, table, "ignore"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12843 | } | 
 | 12844 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12845 | PyDoc_STRVAR(upper__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12846 |              "S.upper() -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12847 | \n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12848 | Return a copy of S converted to uppercase."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12849 |  | 
 | 12850 | static PyObject* | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12851 | unicode_upper(PyObject *self) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12852 | { | 
| Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12853 |     if (PyUnicode_READY(self) == -1) | 
 | 12854 |         return NULL; | 
 | 12855 |     if (PyUnicode_IS_ASCII(self)) | 
 | 12856 |         return ascii_upper_or_lower(self, 0); | 
| Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12857 |     return case_operation(self, do_upper); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12858 | } | 
 | 12859 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12860 | PyDoc_STRVAR(zfill__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12861 |              "S.zfill(width) -> str\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12862 | \n\ | 
| Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12863 | Pad a numeric string S with zeros on the left, to fill a field\n\ | 
 | 12864 | of the specified width. The string S is never truncated."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12865 |  | 
 | 12866 | static PyObject * | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12867 | unicode_zfill(PyObject *self, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12868 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12869 |     Py_ssize_t fill; | 
| Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12870 |     PyObject *u; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12871 |     Py_ssize_t width; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12872 |     int kind; | 
 | 12873 |     void *data; | 
 | 12874 |     Py_UCS4 chr; | 
 | 12875 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12876 |     if (!PyArg_ParseTuple(args, "n:zfill", &width)) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12877 |         return NULL; | 
 | 12878 |  | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12879 |     if (PyUnicode_READY(self) == -1) | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12880 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12881 |  | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12882 |     if (PyUnicode_GET_LENGTH(self) >= width) | 
 | 12883 |         return unicode_result_unchanged(self); | 
 | 12884 |  | 
 | 12885 |     fill = width - PyUnicode_GET_LENGTH(self); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12886 |  | 
 | 12887 |     u = pad(self, fill, 0, '0'); | 
 | 12888 |  | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12889 |     if (u == NULL) | 
 | 12890 |         return NULL; | 
 | 12891 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12892 |     kind = PyUnicode_KIND(u); | 
 | 12893 |     data = PyUnicode_DATA(u); | 
 | 12894 |     chr = PyUnicode_READ(kind, data, fill); | 
 | 12895 |  | 
 | 12896 |     if (chr == '+' || chr == '-') { | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12897 |         /* move sign to beginning of string */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12898 |         PyUnicode_WRITE(kind, data, 0, chr); | 
 | 12899 |         PyUnicode_WRITE(kind, data, fill, '0'); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12900 |     } | 
 | 12901 |  | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12902 |     assert(_PyUnicode_CheckConsistency(u, 1)); | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12903 |     return u; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12904 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12905 |  | 
 | 12906 | #if 0 | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12907 | static PyObject * | 
 | 12908 | unicode__decimal2ascii(PyObject *self) | 
 | 12909 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12910 |     return PyUnicode_TransformDecimalAndSpaceToASCII(self); | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12911 | } | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12912 | #endif | 
 | 12913 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12914 | PyDoc_STRVAR(startswith__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12915 |              "S.startswith(prefix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12916 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12917 | Return True if S starts with the specified prefix, False otherwise.\n\ | 
 | 12918 | With optional start, test S beginning at that position.\n\ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12919 | With optional end, stop comparing S at that position.\n\ | 
 | 12920 | prefix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12921 |  | 
 | 12922 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12923 | unicode_startswith(PyObject *self, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12924 |                    PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12925 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12926 |     PyObject *subobj; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12927 |     PyObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12928 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12929 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12930 |     int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12931 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12932 |     if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12933 |         return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12934 |     if (PyTuple_Check(subobj)) { | 
 | 12935 |         Py_ssize_t i; | 
 | 12936 |         for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12937 |             substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12938 |             if (substring == NULL) | 
 | 12939 |                 return NULL; | 
 | 12940 |             result = tailmatch(self, substring, start, end, -1); | 
 | 12941 |             Py_DECREF(substring); | 
| Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12942 |             if (result == -1) | 
 | 12943 |                 return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12944 |             if (result) { | 
 | 12945 |                 Py_RETURN_TRUE; | 
 | 12946 |             } | 
 | 12947 |         } | 
 | 12948 |         /* nothing matched */ | 
 | 12949 |         Py_RETURN_FALSE; | 
 | 12950 |     } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12951 |     substring = PyUnicode_FromObject(subobj); | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12952 |     if (substring == NULL) { | 
 | 12953 |         if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 12954 |             PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " | 
 | 12955 |                          "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12956 |         return NULL; | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12957 |     } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12958 |     result = tailmatch(self, substring, start, end, -1); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12959 |     Py_DECREF(substring); | 
| Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12960 |     if (result == -1) | 
 | 12961 |         return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12962 |     return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12963 | } | 
 | 12964 |  | 
 | 12965 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12966 | PyDoc_STRVAR(endswith__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12967 |              "S.endswith(suffix[, start[, end]]) -> bool\n\ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12968 | \n\ | 
| Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12969 | Return True if S ends with the specified suffix, False otherwise.\n\ | 
 | 12970 | With optional start, test S beginning at that position.\n\ | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12971 | With optional end, stop comparing S at that position.\n\ | 
 | 12972 | suffix can also be a tuple of strings to try."); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12973 |  | 
 | 12974 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12975 | unicode_endswith(PyObject *self, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12976 |                  PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12977 | { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12978 |     PyObject *subobj; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12979 |     PyObject *substring; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12980 |     Py_ssize_t start = 0; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12981 |     Py_ssize_t end = PY_SSIZE_T_MAX; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12982 |     int result; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12983 |  | 
| Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12984 |     if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12985 |         return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12986 |     if (PyTuple_Check(subobj)) { | 
 | 12987 |         Py_ssize_t i; | 
 | 12988 |         for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12989 |             substring = PyUnicode_FromObject( | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12990 |                 PyTuple_GET_ITEM(subobj, i)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12991 |             if (substring == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12992 |                 return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12993 |             result = tailmatch(self, substring, start, end, +1); | 
 | 12994 |             Py_DECREF(substring); | 
| Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12995 |             if (result == -1) | 
 | 12996 |                 return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12997 |             if (result) { | 
 | 12998 |                 Py_RETURN_TRUE; | 
 | 12999 |             } | 
 | 13000 |         } | 
 | 13001 |         Py_RETURN_FALSE; | 
 | 13002 |     } | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13003 |     substring = PyUnicode_FromObject(subobj); | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13004 |     if (substring == NULL) { | 
 | 13005 |         if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 13006 |             PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " | 
 | 13007 |                          "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13008 |         return NULL; | 
| Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13009 |     } | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13010 |     result = tailmatch(self, substring, start, end, +1); | 
| Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13011 |     Py_DECREF(substring); | 
| Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13012 |     if (result == -1) | 
 | 13013 |         return NULL; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13014 |     return PyBool_FromLong(result); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13015 | } | 
 | 13016 |  | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13017 | Py_LOCAL_INLINE(void) | 
| Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13018 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13019 | { | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13020 |     if (!writer->readonly) | 
 | 13021 |         writer->size = PyUnicode_GET_LENGTH(writer->buffer); | 
 | 13022 |     else { | 
 | 13023 |         /* Copy-on-write mode: set buffer size to 0 so | 
 | 13024 |          * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on | 
 | 13025 |          * next write. */ | 
 | 13026 |         writer->size = 0; | 
 | 13027 |     } | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13028 |     writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); | 
 | 13029 |     writer->data = PyUnicode_DATA(writer->buffer); | 
 | 13030 |     writer->kind = PyUnicode_KIND(writer->buffer); | 
 | 13031 | } | 
 | 13032 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13033 | void | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13034 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13035 | { | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13036 |     memset(writer, 0, sizeof(*writer)); | 
 | 13037 | #ifdef Py_DEBUG | 
 | 13038 |     writer->kind = 5;    /* invalid kind */ | 
 | 13039 | #endif | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13040 |     writer->min_char = 127; | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13041 | } | 
 | 13042 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13043 | int | 
 | 13044 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, | 
 | 13045 |                                  Py_ssize_t length, Py_UCS4 maxchar) | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13046 | { | 
 | 13047 |     Py_ssize_t newlen; | 
 | 13048 |     PyObject *newbuffer; | 
 | 13049 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13050 |     assert(length > 0); | 
 | 13051 |  | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13052 |     if (length > PY_SSIZE_T_MAX - writer->pos) { | 
 | 13053 |         PyErr_NoMemory(); | 
 | 13054 |         return -1; | 
 | 13055 |     } | 
 | 13056 |     newlen = writer->pos + length; | 
 | 13057 |  | 
| Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13058 |     maxchar = Py_MAX(maxchar, writer->min_char); | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13059 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13060 |     if (writer->buffer == NULL) { | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13061 |         assert(!writer->readonly); | 
 | 13062 |         if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) { | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13063 |             /* overallocate 25% to limit the number of resize */ | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13064 |             newlen += newlen / 4; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13065 |         } | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13066 |         if (newlen < writer->min_length) | 
 | 13067 |             newlen = writer->min_length; | 
 | 13068 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13069 |         writer->buffer = PyUnicode_New(newlen, maxchar); | 
 | 13070 |         if (writer->buffer == NULL) | 
 | 13071 |             return -1; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13072 |     } | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13073 |     else if (newlen > writer->size) { | 
 | 13074 |         if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) { | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13075 |             /* overallocate 25% to limit the number of resize */ | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13076 |             newlen += newlen / 4; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13077 |         } | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13078 |         if (newlen < writer->min_length) | 
 | 13079 |             newlen = writer->min_length; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13080 |  | 
| Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13081 |         if (maxchar > writer->maxchar || writer->readonly) { | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13082 |             /* resize + widen */ | 
 | 13083 |             newbuffer = PyUnicode_New(newlen, maxchar); | 
 | 13084 |             if (newbuffer == NULL) | 
 | 13085 |                 return -1; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13086 |             _PyUnicode_FastCopyCharacters(newbuffer, 0, | 
 | 13087 |                                           writer->buffer, 0, writer->pos); | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13088 |             Py_DECREF(writer->buffer); | 
| Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13089 |             writer->readonly = 0; | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13090 |         } | 
 | 13091 |         else { | 
 | 13092 |             newbuffer = resize_compact(writer->buffer, newlen); | 
 | 13093 |             if (newbuffer == NULL) | 
 | 13094 |                 return -1; | 
 | 13095 |         } | 
 | 13096 |         writer->buffer = newbuffer; | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13097 |     } | 
 | 13098 |     else if (maxchar > writer->maxchar) { | 
| Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13099 |         assert(!writer->readonly); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13100 |         newbuffer = PyUnicode_New(writer->size, maxchar); | 
 | 13101 |         if (newbuffer == NULL) | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13102 |             return -1; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13103 |         _PyUnicode_FastCopyCharacters(newbuffer, 0, | 
 | 13104 |                                       writer->buffer, 0, writer->pos); | 
 | 13105 |         Py_DECREF(writer->buffer); | 
 | 13106 |         writer->buffer = newbuffer; | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13107 |     } | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13108 |     _PyUnicodeWriter_Update(writer); | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13109 |     return 0; | 
 | 13110 | } | 
 | 13111 |  | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13112 | Py_LOCAL_INLINE(int) | 
 | 13113 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) | 
| Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13114 | { | 
 | 13115 |     if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) | 
 | 13116 |         return -1; | 
 | 13117 |     PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); | 
 | 13118 |     writer->pos++; | 
 | 13119 |     return 0; | 
 | 13120 | } | 
 | 13121 |  | 
 | 13122 | int | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13123 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) | 
 | 13124 | { | 
 | 13125 |     return _PyUnicodeWriter_WriteCharInline(writer, ch); | 
 | 13126 | } | 
 | 13127 |  | 
 | 13128 | int | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13129 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) | 
 | 13130 | { | 
 | 13131 |     Py_UCS4 maxchar; | 
 | 13132 |     Py_ssize_t len; | 
 | 13133 |  | 
 | 13134 |     if (PyUnicode_READY(str) == -1) | 
 | 13135 |         return -1; | 
 | 13136 |     len = PyUnicode_GET_LENGTH(str); | 
 | 13137 |     if (len == 0) | 
 | 13138 |         return 0; | 
 | 13139 |     maxchar = PyUnicode_MAX_CHAR_VALUE(str); | 
 | 13140 |     if (maxchar > writer->maxchar || len > writer->size - writer->pos) { | 
| Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13141 |         if (writer->buffer == NULL && !writer->overallocate) { | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13142 |             writer->readonly = 1; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13143 |             Py_INCREF(str); | 
 | 13144 |             writer->buffer = str; | 
 | 13145 |             _PyUnicodeWriter_Update(writer); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13146 |             writer->pos += len; | 
 | 13147 |             return 0; | 
 | 13148 |         } | 
 | 13149 |         if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) | 
 | 13150 |             return -1; | 
 | 13151 |     } | 
 | 13152 |     _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, | 
 | 13153 |                                   str, 0, len); | 
 | 13154 |     writer->pos += len; | 
 | 13155 |     return 0; | 
 | 13156 | } | 
 | 13157 |  | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13158 | int | 
| Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13159 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, | 
 | 13160 |                                 Py_ssize_t start, Py_ssize_t end) | 
 | 13161 | { | 
 | 13162 |     Py_UCS4 maxchar; | 
 | 13163 |     Py_ssize_t len; | 
 | 13164 |  | 
 | 13165 |     if (PyUnicode_READY(str) == -1) | 
 | 13166 |         return -1; | 
 | 13167 |  | 
 | 13168 |     assert(0 <= start); | 
 | 13169 |     assert(end <= PyUnicode_GET_LENGTH(str)); | 
 | 13170 |     assert(start <= end); | 
 | 13171 |  | 
 | 13172 |     if (end == 0) | 
 | 13173 |         return 0; | 
 | 13174 |  | 
 | 13175 |     if (start == 0 && end == PyUnicode_GET_LENGTH(str)) | 
 | 13176 |         return _PyUnicodeWriter_WriteStr(writer, str); | 
 | 13177 |  | 
 | 13178 |     if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) | 
 | 13179 |         maxchar = _PyUnicode_FindMaxChar(str, start, end); | 
 | 13180 |     else | 
 | 13181 |         maxchar = writer->maxchar; | 
 | 13182 |     len = end - start; | 
 | 13183 |  | 
 | 13184 |     if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) | 
 | 13185 |         return -1; | 
 | 13186 |  | 
 | 13187 |     _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, | 
 | 13188 |                                   str, start, len); | 
 | 13189 |     writer->pos += len; | 
 | 13190 |     return 0; | 
 | 13191 | } | 
 | 13192 |  | 
 | 13193 | int | 
| Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13194 | _PyUnicodeWriter_WriteCstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t len) | 
 | 13195 | { | 
 | 13196 |     Py_UCS4 maxchar; | 
 | 13197 |  | 
 | 13198 |     maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); | 
 | 13199 |     if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) | 
 | 13200 |         return -1; | 
 | 13201 |     unicode_write_cstr(writer->buffer, writer->pos, str, len); | 
 | 13202 |     writer->pos += len; | 
 | 13203 |     return 0; | 
 | 13204 | } | 
 | 13205 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13206 | PyObject * | 
| Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13207 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13208 | { | 
| Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13209 |     PyObject *str; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13210 |     if (writer->pos == 0) { | 
| Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13211 |         Py_CLEAR(writer->buffer); | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13212 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13213 |     } | 
| Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13214 |     if (writer->readonly) { | 
| Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13215 |         str = writer->buffer; | 
 | 13216 |         writer->buffer = NULL; | 
 | 13217 |         assert(PyUnicode_GET_LENGTH(str) == writer->pos); | 
 | 13218 |         return str; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13219 |     } | 
 | 13220 |     if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { | 
 | 13221 |         PyObject *newbuffer; | 
 | 13222 |         newbuffer = resize_compact(writer->buffer, writer->pos); | 
 | 13223 |         if (newbuffer == NULL) { | 
 | 13224 |             Py_DECREF(writer->buffer); | 
| Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13225 |             writer->buffer = NULL; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13226 |             return NULL; | 
 | 13227 |         } | 
 | 13228 |         writer->buffer = newbuffer; | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13229 |     } | 
| Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13230 |     str = writer->buffer; | 
 | 13231 |     writer->buffer = NULL; | 
 | 13232 |     assert(_PyUnicode_CheckConsistency(str, 1)); | 
 | 13233 |     return unicode_result_ready(str); | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13234 | } | 
 | 13235 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13236 | void | 
| Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13237 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) | 
| Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13238 | { | 
 | 13239 |     Py_CLEAR(writer->buffer); | 
 | 13240 | } | 
 | 13241 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13242 | #include "stringlib/unicode_format.h" | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13243 |  | 
 | 13244 | PyDoc_STRVAR(format__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13245 |              "S.format(*args, **kwargs) -> str\n\ | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13246 | \n\ | 
| Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13247 | Return a formatted version of S, using substitutions from args and kwargs.\n\ | 
 | 13248 | The substitutions are identified by braces ('{' and '}')."); | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13249 |  | 
| Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13250 | PyDoc_STRVAR(format_map__doc__, | 
 | 13251 |              "S.format_map(mapping) -> str\n\ | 
 | 13252 | \n\ | 
| Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13253 | Return a formatted version of S, using substitutions from mapping.\n\ | 
 | 13254 | The substitutions are identified by braces ('{' and '}')."); | 
| Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13255 |  | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13256 | static PyObject * | 
 | 13257 | unicode__format__(PyObject* self, PyObject* args) | 
 | 13258 | { | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13259 |     PyObject *format_spec; | 
 | 13260 |     _PyUnicodeWriter writer; | 
 | 13261 |     int ret; | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13262 |  | 
 | 13263 |     if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) | 
 | 13264 |         return NULL; | 
 | 13265 |  | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13266 |     if (PyUnicode_READY(self) == -1) | 
 | 13267 |         return NULL; | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13268 |     _PyUnicodeWriter_Init(&writer); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13269 |     ret = _PyUnicode_FormatAdvancedWriter(&writer, | 
 | 13270 |                                           self, format_spec, 0, | 
 | 13271 |                                           PyUnicode_GET_LENGTH(format_spec)); | 
 | 13272 |     if (ret == -1) { | 
 | 13273 |         _PyUnicodeWriter_Dealloc(&writer); | 
 | 13274 |         return NULL; | 
 | 13275 |     } | 
 | 13276 |     return _PyUnicodeWriter_Finish(&writer); | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13277 | } | 
 | 13278 |  | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13279 | PyDoc_STRVAR(p_format__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13280 |              "S.__format__(format_spec) -> str\n\ | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13281 | \n\ | 
| Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13282 | Return a formatted version of S as described by format_spec."); | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13283 |  | 
 | 13284 | static PyObject * | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13285 | unicode__sizeof__(PyObject *v) | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13286 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13287 |     Py_ssize_t size; | 
 | 13288 |  | 
 | 13289 |     /* If it's a compact object, account for base structure + | 
 | 13290 |        character data. */ | 
 | 13291 |     if (PyUnicode_IS_COMPACT_ASCII(v)) | 
 | 13292 |         size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; | 
 | 13293 |     else if (PyUnicode_IS_COMPACT(v)) | 
 | 13294 |         size = sizeof(PyCompactUnicodeObject) + | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13295 |             (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13296 |     else { | 
 | 13297 |         /* If it is a two-block object, account for base object, and | 
 | 13298 |            for character block if present. */ | 
 | 13299 |         size = sizeof(PyUnicodeObject); | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13300 |         if (_PyUnicode_DATA_ANY(v)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13301 |             size += (PyUnicode_GET_LENGTH(v) + 1) * | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13302 |                 PyUnicode_KIND(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13303 |     } | 
 | 13304 |     /* 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] | 13305 |        with the data pointer. Check if the data is not shared. */ | 
| Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13306 |     if (_PyUnicode_HAS_WSTR_MEMORY(v)) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13307 |         size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); | 
| Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13308 |     if (_PyUnicode_HAS_UTF8_MEMORY(v)) | 
| Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13309 |         size += PyUnicode_UTF8_LENGTH(v) + 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13310 |  | 
 | 13311 |     return PyLong_FromSsize_t(size); | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13312 | } | 
 | 13313 |  | 
 | 13314 | PyDoc_STRVAR(sizeof__doc__, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13315 |              "S.__sizeof__() -> size of S in memory, in bytes"); | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13316 |  | 
 | 13317 | static PyObject * | 
| Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13318 | unicode_getnewargs(PyObject *v) | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13319 | { | 
| Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13320 |     PyObject *copy = _PyUnicode_Copy(v); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13321 |     if (!copy) | 
 | 13322 |         return NULL; | 
 | 13323 |     return Py_BuildValue("(N)", copy); | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13324 | } | 
 | 13325 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13326 | static PyMethodDef unicode_methods[] = { | 
| Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13327 |     {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13328 |     {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, | 
| Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13329 |     {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, | 
 | 13330 |     {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13331 |     {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, | 
 | 13332 |     {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, | 
| Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13333 |     {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13334 |     {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, | 
 | 13335 |     {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, | 
 | 13336 |     {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, | 
 | 13337 |     {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, | 
 | 13338 |     {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13339 |     {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13340 |     {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, | 
 | 13341 |     {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, | 
 | 13342 |     {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13343 |     {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13344 |     {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, | 
 | 13345 |     {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, | 
 | 13346 |     {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13347 |     {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13348 |     {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, | 
| Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13349 |     {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, | 
| Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13350 |     {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13351 |     {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, | 
 | 13352 |     {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, | 
 | 13353 |     {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, | 
 | 13354 |     {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, | 
 | 13355 |     {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, | 
 | 13356 |     {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, | 
 | 13357 |     {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, | 
 | 13358 |     {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, | 
 | 13359 |     {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, | 
 | 13360 |     {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, | 
 | 13361 |     {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, | 
 | 13362 |     {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, | 
 | 13363 |     {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, | 
 | 13364 |     {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, | 
| Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13365 |     {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13366 |     {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, | 
| Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13367 |     {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, | 
| Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13368 |     {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, | 
| Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13369 |     {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, | 
| Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13370 |     {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13371 |     UNICODE_MAKETRANS_METHODDEF | 
| Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13372 |     {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, | 
| Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13373 | #if 0 | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13374 |     /* These methods are just used for debugging the implementation. */ | 
| Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13375 |     {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13376 | #endif | 
 | 13377 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13378 |     {"__getnewargs__",  (PyCFunction)unicode_getnewargs, METH_NOARGS}, | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13379 |     {NULL, NULL} | 
 | 13380 | }; | 
 | 13381 |  | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13382 | static PyObject * | 
 | 13383 | unicode_mod(PyObject *v, PyObject *w) | 
 | 13384 | { | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13385 |     if (!PyUnicode_Check(v)) | 
 | 13386 |         Py_RETURN_NOTIMPLEMENTED; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13387 |     return PyUnicode_Format(v, w); | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13388 | } | 
 | 13389 |  | 
 | 13390 | static PyNumberMethods unicode_as_number = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13391 |     0,              /*nb_add*/ | 
 | 13392 |     0,              /*nb_subtract*/ | 
 | 13393 |     0,              /*nb_multiply*/ | 
 | 13394 |     unicode_mod,            /*nb_remainder*/ | 
| Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13395 | }; | 
 | 13396 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13397 | static PySequenceMethods unicode_as_sequence = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13398 |     (lenfunc) unicode_length,       /* sq_length */ | 
 | 13399 |     PyUnicode_Concat,           /* sq_concat */ | 
 | 13400 |     (ssizeargfunc) unicode_repeat,  /* sq_repeat */ | 
 | 13401 |     (ssizeargfunc) unicode_getitem,     /* sq_item */ | 
 | 13402 |     0,                  /* sq_slice */ | 
 | 13403 |     0,                  /* sq_ass_item */ | 
 | 13404 |     0,                  /* sq_ass_slice */ | 
 | 13405 |     PyUnicode_Contains,         /* sq_contains */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13406 | }; | 
 | 13407 |  | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13408 | static PyObject* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13409 | unicode_subscript(PyObject* self, PyObject* item) | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13410 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13411 |     if (PyUnicode_READY(self) == -1) | 
 | 13412 |         return NULL; | 
 | 13413 |  | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13414 |     if (PyIndex_Check(item)) { | 
 | 13415 |         Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13416 |         if (i == -1 && PyErr_Occurred()) | 
 | 13417 |             return NULL; | 
 | 13418 |         if (i < 0) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13419 |             i += PyUnicode_GET_LENGTH(self); | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13420 |         return unicode_getitem(self, i); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13421 |     } else if (PySlice_Check(item)) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13422 |         Py_ssize_t start, stop, step, slicelength, cur, i; | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13423 |         PyObject *result; | 
 | 13424 |         void *src_data, *dest_data; | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13425 |         int src_kind, dest_kind; | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13426 |         Py_UCS4 ch, max_char, kind_limit; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13427 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13428 |         if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13429 |                                  &start, &stop, &step, &slicelength) < 0) { | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13430 |             return NULL; | 
 | 13431 |         } | 
 | 13432 |  | 
 | 13433 |         if (slicelength <= 0) { | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13434 |             _Py_RETURN_UNICODE_EMPTY(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13435 |         } else if (start == 0 && step == 1 && | 
| Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13436 |                    slicelength == PyUnicode_GET_LENGTH(self)) { | 
 | 13437 |             return unicode_result_unchanged(self); | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13438 |         } else if (step == 1) { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13439 |             return PyUnicode_Substring(self, | 
| Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13440 |                                        start, start + slicelength); | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13441 |         } | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13442 |         /* General case */ | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13443 |         src_kind = PyUnicode_KIND(self); | 
 | 13444 |         src_data = PyUnicode_DATA(self); | 
| Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13445 |         if (!PyUnicode_IS_ASCII(self)) { | 
 | 13446 |             kind_limit = kind_maxchar_limit(src_kind); | 
 | 13447 |             max_char = 0; | 
 | 13448 |             for (cur = start, i = 0; i < slicelength; cur += step, i++) { | 
 | 13449 |                 ch = PyUnicode_READ(src_kind, src_data, cur); | 
 | 13450 |                 if (ch > max_char) { | 
 | 13451 |                     max_char = ch; | 
 | 13452 |                     if (max_char >= kind_limit) | 
 | 13453 |                         break; | 
 | 13454 |                 } | 
| Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13455 |             } | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13456 |         } | 
| Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13457 |         else | 
 | 13458 |             max_char = 127; | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13459 |         result = PyUnicode_New(slicelength, max_char); | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13460 |         if (result == NULL) | 
 | 13461 |             return NULL; | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13462 |         dest_kind = PyUnicode_KIND(result); | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13463 |         dest_data = PyUnicode_DATA(result); | 
 | 13464 |  | 
 | 13465 |         for (cur = start, i = 0; i < slicelength; cur += step, i++) { | 
| Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13466 |             Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); | 
 | 13467 |             PyUnicode_WRITE(dest_kind, dest_data, i, ch); | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13468 |         } | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13469 |         assert(_PyUnicode_CheckConsistency(result, 1)); | 
| Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13470 |         return result; | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13471 |     } else { | 
 | 13472 |         PyErr_SetString(PyExc_TypeError, "string indices must be integers"); | 
 | 13473 |         return NULL; | 
 | 13474 |     } | 
 | 13475 | } | 
 | 13476 |  | 
 | 13477 | static PyMappingMethods unicode_as_mapping = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13478 |     (lenfunc)unicode_length,        /* mp_length */ | 
 | 13479 |     (binaryfunc)unicode_subscript,  /* mp_subscript */ | 
 | 13480 |     (objobjargproc)0,           /* mp_ass_subscript */ | 
| Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13481 | }; | 
 | 13482 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13483 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13484 | /* Helpers for PyUnicode_Format() */ | 
 | 13485 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13486 | struct unicode_formatter_t { | 
 | 13487 |     PyObject *args; | 
 | 13488 |     int args_owned; | 
 | 13489 |     Py_ssize_t arglen, argidx; | 
 | 13490 |     PyObject *dict; | 
 | 13491 |  | 
 | 13492 |     enum PyUnicode_Kind fmtkind; | 
 | 13493 |     Py_ssize_t fmtcnt, fmtpos; | 
 | 13494 |     void *fmtdata; | 
 | 13495 |     PyObject *fmtstr; | 
 | 13496 |  | 
 | 13497 |     _PyUnicodeWriter writer; | 
 | 13498 | }; | 
 | 13499 |  | 
 | 13500 | struct unicode_format_arg_t { | 
 | 13501 |     Py_UCS4 ch; | 
 | 13502 |     int flags; | 
 | 13503 |     Py_ssize_t width; | 
 | 13504 |     int prec; | 
 | 13505 |     int sign; | 
 | 13506 | }; | 
 | 13507 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13508 | static PyObject * | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13509 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13510 | { | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13511 |     Py_ssize_t argidx = ctx->argidx; | 
 | 13512 |  | 
 | 13513 |     if (argidx < ctx->arglen) { | 
 | 13514 |         ctx->argidx++; | 
 | 13515 |         if (ctx->arglen < 0) | 
 | 13516 |             return ctx->args; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13517 |         else | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13518 |             return PyTuple_GetItem(ctx->args, argidx); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13519 |     } | 
 | 13520 |     PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13521 |                     "not enough arguments for format string"); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13522 |     return NULL; | 
 | 13523 | } | 
 | 13524 |  | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13525 | /* 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] | 13526 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13527 | /* Format a float into the writer if the writer is not NULL, or into *p_output | 
 | 13528 |    otherwise. | 
 | 13529 |  | 
 | 13530 |    Return 0 on success, raise an exception and return -1 on error. */ | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13531 | static int | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13532 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, | 
 | 13533 |             PyObject **p_output, | 
 | 13534 |             _PyUnicodeWriter *writer) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13535 | { | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13536 |     char *p; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13537 |     double x; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13538 |     Py_ssize_t len; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13539 |     int prec; | 
 | 13540 |     int dtoa_flags; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13541 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13542 |     x = PyFloat_AsDouble(v); | 
 | 13543 |     if (x == -1.0 && PyErr_Occurred()) | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13544 |         return -1; | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13545 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13546 |     prec = arg->prec; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13547 |     if (prec < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13548 |         prec = 6; | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13549 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13550 |     if (arg->flags & F_ALT) | 
 | 13551 |         dtoa_flags = Py_DTSF_ALT; | 
 | 13552 |     else | 
 | 13553 |         dtoa_flags = 0; | 
 | 13554 |     p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); | 
| Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13555 |     if (p == NULL) | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13556 |         return -1; | 
 | 13557 |     len = strlen(p); | 
 | 13558 |     if (writer) { | 
| Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13559 |         if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { | 
 | 13560 |             PyMem_Free(p); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13561 |             return -1; | 
| Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13562 |         } | 
| Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13563 |         unicode_write_cstr(writer->buffer, writer->pos, p, len); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13564 |         writer->pos += len; | 
 | 13565 |     } | 
 | 13566 |     else | 
 | 13567 |         *p_output = _PyUnicode_FromASCII(p, len); | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13568 |     PyMem_Free(p); | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13569 |     return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13570 | } | 
 | 13571 |  | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13572 | /* formatlong() emulates the format codes d, u, o, x and X, and | 
 | 13573 |  * the F_ALT flag, for Python's long (unbounded) ints.  It's not used for | 
 | 13574 |  * Python's regular ints. | 
 | 13575 |  * Return value:  a new PyUnicodeObject*, or NULL if error. | 
 | 13576 |  *     The output string is of the form | 
 | 13577 |  *         "-"? ("0x" | "0X")? digit+ | 
 | 13578 |  *     "0x"/"0X" are present only for x and X conversions, with F_ALT | 
 | 13579 |  *         set in flags.  The case of hex digits will be correct, | 
 | 13580 |  *     There will be at least prec digits, zero-filled on the left if | 
 | 13581 |  *         necessary to get that many. | 
 | 13582 |  * val          object to be converted | 
 | 13583 |  * flags        bitmask of format flags; only F_ALT is looked at | 
 | 13584 |  * prec         minimum number of digits; 0-fill on left if needed | 
 | 13585 |  * type         a character in [duoxX]; u acts the same as d | 
 | 13586 |  * | 
 | 13587 |  * CAUTION:  o, x and X conversions on regular ints can never | 
 | 13588 |  * produce a '-' sign, but can for Python's unbounded ints. | 
 | 13589 |  */ | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13590 | static PyObject* | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13591 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13592 | { | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13593 |     PyObject *result = NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13594 |     char *buf; | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13595 |     Py_ssize_t i; | 
 | 13596 |     int sign;           /* 1 if '-', else 0 */ | 
 | 13597 |     int len;            /* number of characters */ | 
 | 13598 |     Py_ssize_t llen; | 
 | 13599 |     int numdigits;      /* len == numnondigits + numdigits */ | 
 | 13600 |     int numnondigits = 0; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13601 |     int prec = arg->prec; | 
 | 13602 |     int type = arg->ch; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13603 |  | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13604 |     /* Avoid exceeding SSIZE_T_MAX */ | 
 | 13605 |     if (prec > INT_MAX-3) { | 
 | 13606 |         PyErr_SetString(PyExc_OverflowError, | 
 | 13607 |                         "precision too large"); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13608 |         return NULL; | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13609 |     } | 
 | 13610 |  | 
 | 13611 |     assert(PyLong_Check(val)); | 
 | 13612 |  | 
 | 13613 |     switch (type) { | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13614 |     default: | 
 | 13615 |         assert(!"'type' not in [diuoxX]"); | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13616 |     case 'd': | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13617 |     case 'i': | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13618 |     case 'u': | 
| Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13619 |         /* int and int subclasses should print numerically when a numeric */ | 
 | 13620 |         /* format code is used (see issue18780) */ | 
 | 13621 |         result = PyNumber_ToBase(val, 10); | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13622 |         break; | 
 | 13623 |     case 'o': | 
 | 13624 |         numnondigits = 2; | 
 | 13625 |         result = PyNumber_ToBase(val, 8); | 
 | 13626 |         break; | 
 | 13627 |     case 'x': | 
 | 13628 |     case 'X': | 
 | 13629 |         numnondigits = 2; | 
 | 13630 |         result = PyNumber_ToBase(val, 16); | 
 | 13631 |         break; | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13632 |     } | 
 | 13633 |     if (!result) | 
 | 13634 |         return NULL; | 
 | 13635 |  | 
 | 13636 |     assert(unicode_modifiable(result)); | 
 | 13637 |     assert(PyUnicode_IS_READY(result)); | 
 | 13638 |     assert(PyUnicode_IS_ASCII(result)); | 
 | 13639 |  | 
 | 13640 |     /* To modify the string in-place, there can only be one reference. */ | 
 | 13641 |     if (Py_REFCNT(result) != 1) { | 
| Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13642 |         Py_DECREF(result); | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13643 |         PyErr_BadInternalCall(); | 
 | 13644 |         return NULL; | 
 | 13645 |     } | 
 | 13646 |     buf = PyUnicode_DATA(result); | 
 | 13647 |     llen = PyUnicode_GET_LENGTH(result); | 
 | 13648 |     if (llen > INT_MAX) { | 
| Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13649 |         Py_DECREF(result); | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13650 |         PyErr_SetString(PyExc_ValueError, | 
 | 13651 |                         "string too large in _PyBytes_FormatLong"); | 
 | 13652 |         return NULL; | 
 | 13653 |     } | 
 | 13654 |     len = (int)llen; | 
 | 13655 |     sign = buf[0] == '-'; | 
 | 13656 |     numnondigits += sign; | 
 | 13657 |     numdigits = len - numnondigits; | 
 | 13658 |     assert(numdigits > 0); | 
 | 13659 |  | 
 | 13660 |     /* Get rid of base marker unless F_ALT */ | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13661 |     if (((arg->flags & F_ALT) == 0 && | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13662 |         (type == 'o' || type == 'x' || type == 'X'))) { | 
 | 13663 |         assert(buf[sign] == '0'); | 
 | 13664 |         assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || | 
 | 13665 |                buf[sign+1] == 'o'); | 
 | 13666 |         numnondigits -= 2; | 
 | 13667 |         buf += 2; | 
 | 13668 |         len -= 2; | 
 | 13669 |         if (sign) | 
 | 13670 |             buf[0] = '-'; | 
 | 13671 |         assert(len == numnondigits + numdigits); | 
 | 13672 |         assert(numdigits > 0); | 
 | 13673 |     } | 
 | 13674 |  | 
 | 13675 |     /* Fill with leading zeroes to meet minimum width. */ | 
 | 13676 |     if (prec > numdigits) { | 
 | 13677 |         PyObject *r1 = PyBytes_FromStringAndSize(NULL, | 
 | 13678 |                                 numnondigits + prec); | 
 | 13679 |         char *b1; | 
 | 13680 |         if (!r1) { | 
 | 13681 |             Py_DECREF(result); | 
 | 13682 |             return NULL; | 
 | 13683 |         } | 
 | 13684 |         b1 = PyBytes_AS_STRING(r1); | 
 | 13685 |         for (i = 0; i < numnondigits; ++i) | 
 | 13686 |             *b1++ = *buf++; | 
 | 13687 |         for (i = 0; i < prec - numdigits; i++) | 
 | 13688 |             *b1++ = '0'; | 
 | 13689 |         for (i = 0; i < numdigits; i++) | 
 | 13690 |             *b1++ = *buf++; | 
 | 13691 |         *b1 = '\0'; | 
 | 13692 |         Py_DECREF(result); | 
 | 13693 |         result = r1; | 
 | 13694 |         buf = PyBytes_AS_STRING(result); | 
 | 13695 |         len = numnondigits + prec; | 
 | 13696 |     } | 
 | 13697 |  | 
 | 13698 |     /* Fix up case for hex conversions. */ | 
 | 13699 |     if (type == 'X') { | 
 | 13700 |         /* Need to convert all lower case letters to upper case. | 
 | 13701 |            and need to convert 0x to 0X (and -0x to -0X). */ | 
 | 13702 |         for (i = 0; i < len; i++) | 
 | 13703 |             if (buf[i] >= 'a' && buf[i] <= 'x') | 
 | 13704 |                 buf[i] -= 'a'-'A'; | 
 | 13705 |     } | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13706 |     if (!PyUnicode_Check(result) | 
 | 13707 |         || buf != PyUnicode_DATA(result)) { | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13708 |         PyObject *unicode; | 
| Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13709 |         unicode = _PyUnicode_FromASCII(buf, len); | 
| Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13710 |         Py_DECREF(result); | 
 | 13711 |         result = unicode; | 
 | 13712 |     } | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13713 |     else if (len != PyUnicode_GET_LENGTH(result)) { | 
 | 13714 |         if (PyUnicode_Resize(&result, len) < 0) | 
 | 13715 |             Py_CLEAR(result); | 
 | 13716 |     } | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13717 |     return result; | 
| Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13718 | } | 
 | 13719 |  | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13720 | /* Format an integer. | 
 | 13721 |  * Return 1 if the number has been formatted into the writer, | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13722 |  *        0 if the number has been formatted into *p_output | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13723 |  *       -1 and raise an exception on error */ | 
 | 13724 | static int | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13725 | mainformatlong(PyObject *v, | 
 | 13726 |                struct unicode_format_arg_t *arg, | 
 | 13727 |                PyObject **p_output, | 
 | 13728 |                _PyUnicodeWriter *writer) | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13729 | { | 
 | 13730 |     PyObject *iobj, *res; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13731 |     char type = (char)arg->ch; | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13732 |  | 
 | 13733 |     if (!PyNumber_Check(v)) | 
 | 13734 |         goto wrongtype; | 
 | 13735 |  | 
 | 13736 |     if (!PyLong_Check(v)) { | 
 | 13737 |         iobj = PyNumber_Long(v); | 
 | 13738 |         if (iobj == NULL) { | 
 | 13739 |             if (PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 13740 |                 goto wrongtype; | 
 | 13741 |             return -1; | 
 | 13742 |         } | 
 | 13743 |         assert(PyLong_Check(iobj)); | 
 | 13744 |     } | 
 | 13745 |     else { | 
 | 13746 |         iobj = v; | 
 | 13747 |         Py_INCREF(iobj); | 
 | 13748 |     } | 
 | 13749 |  | 
 | 13750 |     if (PyLong_CheckExact(v) | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13751 |         && arg->width == -1 && arg->prec == -1 | 
 | 13752 |         && !(arg->flags & (F_SIGN | F_BLANK)) | 
 | 13753 |         && type != 'X') | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13754 |     { | 
 | 13755 |         /* Fast path */ | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13756 |         int alternate = arg->flags & F_ALT; | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13757 |         int base; | 
 | 13758 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13759 |         switch(type) | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13760 |         { | 
 | 13761 |             default: | 
 | 13762 |                 assert(0 && "'type' not in [diuoxX]"); | 
 | 13763 |             case 'd': | 
 | 13764 |             case 'i': | 
 | 13765 |             case 'u': | 
 | 13766 |                 base = 10; | 
 | 13767 |                 break; | 
 | 13768 |             case 'o': | 
 | 13769 |                 base = 8; | 
 | 13770 |                 break; | 
 | 13771 |             case 'x': | 
 | 13772 |             case 'X': | 
 | 13773 |                 base = 16; | 
 | 13774 |                 break; | 
 | 13775 |         } | 
 | 13776 |  | 
| Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13777 |         if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { | 
 | 13778 |             Py_DECREF(iobj); | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13779 |             return -1; | 
| Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13780 |         } | 
 | 13781 |         Py_DECREF(iobj); | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13782 |         return 1; | 
 | 13783 |     } | 
 | 13784 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13785 |     res = formatlong(iobj, arg); | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13786 |     Py_DECREF(iobj); | 
 | 13787 |     if (res == NULL) | 
 | 13788 |         return -1; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13789 |     *p_output = res; | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13790 |     return 0; | 
 | 13791 |  | 
 | 13792 | wrongtype: | 
 | 13793 |     PyErr_Format(PyExc_TypeError, | 
 | 13794 |             "%%%c format: a number is required, " | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13795 |             "not %.200s", | 
 | 13796 |             type, Py_TYPE(v)->tp_name); | 
| Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13797 |     return -1; | 
 | 13798 | } | 
 | 13799 |  | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13800 | static Py_UCS4 | 
 | 13801 | formatchar(PyObject *v) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13802 | { | 
| Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13803 |     /* presume that the buffer is at least 3 characters long */ | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13804 |     if (PyUnicode_Check(v)) { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13805 |         if (PyUnicode_GET_LENGTH(v) == 1) { | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13806 |             return PyUnicode_READ_CHAR(v, 0); | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13807 |         } | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13808 |         goto onError; | 
 | 13809 |     } | 
 | 13810 |     else { | 
 | 13811 |         /* Integer input truncated to a character */ | 
 | 13812 |         long x; | 
 | 13813 |         x = PyLong_AsLong(v); | 
 | 13814 |         if (x == -1 && PyErr_Occurred()) | 
 | 13815 |             goto onError; | 
 | 13816 |  | 
| Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13817 |         if (x < 0 || x > MAX_UNICODE) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13818 |             PyErr_SetString(PyExc_OverflowError, | 
 | 13819 |                             "%c arg not in range(0x110000)"); | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13820 |             return (Py_UCS4) -1; | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13821 |         } | 
 | 13822 |  | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13823 |         return (Py_UCS4) x; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13824 |     } | 
| Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13825 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13826 |   onError: | 
| Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13827 |     PyErr_SetString(PyExc_TypeError, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13828 |                     "%c requires int or char"); | 
| Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13829 |     return (Py_UCS4) -1; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13830 | } | 
 | 13831 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13832 | /* Parse options of an argument: flags, width, precision. | 
 | 13833 |    Handle also "%(name)" syntax. | 
 | 13834 |  | 
 | 13835 |    Return 0 if the argument has been formatted into arg->str. | 
 | 13836 |    Return 1 if the argument has been written into ctx->writer, | 
 | 13837 |    Raise an exception and return -1 on error. */ | 
 | 13838 | static int | 
 | 13839 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, | 
 | 13840 |                          struct unicode_format_arg_t *arg) | 
 | 13841 | { | 
 | 13842 | #define FORMAT_READ(ctx) \ | 
 | 13843 |         PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) | 
 | 13844 |  | 
 | 13845 |     PyObject *v; | 
 | 13846 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13847 |     if (arg->ch == '(') { | 
 | 13848 |         /* Get argument value from a dictionary. Example: "%(name)s". */ | 
 | 13849 |         Py_ssize_t keystart; | 
 | 13850 |         Py_ssize_t keylen; | 
 | 13851 |         PyObject *key; | 
 | 13852 |         int pcount = 1; | 
 | 13853 |  | 
 | 13854 |         if (ctx->dict == NULL) { | 
 | 13855 |             PyErr_SetString(PyExc_TypeError, | 
 | 13856 |                             "format requires a mapping"); | 
 | 13857 |             return -1; | 
 | 13858 |         } | 
 | 13859 |         ++ctx->fmtpos; | 
 | 13860 |         --ctx->fmtcnt; | 
 | 13861 |         keystart = ctx->fmtpos; | 
 | 13862 |         /* Skip over balanced parentheses */ | 
 | 13863 |         while (pcount > 0 && --ctx->fmtcnt >= 0) { | 
 | 13864 |             arg->ch = FORMAT_READ(ctx); | 
 | 13865 |             if (arg->ch == ')') | 
 | 13866 |                 --pcount; | 
 | 13867 |             else if (arg->ch == '(') | 
 | 13868 |                 ++pcount; | 
 | 13869 |             ctx->fmtpos++; | 
 | 13870 |         } | 
 | 13871 |         keylen = ctx->fmtpos - keystart - 1; | 
 | 13872 |         if (ctx->fmtcnt < 0 || pcount > 0) { | 
 | 13873 |             PyErr_SetString(PyExc_ValueError, | 
 | 13874 |                             "incomplete format key"); | 
 | 13875 |             return -1; | 
 | 13876 |         } | 
 | 13877 |         key = PyUnicode_Substring(ctx->fmtstr, | 
 | 13878 |                                   keystart, keystart + keylen); | 
 | 13879 |         if (key == NULL) | 
 | 13880 |             return -1; | 
 | 13881 |         if (ctx->args_owned) { | 
 | 13882 |             Py_DECREF(ctx->args); | 
 | 13883 |             ctx->args_owned = 0; | 
 | 13884 |         } | 
 | 13885 |         ctx->args = PyObject_GetItem(ctx->dict, key); | 
 | 13886 |         Py_DECREF(key); | 
 | 13887 |         if (ctx->args == NULL) | 
 | 13888 |             return -1; | 
 | 13889 |         ctx->args_owned = 1; | 
 | 13890 |         ctx->arglen = -1; | 
 | 13891 |         ctx->argidx = -2; | 
 | 13892 |     } | 
 | 13893 |  | 
 | 13894 |     /* Parse flags. Example: "%+i" => flags=F_SIGN. */ | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13895 |     while (--ctx->fmtcnt >= 0) { | 
 | 13896 |         arg->ch = FORMAT_READ(ctx); | 
 | 13897 |         ctx->fmtpos++; | 
 | 13898 |         switch (arg->ch) { | 
 | 13899 |         case '-': arg->flags |= F_LJUST; continue; | 
 | 13900 |         case '+': arg->flags |= F_SIGN; continue; | 
 | 13901 |         case ' ': arg->flags |= F_BLANK; continue; | 
 | 13902 |         case '#': arg->flags |= F_ALT; continue; | 
 | 13903 |         case '0': arg->flags |= F_ZERO; continue; | 
 | 13904 |         } | 
 | 13905 |         break; | 
 | 13906 |     } | 
 | 13907 |  | 
 | 13908 |     /* Parse width. Example: "%10s" => width=10 */ | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13909 |     if (arg->ch == '*') { | 
 | 13910 |         v = unicode_format_getnextarg(ctx); | 
 | 13911 |         if (v == NULL) | 
 | 13912 |             return -1; | 
 | 13913 |         if (!PyLong_Check(v)) { | 
 | 13914 |             PyErr_SetString(PyExc_TypeError, | 
 | 13915 |                             "* wants int"); | 
 | 13916 |             return -1; | 
 | 13917 |         } | 
| Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13918 |         arg->width = PyLong_AsSsize_t(v); | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13919 |         if (arg->width == -1 && PyErr_Occurred()) | 
 | 13920 |             return -1; | 
 | 13921 |         if (arg->width < 0) { | 
 | 13922 |             arg->flags |= F_LJUST; | 
 | 13923 |             arg->width = -arg->width; | 
 | 13924 |         } | 
 | 13925 |         if (--ctx->fmtcnt >= 0) { | 
 | 13926 |             arg->ch = FORMAT_READ(ctx); | 
 | 13927 |             ctx->fmtpos++; | 
 | 13928 |         } | 
 | 13929 |     } | 
 | 13930 |     else if (arg->ch >= '0' && arg->ch <= '9') { | 
 | 13931 |         arg->width = arg->ch - '0'; | 
 | 13932 |         while (--ctx->fmtcnt >= 0) { | 
 | 13933 |             arg->ch = FORMAT_READ(ctx); | 
 | 13934 |             ctx->fmtpos++; | 
 | 13935 |             if (arg->ch < '0' || arg->ch > '9') | 
 | 13936 |                 break; | 
 | 13937 |             /* Since arg->ch is unsigned, the RHS would end up as unsigned, | 
 | 13938 |                mixing signed and unsigned comparison. Since arg->ch is between | 
 | 13939 |                '0' and '9', casting to int is safe. */ | 
 | 13940 |             if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { | 
 | 13941 |                 PyErr_SetString(PyExc_ValueError, | 
 | 13942 |                                 "width too big"); | 
 | 13943 |                 return -1; | 
 | 13944 |             } | 
 | 13945 |             arg->width = arg->width*10 + (arg->ch - '0'); | 
 | 13946 |         } | 
 | 13947 |     } | 
 | 13948 |  | 
 | 13949 |     /* Parse precision. Example: "%.3f" => prec=3 */ | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13950 |     if (arg->ch == '.') { | 
 | 13951 |         arg->prec = 0; | 
 | 13952 |         if (--ctx->fmtcnt >= 0) { | 
 | 13953 |             arg->ch = FORMAT_READ(ctx); | 
 | 13954 |             ctx->fmtpos++; | 
 | 13955 |         } | 
 | 13956 |         if (arg->ch == '*') { | 
 | 13957 |             v = unicode_format_getnextarg(ctx); | 
 | 13958 |             if (v == NULL) | 
 | 13959 |                 return -1; | 
 | 13960 |             if (!PyLong_Check(v)) { | 
 | 13961 |                 PyErr_SetString(PyExc_TypeError, | 
 | 13962 |                                 "* wants int"); | 
 | 13963 |                 return -1; | 
 | 13964 |             } | 
| Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13965 |             arg->prec = _PyLong_AsInt(v); | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13966 |             if (arg->prec == -1 && PyErr_Occurred()) | 
 | 13967 |                 return -1; | 
 | 13968 |             if (arg->prec < 0) | 
 | 13969 |                 arg->prec = 0; | 
 | 13970 |             if (--ctx->fmtcnt >= 0) { | 
 | 13971 |                 arg->ch = FORMAT_READ(ctx); | 
 | 13972 |                 ctx->fmtpos++; | 
 | 13973 |             } | 
 | 13974 |         } | 
 | 13975 |         else if (arg->ch >= '0' && arg->ch <= '9') { | 
 | 13976 |             arg->prec = arg->ch - '0'; | 
 | 13977 |             while (--ctx->fmtcnt >= 0) { | 
 | 13978 |                 arg->ch = FORMAT_READ(ctx); | 
 | 13979 |                 ctx->fmtpos++; | 
 | 13980 |                 if (arg->ch < '0' || arg->ch > '9') | 
 | 13981 |                     break; | 
 | 13982 |                 if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { | 
 | 13983 |                     PyErr_SetString(PyExc_ValueError, | 
| Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 13984 |                                     "precision too big"); | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13985 |                     return -1; | 
 | 13986 |                 } | 
 | 13987 |                 arg->prec = arg->prec*10 + (arg->ch - '0'); | 
 | 13988 |             } | 
 | 13989 |         } | 
 | 13990 |     } | 
 | 13991 |  | 
 | 13992 |     /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ | 
 | 13993 |     if (ctx->fmtcnt >= 0) { | 
 | 13994 |         if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { | 
 | 13995 |             if (--ctx->fmtcnt >= 0) { | 
 | 13996 |                 arg->ch = FORMAT_READ(ctx); | 
 | 13997 |                 ctx->fmtpos++; | 
 | 13998 |             } | 
 | 13999 |         } | 
 | 14000 |     } | 
 | 14001 |     if (ctx->fmtcnt < 0) { | 
 | 14002 |         PyErr_SetString(PyExc_ValueError, | 
 | 14003 |                         "incomplete format"); | 
 | 14004 |         return -1; | 
 | 14005 |     } | 
 | 14006 |     return 0; | 
 | 14007 |  | 
 | 14008 | #undef FORMAT_READ | 
 | 14009 | } | 
 | 14010 |  | 
 | 14011 | /* Format one argument. Supported conversion specifiers: | 
 | 14012 |  | 
 | 14013 |    - "s", "r", "a": any type | 
 | 14014 |    - "i", "d", "u", "o", "x", "X": int | 
 | 14015 |    - "e", "E", "f", "F", "g", "G": float | 
 | 14016 |    - "c": int or str (1 character) | 
 | 14017 |  | 
| Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14018 |    When possible, the output is written directly into the Unicode writer | 
 | 14019 |    (ctx->writer). A string is created when padding is required. | 
 | 14020 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14021 |    Return 0 if the argument has been formatted into *p_str, | 
 | 14022 |           1 if the argument has been written into ctx->writer, | 
| Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14023 |          -1 on error. */ | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14024 | static int | 
 | 14025 | unicode_format_arg_format(struct unicode_formatter_t *ctx, | 
 | 14026 |                           struct unicode_format_arg_t *arg, | 
 | 14027 |                           PyObject **p_str) | 
 | 14028 | { | 
 | 14029 |     PyObject *v; | 
 | 14030 |     _PyUnicodeWriter *writer = &ctx->writer; | 
 | 14031 |  | 
 | 14032 |     if (ctx->fmtcnt == 0) | 
 | 14033 |         ctx->writer.overallocate = 0; | 
 | 14034 |  | 
 | 14035 |     if (arg->ch == '%') { | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14036 |         if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14037 |             return -1; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14038 |         return 1; | 
 | 14039 |     } | 
 | 14040 |  | 
 | 14041 |     v = unicode_format_getnextarg(ctx); | 
 | 14042 |     if (v == NULL) | 
 | 14043 |         return -1; | 
 | 14044 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14045 |  | 
 | 14046 |     switch (arg->ch) { | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14047 |     case 's': | 
 | 14048 |     case 'r': | 
 | 14049 |     case 'a': | 
 | 14050 |         if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { | 
 | 14051 |             /* Fast path */ | 
 | 14052 |             if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) | 
 | 14053 |                 return -1; | 
 | 14054 |             return 1; | 
 | 14055 |         } | 
 | 14056 |  | 
 | 14057 |         if (PyUnicode_CheckExact(v) && arg->ch == 's') { | 
 | 14058 |             *p_str = v; | 
 | 14059 |             Py_INCREF(*p_str); | 
 | 14060 |         } | 
 | 14061 |         else { | 
 | 14062 |             if (arg->ch == 's') | 
 | 14063 |                 *p_str = PyObject_Str(v); | 
 | 14064 |             else if (arg->ch == 'r') | 
 | 14065 |                 *p_str = PyObject_Repr(v); | 
 | 14066 |             else | 
 | 14067 |                 *p_str = PyObject_ASCII(v); | 
 | 14068 |         } | 
 | 14069 |         break; | 
 | 14070 |  | 
 | 14071 |     case 'i': | 
 | 14072 |     case 'd': | 
 | 14073 |     case 'u': | 
 | 14074 |     case 'o': | 
 | 14075 |     case 'x': | 
 | 14076 |     case 'X': | 
 | 14077 |     { | 
 | 14078 |         int ret = mainformatlong(v, arg, p_str, writer); | 
 | 14079 |         if (ret != 0) | 
 | 14080 |             return ret; | 
 | 14081 |         arg->sign = 1; | 
 | 14082 |         break; | 
 | 14083 |     } | 
 | 14084 |  | 
 | 14085 |     case 'e': | 
 | 14086 |     case 'E': | 
 | 14087 |     case 'f': | 
 | 14088 |     case 'F': | 
 | 14089 |     case 'g': | 
 | 14090 |     case 'G': | 
 | 14091 |         if (arg->width == -1 && arg->prec == -1 | 
 | 14092 |             && !(arg->flags & (F_SIGN | F_BLANK))) | 
 | 14093 |         { | 
 | 14094 |             /* Fast path */ | 
 | 14095 |             if (formatfloat(v, arg, NULL, writer) == -1) | 
 | 14096 |                 return -1; | 
 | 14097 |             return 1; | 
 | 14098 |         } | 
 | 14099 |  | 
 | 14100 |         arg->sign = 1; | 
 | 14101 |         if (formatfloat(v, arg, p_str, NULL) == -1) | 
 | 14102 |             return -1; | 
 | 14103 |         break; | 
 | 14104 |  | 
 | 14105 |     case 'c': | 
 | 14106 |     { | 
 | 14107 |         Py_UCS4 ch = formatchar(v); | 
 | 14108 |         if (ch == (Py_UCS4) -1) | 
 | 14109 |             return -1; | 
 | 14110 |         if (arg->width == -1 && arg->prec == -1) { | 
 | 14111 |             /* Fast path */ | 
| Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14112 |             if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14113 |                 return -1; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14114 |             return 1; | 
 | 14115 |         } | 
 | 14116 |         *p_str = PyUnicode_FromOrdinal(ch); | 
 | 14117 |         break; | 
 | 14118 |     } | 
 | 14119 |  | 
 | 14120 |     default: | 
 | 14121 |         PyErr_Format(PyExc_ValueError, | 
 | 14122 |                      "unsupported format character '%c' (0x%x) " | 
 | 14123 |                      "at index %zd", | 
 | 14124 |                      (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', | 
 | 14125 |                      (int)arg->ch, | 
 | 14126 |                      ctx->fmtpos - 1); | 
 | 14127 |         return -1; | 
 | 14128 |     } | 
 | 14129 |     if (*p_str == NULL) | 
 | 14130 |         return -1; | 
 | 14131 |     assert (PyUnicode_Check(*p_str)); | 
 | 14132 |     return 0; | 
 | 14133 | } | 
 | 14134 |  | 
 | 14135 | static int | 
 | 14136 | unicode_format_arg_output(struct unicode_formatter_t *ctx, | 
 | 14137 |                           struct unicode_format_arg_t *arg, | 
 | 14138 |                           PyObject *str) | 
 | 14139 | { | 
 | 14140 |     Py_ssize_t len; | 
 | 14141 |     enum PyUnicode_Kind kind; | 
 | 14142 |     void *pbuf; | 
 | 14143 |     Py_ssize_t pindex; | 
 | 14144 |     Py_UCS4 signchar; | 
 | 14145 |     Py_ssize_t buflen; | 
| Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14146 |     Py_UCS4 maxchar; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14147 |     Py_ssize_t sublen; | 
 | 14148 |     _PyUnicodeWriter *writer = &ctx->writer; | 
 | 14149 |     Py_UCS4 fill; | 
 | 14150 |  | 
 | 14151 |     fill = ' '; | 
 | 14152 |     if (arg->sign && arg->flags & F_ZERO) | 
 | 14153 |         fill = '0'; | 
 | 14154 |  | 
 | 14155 |     if (PyUnicode_READY(str) == -1) | 
 | 14156 |         return -1; | 
 | 14157 |  | 
 | 14158 |     len = PyUnicode_GET_LENGTH(str); | 
 | 14159 |     if ((arg->width == -1 || arg->width <= len) | 
 | 14160 |         && (arg->prec == -1 || arg->prec >= len) | 
 | 14161 |         && !(arg->flags & (F_SIGN | F_BLANK))) | 
 | 14162 |     { | 
 | 14163 |         /* Fast path */ | 
 | 14164 |         if (_PyUnicodeWriter_WriteStr(writer, str) == -1) | 
 | 14165 |             return -1; | 
 | 14166 |         return 0; | 
 | 14167 |     } | 
 | 14168 |  | 
 | 14169 |     /* Truncate the string for "s", "r" and "a" formats | 
 | 14170 |        if the precision is set */ | 
 | 14171 |     if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { | 
 | 14172 |         if (arg->prec >= 0 && len > arg->prec) | 
 | 14173 |             len = arg->prec; | 
 | 14174 |     } | 
 | 14175 |  | 
 | 14176 |     /* Adjust sign and width */ | 
 | 14177 |     kind = PyUnicode_KIND(str); | 
 | 14178 |     pbuf = PyUnicode_DATA(str); | 
 | 14179 |     pindex = 0; | 
 | 14180 |     signchar = '\0'; | 
 | 14181 |     if (arg->sign) { | 
 | 14182 |         Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); | 
 | 14183 |         if (ch == '-' || ch == '+') { | 
 | 14184 |             signchar = ch; | 
 | 14185 |             len--; | 
 | 14186 |             pindex++; | 
 | 14187 |         } | 
 | 14188 |         else if (arg->flags & F_SIGN) | 
 | 14189 |             signchar = '+'; | 
 | 14190 |         else if (arg->flags & F_BLANK) | 
 | 14191 |             signchar = ' '; | 
 | 14192 |         else | 
 | 14193 |             arg->sign = 0; | 
 | 14194 |     } | 
 | 14195 |     if (arg->width < len) | 
 | 14196 |         arg->width = len; | 
 | 14197 |  | 
 | 14198 |     /* Prepare the writer */ | 
| Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14199 |     maxchar = writer->maxchar; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14200 |     if (!(arg->flags & F_LJUST)) { | 
 | 14201 |         if (arg->sign) { | 
 | 14202 |             if ((arg->width-1) > len) | 
| Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14203 |                 maxchar = Py_MAX(maxchar, fill); | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14204 |         } | 
 | 14205 |         else { | 
 | 14206 |             if (arg->width > len) | 
| Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14207 |                 maxchar = Py_MAX(maxchar, fill); | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14208 |         } | 
 | 14209 |     } | 
| Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14210 |     if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { | 
 | 14211 |         Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); | 
| Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14212 |         maxchar = Py_MAX(maxchar, strmaxchar); | 
| Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14213 |     } | 
 | 14214 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14215 |     buflen = arg->width; | 
 | 14216 |     if (arg->sign && len == arg->width) | 
 | 14217 |         buflen++; | 
| Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14218 |     if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14219 |         return -1; | 
 | 14220 |  | 
 | 14221 |     /* Write the sign if needed */ | 
 | 14222 |     if (arg->sign) { | 
 | 14223 |         if (fill != ' ') { | 
 | 14224 |             PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); | 
 | 14225 |             writer->pos += 1; | 
 | 14226 |         } | 
 | 14227 |         if (arg->width > len) | 
 | 14228 |             arg->width--; | 
 | 14229 |     } | 
 | 14230 |  | 
 | 14231 |     /* Write the numeric prefix for "x", "X" and "o" formats | 
 | 14232 |        if the alternate form is used. | 
 | 14233 |        For example, write "0x" for the "%#x" format. */ | 
 | 14234 |     if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { | 
 | 14235 |         assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); | 
 | 14236 |         assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); | 
 | 14237 |         if (fill != ' ') { | 
 | 14238 |             PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); | 
 | 14239 |             PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); | 
 | 14240 |             writer->pos += 2; | 
 | 14241 |             pindex += 2; | 
 | 14242 |         } | 
 | 14243 |         arg->width -= 2; | 
 | 14244 |         if (arg->width < 0) | 
 | 14245 |             arg->width = 0; | 
 | 14246 |         len -= 2; | 
 | 14247 |     } | 
 | 14248 |  | 
 | 14249 |     /* Pad left with the fill character if needed */ | 
 | 14250 |     if (arg->width > len && !(arg->flags & F_LJUST)) { | 
 | 14251 |         sublen = arg->width - len; | 
 | 14252 |         FILL(writer->kind, writer->data, fill, writer->pos, sublen); | 
 | 14253 |         writer->pos += sublen; | 
 | 14254 |         arg->width = len; | 
 | 14255 |     } | 
 | 14256 |  | 
 | 14257 |     /* If padding with spaces: write sign if needed and/or numeric prefix if | 
 | 14258 |        the alternate form is used */ | 
 | 14259 |     if (fill == ' ') { | 
 | 14260 |         if (arg->sign) { | 
 | 14261 |             PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); | 
 | 14262 |             writer->pos += 1; | 
 | 14263 |         } | 
 | 14264 |         if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { | 
 | 14265 |             assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); | 
 | 14266 |             assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); | 
 | 14267 |             PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); | 
 | 14268 |             PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); | 
 | 14269 |             writer->pos += 2; | 
 | 14270 |             pindex += 2; | 
 | 14271 |         } | 
 | 14272 |     } | 
 | 14273 |  | 
 | 14274 |     /* Write characters */ | 
 | 14275 |     if (len) { | 
 | 14276 |         _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, | 
 | 14277 |                                       str, pindex, len); | 
 | 14278 |         writer->pos += len; | 
 | 14279 |     } | 
 | 14280 |  | 
 | 14281 |     /* Pad right with the fill character if needed */ | 
 | 14282 |     if (arg->width > len) { | 
 | 14283 |         sublen = arg->width - len; | 
 | 14284 |         FILL(writer->kind, writer->data, ' ', writer->pos, sublen); | 
 | 14285 |         writer->pos += sublen; | 
 | 14286 |     } | 
 | 14287 |     return 0; | 
 | 14288 | } | 
 | 14289 |  | 
 | 14290 | /* Helper of PyUnicode_Format(): format one arg. | 
 | 14291 |    Return 0 on success, raise an exception and return -1 on error. */ | 
 | 14292 | static int | 
 | 14293 | unicode_format_arg(struct unicode_formatter_t *ctx) | 
 | 14294 | { | 
 | 14295 |     struct unicode_format_arg_t arg; | 
 | 14296 |     PyObject *str; | 
 | 14297 |     int ret; | 
 | 14298 |  | 
| Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14299 |     arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); | 
 | 14300 |     arg.flags = 0; | 
 | 14301 |     arg.width = -1; | 
 | 14302 |     arg.prec = -1; | 
 | 14303 |     arg.sign = 0; | 
 | 14304 |     str = NULL; | 
 | 14305 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14306 |     ret = unicode_format_arg_parse(ctx, &arg); | 
 | 14307 |     if (ret == -1) | 
 | 14308 |         return -1; | 
 | 14309 |  | 
 | 14310 |     ret = unicode_format_arg_format(ctx, &arg, &str); | 
 | 14311 |     if (ret == -1) | 
 | 14312 |         return -1; | 
 | 14313 |  | 
 | 14314 |     if (ret != 1) { | 
 | 14315 |         ret = unicode_format_arg_output(ctx, &arg, str); | 
 | 14316 |         Py_DECREF(str); | 
 | 14317 |         if (ret == -1) | 
 | 14318 |             return -1; | 
 | 14319 |     } | 
 | 14320 |  | 
 | 14321 |     if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { | 
 | 14322 |         PyErr_SetString(PyExc_TypeError, | 
 | 14323 |                         "not all arguments converted during string formatting"); | 
 | 14324 |         return -1; | 
 | 14325 |     } | 
 | 14326 |     return 0; | 
 | 14327 | } | 
 | 14328 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14329 | PyObject * | 
 | 14330 | PyUnicode_Format(PyObject *format, PyObject *args) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14331 | { | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14332 |     struct unicode_formatter_t ctx; | 
| Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14333 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14334 |     if (format == NULL || args == NULL) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14335 |         PyErr_BadInternalCall(); | 
 | 14336 |         return NULL; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14337 |     } | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14338 |  | 
 | 14339 |     ctx.fmtstr = PyUnicode_FromObject(format); | 
 | 14340 |     if (ctx.fmtstr == NULL) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14341 |         return NULL; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14342 |     if (PyUnicode_READY(ctx.fmtstr) == -1) { | 
 | 14343 |         Py_DECREF(ctx.fmtstr); | 
 | 14344 |         return NULL; | 
 | 14345 |     } | 
 | 14346 |     ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); | 
 | 14347 |     ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); | 
 | 14348 |     ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); | 
 | 14349 |     ctx.fmtpos = 0; | 
| Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14350 |  | 
| Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14351 |     _PyUnicodeWriter_Init(&ctx.writer); | 
 | 14352 |     ctx.writer.min_length = ctx.fmtcnt + 100; | 
 | 14353 |     ctx.writer.overallocate = 1; | 
| Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14354 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14355 |     if (PyTuple_Check(args)) { | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14356 |         ctx.arglen = PyTuple_Size(args); | 
 | 14357 |         ctx.argidx = 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14358 |     } | 
 | 14359 |     else { | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14360 |         ctx.arglen = -1; | 
 | 14361 |         ctx.argidx = -2; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14362 |     } | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14363 |     ctx.args_owned = 0; | 
| Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14364 |     if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14365 |         ctx.dict = args; | 
 | 14366 |     else | 
 | 14367 |         ctx.dict = NULL; | 
 | 14368 |     ctx.args = args; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14369 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14370 |     while (--ctx.fmtcnt >= 0) { | 
 | 14371 |         if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { | 
| Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14372 |             Py_ssize_t nonfmtpos; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14373 |  | 
 | 14374 |             nonfmtpos = ctx.fmtpos++; | 
 | 14375 |             while (ctx.fmtcnt >= 0 && | 
 | 14376 |                    PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { | 
 | 14377 |                 ctx.fmtpos++; | 
 | 14378 |                 ctx.fmtcnt--; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14379 |             } | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14380 |             if (ctx.fmtcnt < 0) { | 
 | 14381 |                 ctx.fmtpos--; | 
 | 14382 |                 ctx.writer.overallocate = 0; | 
| Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14383 |             } | 
| Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14384 |  | 
| Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14385 |             if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, | 
 | 14386 |                                                 nonfmtpos, ctx.fmtpos) < 0) | 
 | 14387 |                 goto onError; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14388 |         } | 
 | 14389 |         else { | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14390 |             ctx.fmtpos++; | 
 | 14391 |             if (unicode_format_arg(&ctx) == -1) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14392 |                 goto onError; | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14393 |         } | 
 | 14394 |     } | 
| Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14395 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14396 |     if (ctx.argidx < ctx.arglen && !ctx.dict) { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14397 |         PyErr_SetString(PyExc_TypeError, | 
 | 14398 |                         "not all arguments converted during string formatting"); | 
 | 14399 |         goto onError; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14400 |     } | 
 | 14401 |  | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14402 |     if (ctx.args_owned) { | 
 | 14403 |         Py_DECREF(ctx.args); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14404 |     } | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14405 |     Py_DECREF(ctx.fmtstr); | 
 | 14406 |     return _PyUnicodeWriter_Finish(&ctx.writer); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14407 |  | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14408 |   onError: | 
| Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14409 |     Py_DECREF(ctx.fmtstr); | 
 | 14410 |     _PyUnicodeWriter_Dealloc(&ctx.writer); | 
 | 14411 |     if (ctx.args_owned) { | 
 | 14412 |         Py_DECREF(ctx.args); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14413 |     } | 
 | 14414 |     return NULL; | 
 | 14415 | } | 
 | 14416 |  | 
| Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14417 | static PyObject * | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14418 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | 
 | 14419 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14420 | static PyObject * | 
 | 14421 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 14422 | { | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14423 |     PyObject *x = NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14424 |     static char *kwlist[] = {"object", "encoding", "errors", 0}; | 
 | 14425 |     char *encoding = NULL; | 
 | 14426 |     char *errors = NULL; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14427 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14428 |     if (type != &PyUnicode_Type) | 
 | 14429 |         return unicode_subtype_new(type, args, kwds); | 
 | 14430 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14431 |                                      kwlist, &x, &encoding, &errors)) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14432 |         return NULL; | 
 | 14433 |     if (x == NULL) | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14434 |         _Py_RETURN_UNICODE_EMPTY(); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14435 |     if (encoding == NULL && errors == NULL) | 
 | 14436 |         return PyObject_Str(x); | 
 | 14437 |     else | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14438 |         return PyUnicode_FromEncodedObject(x, encoding, errors); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14439 | } | 
 | 14440 |  | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14441 | static PyObject * | 
 | 14442 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 14443 | { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14444 |     PyObject *unicode, *self; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14445 |     Py_ssize_t length, char_size; | 
 | 14446 |     int share_wstr, share_utf8; | 
 | 14447 |     unsigned int kind; | 
 | 14448 |     void *data; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14449 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14450 |     assert(PyType_IsSubtype(type, &PyUnicode_Type)); | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14451 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14452 |     unicode = unicode_new(&PyUnicode_Type, args, kwds); | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14453 |     if (unicode == NULL) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14454 |         return NULL; | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14455 |     assert(_PyUnicode_CHECK(unicode)); | 
| Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14456 |     if (PyUnicode_READY(unicode) == -1) { | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14457 |         Py_DECREF(unicode); | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14458 |         return NULL; | 
| Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14459 |     } | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14460 |  | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14461 |     self = type->tp_alloc(type, 0); | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14462 |     if (self == NULL) { | 
 | 14463 |         Py_DECREF(unicode); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14464 |         return NULL; | 
 | 14465 |     } | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14466 |     kind = PyUnicode_KIND(unicode); | 
 | 14467 |     length = PyUnicode_GET_LENGTH(unicode); | 
 | 14468 |  | 
 | 14469 |     _PyUnicode_LENGTH(self) = length; | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14470 | #ifdef Py_DEBUG | 
 | 14471 |     _PyUnicode_HASH(self) = -1; | 
 | 14472 | #else | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14473 |     _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14474 | #endif | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14475 |     _PyUnicode_STATE(self).interned = 0; | 
 | 14476 |     _PyUnicode_STATE(self).kind = kind; | 
 | 14477 |     _PyUnicode_STATE(self).compact = 0; | 
| Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14478 |     _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14479 |     _PyUnicode_STATE(self).ready = 1; | 
 | 14480 |     _PyUnicode_WSTR(self) = NULL; | 
 | 14481 |     _PyUnicode_UTF8_LENGTH(self) = 0; | 
 | 14482 |     _PyUnicode_UTF8(self) = NULL; | 
 | 14483 |     _PyUnicode_WSTR_LENGTH(self) = 0; | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14484 |     _PyUnicode_DATA_ANY(self) = NULL; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14485 |  | 
 | 14486 |     share_utf8 = 0; | 
 | 14487 |     share_wstr = 0; | 
 | 14488 |     if (kind == PyUnicode_1BYTE_KIND) { | 
 | 14489 |         char_size = 1; | 
 | 14490 |         if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) | 
 | 14491 |             share_utf8 = 1; | 
 | 14492 |     } | 
 | 14493 |     else if (kind == PyUnicode_2BYTE_KIND) { | 
 | 14494 |         char_size = 2; | 
 | 14495 |         if (sizeof(wchar_t) == 2) | 
 | 14496 |             share_wstr = 1; | 
 | 14497 |     } | 
 | 14498 |     else { | 
 | 14499 |         assert(kind == PyUnicode_4BYTE_KIND); | 
 | 14500 |         char_size = 4; | 
 | 14501 |         if (sizeof(wchar_t) == 4) | 
 | 14502 |             share_wstr = 1; | 
 | 14503 |     } | 
 | 14504 |  | 
 | 14505 |     /* Ensure we won't overflow the length. */ | 
 | 14506 |     if (length > (PY_SSIZE_T_MAX / char_size - 1)) { | 
 | 14507 |         PyErr_NoMemory(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14508 |         goto onError; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14509 |     } | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14510 |     data = PyObject_MALLOC((length + 1) * char_size); | 
 | 14511 |     if (data == NULL) { | 
 | 14512 |         PyErr_NoMemory(); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14513 |         goto onError; | 
 | 14514 |     } | 
 | 14515 |  | 
| Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14516 |     _PyUnicode_DATA_ANY(self) = data; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14517 |     if (share_utf8) { | 
 | 14518 |         _PyUnicode_UTF8_LENGTH(self) = length; | 
 | 14519 |         _PyUnicode_UTF8(self) = data; | 
 | 14520 |     } | 
 | 14521 |     if (share_wstr) { | 
 | 14522 |         _PyUnicode_WSTR_LENGTH(self) = length; | 
 | 14523 |         _PyUnicode_WSTR(self) = (wchar_t *)data; | 
 | 14524 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14525 |  | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14526 |     Py_MEMCPY(data, PyUnicode_DATA(unicode), | 
| Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14527 |               kind * (length + 1)); | 
| Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14528 |     assert(_PyUnicode_CheckConsistency(self, 1)); | 
| Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14529 | #ifdef Py_DEBUG | 
 | 14530 |     _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); | 
 | 14531 | #endif | 
| Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14532 |     Py_DECREF(unicode); | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14533 |     return self; | 
| Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14534 |  | 
 | 14535 | onError: | 
 | 14536 |     Py_DECREF(unicode); | 
 | 14537 |     Py_DECREF(self); | 
 | 14538 |     return NULL; | 
| Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14539 | } | 
 | 14540 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14541 | PyDoc_STRVAR(unicode_doc, | 
| Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14542 | "str(object='') -> str\n\ | 
 | 14543 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14544 | \n\ | 
| Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14545 | Create a new string object from the given object. If encoding or\n\ | 
 | 14546 | errors is specified, then the object must expose a data buffer\n\ | 
 | 14547 | that will be decoded using the given encoding and error handler.\n\ | 
 | 14548 | Otherwise, returns the result of object.__str__() (if defined)\n\ | 
 | 14549 | or repr(object).\n\ | 
 | 14550 | encoding defaults to sys.getdefaultencoding().\n\ | 
 | 14551 | errors defaults to 'strict'."); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14552 |  | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14553 | static PyObject *unicode_iter(PyObject *seq); | 
 | 14554 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14555 | PyTypeObject PyUnicode_Type = { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14556 |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14557 |     "str",              /* tp_name */ | 
 | 14558 |     sizeof(PyUnicodeObject),        /* tp_size */ | 
 | 14559 |     0,                  /* tp_itemsize */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14560 |     /* Slots */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14561 |     (destructor)unicode_dealloc,    /* tp_dealloc */ | 
 | 14562 |     0,                  /* tp_print */ | 
 | 14563 |     0,                  /* tp_getattr */ | 
 | 14564 |     0,                  /* tp_setattr */ | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14565 |     0,                  /* tp_reserved */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14566 |     unicode_repr,           /* tp_repr */ | 
 | 14567 |     &unicode_as_number,         /* tp_as_number */ | 
 | 14568 |     &unicode_as_sequence,       /* tp_as_sequence */ | 
 | 14569 |     &unicode_as_mapping,        /* tp_as_mapping */ | 
 | 14570 |     (hashfunc) unicode_hash,        /* tp_hash*/ | 
 | 14571 |     0,                  /* tp_call*/ | 
 | 14572 |     (reprfunc) unicode_str,     /* tp_str */ | 
 | 14573 |     PyObject_GenericGetAttr,        /* tp_getattro */ | 
 | 14574 |     0,                  /* tp_setattro */ | 
 | 14575 |     0,                  /* tp_as_buffer */ | 
 | 14576 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14577 |     Py_TPFLAGS_UNICODE_SUBCLASS,    /* tp_flags */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14578 |     unicode_doc,            /* tp_doc */ | 
 | 14579 |     0,                  /* tp_traverse */ | 
 | 14580 |     0,                  /* tp_clear */ | 
 | 14581 |     PyUnicode_RichCompare,      /* tp_richcompare */ | 
 | 14582 |     0,                  /* tp_weaklistoffset */ | 
 | 14583 |     unicode_iter,           /* tp_iter */ | 
 | 14584 |     0,                  /* tp_iternext */ | 
 | 14585 |     unicode_methods,            /* tp_methods */ | 
 | 14586 |     0,                  /* tp_members */ | 
 | 14587 |     0,                  /* tp_getset */ | 
 | 14588 |     &PyBaseObject_Type,         /* tp_base */ | 
 | 14589 |     0,                  /* tp_dict */ | 
 | 14590 |     0,                  /* tp_descr_get */ | 
 | 14591 |     0,                  /* tp_descr_set */ | 
 | 14592 |     0,                  /* tp_dictoffset */ | 
 | 14593 |     0,                  /* tp_init */ | 
 | 14594 |     0,                  /* tp_alloc */ | 
 | 14595 |     unicode_new,            /* tp_new */ | 
 | 14596 |     PyObject_Del,           /* tp_free */ | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14597 | }; | 
 | 14598 |  | 
 | 14599 | /* Initialize the Unicode implementation */ | 
 | 14600 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14601 | int _PyUnicode_Init(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14602 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14603 |     /* XXX - move this array to unicodectype.c ? */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14604 |     Py_UCS2 linebreak[] = { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14605 |         0x000A, /* LINE FEED */ | 
 | 14606 |         0x000D, /* CARRIAGE RETURN */ | 
 | 14607 |         0x001C, /* FILE SEPARATOR */ | 
 | 14608 |         0x001D, /* GROUP SEPARATOR */ | 
 | 14609 |         0x001E, /* RECORD SEPARATOR */ | 
 | 14610 |         0x0085, /* NEXT LINE */ | 
 | 14611 |         0x2028, /* LINE SEPARATOR */ | 
 | 14612 |         0x2029, /* PARAGRAPH SEPARATOR */ | 
 | 14613 |     }; | 
 | 14614 |  | 
| Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14615 |     /* Init the implementation */ | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14616 |     _Py_INCREF_UNICODE_EMPTY(); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14617 |     if (!unicode_empty) | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14618 |         Py_FatalError("Can't create empty string"); | 
| Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14619 |     Py_DECREF(unicode_empty); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14620 |  | 
| Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14621 |     if (PyType_Ready(&PyUnicode_Type) < 0) | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14622 |         Py_FatalError("Can't initialize 'unicode'"); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14623 |  | 
 | 14624 |     /* initialize the linebreak bloom filter */ | 
 | 14625 |     bloom_linebreak = make_bloom_mask( | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14626 |         PyUnicode_2BYTE_KIND, linebreak, | 
| Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14627 |         Py_ARRAY_LENGTH(linebreak)); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14628 |  | 
| Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14629 |     if (PyType_Ready(&EncodingMapType) < 0) | 
 | 14630 |          Py_FatalError("Can't initialize encoding map type"); | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14631 |  | 
| Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14632 |     if (PyType_Ready(&PyFieldNameIter_Type) < 0) | 
 | 14633 |         Py_FatalError("Can't initialize field name iterator type"); | 
 | 14634 |  | 
 | 14635 |     if (PyType_Ready(&PyFormatterIter_Type) < 0) | 
 | 14636 |         Py_FatalError("Can't initialize formatter iter type"); | 
| Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14637 |  | 
| Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14638 | #ifdef HAVE_MBCS | 
 | 14639 |     winver.dwOSVersionInfoSize = sizeof(winver); | 
 | 14640 |     if (!GetVersionEx((OSVERSIONINFO*)&winver)) { | 
 | 14641 |         PyErr_SetFromWindowsErr(0); | 
 | 14642 |         return -1; | 
 | 14643 |     } | 
 | 14644 | #endif | 
 | 14645 |     return 0; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14646 | } | 
 | 14647 |  | 
 | 14648 | /* Finalize the Unicode implementation */ | 
 | 14649 |  | 
| Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14650 | int | 
 | 14651 | PyUnicode_ClearFreeList(void) | 
 | 14652 | { | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14653 |     return 0; | 
| Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14654 | } | 
 | 14655 |  | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14656 | void | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14657 | _PyUnicode_Fini(void) | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14658 | { | 
| Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14659 |     int i; | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14660 |  | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14661 |     Py_CLEAR(unicode_empty); | 
| Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14662 |  | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14663 |     for (i = 0; i < 256; i++) | 
 | 14664 |         Py_CLEAR(unicode_latin1[i]); | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14665 |     _PyUnicode_ClearStaticStrings(); | 
| Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14666 |     (void)PyUnicode_ClearFreeList(); | 
| Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14667 | } | 
| Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14668 |  | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14669 | void | 
 | 14670 | PyUnicode_InternInPlace(PyObject **p) | 
 | 14671 | { | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 14672 |     PyObject *s = *p; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14673 |     PyObject *t; | 
| Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14674 | #ifdef Py_DEBUG | 
 | 14675 |     assert(s != NULL); | 
 | 14676 |     assert(_PyUnicode_CHECK(s)); | 
 | 14677 | #else | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14678 |     if (s == NULL || !PyUnicode_Check(s)) | 
| Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14679 |         return; | 
 | 14680 | #endif | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14681 |     /* If it's a subclass, we don't really know what putting | 
 | 14682 |        it in the interned dict might do. */ | 
 | 14683 |     if (!PyUnicode_CheckExact(s)) | 
 | 14684 |         return; | 
 | 14685 |     if (PyUnicode_CHECK_INTERNED(s)) | 
 | 14686 |         return; | 
 | 14687 |     if (interned == NULL) { | 
 | 14688 |         interned = PyDict_New(); | 
 | 14689 |         if (interned == NULL) { | 
 | 14690 |             PyErr_Clear(); /* Don't leave an exception */ | 
 | 14691 |             return; | 
 | 14692 |         } | 
 | 14693 |     } | 
 | 14694 |     /* It might be that the GetItem call fails even | 
 | 14695 |        though the key is present in the dictionary, | 
 | 14696 |        namely when this happens during a stack overflow. */ | 
 | 14697 |     Py_ALLOW_RECURSION | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14698 |     t = PyDict_GetItem(interned, s); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14699 |     Py_END_ALLOW_RECURSION | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14700 |  | 
| Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 14701 |     if (t) { | 
 | 14702 |         Py_INCREF(t); | 
 | 14703 |         Py_DECREF(*p); | 
 | 14704 |         *p = t; | 
 | 14705 |         return; | 
 | 14706 |     } | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14707 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14708 |     PyThreadState_GET()->recursion_critical = 1; | 
| Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14709 |     if (PyDict_SetItem(interned, s, s) < 0) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14710 |         PyErr_Clear(); | 
 | 14711 |         PyThreadState_GET()->recursion_critical = 0; | 
 | 14712 |         return; | 
 | 14713 |     } | 
 | 14714 |     PyThreadState_GET()->recursion_critical = 0; | 
 | 14715 |     /* The two references in interned are not counted by refcnt. | 
 | 14716 |        The deallocator will take care of this */ | 
 | 14717 |     Py_REFCNT(s) -= 2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14718 |     _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14719 | } | 
 | 14720 |  | 
 | 14721 | void | 
 | 14722 | PyUnicode_InternImmortal(PyObject **p) | 
 | 14723 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14724 |     PyUnicode_InternInPlace(p); | 
 | 14725 |     if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { | 
| Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14726 |         _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14727 |         Py_INCREF(*p); | 
 | 14728 |     } | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14729 | } | 
 | 14730 |  | 
 | 14731 | PyObject * | 
 | 14732 | PyUnicode_InternFromString(const char *cp) | 
 | 14733 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14734 |     PyObject *s = PyUnicode_FromString(cp); | 
 | 14735 |     if (s == NULL) | 
 | 14736 |         return NULL; | 
 | 14737 |     PyUnicode_InternInPlace(&s); | 
 | 14738 |     return s; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14739 | } | 
 | 14740 |  | 
| Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14741 | void | 
 | 14742 | _Py_ReleaseInternedUnicodeStrings(void) | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14743 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14744 |     PyObject *keys; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14745 |     PyObject *s; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14746 |     Py_ssize_t i, n; | 
 | 14747 |     Py_ssize_t immortal_size = 0, mortal_size = 0; | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14748 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14749 |     if (interned == NULL || !PyDict_Check(interned)) | 
 | 14750 |         return; | 
 | 14751 |     keys = PyDict_Keys(interned); | 
 | 14752 |     if (keys == NULL || !PyList_Check(keys)) { | 
 | 14753 |         PyErr_Clear(); | 
 | 14754 |         return; | 
 | 14755 |     } | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14756 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14757 |     /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak | 
 | 14758 |        detector, interned unicode strings are not forcibly deallocated; | 
 | 14759 |        rather, we give them their stolen references back, and then clear | 
 | 14760 |        and DECREF the interned dict. */ | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14761 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14762 |     n = PyList_GET_SIZE(keys); | 
 | 14763 |     fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14764 |             n); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14765 |     for (i = 0; i < n; i++) { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14766 |         s = PyList_GET_ITEM(keys, i); | 
| Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14767 |         if (PyUnicode_READY(s) == -1) { | 
 | 14768 |             assert(0 && "could not ready string"); | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14769 |             fprintf(stderr, "could not ready string\n"); | 
| Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14770 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14771 |         switch (PyUnicode_CHECK_INTERNED(s)) { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14772 |         case SSTATE_NOT_INTERNED: | 
 | 14773 |             /* XXX Shouldn't happen */ | 
 | 14774 |             break; | 
 | 14775 |         case SSTATE_INTERNED_IMMORTAL: | 
 | 14776 |             Py_REFCNT(s) += 1; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14777 |             immortal_size += PyUnicode_GET_LENGTH(s); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14778 |             break; | 
 | 14779 |         case SSTATE_INTERNED_MORTAL: | 
 | 14780 |             Py_REFCNT(s) += 2; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14781 |             mortal_size += PyUnicode_GET_LENGTH(s); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14782 |             break; | 
 | 14783 |         default: | 
 | 14784 |             Py_FatalError("Inconsistent interned string state."); | 
 | 14785 |         } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14786 |         _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14787 |     } | 
 | 14788 |     fprintf(stderr, "total size of all interned strings: " | 
 | 14789 |             "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " | 
 | 14790 |             "mortal/immortal\n", mortal_size, immortal_size); | 
 | 14791 |     Py_DECREF(keys); | 
 | 14792 |     PyDict_Clear(interned); | 
| Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14793 |     Py_CLEAR(interned); | 
| Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14794 | } | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14795 |  | 
 | 14796 |  | 
 | 14797 | /********************* Unicode Iterator **************************/ | 
 | 14798 |  | 
 | 14799 | typedef struct { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14800 |     PyObject_HEAD | 
 | 14801 |     Py_ssize_t it_index; | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14802 |     PyObject *it_seq;    /* Set to NULL when iterator is exhausted */ | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14803 | } unicodeiterobject; | 
 | 14804 |  | 
 | 14805 | static void | 
 | 14806 | unicodeiter_dealloc(unicodeiterobject *it) | 
 | 14807 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14808 |     _PyObject_GC_UNTRACK(it); | 
 | 14809 |     Py_XDECREF(it->it_seq); | 
 | 14810 |     PyObject_GC_Del(it); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14811 | } | 
 | 14812 |  | 
 | 14813 | static int | 
 | 14814 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) | 
 | 14815 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14816 |     Py_VISIT(it->it_seq); | 
 | 14817 |     return 0; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14818 | } | 
 | 14819 |  | 
 | 14820 | static PyObject * | 
 | 14821 | unicodeiter_next(unicodeiterobject *it) | 
 | 14822 | { | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14823 |     PyObject *seq, *item; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14824 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14825 |     assert(it != NULL); | 
 | 14826 |     seq = it->it_seq; | 
 | 14827 |     if (seq == NULL) | 
 | 14828 |         return NULL; | 
| Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14829 |     assert(_PyUnicode_CHECK(seq)); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14830 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14831 |     if (it->it_index < PyUnicode_GET_LENGTH(seq)) { | 
 | 14832 |         int kind = PyUnicode_KIND(seq); | 
 | 14833 |         void *data = PyUnicode_DATA(seq); | 
 | 14834 |         Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); | 
 | 14835 |         item = PyUnicode_FromOrdinal(chr); | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14836 |         if (item != NULL) | 
 | 14837 |             ++it->it_index; | 
 | 14838 |         return item; | 
 | 14839 |     } | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14840 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14841 |     Py_DECREF(seq); | 
 | 14842 |     it->it_seq = NULL; | 
 | 14843 |     return NULL; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14844 | } | 
 | 14845 |  | 
 | 14846 | static PyObject * | 
 | 14847 | unicodeiter_len(unicodeiterobject *it) | 
 | 14848 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14849 |     Py_ssize_t len = 0; | 
 | 14850 |     if (it->it_seq) | 
| Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14851 |         len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14852 |     return PyLong_FromSsize_t(len); | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14853 | } | 
 | 14854 |  | 
 | 14855 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); | 
 | 14856 |  | 
| Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14857 | static PyObject * | 
 | 14858 | unicodeiter_reduce(unicodeiterobject *it) | 
 | 14859 | { | 
 | 14860 |     if (it->it_seq != NULL) { | 
| Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14861 |         return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), | 
| Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14862 |                              it->it_seq, it->it_index); | 
 | 14863 |     } else { | 
 | 14864 |         PyObject *u = PyUnicode_FromUnicode(NULL, 0); | 
 | 14865 |         if (u == NULL) | 
 | 14866 |             return NULL; | 
| Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14867 |         return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); | 
| Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14868 |     } | 
 | 14869 | } | 
 | 14870 |  | 
 | 14871 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); | 
 | 14872 |  | 
 | 14873 | static PyObject * | 
 | 14874 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) | 
 | 14875 | { | 
 | 14876 |     Py_ssize_t index = PyLong_AsSsize_t(state); | 
 | 14877 |     if (index == -1 && PyErr_Occurred()) | 
 | 14878 |         return NULL; | 
 | 14879 |     if (index < 0) | 
 | 14880 |         index = 0; | 
 | 14881 |     it->it_index = index; | 
 | 14882 |     Py_RETURN_NONE; | 
 | 14883 | } | 
 | 14884 |  | 
 | 14885 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); | 
 | 14886 |  | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14887 | static PyMethodDef unicodeiter_methods[] = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14888 |     {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, | 
| Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14889 |      length_hint_doc}, | 
| Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14890 |     {"__reduce__",      (PyCFunction)unicodeiter_reduce, METH_NOARGS, | 
 | 14891 |      reduce_doc}, | 
 | 14892 |     {"__setstate__",    (PyCFunction)unicodeiter_setstate, METH_O, | 
 | 14893 |      setstate_doc}, | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14894 |     {NULL,      NULL}       /* sentinel */ | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14895 | }; | 
 | 14896 |  | 
 | 14897 | PyTypeObject PyUnicodeIter_Type = { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14898 |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
 | 14899 |     "str_iterator",         /* tp_name */ | 
 | 14900 |     sizeof(unicodeiterobject),      /* tp_basicsize */ | 
 | 14901 |     0,                  /* tp_itemsize */ | 
 | 14902 |     /* methods */ | 
 | 14903 |     (destructor)unicodeiter_dealloc,    /* tp_dealloc */ | 
 | 14904 |     0,                  /* tp_print */ | 
 | 14905 |     0,                  /* tp_getattr */ | 
 | 14906 |     0,                  /* tp_setattr */ | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14907 |     0,                  /* tp_reserved */ | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14908 |     0,                  /* tp_repr */ | 
 | 14909 |     0,                  /* tp_as_number */ | 
 | 14910 |     0,                  /* tp_as_sequence */ | 
 | 14911 |     0,                  /* tp_as_mapping */ | 
 | 14912 |     0,                  /* tp_hash */ | 
 | 14913 |     0,                  /* tp_call */ | 
 | 14914 |     0,                  /* tp_str */ | 
 | 14915 |     PyObject_GenericGetAttr,        /* tp_getattro */ | 
 | 14916 |     0,                  /* tp_setattro */ | 
 | 14917 |     0,                  /* tp_as_buffer */ | 
 | 14918 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | 
 | 14919 |     0,                  /* tp_doc */ | 
 | 14920 |     (traverseproc)unicodeiter_traverse, /* tp_traverse */ | 
 | 14921 |     0,                  /* tp_clear */ | 
 | 14922 |     0,                  /* tp_richcompare */ | 
 | 14923 |     0,                  /* tp_weaklistoffset */ | 
 | 14924 |     PyObject_SelfIter,          /* tp_iter */ | 
 | 14925 |     (iternextfunc)unicodeiter_next,     /* tp_iternext */ | 
 | 14926 |     unicodeiter_methods,            /* tp_methods */ | 
 | 14927 |     0, | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14928 | }; | 
 | 14929 |  | 
 | 14930 | static PyObject * | 
 | 14931 | unicode_iter(PyObject *seq) | 
 | 14932 | { | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14933 |     unicodeiterobject *it; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14934 |  | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14935 |     if (!PyUnicode_Check(seq)) { | 
 | 14936 |         PyErr_BadInternalCall(); | 
 | 14937 |         return NULL; | 
 | 14938 |     } | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14939 |     if (PyUnicode_READY(seq) == -1) | 
 | 14940 |         return NULL; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14941 |     it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); | 
 | 14942 |     if (it == NULL) | 
 | 14943 |         return NULL; | 
 | 14944 |     it->it_index = 0; | 
 | 14945 |     Py_INCREF(seq); | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14946 |     it->it_seq = seq; | 
| Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14947 |     _PyObject_GC_TRACK(it); | 
 | 14948 |     return (PyObject *)it; | 
| Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14949 | } | 
 | 14950 |  | 
| Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14951 |  | 
 | 14952 | size_t | 
 | 14953 | Py_UNICODE_strlen(const Py_UNICODE *u) | 
 | 14954 | { | 
 | 14955 |     int res = 0; | 
 | 14956 |     while(*u++) | 
 | 14957 |         res++; | 
 | 14958 |     return res; | 
 | 14959 | } | 
 | 14960 |  | 
 | 14961 | Py_UNICODE* | 
 | 14962 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) | 
 | 14963 | { | 
 | 14964 |     Py_UNICODE *u = s1; | 
 | 14965 |     while ((*u++ = *s2++)); | 
 | 14966 |     return s1; | 
 | 14967 | } | 
 | 14968 |  | 
 | 14969 | Py_UNICODE* | 
 | 14970 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) | 
 | 14971 | { | 
 | 14972 |     Py_UNICODE *u = s1; | 
 | 14973 |     while ((*u++ = *s2++)) | 
 | 14974 |         if (n-- == 0) | 
 | 14975 |             break; | 
 | 14976 |     return s1; | 
 | 14977 | } | 
 | 14978 |  | 
 | 14979 | Py_UNICODE* | 
 | 14980 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) | 
 | 14981 | { | 
 | 14982 |     Py_UNICODE *u1 = s1; | 
 | 14983 |     u1 += Py_UNICODE_strlen(u1); | 
 | 14984 |     Py_UNICODE_strcpy(u1, s2); | 
 | 14985 |     return s1; | 
 | 14986 | } | 
 | 14987 |  | 
 | 14988 | int | 
 | 14989 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) | 
 | 14990 | { | 
 | 14991 |     while (*s1 && *s2 && *s1 == *s2) | 
 | 14992 |         s1++, s2++; | 
 | 14993 |     if (*s1 && *s2) | 
 | 14994 |         return (*s1 < *s2) ? -1 : +1; | 
 | 14995 |     if (*s1) | 
 | 14996 |         return 1; | 
 | 14997 |     if (*s2) | 
 | 14998 |         return -1; | 
 | 14999 |     return 0; | 
 | 15000 | } | 
 | 15001 |  | 
 | 15002 | int | 
 | 15003 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) | 
 | 15004 | { | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15005 |     Py_UNICODE u1, u2; | 
| Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15006 |     for (; n != 0; n--) { | 
 | 15007 |         u1 = *s1; | 
 | 15008 |         u2 = *s2; | 
 | 15009 |         if (u1 != u2) | 
 | 15010 |             return (u1 < u2) ? -1 : +1; | 
 | 15011 |         if (u1 == '\0') | 
 | 15012 |             return 0; | 
 | 15013 |         s1++; | 
 | 15014 |         s2++; | 
 | 15015 |     } | 
 | 15016 |     return 0; | 
 | 15017 | } | 
 | 15018 |  | 
 | 15019 | Py_UNICODE* | 
 | 15020 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) | 
 | 15021 | { | 
 | 15022 |     const Py_UNICODE *p; | 
 | 15023 |     for (p = s; *p; p++) | 
 | 15024 |         if (*p == c) | 
 | 15025 |             return (Py_UNICODE*)p; | 
 | 15026 |     return NULL; | 
 | 15027 | } | 
 | 15028 |  | 
 | 15029 | Py_UNICODE* | 
 | 15030 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) | 
 | 15031 | { | 
 | 15032 |     const Py_UNICODE *p; | 
 | 15033 |     p = s + Py_UNICODE_strlen(s); | 
 | 15034 |     while (p != s) { | 
 | 15035 |         p--; | 
 | 15036 |         if (*p == c) | 
 | 15037 |             return (Py_UNICODE*)p; | 
 | 15038 |     } | 
 | 15039 |     return NULL; | 
 | 15040 | } | 
| Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15041 |  | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15042 | Py_UNICODE* | 
| Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15043 | PyUnicode_AsUnicodeCopy(PyObject *unicode) | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15044 | { | 
| Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15045 |     Py_UNICODE *u, *copy; | 
| Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15046 |     Py_ssize_t len, size; | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15047 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15048 |     if (!PyUnicode_Check(unicode)) { | 
 | 15049 |         PyErr_BadArgument(); | 
 | 15050 |         return NULL; | 
 | 15051 |     } | 
| Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15052 |     u = PyUnicode_AsUnicodeAndSize(unicode, &len); | 
| Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15053 |     if (u == NULL) | 
 | 15054 |         return NULL; | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15055 |     /* Ensure we won't overflow the size. */ | 
| Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15056 |     if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15057 |         PyErr_NoMemory(); | 
 | 15058 |         return NULL; | 
 | 15059 |     } | 
| Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15060 |     size = len + 1; /* copy the null character */ | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15061 |     size *= sizeof(Py_UNICODE); | 
 | 15062 |     copy = PyMem_Malloc(size); | 
 | 15063 |     if (copy == NULL) { | 
 | 15064 |         PyErr_NoMemory(); | 
 | 15065 |         return NULL; | 
 | 15066 |     } | 
| Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15067 |     memcpy(copy, u, size); | 
| Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15068 |     return copy; | 
 | 15069 | } | 
| Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15070 |  | 
| Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15071 | /* A _string module, to export formatter_parser and formatter_field_name_split | 
 | 15072 |    to the string.Formatter class implemented in Python. */ | 
 | 15073 |  | 
 | 15074 | static PyMethodDef _string_methods[] = { | 
 | 15075 |     {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, | 
 | 15076 |      METH_O, PyDoc_STR("split the argument as a field name")}, | 
 | 15077 |     {"formatter_parser", (PyCFunction) formatter_parser, | 
 | 15078 |      METH_O, PyDoc_STR("parse the argument as a format string")}, | 
 | 15079 |     {NULL, NULL} | 
 | 15080 | }; | 
 | 15081 |  | 
 | 15082 | static struct PyModuleDef _string_module = { | 
 | 15083 |     PyModuleDef_HEAD_INIT, | 
 | 15084 |     "_string", | 
 | 15085 |     PyDoc_STR("string helper module"), | 
 | 15086 |     0, | 
 | 15087 |     _string_methods, | 
 | 15088 |     NULL, | 
 | 15089 |     NULL, | 
 | 15090 |     NULL, | 
 | 15091 |     NULL | 
 | 15092 | }; | 
 | 15093 |  | 
 | 15094 | PyMODINIT_FUNC | 
 | 15095 | PyInit__string(void) | 
 | 15096 | { | 
 | 15097 |     return PyModule_Create(&_string_module); | 
 | 15098 | } | 
 | 15099 |  | 
 | 15100 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15101 | #ifdef __cplusplus | 
 | 15102 | } | 
 | 15103 | #endif |